Class: DockerMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_machine.rb,
lib/docker_machine/version.rb

Defined Under Namespace

Classes: CLIError

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errObject (readonly)

Returns the value of attribute err.



14
15
16
# File 'lib/docker_machine.rb', line 14

def err
  @err
end

#outObject (readonly)

Returns the value of attribute out.



14
15
16
# File 'lib/docker_machine.rb', line 14

def out
  @out
end

Instance Method Details

#call(cli_args, opts = {}) ⇒ Object

Public: Stupid simple interface to docker-machine cli.

cli_args: String of direct passthrough to docker-machine. E.g.

DockerMachine.new.call('ls --format "{{.Name}}"')

opts: Hash of options for DockerMachine to use internally (default: {}).

    Keys are symbols
:stream_logs - Boolean to either stream the command logs to STDOUT/ERR or
               just save them into #out and #err (default: nil).
:debug - Boolean if you would like extra output (default: nil).


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/docker_machine.rb', line 26

def call cli_args, opts = {}
  if opts[:stream_logs]
    err_wr = :err
    out_wr = :out
  else
    err_rd, err_wr = IO.pipe
    out_rd, out_wr = IO.pipe
  end

  cmd = "docker-machine #{cli_args}"
  puts "Command to docker-machine is: `#{cmd}`" if opts[:debug] == true

  Process.wait spawn(
    'docker-machine', *cli_args.split, err: err_wr, out: out_wr
  )

  code = $?.exitstatus
  case code
  when 0
    true
  end
ensure
  # cleanup the pipes
  err_wr.close if err_wr.respond_to? :close
  out_wr.close if out_wr.respond_to? :close
  if out_rd.respond_to? :close
    @out = out_rd.read
    out_rd.close
  end
  if err_rd.respond_to? :close
    @err = err_rd.read
    err_rd.close
  end

  # handle exit, after because CLIError uses @err and @out
  raise DockerMachine::CLIError.new self, code if code > 0
end