File: exec.rb

package info (click to toggle)
vagrant 2.2.14%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,800 kB
  • sloc: ruby: 97,301; sh: 375; makefile: 16; lisp: 1
file content (109 lines) | stat: -rw-r--r-- 3,278 bytes parent folder | download | duplicates (5)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'vagrant/util/safe_exec'

module VagrantPlugins
  module DockerProvider
    module Command
      class Exec < Vagrant.plugin("2", :command)
        def self.synopsis
          "attach to an already-running docker container"
        end

        def execute
          options = {}
          options[:detach] = false
          options[:pty] = false
          options[:interactive] = false
          options[:prefix] = true

          opts = OptionParser.new do |o|
            o.banner = "Usage: vagrant docker-exec [options] [name] -- <command> [args]"
            o.separator ""
            o.separator "Options:"
            o.separator ""

            o.on("--[no-]detach", "Run in the background") do |d|
              options[:detach] = d
            end

            o.on("-i", "--[no-]interactive", "Keep STDIN open even if not attached") do |i|
              options[:interactive] = i
            end

            o.on("-t", "--[no-]tty", "Allocate a pty") do |t|
              options[:pty] = t
            end

            o.on("-u", "--user USER", "User or UID") do |u|
              options[:user] = u
            end

            o.on("--[no-]prefix", "Prefix output with machine names") do |p|
              options[:prefix] = p
            end
          end

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

          # Parse the options
          argv = parse_options(opts)
          return if !argv

          # Show the error if we don't have "--" _after_ parse_options
          # so that "-h" and "--help" work properly.
          if !split_index
            raise Errors::ExecCommandRequired
          end

          target_opts = { provider: :docker }
          target_opts[:single_target] = options[:pty]

          with_target_vms(argv, target_opts) do |machine|
            if machine.state.id != :running
              @env.ui.info("#{machine.id} is not running.")
              next
            end
            exec_command(machine, command, options)
          end

          return 0
        end

        def exec_command(machine, command, options)
          exec_cmd = %w(docker exec)
          exec_cmd << "-i" if options[:interactive]
          exec_cmd << "-t" if options[:pty]
          exec_cmd << "-u" << options[:user] if options[:user]
          exec_cmd << machine.id
          exec_cmd += options[:extra_args] if options[:extra_args]
          exec_cmd += command

          # Run this interactively if asked.
          exec_options = options

          if options[:pty]
            Vagrant::Util::SafeExec.exec(exec_cmd[0], *exec_cmd[1..-1])
          else
            output = ""
            machine.provider.driver.execute(*exec_cmd, exec_options) do |type, data|
              output += data
            end

            output_options = {}
            output_options[:prefix] = false if !options[:prefix]

            if !output.empty?
              machine.ui.output(output.chomp, **output_options)
            end
          end
        end
      end
    end
  end
end