Class: Longshoreman::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/longshoreman/container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image = nil, name = nil, extra_args = {}) ⇒ Container

Returns a new instance of Container.



3
4
5
6
7
8
9
10
# File 'lib/longshoreman/container.rb', line 3

def initialize(image = nil, name = nil, extra_args = {})
  if image && name
    create(image, name, extra_args)
    start
  else
    @raw = nil
  end
end

Instance Attribute Details

#rawObject

Returns the value of attribute raw.



94
95
96
# File 'lib/longshoreman/container.rb', line 94

def raw
  @raw
end

Instance Method Details

#all(opts = {}) ⇒ Object



12
13
14
# File 'lib/longshoreman/container.rb', line 12

def all(opts = {})
  @raw.all(opts)
end

#cleanupObject

Wrapper to clean up container in a single call name can be a name or id



18
19
20
21
22
23
# File 'lib/longshoreman/container.rb', line 18

def cleanup
  if @raw
    @raw.stop
    @raw.delete(:force => true)
  end
end

#create(image, name, extra_args = {}) ⇒ Object

Create a docker container from an image (or repository:tag string), name, and optional extra args.



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
# File 'lib/longshoreman/container.rb', line 27

def create(image, name, extra_args={})
  # If this ends up getting integrated, we'll use @logger.error here
  case image.class.to_s
  when "Docker::Image"
    i = image.id
  when "String"
    if image.include? ':' # repository:tag format
      i = image
    else
      puts "Image string must be in 'repository:tag' format."
      return
    end
  else
    puts "image must be Docker::Image or in 'repository:tag' format"
    return
  end

  # Don't change the non-capitalized 'name' here.  The Docker API gem extracts
  # this key and uses it to name the container on create.
  main_args = {
    'name' => name,
    'Hostname' => name,
    'Image' => i,
    'PublishAllPorts' => true,
    'CapAdd' => ['NET_ADMIN'],
  }
  @raw = Docker::Container.create(main_args.merge(extra_args))
end

#get(id, opts = {}) ⇒ Object

This get method is not a “pass thru”



57
58
59
# File 'lib/longshoreman/container.rb', line 57

def get(id, opts = {})
  @raw = Docker::Container.get(id, opts)
end

#idObject



61
62
63
# File 'lib/longshoreman/container.rb', line 61

def id
  @raw.id
end

#network_delay(ms) ⇒ Object



86
87
88
# File 'lib/longshoreman/container.rb', line 86

def network_delay(ms)
  @raw.exec(['tc', 'qdisc', 'add', 'dev', 'eth0', 'root', 'netem', 'delay', '#{ms}ms'])
end

#removeObject Also known as: delete



65
66
67
# File 'lib/longshoreman/container.rb', line 65

def remove
  @raw.remove(:force => true)
end

#remove_network_delayObject



90
91
92
# File 'lib/longshoreman/container.rb', line 90

def remove_network_delay
  @raw.exec(['tc', 'qdisc', 'del', 'dev', 'eth0', 'root', 'netem'])
end

#rport(exposed_port, protocol = "tcp") ⇒ Object

Return the randomized port number associated with the exposed port.



71
72
73
74
75
76
# File 'lib/longshoreman/container.rb', line 71

def rport(exposed_port, protocol="tcp")
  # Get all mapped ports
  ports = @raw.json["NetworkSettings"]["Ports"]
  # We're going to expect 1:1 mapping here
  ports["#{exposed_port.to_s}/#{protocol}"][0]["HostPort"].to_i
end

#startObject



78
79
80
# File 'lib/longshoreman/container.rb', line 78

def start
  @raw.start
end

#stopObject



82
83
84
# File 'lib/longshoreman/container.rb', line 82

def stop
  @raw.stop
end