Class: Vagrant::Command::SSH

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant/command/ssh.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Vagrant::Command::Base

Instance Method Details

#executeObject



6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vagrant/command/ssh.rb', line 6

def execute
  options = {}

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: vagrant ssh [vm-name] [-c command] [-- extra ssh args]"

    opts.separator ""

    opts.on("-c", "--command COMMAND", "Execute an SSH command directly.") do |c|
      options[:command] = c
    end
    opts.on("-p", "--plain", "Plain mode, leaves authentication up to user.") do |p|
      options[:plain_mode] = p
    end
  end

  # Parse the options and return if we don't have any target.
  argv = parse_options(opts)
  return if !argv

  # Parse out the extra args to send to SSH, which is everything
  # after the "--"
  ssh_args = ARGV.drop_while { |i| i != "--" }
  ssh_args = ssh_args[1..-1]
  options[:ssh_args] = ssh_args

  # If the remaining arguments ARE the SSH arguments, then just
  # clear it out. This happens because optparse returns what is
  # after the "--" as remaining ARGV, and Vagrant can think it is
  # a multi-vm name (wrong!)
  argv = [] if argv == ssh_args

  # Execute the actual SSH
  with_target_vms(argv[0], :single_target => true) do |vm|
    # Basic checks that are required for proper SSH
    raise Errors::VMNotCreatedError if !vm.created?
    raise Errors::VMInaccessible if !vm.state == :inaccessible
    raise Errors::VMNotRunningError if vm.state != :running

    if options[:command]
      ssh_execute(vm, options[:command])
    else
      opts = {
        :plain_mode => options[:plain_mode],
        :extra_args => options[:ssh_args]
      }

      ssh_connect(vm, opts)
    end
  end
end