From 16b5ad74caedbc5a318697d1d83a02dcff26ac3e Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 18 Jan 2019 10:13:27 -0800 Subject: [PATCH] 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 --- lib/vagrant/errors.rb | 4 ++++ lib/vagrant/plugin/v2/trigger.rb | 11 +++++++++- templates/locales/en.yml | 2 ++ test/unit/vagrant/plugin/v2/trigger_test.rb | 23 +++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index fe146dc46..44bed4d4c 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -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 diff --git a/lib/vagrant/plugin/v2/trigger.rb b/lib/vagrant/plugin/v2/trigger.rb index 496fe925f..936a303c7 100644 --- a/lib/vagrant/plugin/v2/trigger.rb +++ b/lib/vagrant/plugin/v2/trigger.rb @@ -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, diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 230b09737..20f88a056 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -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: |- diff --git a/test/unit/vagrant/plugin/v2/trigger_test.rb b/test/unit/vagrant/plugin/v2/trigger_test.rb index c7f119d67..512bf360b 100644 --- a/test/unit/vagrant/plugin/v2/trigger_test.rb +++ b/test/unit/vagrant/plugin/v2/trigger_test.rb @@ -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)