Add abort option to core triggers
This commit adds a new option `abort`, which when configured, will exit the Vagrant process completely. If set to `true`, it will exit cleanly with exit code 0. Otherwise, the exit code can be configured.
This commit is contained in:
parent
7402823ad0
commit
58ebd52f99
|
@ -124,6 +124,10 @@ module Vagrant
|
||||||
warn(trigger.warn)
|
warn(trigger.warn)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if trigger.abort
|
||||||
|
trigger_abort(trigger.abort)
|
||||||
|
end
|
||||||
|
|
||||||
if trigger.run
|
if trigger.run
|
||||||
run(trigger.run, trigger.on_error, trigger.exit_codes)
|
run(trigger.run, trigger.on_error, trigger.exit_codes)
|
||||||
end
|
end
|
||||||
|
@ -241,6 +245,14 @@ module Vagrant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Exits Vagrant immediately
|
||||||
|
#
|
||||||
|
# @param [Integer] code Code to exit Vagrant on
|
||||||
|
def trigger_abort(exit_code)
|
||||||
|
@machine.ui.warn(I18n.t("vagrant.trigger.abort"))
|
||||||
|
exit(exit_code)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -72,6 +72,12 @@ module VagrantPlugins
|
||||||
# @return [Integer, Array]
|
# @return [Integer, Array]
|
||||||
attr_accessor :exit_codes
|
attr_accessor :exit_codes
|
||||||
|
|
||||||
|
# If set to true, trigger will halt Vagrant immediately and exit 0
|
||||||
|
# Can also be configured to have a custom exit code
|
||||||
|
#
|
||||||
|
# @return [Integer]
|
||||||
|
attr_accessor :abort
|
||||||
|
|
||||||
def initialize(command)
|
def initialize(command)
|
||||||
@logger = Log4r::Logger.new("vagrant::config::vm::trigger::config")
|
@logger = Log4r::Logger.new("vagrant::config::vm::trigger::config")
|
||||||
|
|
||||||
|
@ -84,6 +90,7 @@ module VagrantPlugins
|
||||||
@run = UNSET_VALUE
|
@run = UNSET_VALUE
|
||||||
@run_remote = UNSET_VALUE
|
@run_remote = UNSET_VALUE
|
||||||
@exit_codes = UNSET_VALUE
|
@exit_codes = UNSET_VALUE
|
||||||
|
@abort = UNSET_VALUE
|
||||||
|
|
||||||
# Internal options
|
# Internal options
|
||||||
@id = SecureRandom.uuid
|
@id = SecureRandom.uuid
|
||||||
|
@ -104,6 +111,7 @@ module VagrantPlugins
|
||||||
@run_remote = nil if @run_remote == UNSET_VALUE
|
@run_remote = nil if @run_remote == UNSET_VALUE
|
||||||
@only_on = nil if @only_on == UNSET_VALUE
|
@only_on = nil if @only_on == UNSET_VALUE
|
||||||
@exit_codes = DEFAULT_EXIT_CODE if @exit_codes == UNSET_VALUE
|
@exit_codes = DEFAULT_EXIT_CODE if @exit_codes == UNSET_VALUE
|
||||||
|
@abort = nil if @abort == UNSET_VALUE
|
||||||
|
|
||||||
# these values are expected to always be an Array internally,
|
# these values are expected to always be an Array internally,
|
||||||
# but can be set as a single String or Symbol
|
# but can be set as a single String or Symbol
|
||||||
|
@ -149,6 +157,9 @@ module VagrantPlugins
|
||||||
@run_remote = new_run
|
@run_remote = new_run
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if @abort == true
|
||||||
|
@abort = 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Array] array of strings of error messages from config option validation
|
# @return [Array] array of strings of error messages from config option validation
|
||||||
|
@ -208,6 +219,12 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if @abort && !@abort.is_a?(Integer)
|
||||||
|
errors << I18n.t("vagrant.config.triggers.abort_bad_type", cmd: @command)
|
||||||
|
elsif @abort == false
|
||||||
|
machine.ui.warn(I18n.t("vagrant.config.triggers.abort_false_type"))
|
||||||
|
end
|
||||||
|
|
||||||
errors
|
errors
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -287,6 +287,8 @@ en:
|
||||||
trigger:
|
trigger:
|
||||||
on_error_continue: |-
|
on_error_continue: |-
|
||||||
Trigger configured to continue on error...
|
Trigger configured to continue on error...
|
||||||
|
abort: |-
|
||||||
|
Vagrant has been configured to abort. Terminating now...
|
||||||
start: |-
|
start: |-
|
||||||
Running triggers %{stage} %{action} ...
|
Running triggers %{stage} %{action} ...
|
||||||
fire_with_name: |-
|
fire_with_name: |-
|
||||||
|
@ -1768,6 +1770,12 @@ en:
|
||||||
on_error_bad_type: |-
|
on_error_bad_type: |-
|
||||||
Invalid type set for `on_error` on trigger for command '%{cmd}'. `on_error` can
|
Invalid type set for `on_error` on trigger for command '%{cmd}'. `on_error` can
|
||||||
only be `:halt` (default) or `:continue`.
|
only be `:halt` (default) or `:continue`.
|
||||||
|
abort_bad_type: |-
|
||||||
|
Trigger option `abort` for command '%{cmd}' must be either set to
|
||||||
|
`true` (defaults to exit code 1) or an integer to use as an exit code.
|
||||||
|
abort_false_type: |-
|
||||||
|
`false` is not a valid option for the `abort` option for a trigger. This
|
||||||
|
will be ignored...
|
||||||
exit_codes_bad_type: |-
|
exit_codes_bad_type: |-
|
||||||
Invalid type set for `exit_codes` on trigger for command '%{cmd}'. `exit_codes` can
|
Invalid type set for `exit_codes` on trigger for command '%{cmd}'. `exit_codes` can
|
||||||
only be a single integer or an array of integers.
|
only be a single integer or an array of integers.
|
||||||
|
|
|
@ -66,6 +66,7 @@ describe VagrantPlugins::Kernel_V2::VagrantConfigTrigger do
|
||||||
before do
|
before do
|
||||||
cfg.only_on = :guest
|
cfg.only_on = :guest
|
||||||
cfg.ignore = "up"
|
cfg.ignore = "up"
|
||||||
|
cfg.abort = true
|
||||||
arr_cfg.only_on = ["guest", /other/]
|
arr_cfg.only_on = ["guest", /other/]
|
||||||
arr_cfg.ignore = ["up", "destroy"]
|
arr_cfg.ignore = ["up", "destroy"]
|
||||||
end
|
end
|
||||||
|
@ -93,6 +94,12 @@ describe VagrantPlugins::Kernel_V2::VagrantConfigTrigger do
|
||||||
expect(a).to be_a(Symbol)
|
expect(a).to be_a(Symbol)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "converts aborts true to exit code 0" do
|
||||||
|
cfg.finalize!
|
||||||
|
|
||||||
|
expect(cfg.abort).to eq(1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "defining a basic trigger config" do
|
describe "defining a basic trigger config" do
|
||||||
|
@ -104,6 +111,7 @@ describe VagrantPlugins::Kernel_V2::VagrantConfigTrigger do
|
||||||
cfg.warn = "Warning!!"
|
cfg.warn = "Warning!!"
|
||||||
cfg.on_error = :continue
|
cfg.on_error = :continue
|
||||||
cfg.ignore = :up
|
cfg.ignore = :up
|
||||||
|
cfg.abort = 3
|
||||||
cfg.only_on = "guest"
|
cfg.only_on = "guest"
|
||||||
cfg.run = {inline: "apt-get update"}
|
cfg.run = {inline: "apt-get update"}
|
||||||
cfg.run_remote = {inline: "apt-get update", env: {"VAR"=>"VAL"}}
|
cfg.run_remote = {inline: "apt-get update", env: {"VAR"=>"VAL"}}
|
||||||
|
@ -118,6 +126,7 @@ describe VagrantPlugins::Kernel_V2::VagrantConfigTrigger do
|
||||||
expect(cfg.only_on).to eq(["guest"])
|
expect(cfg.only_on).to eq(["guest"])
|
||||||
expect(cfg.run).to be_a(VagrantPlugins::Shell::Config)
|
expect(cfg.run).to be_a(VagrantPlugins::Shell::Config)
|
||||||
expect(cfg.run_remote).to be_a(VagrantPlugins::Shell::Config)
|
expect(cfg.run_remote).to be_a(VagrantPlugins::Shell::Config)
|
||||||
|
expect(cfg.abort).to eq(3)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -373,4 +373,16 @@ describe Vagrant::Plugin::V2::Trigger do
|
||||||
to raise_error("Nope!")
|
to raise_error("Nope!")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "#trigger_abort" do
|
||||||
|
|
||||||
|
it "system exits when called" do
|
||||||
|
output = ""
|
||||||
|
allow(machine.ui).to receive(:warn) do |data|
|
||||||
|
output << data
|
||||||
|
end
|
||||||
|
|
||||||
|
expect { subject.send(:trigger_abort, 3) }.to raise_error(SystemExit)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,3 +44,5 @@ The trigger class takes various options.
|
||||||
* `warn` (string) - A warning message that will be printed at the beginning of a trigger.
|
* `warn` (string) - A warning message that will be printed at the beginning of a trigger.
|
||||||
|
|
||||||
* `exit_codes` (integer, array) - A set of acceptable exit codes to continue on. Defaults to `0` if option is absent. For now only valid with the `run` option.
|
* `exit_codes` (integer, array) - A set of acceptable exit codes to continue on. Defaults to `0` if option is absent. For now only valid with the `run` option.
|
||||||
|
|
||||||
|
* `abort` (integer,boolean) - An option that will exit the running Vagrant process once the trigger fires. If set to `true`, Vagrant will use exit code 1. Otherwise, an integer can be provided and Vagrant will it as its exit code when aborting.
|
||||||
|
|
Loading…
Reference in New Issue