Is it possible to restart a machine when provisioning a machine using Vagrant and pickup where the script left off?
Asked Answered
S

5

25

I was reading a tutorial in bash where they said to restart the machine, there was no option to restart a service directly, it was a matter of restarting the machine, and then there were more commands after that that still needed to be run when provisioning.

So is there any way to restart a box amid provisioning and then pick up where you left off after that?

Search answered 20/1, 2016 at 21:31 Comment(3)
What provisioning do you use?Biogen
vagrant vbguest plugin does it so it should be possible. never look deep how they were doing though and its a plugin so can be a little different but you might get some good ideaWord
@Biogen Bash at this point...Search
B
30

As far as I know you can't have a single script/set of commands that would carry on where it left off if it attempts to restart the OS, such as:

  config.vm.provision "shell", inline: <<-SHELL
    echo $(date) > ~/rebootexample
    reboot
    echo $(date) >> ~/rebootexample
  SHELL

In this example the second echo call would not be carried out.

You could split the script/commands up and use a plugin such as vagrant reload.

An example snippet of a Vagrantfile to highlight its possible use:

  # execute code before reload
  config.vm.provision "shell", inline: <<-SHELL
     echo $(date) > ~/rebootexample
  SHELL

  # trigger reload
  config.vm.provision :reload

  # execute code after reload
  config.vm.provision "shell", inline: <<-SHELL
     echo $(date) >> ~/rebootexample
  SHELL
Bharat answered 24/1, 2016 at 12:13 Comment(3)
The official Vagrant shell provisioner now has this capability natively. It depends on whether the guest has reboot capability implemented, but in my testing, the linux guests that I work with are just fine. To use this feature, divide your shell provisioning into two separate shell scripts. Then add this line between them: config.vm.provision 'shell', reboot: true. You will then see ==> proxmox: Waiting for machine to reboot... in the install output from vagrant up. Problem solved. This should be added to this answer if possible via edit. But the edit queue is full.Cardio
You'll need to vagrant plugin install vagrant-reload to execute the #trigger reload stepMartica
The feature that does exactly what the question asks for is config.vm.provision 'shell', reboot: true. This feature was added after the date of this answer. This answer is no longer correct. Documentation for this feature is here: developer.hashicorp.com/vagrant/docs/provisioning/shell#rebootCardio
A
3

I've never done this, but if I had to I would split the script into two pieces, one before restart that includes the restart command, then another that's post install.

The first one would also create a lock file.

The overall script would run the first script if the lock file didn't exist or run the second one if the file exists. This overall script would be set up for startup.

Apologue answered 21/1, 2016 at 16:2 Comment(1)
could you give a basic example of what something like this would look like?Noncontributory
C
2

This can be done like so:

config.vm.provision 'shell', path: 'part1.sh'
config.vm.provision 'shell', reboot: true
config.vm.provision 'shell', path: 'part2.sh'

https://developer.hashicorp.com/vagrant/docs/provisioning/shell#reboot

Cardio answered 3/11, 2022 at 15:47 Comment(0)
S
1

One trick you can employ is to send restart signal and save rest of the provisioning work as a script to be run on boot:

config.vm.provision "shell", inline: <<-SHELL
  echo "Do your thing... DONE"
cat <<-RCLOCAL | sed -s 's_^      __' > /etc/rc.local
      #!/bin/bash
      echo "This will be run once on next boot and then it's destroyed and never run again"
      rm /etc/rc.local
RCLOCAL
  chmod o+x /etc/rc.local
  shutdown -r now #restart
SHELL

This was tested to work on debian 9, so you may need to enable services or find another way to get your code bootsrapped to run on the next boot if you're running something else.

Unfortunately you can't simply do:

config.vm.provision "shell", inline: "shutdown -r now"
config.vm.provision "shell", inline: "echo 'hello world'"

results in ==>
The SSH connection was unexpectedly closed by the remote end. This
usually indicates that SSH within the guest machine was unable to
properly start up. Please boot the VM in GUI mode to check whether
it is booting properly.
Spicy answered 22/2, 2018 at 15:40 Comment(0)
C
0

Vagrant has a reboot option for provisioning, however, the reboot guest capabilities is currently not support for Linux.

You can check my plugin out here, https://github.com/secret104278/vagrant_reboot_linux/tree/master , I've implement the function for Linux to reboot.

Cretaceous answered 13/5, 2019 at 1:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.