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

Inherits:
Object
  • Object
show all
Includes:
Util
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.



20
21
22
23
# File 'lib/vagrant/action/builtin/ssh_run.rb', line 20

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

Instance Method Details

#_raw_ssh_exec(env, info, opts) ⇒ Object



82
83
84
# File 'lib/vagrant/action/builtin/ssh_run.rb', line 82

def _raw_ssh_exec(env, info, opts)
  Util::SSH.exec(info, opts)
end

#call(env) ⇒ Object



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
# File 'lib/vagrant/action/builtin/ssh_run.rb', line 25

def call(env)
  # Grab the SSH info from the machine or the environment
  info = env[:ssh_info]
  info ||= env[:machine].ssh_info

  # If the result is nil, then the machine is telling us that it is
  # not yet ready for SSH, so we raise this exception.
  raise Errors::SSHNotReady if info.nil?

  info[:private_key_path] ||= []

  if info[:keys_only] && info[:private_key_path].empty?
    raise Errors::SSHRunRequiresKeys
  end

  # Get the command and wrap it in a login shell
  command = ShellQuote.escape(env[:ssh_run_command], "'")

  if env[:machine].config.vm.communicator == :winssh
    shell = env[:machine].config.winssh.shell
  else
    shell = env[:machine].config.ssh.shell
  end

  if shell == "cmd"
    # Add an extra space to the command so cmd.exe quoting works
    # properly
    command = "#{shell} /C #{command} "
  elsif shell == "powershell"
    command = "$ProgressPreference = \"SilentlyContinue\"; #{command}"
    command = Base64.strict_encode64(command.encode("UTF-16LE", "UTF-8"))
    command = "#{shell} -encodedCommand #{command}"
  else
    command = "#{shell} -c '#{command}'"
  end

  # Execute!
  opts = env[:ssh_opts] || {}
  opts[:extra_args] ||= []

  # Allow the user to specify a tty or non-tty manually, but if they
  # don't then we default to a TTY unless they are using WinSSH
  if !opts[:extra_args].include?("-t") &&
      !opts[:extra_args].include?("-T") &&
      env[:tty] &&
      env[:machine].config.vm.communicator != :winssh
    opts[:extra_args] << "-t"
  end

  opts[:extra_args] << command
  opts[:subprocess] = true
  env[:ssh_run_exit_status] = _raw_ssh_exec(env, info, opts)

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