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
PS_SIZE =

Pattern that parses FIRST occurrence of container size from a string, along with its units.

/^([0-9.]+)\s*([kmgt]?B)/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



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

def initialize(id, image, size, status, names, labels, ports)
  if size.is_a?(String) && (m = PS_SIZE.match(size))
    scalar, units = m[1], m[2]
    scalar = scalar.to_f # lazy: invalid --> 0.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.



13
14
15
# File 'lib/docker/compose/container.rb', line 13

def exitstatus
  @exitstatus
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/docker/compose/container.rb', line 13

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image.



13
14
15
# File 'lib/docker/compose/container.rb', line 13

def image
  @image
end

#labelsObject (readonly)

Returns the value of attribute labels.



14
15
16
# File 'lib/docker/compose/container.rb', line 14

def labels
  @labels
end

#namesObject (readonly)

Returns the value of attribute names.



14
15
16
# File 'lib/docker/compose/container.rb', line 14

def names
  @names
end

#portsObject (readonly)

Returns the value of attribute ports.



14
15
16
# File 'lib/docker/compose/container.rb', line 14

def ports
  @ports
end

#sizeObject (readonly)

Returns the value of attribute size.



13
14
15
# File 'lib/docker/compose/container.rb', line 13

def size
  @size
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/docker/compose/container.rb', line 13

def status
  @status
end

Instance Method Details

#nameString



71
72
73
# File 'lib/docker/compose/container.rb', line 71

def name
  names.first
end

#up?Boolean



76
77
78
# File 'lib/docker/compose/container.rb', line 76

def up?
  self.status == :up
end