class Kontena::Cli::Stacks::YAML::ServiceExtender

Attributes

service_config[R]

Public Class Methods

new(service_config) click to toggle source

@param [Hash] service_config

# File lib/kontena/cli/stacks/yaml/service_extender.rb, line 8
def initialize(service_config)
  @service_config = service_config
end

Public Instance Methods

extend_from(from) click to toggle source

@param [Hash] from @return [Hash]

# File lib/kontena/cli/stacks/yaml/service_extender.rb, line 14
def extend_from(from)
  service_config['environment'] = extend_env_vars(from['env'], service_config['environment'])
  service_config['secrets']     = extend_secrets( from['secrets'], service_config['secrets'])
  build_args                    = extend_build_args(safe_dig(from, 'build', 'args'), safe_dig(service_config, 'build', 'args'))
  unless build_args.empty?
    service_config['build'] ||= {}
    service_config['build']['args'] = build_args
  end
  from.merge(service_config)
end

Private Instance Methods

env_to_hash(env_array) click to toggle source
# File lib/kontena/cli/stacks/yaml/service_extender.rb, line 27
def env_to_hash(env_array)
  env_array.map { |env| env.split('=', 2) }.to_h
end
extend_build_args(from, to) click to toggle source

Basic merge of two hashes, “to” is dominant.

# File lib/kontena/cli/stacks/yaml/service_extender.rb, line 58
def extend_build_args(from, to)
  from ||= {}
  to   ||= {}
  from.merge(to)
end
extend_env_vars(from, to) click to toggle source

Takes two arrays of “key=value” pairs and merges them. Keys in “from”-array will not overwrite keys that already exist in “to”-array.

@param [Array] from @param [Array] to @return [Array]

# File lib/kontena/cli/stacks/yaml/service_extender.rb, line 37
def extend_env_vars(from, to)
  env_to_hash(from || []).merge(env_to_hash(to || [])).map { |k,v| [k.to_s, v.to_s].join('=') }
end
extend_secrets(from, to) click to toggle source

Takes two arrays of hashes containing { 'secret' => 'str', 'type' => 'str', 'name' => 'str' } and merges them. 'secret' is the primary key, secrets found in “to” are not overwritten.

@param [Array] from @param [Array] to @return [Array]

# File lib/kontena/cli/stacks/yaml/service_extender.rb, line 47
def extend_secrets(from, to)
  from ||= []
  to   ||= []
  uniq_from = []
  from.each do |from_hash|
    uniq_from << from_hash unless to.find {|to_hash| from_hash['secret'] == to_hash['secret'] }
  end
  to + uniq_from
end