Class: Vagrant::Action::Builtin::SSHRun

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

Overview

This class will run a single command on the remote machine and will mirror the output to the UI. The resulting exit status of the command will exist in the ‘:ssh_run_exit_status` key in the environment.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ SSHRun

Returns a new instance of SSHRun.



10
11
12
13
# File 'lib/vagrant/action/builtin/ssh_run.rb', line 10

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::action::builtin::ssh_run")
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vagrant/action/builtin/ssh_run.rb', line 15

def call(env)
  command = env[:ssh_run_command]

  @logger.debug("Executing command: #{command}")
  exit_status = 0
  exit_status = env[:machine].communicate.execute(command, :error_check => false) do |type, data|
    # Determine the proper channel to send the output onto depending
    # on the type of data we are receiving.
    channel = type == :stdout ? :out : :error

    # Print the output as it comes in, but don't prefix it and don't
    # force a new line so that the output is properly preserved however
    # it may be formatted.
    env[:ui].info(data.to_s,
                  :prefix => false,
                  :new_line => false,
                  :channel => channel)
  end

  # Set the exit status on a known environmental variable
  env[:ssh_run_exit_status] = exit_status

  # Call the next middleware
  @app.call(env)
end