Class: VagrantPlugins::Triggers::DSL
- Inherits:
-
Object
- Object
- VagrantPlugins::Triggers::DSL
- Defined in:
- lib/vagrant-triggers/dsl.rb
Class Method Summary collapse
Instance Method Summary collapse
- #error(message, *opts) ⇒ Object
-
#initialize(machine, options = {}) ⇒ DSL
constructor
A new instance of DSL.
- #method_missing(method, *args, &block) ⇒ Object
- #run(raw_command, options = {}) ⇒ Object (also: #execute)
- #run_remote(raw_command, options = {}) ⇒ Object (also: #execute_remote)
Constructor Details
#initialize(machine, options = {}) ⇒ DSL
Returns a new instance of DSL.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vagrant-triggers/dsl.rb', line 16 def initialize(machine, = {}) if [:vm] match = false Array([: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 = { :good_exit => [0] }.merge() @ui = machine.ui @command_output = lambda do |channel, data, | ui_method = (channel == :stdout) ? :info : :error @buffer[channel] += data @ui.send(ui_method, data) if [channel] end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/vagrant-triggers/dsl.rb', line 85 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 |
Class Method Details
.fire!(trigger, machine) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/vagrant-triggers/dsl.rb', line 8 def self.fire!(trigger, machine) begin dsl = new(machine, trigger[:options]) dsl.instance_eval &trigger[:proc] if trigger[:proc] rescue Errors::NotMatchingMachine end end |
Instance Method Details
#error(message, *opts) ⇒ Object
38 39 40 |
# File 'lib/vagrant-triggers/dsl.rb', line 38 def error(, *opts) raise Errors::DSLError, @ui.error(, *opts) end |
#run(raw_command, options = {}) ⇒ Object Also known as: execute
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vagrant-triggers/dsl.rb', line 42 def run(raw_command, = {}) command = shellsplit(raw_command) .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, ) 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("local", raw_command, result, ) end |
#run_remote(raw_command, options = {}) ⇒ Object Also known as: execute_remote
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/vagrant-triggers/dsl.rb', line 68 def run_remote(raw_command, = {}) .merge!(@options) { |key, old, new| old } info I18n.t("vagrant_triggers.action.trigger.executing_remote_command", :command => raw_command) @buffer.clear exit_code = 1 begin exit_code = @machine.communicate.sudo(raw_command, :elevated => true, :good_exit => (0..255).to_a) do |channel, data| @command_output.call(channel, data, ) end rescue => e @command_output.call(:stderr, e., ) end info I18n.t("vagrant_triggers.action.trigger.remote_command_finished") process_result("remote", raw_command, Vagrant::Util::Subprocess::Result.new(exit_code, @buffer[:stdout], @buffer[:stderr]), ) end |