Reload VM in Vagrant script

时间:2016-04-15 14:55:31

标签: bash unix debian vagrant locale

Introduction
I'm using Vagrant and want to create a box that fits my needs. I'm currently building my provisioning script but I have a problem which would require me to relog into the box.

What I'm trying to achieve
I want to set my locales to the German language

What I'm doing
After logging into my vagrant box with vagrant ssh I'm running the following commands

sudo apt-get update 
sudo cp /var/www/projectfantasy/www/vagrant_ressources/locale.gen /etc/
sudo locale-gen de_DE.UTF-8

These steps are done with the help of Debian's wiki. The last step is

To use the new settings with your programs, log out and back in.

And now I am where I need help. How would I relog while being in the vagrant provisioning script? When I don't relog I'm getting the following warnings when installing extra packages.

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "de_DE.UTF-8",
    LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory

I mean, these are only warnings so that shouldn't be a problem, right? But I don't want to have warnings and would like to know how I could fix this issue.

When I relog and try to install those packages again it works without any problems.

2 个答案:

答案 0 :(得分:2)

不幸的是,这并不是那么简单 - 如果您在配置脚本中发出reboot命令或shutdown -r now,VM将重新启动(确实),但随后配置将不会继续。

幸运的是,有些人编写了插件(https://github.com/exratione/vagrant-provision-reboothttps://github.com/aidanns/vagrant-reload)我在第二次使用时遇到过它的工作

确保安装插件

$ vagrant plugin install vagrant-reload
在您的vagrantfile中,您的配置将如下所示

 config.vm.provision "shell", path: "vagrant_ressources/preparations.sh"
 config.vm.provision :reload
 config.vm.provision "shell", path: "vagrant_ressources/bootstrap.sh"

所以prepare.sh将执行,然后VM将重新加载并且bootstrap.sh将执行

在prepare.sh中,生成语言环境后,确保在语言环境文件中设置变量:

sudo locale-gen 
echo -e 'LANG=de_DE.UTF-8\nLC_ALL=de_DE.UTF-8' > /etc/default/locale
sudo timedatectl set-timezone Europe/Berlin

PS:关于您使用shell配置的一个注释。默认情况下,配置以root权限运行,因此不需要所有sudo;如果你想运行配置,因为流浪者用户将其设置为config.vm.provision :shell, privileged: false ...,你将需要sudo

答案 1 :(得分:0)

我无法重现您的警告,但我发现这将允许您设置LC_ALL:

sudo update-locale LC_ALL=de_DE.UTF-8

(来自https://askubuntu.com/questions/114759/warning-setlocale-lc-all-cannot-change-locale,但也适用于debian jessie)