Filter triggers based on only_on restraint

This commit is contained in:
Brian Cain 2018-03-29 10:22:57 -07:00
parent 09bb98679c
commit eac2fcf71e
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
3 changed files with 52 additions and 12 deletions

View File

@ -10,8 +10,7 @@ module Vagrant
attr_reader :config
# This class is responsible for setting up basic triggers that were
# defined inside a Vagrantfile. It should take the Trigger config
# and convert it to action hooks.
# defined inside a Vagrantfile.
#
# @param [Object] env Vagrant environment
# @param [Object] ui Machines ui object
@ -33,7 +32,6 @@ module Vagrant
triggers = config.before_triggers.select { |t| t.command == action }
triggers = filter_triggers(triggers, guest_name)
binding.pry
unless triggers.empty?
@logger.info("Firing trigger for action #{action} on guest #{guest_name}")
# TODO I18N me
@ -51,7 +49,6 @@ module Vagrant
triggers = config.after_triggers.select { |t| t.command == action }
triggers = filter_triggers(triggers, guest_name)
binding.pry
unless triggers.empty?
@logger.info("Firing triggers for action #{action} on guest #{guest_name}")
# TODO I18N me
@ -66,13 +63,28 @@ module Vagrant
# Internal methods, don't call these.
#-------------------------------------------------------------------
# Filters triggers to be fired based on restraints
# Filters triggers to be fired based on configured restraints
#
# @param [Array] triggers An array of triggers to be filtered
# @param [String] guest_name The name of the current guest
# @return [Array] The filtered array of triggers
def filter_triggers(triggers, guest_name)
binding.pry
# look for only_on triggers and if it doesn't match guest name, throw it away
# look for only_on trigger constraint and if it doesn't match guest
# name, throw it away also be sure to preserve order
filter = triggers.dup
filter.each do |trigger|
index = nil
if !trigger.only_on.nil?
index = trigger.only_on.index { |i| i.match?(guest_name) }
end
if !index.nil?
@logger.debug("Trigger #{trigger.id} will be ignored for #{guest_name}")
triggers.delete_at(index)
end
end
return triggers
end
@ -83,6 +95,34 @@ module Vagrant
# ensure on_error is respected by exiting or continuing
triggers.each do |trigger|
@logger.debug("Running trigger #{trigger.id}...")
# TODO: I18n me
if !trigger.name.nil?
@machine_ui.info("Running trigger: #{trigger.name}...")
else
@machine_ui.info("Running trigger...")
end
if !trigger.info.nil?
@logger.debug("Executing trigger info message...")
self.info(trigger.info)
end
if !trigger.warn.nil?
@logger.debug("Executing trigger warn message...")
self.warn(trigger.info)
end
if !trigger.run.nil?
@logger.debug("Executing trigger run script...")
self.run(trigger.run, trigger.on_error)
end
if !trigger.run_remote.nil?
@logger.debug("Executing trigger run_remote script on #{guest_name}...")
self.run_remote(trigger.run, trigger.on_error, guest_name)
end
end
end
@ -103,15 +143,13 @@ module Vagrant
# Runs a script on a guest
#
# @param [ShellProvisioner/Config] config A Shell provisioner config
def run(config)
@logger.info("Running script on the host...")
def run(config, on_error)
end
# Runs a script on the host
#
# @param [ShellProvisioner/Config] config A Shell provisioner config
def run_remote(config)
@logger.info("Running script on the guest...")
def run_remote(config, on_error, guest_name)
# make sure guest actually exists, if not, display a WARNING
end
end

View File

@ -4,6 +4,9 @@ require File.expand_path("../vm_trigger", __FILE__)
module VagrantPlugins
module Kernel_V2
class TriggerConfig < Vagrant.plugin("2", :config)
# The TriggerConfig class is what gets called when a user
# defines a new trigger in their Vagrantfile. The two entry points are
# either `config.trigger.before` or `config.trigger.after`.
def initialize
@logger = Log4r::Logger.new("vagrant::config::trigger")

View File

@ -103,7 +103,6 @@ module VagrantPlugins
# Guests are stored internally as strings
if !@only_on.nil?
@only_on = Array(@only_on)
@only_on.map! { |o| o.to_s }
end
# Commands must be stored internally as symbols