Class: VagrantPlugins::DockerExec::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-docker-exec/command/exec.rb,
lib/vagrant-docker-exec/command/shell.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



4
5
6
# File 'lib/vagrant-docker-exec/command/exec.rb', line 4

def self.synopsis
  "run a new command in a running docker container"
end

Instance Method Details

#exec_command(machine, options, command) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-docker-exec/command/exec.rb', line 64

def exec_command(machine, options, command)

  exec_cmd = %W(docker exec)
  exec_cmd << "-i" << "-t" if options[:pty]
  exec_cmd << machine.id.to_s
  exec_cmd += options[:extra_args] if options[:extra_args]
  exec_cmd.concat(command)

  # Run this interactively if asked.
  exec_options = options
  exec_options[:stdin] = true if options[:pty]

  #@env.ui.info(exec_cmd.flatten)

  output = ""
  machine.provider.driver.execute(*exec_cmd, exec_options) do |type, data|
    output += data
  end

  output_options = {}
  machine.ui.output(output.chomp!, **output_options) if !output.empty?

end

#executeObject



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
# File 'lib/vagrant-docker-exec/command/exec.rb', line 8

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

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

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

    o.on("-t", "--[no-]tty", "Allocate a pty") do |t|
      options[:pty] = t
    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
    @env.ui.error(I18n.t("vagrant_docker_exec.exec_command_required"))
    return 1
  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

    # Run it!
    exec_command(machine, options, command)
  end

  return 0
end