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



60
61
62
# File 'lib/docker-compose/utils/compose_utils.rb', line 60

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
49
50
51
52
53
54
55
# File 'lib/docker-compose/utils/compose_utils.rb', line 30

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

  if image.nil?
    return nil
  end

  unless image.index('/').nil?
    path_split = image.rpartition('/')
    image = path_split.last
    repo = path_split.first + '/'
  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

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

Generate a pair key:hash with format service:label

The label will be the conainer name if not specified.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/docker-compose/utils/compose_utils.rb', line 130

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/docker-compose/utils/compose_utils.rb', line 68

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/docker-compose/utils/compose_utils.rb', line 96

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;



156
157
158
159
160
161
# File 'lib/docker-compose/utils/compose_utils.rb', line 156

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