Module: ComposeUtils

Defined in:
lib/docker-compose/utils/compose_utils.rb

Class Method Summary collapse

Class Method Details

.dir_nameObject

Returns the directory name where compose file is saved (used in container naming)



9
10
11
# File 'lib/docker-compose/utils/compose_utils.rb', line 9

def self.dir_name
  @dir_name
end

.format_command(command) ⇒ Object

Transform docker command from string to an array of commands



53
54
55
# File 'lib/docker-compose/utils/compose_utils.rb', line 53

def self.format_command(command)
  command.nil? ? nil : command.split(' ')
end

.format_image(image) ⇒ Object

Format a given docker image in a complete structure (base image + tag)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/docker-compose/utils/compose_utils.rb', line 30

def self.format_image(image)
  base_image = nil
  tag = nil

  if image.nil?
    return nil
  end

  if image.index(':').nil?
    base_image = image
    tag = 'latest'
  else
    image_split = image.split(':')
    base_image = image_split[0]
    tag = image_split[1]
  end

  "#{base_image}:#{tag}"
end

Generate a pair key:hash with format service:label

The label will be the conainer name if not specified.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/docker-compose/utils/compose_utils.rb', line 123

def self.format_links(links_array)
  links = {}

  return if links_array.nil?

  links_array.each do |link|
    parts = link.split(':')

    case parts.length
      when 1
        links[parts[0]] = parts[0]

      when 2
        links[parts[0]] = parts[1]
    end
  end

  links
end

.format_port(port_entry) ⇒ Object

Read a port specification in string format and create a compose port structure



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/docker-compose/utils/compose_utils.rb', line 61

def self.format_port(port_entry)
  compose_port = nil
  container_port = nil
  host_port = nil
  host_ip = nil

  port_parts = port_entry.to_s.split(':')

  case port_parts.length
    # [container port]
    when 1
      compose_port = ComposePort.new(port_parts[0])

    # [host port]:[container port]
    when 2
      compose_port = ComposePort.new(port_parts[1], port_parts[0])

    # [host ip]:[host port]:[container port]
    when 3
      compose_port = ComposePort.new(port_parts[2], port_parts[1], port_parts[0])
  end

  compose_port
end

.format_ports_from_running_container(port_entry) ⇒ Object

Format ports from running container



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/docker-compose/utils/compose_utils.rb', line 89

def self.format_ports_from_running_container(port_entry)
  entries = []
  container_port = nil
  host_ip = nil
  host_port = nil

  if port_entry.nil?
    return entries
  end

  port_entry.each do |key, value|
    container_port = key.gsub(/\D/, '').to_i
    # Ports that are EXPOSEd but not published won't have a Host IP/Port,
    # only a Container Port.
    if value.nil?
      host_ip = ''
      host_port = ''
    else
      host_ip = value.first['HostIp']
      host_port = value.first['HostPort']
    end

    entries << "#{container_port}:#{host_ip}:#{host_port}"
  end

  entries
end

.generate_container_name(container_name, container_label) ⇒ Object

Generate a container name, based on:

  • directory where the compose file is saved;

  • container name (or label, if name isn’t provided);

  • a sequential index;



149
150
151
152
153
154
# File 'lib/docker-compose/utils/compose_utils.rb', line 149

def self.generate_container_name(container_name, container_label)
  label = container_name.nil? ? container_label : container_name
  index = next_available_id

  "#{@dir_name}_#{label}_#{index}"
end