class Container::DockerCompose

Public Class Methods

docker_compose() click to toggle source
# File lib/container/docker_compose.rb, line 21
def self.docker_compose
   "docker-compose -f #{@@file}"
end
file=(composer_file) click to toggle source
# File lib/container/docker_compose.rb, line 8
def self.file= composer_file
  @@file = composer_file
end
new() click to toggle source
# 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

Public Instance Methods

load_config(file) click to toggle source
# File lib/container/docker_compose.rb, line 17
def load_config(file)
  YAML.load_file(file)
end
parse_env(string = nil) click to toggle source
# 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
services() click to toggle source
# File lib/container/docker_compose.rb, line 24
def services
  @services ||= @config['services']
end
with_label(label_key: 'backup') click to toggle source
# 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