class DockerComposeYamlLoader

Usage:

DockerComposeYamlLoader.load('./path/to/docker-compose.yml')

Constants

VERSION

Attributes

settings[R]

Public Class Methods

load(file_path) click to toggle source
# File lib/docker_compose_yaml_loader.rb, line 14
def self.load(file_path)
  new(file_path).load
end
new(file_path) click to toggle source
# File lib/docker_compose_yaml_loader.rb, line 18
def initialize(file_path)
  @file_path = file_path
end

Public Instance Methods

build(hash, settings = {}) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/docker_compose_yaml_loader.rb, line 30
def build(hash, settings = {})
  return hash if hash.nil?

  extends_hash = {}
  hash.each do |key, value|
    if key == 'extends'
      extends_hash = value
    else
      settings[key.to_s] = if value.is_a? Hash
                             build(value)
                           else
                             value
                           end
    end
  end
  settings = merge(settings, extends(extends_hash)) unless extends_hash.empty?
  settings
end
extends(hash) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/docker_compose_yaml_loader.rb, line 50
def extends(hash)
  file_path = File.join(File.dirname(@file_path), hash['file'])
  service = hash['service']
  other_hash = YAML.load_file(file_path)
  target = other_hash[service] || other_hash['services'][service]
  build(target)
end
load() click to toggle source
# File lib/docker_compose_yaml_loader.rb, line 22
def load
  return unless File.exist?(@file_path)
  hash = YAML.load_file(@file_path)
  @settings = build(hash)
  @settings
end
merge(destination, source) click to toggle source
# File lib/docker_compose_yaml_loader.rb, line 58
def merge(destination, source)
  destination, source = merge_image_or_build(destination, source)
  destination, source = merge_join_array_value(destination, source)
  destination, source = merge_priority_to_destination_value(destination, source)
  destination.merge!(source)
  destination
end

Private Instance Methods

hashify(values) click to toggle source
# File lib/docker_compose_yaml_loader.rb, line 100
def hashify(values)
  return values if values.is_a? Hash

  hash = {}
  if values.is_a? Array
    values.each do |value|
      k, v = value.split(/=:/, 2)
      hash[k] = v
    end
  end
  hash
end
merge_image_or_build(destination, source) click to toggle source
# File lib/docker_compose_yaml_loader.rb, line 68
def merge_image_or_build(destination, source)
  if destination.key?('image') || destination.key?('build')
    source.delete('image')
    source.delete('build')
  else
    destination['image'] = source.delete('image') if source.key?('image')
    destination['build'] = source.delete('build') if source.key?('build')
  end
  [destination, source]
end
merge_join_array_value(destination, source) click to toggle source
# File lib/docker_compose_yaml_loader.rb, line 79
def merge_join_array_value(destination, source)
  %w[ports expose external_links dns dns_search tmpfs].each do |key|
    if destination.key?(key) && source.key?(key)
      destination[key] += source.delete(key)
      destination[key].uniq!
    end
  end
  [destination, source]
end
merge_priority_to_destination_value(destination, source) click to toggle source
# File lib/docker_compose_yaml_loader.rb, line 89
def merge_priority_to_destination_value(destination, source)
  %w[environment labels volumes devices].each do |key|
    destination_hash = hashify(destination[key])
    source_hash = hashify(source[key])
    value = source_hash.merge(destination_hash)
    destination[key] = value unless value.empty?
    source.delete(key)
  end
  [destination, source]
end