首页 > bash > 在Vagrant脚本中重新加载VM

在Vagrant脚本中重新加载VM (Reload VM in Vagrant script)

问题

简介
我正在使用Vagrant并希望创建一个符合我需求的盒子。我目前正在构建我的配置脚本,但是我有一个问题需要我重新进入框中。

我想要实现的目标
我想将我的语言环境设置为德语

我正在做什么
登录我的流浪盒后,vagrant ssh我正在运行以下命令

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

这些步骤是在Debian的wiki的帮助下完成。最后一步是

要在程序中使用新设置,请注销并重新登录。

而现在我需要帮助。在使用vagrant配置脚本时我将如何重新登录?当我不重新安装时,我在安装额外的包时会收到以下警告

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

我的意思是,这些只是警告所以不应该是一个问题,对吧?但我不想发出警告,想知道如何解决这个问题。

当我重新尝试再次安装这些软件包时,它可以正常工作。

解决方法

遗憾的是,这并非如此简单 - 如果您发出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

问题

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.

解决方法

unfortunately, this is not so simple - if you issue a reboot command or shutdown -r now in your provisioning script the VM will restart (indeed) but then the provisioning will not continue.

fortunately, some people wrote plugin (https://github.com/exratione/vagrant-provision-reboot and https://github.com/aidanns/vagrant-reload) I experienced with the second and it works

make sure to install the plugin

$ vagrant plugin install vagrant-reload

in your vagrantfile, your provisioning will look like

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

so preparations.sh will execute, then the VM will reload and bootstrap.sh will execute

in your preparations.sh, after you generate locale, make sure variables are set in the locale file :

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 : one note on your usage of the shell provisioning. By default provisioning is run with root privilege so there is no need for all the sudo ; if you want to run the provisioning as vagrant user run it as config.vm.provision :shell, privileged: false ... and you will need the sudo

相似信息