Class: Cutlass::ContainerControl

Inherits:
Object
  • Object
show all
Defined in:
lib/cutlass/container_control.rb

Overview

This class is exposed via a ContainerBoot instance

Once a container is booted, if a port is bound an instance will return the local port that can be used to send network requests to the container.

In addition bash commands can be executed via ContainerControl#bash_exec

Instance Method Summary collapse

Constructor Details

#initialize(container, ports: []) ⇒ ContainerControl

Returns a new instance of ContainerControl.



12
13
14
15
# File 'lib/cutlass/container_control.rb', line 12

def initialize(container, ports: [])
  @container = container
  @ports = ports
end

Instance Method Details

#bash_exec(cmd, exception_on_failure: true) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cutlass/container_control.rb', line 37

def bash_exec(cmd, exception_on_failure: true)
  stdout_ish, stderr, status = @container.exec(["bash", "-c", cmd])
  stdout = stdout_ish.first

  result = BashResult.new(stdout: stdout, stderr: stderr, status: status)

  return result if result.success?
  return result unless exception_on_failure

  raise "    bash_exec(\#{cmd}) failed\n\n    stdout: \#{stdout}\n    stderr: \#{stderr}\n  EOM\nend\n"

#contains_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/cutlass/container_control.rb', line 29

def contains_file?(path)
  bash_exec("[[ -f '#{path}' ]]", exception_on_failure: false).status == 0
end

#get_file_contents(path) ⇒ Object



33
34
35
# File 'lib/cutlass/container_control.rb', line 33

def get_file_contents(path)
  bash_exec("cat '#{path}'").stdout
end

#get_host_port(port) ⇒ Object



24
25
26
27
# File 'lib/cutlass/container_control.rb', line 24

def get_host_port(port)
  raise "Port not bound inside container: #{port}, bound ports: #{@ports.inspect}" unless @ports.include?(port)
  @container.json["NetworkSettings"]["Ports"]["#{port}/tcp"][0]["HostPort"]
end

#logsObject



17
18
19
20
21
22
# File 'lib/cutlass/container_control.rb', line 17

def logs
  stdout = @container.logs(stdout: 1)
  stderr = @container.logs(stderr: 1)

  BashResult.new(stdout: stdout, stderr: stderr, status: 0)
end