Skip to content

Instantly share code, notes, and snippets.

@ukabu
Last active November 24, 2017 10:38
Show Gist options
  • Save ukabu/6780121 to your computer and use it in GitHub Desktop.
Save ukabu/6780121 to your computer and use it in GitHub Desktop.
# Quick hack to be able to reboot a Vagrant Windows VM during provisioning.
#
# This files goes in the same folder as your vagrant file.
#
# Then you (in your Vagrantfile):
# require_plugin './windows_reboot_plugin'
#
# Your provisioners run in order, so...
# config.vm.provision :shell, :inline => "stuff that need a reboot"
# config.vm.provision :reboot
# config.vm.provision :shell, :inline => "stuff that need to run after a reboot"
#
# The provisioner takes care of remounting the shared folders.
#
# It has been tested on Windows Server 2012 with chef-solo provisioner running after
# reboot.
#
# This will work for the VirtualBox provider, for other providers, a 'remap_shared_folders'
# action must be added to the provider implementation.
require 'vagrant'
# Monkey patch VirtualBox provider to be able to remap shared folders after reboot.
module VagrantPlugins
module ProviderVirtualBox
module Action
class MountSharedFolders < ShareFolders
def initialize(app,env)
super(app, env)
end
def call(env)
@env = env
@app.call(env)
mount_shared_folders
end
end
def self.action_remap_shared_folders
Vagrant::Action::Builder.new.tap do |b|
b.use MountSharedFolders
end
end
end
end
end
class WindowsRebootPlugin < Vagrant.plugin("2")
name "Windows Reboot Plugin"
provisioner "reboot" do
class RebootProvisioner < Vagrant.plugin("2", :provisioner)
def initialize(machine, config)
super
end
def configure(root_config)
end
def provision
@machine.communicate.execute("Restart-Computer -Force")
begin
sleep 10
end until @machine.communicate.ready?
@machine.action('remap_shared_folders')
end
def cleanup
end
end
RebootProvisioner
end
end
@snowch
Copy link

snowch commented Jan 14, 2014

SharedFolder's doesn't seem to be available on Vagrant 1.4+ see https://gist.github.com/scraplesh/7767407

ERROR root: Failed to load plugin: ./windows_reboot_plugin
ERROR root:  -- Error: #<NameError: uninitialized constant VagrantPlugins::ProviderVirtualBox::Action::SharedFolders>

Do you have an idea how this could be fixed?

@dragon788
Copy link

They renamed SharedFolders to synced folders I believe. This code probably needs updated in light of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment