Search code examples
bashunixdebianvagrantlocale

Reload VM in Vagrant script


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.


Solution

  • 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