File: command.rb

package info (click to toggle)
vagrant 1.6.5%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,684 kB
  • ctags: 4,284
  • sloc: ruby: 40,505; sh: 147; makefile: 3; lisp: 1
file content (70 lines) | stat: -rw-r--r-- 2,196 bytes parent folder | download
1
2
3
4
5
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'optparse'

module VagrantPlugins
  module CommandSSH
    class Command < Vagrant.plugin("2", :command)
      def self.synopsis
        "connects to machine via SSH"
      end

      def execute
        options = {}

        opts = OptionParser.new do |o|
          o.banner = "Usage: vagrant ssh [options] [name] [-- extra ssh args]"
          o.separator ""
          o.separator "Options:"
          o.separator ""

          o.on("-c", "--command COMMAND", "Execute an SSH command directly") do |c|
            options[:command] = c
          end

          o.on("-p", "--plain", "Plain mode, leaves authentication up to user") do |p|
            options[:plain_mode] = p
          end
        end

        # Parse out the extra args to send to SSH, which is everything
        # after the "--"
        split_index = @argv.index("--")
        if split_index
          options[:ssh_args] = @argv.drop(split_index + 1)
          @argv              = @argv.take(split_index)
        end

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

        # Execute the actual SSH
        with_target_vms(argv, single_target: true) do |vm|
          ssh_opts = {
            plain_mode: options[:plain_mode],
            extra_args: options[:ssh_args]
          }

          if options[:command]
            @logger.debug("Executing single command on remote machine: #{options[:command]}")
            env = vm.action(:ssh_run,
                            ssh_opts: ssh_opts,
                            ssh_run_command: options[:command],)

            # Exit with the exit status of the command or a 0 if we didn't
            # get one.
            exit_status = env[:ssh_run_exit_status] || 0
            return exit_status
          else
            @logger.debug("Invoking `ssh` action on machine")
            vm.action(:ssh, ssh_opts: ssh_opts)

            # We should never reach this point, since the point of `ssh`
            # is to exec into the proper SSH shell, but we'll just return
            # an exit status of 0 anyways.
            return 0
          end
        end
      end
    end
  end
end