Class: Docker::Compose::Container

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

Constant Summary collapse

PS_FMT =

Format string for ‘docker ps`

'({{.ID}}) ({{.Image}}) ({{.Size}}) ({{.Status}}) ({{.Names}}) ({{.Labels}}) ({{.Ports}})'
PS_FMT_LEN =

Number of template substitutions in PS_FMT

PS_FMT.count('.')
PS_STATUS =

Pattern that parses human-readable values from ps .Status

/^([A-Za-z]+) ?\(?([0-9]*)\)? ?(.*)$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, image, size, status, names, labels, ports) ⇒ Container

Returns a new instance of Container.

Parameters:

  • id (String)
  • image (String)
  • size (String, Numeric)
  • status (String, #map)

    e.g. [‘Exited’, ‘0’, ‘3 minutes ago’]

  • names (String, Array)

    list of container names (CSV)

  • labels (String, Array)

    list of container labels (CSV)

  • ports (String, Array)

    list of exposed ports (CSV)



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
# File 'lib/docker/compose/container.rb', line 20

def initialize(id, image, size, status, names, labels, ports)
  if size.is_a?(String)
    scalar, units = size.split(' ')
    scalar = size[0].to_i # lazy: invalid --> 0
    mult = case units.downcase
    when 'b'  then 1
    when 'kb' then 1_024
    when 'mb' then 1_024^2
    when 'gb' then 1_024^3
    when 'tb' then 1_024^4
    else
      raise Error.new('Service#new', units, 'Impossibly large unit')
    end
    size = scalar * mult
  end

  if status.is_a?(String)
    status = PS_STATUS.match(status)
    raise Error.new('Service#new', status, 'Unrecognized status') unless status
  end

  names = names.split(',').map{ |x| x.strip } if names.is_a?(String)
  labels = labels.split(',').map{ |x| x.strip } if labels.is_a?(String)
  ports = ports.split(',').map{ |x| x.strip } if ports.is_a?(String)

  @id = id
  @image = image
  @size = size
  @status = status[1].downcase.to_sym

  @exitstatus = case @status
  when :up
    nil
  else
    status[2].to_i
  end

  @names = names
  @labels = labels
  @ports = ports
end

Instance Attribute Details

#exitstatusObject (readonly)

Returns the value of attribute exitstatus.



10
11
12
# File 'lib/docker/compose/container.rb', line 10

def exitstatus
  @exitstatus
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/docker/compose/container.rb', line 10

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image.



10
11
12
# File 'lib/docker/compose/container.rb', line 10

def image
  @image
end

#labelsObject (readonly)

Returns the value of attribute labels.



11
12
13
# File 'lib/docker/compose/container.rb', line 11

def labels
  @labels
end

#namesObject (readonly)

Returns the value of attribute names.



11
12
13
# File 'lib/docker/compose/container.rb', line 11

def names
  @names
end

#portsObject (readonly)

Returns the value of attribute ports.



11
12
13
# File 'lib/docker/compose/container.rb', line 11

def ports
  @ports
end

#sizeObject (readonly)

Returns the value of attribute size.



10
11
12
# File 'lib/docker/compose/container.rb', line 10

def size
  @size
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/docker/compose/container.rb', line 10

def status
  @status
end

Instance Method Details

#nameString

Returns:

  • (String)


68
69
70
# File 'lib/docker/compose/container.rb', line 68

def name
  names.first
end

#up?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/docker/compose/container.rb', line 73

def up?
  self.status == :up
end