Add reboot option to shell provisioner

Adds a `reboot` option which allows the guest to be rebooted after
the completion of a shell provisioning task. The guest must support
the `:reboot` capability. Like the `reset` option, the `reboot`
option may be provided without defining `inline` or `file` options
when a reboot may be required between other provisioners.

Fixes #8639
This commit is contained in:
Chris Roberts 2018-12-21 15:50:59 -08:00
parent 1f565b8f27
commit e735453422
5 changed files with 79 additions and 7 deletions

View File

@ -17,6 +17,7 @@ module VagrantPlugins
attr_accessor :sensitive
attr_accessor :powershell_args
attr_accessor :powershell_elevated_interactive
attr_accessor :reboot
attr_accessor :reset
def initialize
@ -32,6 +33,7 @@ module VagrantPlugins
@keep_color = UNSET_VALUE
@name = UNSET_VALUE
@sensitive = UNSET_VALUE
@reboot = UNSET_VALUE
@reset = UNSET_VALUE
@powershell_args = UNSET_VALUE
@powershell_elevated_interactive = UNSET_VALUE
@ -50,6 +52,7 @@ module VagrantPlugins
@keep_color = false if @keep_color == UNSET_VALUE
@name = nil if @name == UNSET_VALUE
@sensitive = false if @sensitive == UNSET_VALUE
@reboot = false if @reboot == UNSET_VALUE
@reset = false if @reset == UNSET_VALUE
@powershell_args = "-ExecutionPolicy Bypass" if @powershell_args == UNSET_VALUE
@powershell_elevated_interactive = false if @powershell_elevated_interactive == UNSET_VALUE
@ -71,7 +74,7 @@ module VagrantPlugins
# Validate that the parameters are properly set
if path && inline
errors << I18n.t("vagrant.provisioners.shell.path_and_inline_set")
elsif !path && !inline && !reset
elsif !path && !inline && !reset && !reboot
errors << I18n.t("vagrant.provisioners.shell.no_path_or_inline")
end

View File

@ -31,7 +31,11 @@ module VagrantPlugins
provision_ssh(args)
end
ensure
@machine.communicate.reset! if config.reset
if config.reboot
@machine.guest.capability(:reboot)
else
@machine.communicate.reset! if config.reset
end
end
protected

View File

@ -134,6 +134,14 @@ describe "VagrantPlugins::Shell::Config" do
result = subject.validate(machine)
expect(result["shell provisioner"]).to be_empty
end
it "returns no error when inline and path are unset but reboot is true" do
subject.reboot = true
subject.finalize!
result = subject.validate(machine)
expect(result["shell provisioner"]).to be_empty
end
end
describe 'finalize!' do

View File

@ -31,7 +31,8 @@ describe "Vagrant::Shell::Provisioner" do
:path => path,
:inline => inline,
:binary => false,
:reset => true
:reset => true,
:reboot => false,
)
}
@ -62,6 +63,55 @@ describe "Vagrant::Shell::Provisioner" do
end
end
context "when reboot is enabled" do
let(:path) { nil }
let(:inline) { "" }
let(:communicator) { double("communicator") }
let(:guest) { double("guest") }
let(:config) {
double(
:config,
:args => "doesn't matter",
:env => {},
:upload_path => "arbitrary",
:remote? => false,
:path => path,
:inline => inline,
:binary => false,
:reset => false,
:reboot => true
)
}
let(:vsp) {
VagrantPlugins::Shell::Provisioner.new(machine, config)
}
before {
allow(machine).to receive(:communicate).and_return(communicator)
allow(machine).to receive(:guest).and_return(guest)
allow(vsp).to receive(:provision_ssh)
}
it "should provision and then reboot the guest" do
expect(vsp).to receive(:provision_ssh)
expect(guest).to receive(:capability).with(:reboot)
vsp.provision
end
context "when path and inline are not set" do
let(:path) { nil }
let(:inline) { nil }
it "should reboot the guest and not provision" do
expect(vsp).not_to receive(:provision_ssh)
expect(guest).to receive(:capability).with(:reboot)
vsp.provision
end
end
end
context "with a script that contains invalid us-ascii byte sequences" do
let(:config) {
double(
@ -73,7 +123,8 @@ describe "Vagrant::Shell::Provisioner" do
:path => nil,
:inline => script_that_is_incorrectly_us_ascii_encoded,
:binary => false,
:reset => false
:reset => false,
:reboot => false
)
}
@ -106,7 +157,8 @@ describe "Vagrant::Shell::Provisioner" do
:path => nil,
:inline => script,
:binary => false,
:reset => false
:reset => false,
:reboot => false
)
}
@ -136,7 +188,8 @@ describe "Vagrant::Shell::Provisioner" do
:binary => false,
:md5 => nil,
:sha1 => 'EXPECTED_VALUE',
:reset => false
:reset => false,
:reboot => false
)
}
@ -167,7 +220,8 @@ describe "Vagrant::Shell::Provisioner" do
:binary => false,
:md5 => 'EXPECTED_VALUE',
:sha1 => nil,
:reset => false
:reset => false,
:reboot => false
)
}

View File

@ -75,6 +75,9 @@ The remainder of the available options are optional:
guests use a scheduled task to run as a true administrator without the
WinRM limitations.
* `reboot` (boolean) - Reboot the guest. This requires the guest to have a
reboot capability implemented.
* `reset` (boolean) - Reset the communicator to the machine after completion. This
is useful when a shell may need to be reloaded.