Class: Container::DockerCompose

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/container/docker_compose.rb

Constant Summary collapse

@@file =
""

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDockerCompose

Returns a new instance of DockerCompose.



11
12
13
14
15
# File 'lib/container/docker_compose.rb', line 11

def initialize
  raise "Container::DockerCompose.file not initialized" if @@file.size == 0
  @file = @@file
  @config = load_config(@file)
end

Class Method Details

.docker_composeObject



21
22
23
# File 'lib/container/docker_compose.rb', line 21

def self.docker_compose
   "docker-compose -f #{@@file}"
end

.file=(composer_file) ⇒ Object



8
9
10
# File 'lib/container/docker_compose.rb', line 8

def self.file= composer_file
  @@file = composer_file
end

Instance Method Details

#load_config(file) ⇒ Object



17
18
19
# File 'lib/container/docker_compose.rb', line 17

def load_config(file)
  YAML.load_file(file)
end

#parse_env(string = nil) ⇒ Object



28
29
30
31
32
33
# File 'lib/container/docker_compose.rb', line 28

def parse_env(string = nil)
  #addapted from https://www.shakacode.com/blog/masking-pii-with-ruby-gsub-with-regular-expression-named-match-groups/
  # and https://cmsdk.com/javascript/regex-to-match-string-with-contains-closed-brackets.html
  # replace { ${XXX} ${YYYY}} => { ENV['XXX'] ENV['YYYY'] }
  string.gsub(/(?<match>\$\{(?<variable>\g<match>|[^${}]++)*\})/) { |match| ENV[$~[:variable]] } if string
end

#servicesObject



24
25
26
# File 'lib/container/docker_compose.rb', line 24

def services
  @services ||= @config['services']
end

#with_label(label_key: 'backup') ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/container/docker_compose.rb', line 35

def with_label(label_key: 'backup')
  services.inject({}) do |h, (k, v)|
    image = parse_env(v.dig('image'))
    values = (v.dig('labels')&.
        map { |l| l.split('=') }&.
        select { |l| l.first.split('.').first == label_key.to_s })
    (values&.size || 0) > 0 ? h.merge({{k => image} => values.map{|value| YAML.load(parse_env(value.last))}}) : h
  end
end