Class: Vagrant::Plugin::V2::Trigger

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/plugin/v2/trigger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, config, machine, ui) ⇒ Trigger

This class is responsible for setting up basic triggers that were defined inside a Vagrantfile.

Parameters:



24
25
26
27
28
29
30
31
# File 'lib/vagrant/plugin/v2/trigger.rb', line 24

def initialize(env, config, machine, ui)
  @env        = env
  @config     = config
  @machine    = machine
  @ui         = ui

  @logger = Log4r::Logger.new("vagrant::trigger::#{self.class.to_s.downcase}")
end

Instance Attribute Details

#configKernel_V2::Config::Trigger (readonly)

Returns:

  • (Kernel_V2::Config::Trigger)


15
16
17
# File 'lib/vagrant/plugin/v2/trigger.rb', line 15

def config
  @config
end

Instance Method Details

#fire_triggers(action, stage, guest_name, type) ⇒ Object

Fires all triggers, if any are defined for the action and guest. Returns early and logs a warning if the community plugin vagrant-triggers is installed

Parameters:

  • action (Symbol)

    Vagrant command to fire trigger on

  • stage (Symbol)

    :before or :after

  • guest_name (String)

    The guest that invoked firing the triggers



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vagrant/plugin/v2/trigger.rb', line 39

def fire_triggers(action, stage, guest_name, type)
  if community_plugin_detected?
    @logger.warn("Community plugin `vagrant-triggers detected, so core triggers will not fire")
    return
  end

  if !action
    @logger.warn("Action given is nil, no triggers will fire")
    return
  else
    action = action.to_sym
  end

  # get all triggers matching action
  triggers = []
  if stage == :before
    triggers = config.before_triggers.select do |t|
      t.command == action || (t.command == :all && !t.ignore.include?(action))
    end
  elsif stage == :after
    triggers = config.after_triggers.select do |t|
      t.command == action || (t.command == :all && !t.ignore.include?(action))
    end
  else
    raise Errors::TriggersNoStageGiven,
      action: action,
      stage: stage,
      guest_name: guest_name
  end

  triggers = filter_triggers(triggers, guest_name, type)

  if !triggers.empty?
    @logger.info("Firing trigger for action #{action} on guest #{guest_name}")
    @ui.info(I18n.t("vagrant.trigger.start", type: type, stage: stage, action: action))
    fire(triggers, guest_name)
  end
end