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:



27
28
29
30
31
32
33
34
# File 'lib/vagrant/plugin/v2/trigger.rb', line 27

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)


18
19
20
# File 'lib/vagrant/plugin/v2/trigger.rb', line 18

def config
  @config
end

Instance Method Details

#find(name, stage, guest, type, all: false) ⇒ Array

Find all triggers defined for the named type and guest.

Parameters:

  • name (Symbol)

    Name of type thing to fire trigger on

  • stage (Symbol)

    :before or :after

  • guest (String)

    The guest that invoked firing the triggers

  • type (Symbol)

    Type of trigger to fire

Returns:

  • (Array)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vagrant/plugin/v2/trigger.rb', line 72

def find(name, stage, guest, type, all: false)
  triggers = nil
  name = nameify(name)

  if stage == :before
    triggers = config.before_triggers.select do |t|
      (all && t.command.respond_to?(:to_sym) && t.command.to_sym == :all && !t.ignore.include?(name.to_sym)) ||
        (type == :hook && matched_hook?(t.command, name)) ||
        nameify(t.command) == name
    end
  elsif stage == :after
    triggers = config.after_triggers.select do |t|
      (all && t.command.respond_to?(:to_sym) && t.command.to_sym == :all && !t.ignore.include?(name.to_sym)) ||
        (type == :hook && matched_hook?(t.command, name)) ||
        nameify(t.command) == name
    end
  else
    raise Errors::TriggersNoStageGiven,
      name: name,
      stage: stage,
      type: type,
      guest_name: guest
  end

  filter_triggers(triggers, guest, type)
end

#fire(name, stage, guest, type, all: false) ⇒ Object

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

Parameters:

  • name (Symbol)

    Name of type thing to fire trigger on

  • stage (Symbol)

    :before or :after

  • guest (String)

    The guest that invoked firing the triggers

  • type (Symbol)

    Type of trigger to fire (:action, :hook, :command)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vagrant/plugin/v2/trigger.rb', line 43

def fire(name, stage, guest, type, all: false)
  if community_plugin_detected?
    @logger.warn("Community plugin `vagrant-triggers detected, so core triggers will not fire")
    return
  end

  return @logger.warn("Name given is nil, no triggers will fire") if !name
  return @logger.warn("Name given cannot be symbolized, no triggers will fire") if
    !name.respond_to?(:to_sym)

  name = name.to_sym

  # get all triggers matching action
  triggers = find(name, stage, guest, type, all: all)

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