Handle command triggers with run_remote options

This commit adds some handling around when a machine does not exist at
all but a trigger was defined with a run_remote option
This commit is contained in:
Brian Cain 2019-01-18 10:13:27 -08:00
parent 09846b30fe
commit 16b5ad74ca
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
4 changed files with 39 additions and 1 deletions

View File

@ -804,6 +804,10 @@ module Vagrant
error_key(:triggers_bad_exit_codes)
end
class TriggersGuestNotExist < VagrantError
error_key(:triggers_guest_not_exist)
end
class TriggersGuestNotRunning < VagrantError
error_key(:triggers_guest_not_running)
end

View File

@ -249,7 +249,16 @@ module Vagrant
#
# @param [ShellProvisioner/Config] config A Shell provisioner config
def run_remote(config, on_error, exit_codes)
unless @machine.state.id == :running
if !@machine
# machine doesn't even exist.
if on_error == :halt
raise Errors::TriggersGuestNotExist
else
@ui.warn(I18n.t("vagrant.errors.triggers_guest_not_exist"))
@ui.warn(I18n.t("vagrant.trigger.on_error_continue"))
return
end
elsif @machine.state.id != :running
if on_error == :halt
raise Errors::TriggersGuestNotRunning,
machine_name: @machine.name,

View File

@ -1502,6 +1502,8 @@ en:
Trigger run failed
triggers_guest_not_running: |-
Could not run remote script on %{machine_name} because its state is %{state}
triggers_guest_not_exist: |-
Could not run remote script on guest because it does not yet exist.
triggers_bad_exit_codes: |-
A script exited with an unacceptable exit code %{code}.
triggers_no_block_given: |-

View File

@ -319,6 +319,29 @@ describe Vagrant::Plugin::V2::Trigger do
trigger_run.finalize!
end
context "with no machine existing" do
let(:machine) { nil }
it "raises an error and halts if guest does not exist" do
trigger = trigger_run.after_triggers.first
shell_config = trigger.run_remote
on_error = trigger.on_error
exit_codes = trigger.exit_codes
expect { subject.send(:run_remote, shell_config, on_error, exit_codes) }.
to raise_error(Vagrant::Errors::TriggersGuestNotExist)
end
it "continues on if guest does not exist but is configured to continue on error" do
trigger = trigger_run.before_triggers.first
shell_config = trigger.run_remote
on_error = trigger.on_error
exit_codes = trigger.exit_codes
subject.send(:run_remote, shell_config, on_error, exit_codes)
end
end
it "raises an error and halts if guest is not running" do
allow(machine.state).to receive(:id).and_return(:not_running)