-
Notifications
You must be signed in to change notification settings - Fork 57
/
.vagrant-provision-reboot-plugin.rb
139 lines (121 loc) · 4.15 KB
/
.vagrant-provision-reboot-plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# A quick hack to allow rebooting of a Vagrant VM during provisioning.
#
# This is tested with Vagrant 1.4.3. It may work with slightly earlier versions,
# but definitely won't work with much earlier versions. The code is fragile with
# respect to internal changes in Vagrant.
#
# Adapted from: https://gist.github.com/ukabu/6780121
#
# This file should be placed into the same folder as your Vagrantfile. Then in
# your Vagrantfile, you'll want to do something like the following:
#
# ----------------------------------------------------------------------------
#
# require './vagrant-provision-reboot-plugin'
#
# Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
#
# # Run your pre-reboot provisioning block.
# #config.vm.provision :chef_solo do |chef|
# # ...
# #end
#
# # Run a reboot of a *NIX guest.
# config.vm.provision :unix_reboot
# # Run a reboot of a Windows guest, assuming that you are set up with the
# # relevant plugins and configurations to manage a Windows guest in Vagrant.
# #config.vm.provision :windows_reboot
#
# # Run your post-reboot provisioning block.
# #config.vm.provision :chef_solo do |chef|
# # ...
# #end
#
# ----------------------------------------------------------------------------
#
# The provisioner takes care of remounting the synced folders.
#
# This will work for the VirtualBox provider. For other providers, a
# 'remount_synched_folders' action must be added to the provider implementation.
require 'vagrant'
# Monkey-patch the VirtualBox provider to be able to remap synced folders after
# reboot.
#
# This involves pulling out some code fragments from the existing SyncedFolders
# class - which is unpleasant, but there are no usefully exposed methods such
# that we can run only what we need to.
module VagrantPlugins
module ProviderVirtualBox
module Action
class RemountSyncedFolders < SyncedFolders
def initialize(app, env)
super(app, env)
end
def call(env)
@env = env
@app.call(env)
# Copied out of /lib/vagrant/action/builtin/synced_folders.rb in
# Vagrant 1.4.3. This is going to be fragile with respect to future
# changes, but that's just the way the cookie crumbles.
#
# We can't just run the whole SyncedFolders.call() method because
# it undertakes a lot more setup and will error out if invoked twice
# during "vagrant up" or "vagrant provision".
folders = synced_folders(env[:machine])
folders.each do |impl_name, fs|
plugins[impl_name.to_sym][0].new.enable(env[:machine], fs, impl_opts(impl_name, env))
end
end
end
def self.action_remount_synced_folders
Vagrant::Action::Builder.new.tap do |b|
b.use RemountSyncedFolders
end
end
end
end
end
# Define the plugin.
class RebootPlugin < Vagrant.plugin('2')
name 'Reboot Plugin'
# This plugin provides a provisioner called unix_reboot.
provisioner(:unix_reboot) do
# Create a provisioner.
class RebootProvisioner < Vagrant.plugin('2', :provisioner)
# Initialization, define internal state. Nothing needed.
def initialize(machine, config)
super(machine, config)
end
# Configuration changes to be done. Nothing needed here either.
def configure(root_config)
super(root_config)
end
# Run the provisioning.
def provision
command = 'shutdown -r now'
@machine.ui.info("Issuing command: #{command}")
@machine.communicate.sudo(command) do |type, data|
if type == :stderr
@machine.ui.error(data);
end
end
begin
sleep 5
end until @machine.communicate.ready?
# Now the machine is up again, perform the necessary tasks.
@machine.ui.info("Launching remount_synced_folders action...")
@machine.action('remount_synced_folders')
end
# Nothing needs to be done on cleanup.
def cleanup
super
end
end
RebootProvisioner
end
config(:unix_reboot, :provisioner) do
class Config < Vagrant.plugin(2, :config)
end
Config
end
end