Class: Vagrant::Action::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/runner.rb

Constant Summary collapse

@@reported_interrupt =
false

Instance Method Summary collapse

Constructor Details

#initialize(globals = nil, &block) ⇒ Runner

Returns a new instance of Runner.


12
13
14
15
16
# File 'lib/vagrant/action/runner.rb', line 12

def initialize(globals=nil, &block)
  @globals      = globals || {}
  @lazy_globals = block
  @logger       = Log4r::Logger.new("vagrant::action::runner")
end

Instance Method Details

#run(callable_id, options = nil) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vagrant/action/runner.rb', line 18

def run(callable_id, options=nil)
  callable = callable_id
  if !callable.kind_of?(Builder)
    if callable_id.kind_of?(Class) || callable_id.respond_to?(:call)
      callable = Builder.build(callable_id)
    end
  end

  if !callable || !callable.respond_to?(:call)
    raise ArgumentError,
      "Argument to run must be a callable object or registered action."
  end

  # Create the initial environment with the options given
  environment = {}
  environment.merge!(@globals)
  environment.merge!(@lazy_globals.call) if @lazy_globals
  environment.merge!(options || {})

  # NOTE: Triggers are initialized later in the Action::Runer because of
  # how `@lazy_globals` are evaluated. Rather than trying to guess where
  # the `env` is coming from, we can wait until they're merged into a single
  # hash above.
  env = environment[:env]
  machine = environment[:machine]

  environment[:triggers] = machine.triggers if machine

  if env
    ui = Vagrant::UI::Prefixed.new(env.ui, "vagrant")
    environment[:triggers] ||= Vagrant::Plugin::V2::Trigger.
      new(env, env.vagrantfile.config.trigger, machine, ui)
  end

  # Run the action chain in a busy block, marking the environment as
  # interrupted if a SIGINT occurs, and exiting cleanly once the
  # chain has been run.
  ui = environment[:ui] if environment.key?(:ui)
  int_callback = lambda do
    if environment[:interrupted]
      if ui
        begin
          ui.error I18n.t("vagrant.actions.runner.exit_immediately")
        rescue ThreadError
          # We're being called in a trap-context. Wrap in a thread.
          Thread.new {
            ui.error I18n.t("vagrant.actions.runner.exit_immediately")
          }.join(THREAD_MAX_JOIN_TIMEOUT)
        end
      end
      abort
    end

    if ui && !@@reported_interrupt
      begin
        ui.warn I18n.t("vagrant.actions.runner.waiting_cleanup")
      rescue ThreadError
        # We're being called in a trap-context. Wrap in a thread.
        Thread.new {
          ui.warn I18n.t("vagrant.actions.runner.waiting_cleanup")
        }.join(THREAD_MAX_JOIN_TIMEOUT)
      end
    end
    environment[:interrupted] = true
    @@reported_interrupt = true
  end

  action_name = environment[:action_name]

  # We place a process lock around every action that is called
  @logger.info("Running action: #{action_name} #{callable_id}")
  Util::Busy.busy(int_callback) { callable.call(environment) }

  # Return the environment in case there are things in there that
  # the caller wants to use.
  environment
end