Class: VagrantPlugins::Triggers::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-triggers/dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine, options = {}) ⇒ DSL

Returns a new instance of DSL.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant-triggers/dsl.rb', line 8

def initialize(machine, options = {})
  if options[:vm]
    match = false
    Array(options[:vm]).each do |pattern|
      match = true if machine.name.match(Regexp.new(pattern))
    end
    raise Errors::NotMatchingMachine unless match
  end

  @buffer  = Hash.new("")
  @logger  = Log4r::Logger.new("vagrant::plugins::triggers::dsl")
  @machine = machine
  @options = options
  @ui      = machine.ui

  @command_output = lambda do |channel, data, options|
    ui_method = (channel == :stdout) ? :info : :error
    @buffer[channel] += data
    @ui.send(ui_method, data) if options[channel]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/vagrant-triggers/dsl.rb', line 72

def method_missing(method, *args, &block)
  # If the @ui object responds to the given method, call it
  if @ui.respond_to?(method)
    @ui.send(method, *args, *block)
  else
    super(method, *args, &block)
  end
end

Instance Method Details

#error(message, *opts) ⇒ Object

Raises:



30
31
32
# File 'lib/vagrant-triggers/dsl.rb', line 30

def error(message, *opts)
  raise Errors::DSLError, @ui.error(message, *opts)
end

#run(raw_command, options = {}) ⇒ Object Also known as: execute



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vagrant-triggers/dsl.rb', line 34

def run(raw_command, options = {})
  command = shellsplit(raw_command)
  options.merge!(@options) { |key, old, new| old }
  info I18n.t("vagrant_triggers.action.trigger.executing_command", :command => command.join(" "))
  env_backup = ENV.to_hash
  begin
    result = nil
    Bundler.with_clean_env do
      build_environment
      @buffer.clear
      Dir.chdir(@machine.env.root_path) do
        result = Vagrant::Util::Subprocess.execute(command[0], *command[1..-1], :notify => [:stdout, :stderr]) do |channel, data|
          @command_output.call(channel, data, options)
        end
      end
    end
    info I18n.t("vagrant_triggers.action.trigger.command_finished")
  rescue Vagrant::Errors::CommandUnavailable, Vagrant::Errors::CommandUnavailableWindows
    raise Errors::CommandUnavailable, :command => command[0]
  ensure
    ENV.replace(env_backup)
  end
  process_result(raw_command, result, options)
end

#run_remote(raw_command, options = {}) ⇒ Object Also known as: execute_remote



60
61
62
63
64
65
66
67
68
69
# File 'lib/vagrant-triggers/dsl.rb', line 60

def run_remote(raw_command, options = {})
  options.merge!(@options) { |key, old, new| old }
  info I18n.t("vagrant_triggers.action.trigger.executing_remote_command", :command => raw_command)
  @buffer.clear
  exit_code = @machine.communicate.sudo(raw_command, :elevated => true, :good_exit => (0..255).to_a) do |channel, data|
    @command_output.call(channel, data, options)
  end
  info I18n.t("vagrant_triggers.action.trigger.remote_command_finished")
  process_result(raw_command, Vagrant::Util::Subprocess::Result.new(exit_code, @buffer[:stdout], @buffer[:stderr]), options)
end