require 'picsolve_docker_builder/base' require 'picsolve_docker_builder/helpers/config/variable_object' require 'picsolve_docker_builder/composer/container' module PicsolveDockerBuilder module Helpers # Parses the config file class ConfigManager include PicsolveDockerBuilder::Base def initialize(paths, stage) if paths.is_a?(Array) @paths = paths else @paths = [paths] end # TODO: Do this properly, this is a very very dirty hack stage = ( !ENV['STAGE'].nil? && ENV['STAGE'].downcase ) || 'ci' if stage.nil? @stage = stage end def default {} end def config return @config unless @config.nil? read parse_variables end def eval_container_config(config) # eval environment if key exits config['environment'] = eval_container_env(config['environment']) \ if config.key? 'environment' config end def eval_container_env_split(env) output = {} env.each do |key, lst| output[key] = {} lst.each do |elem| ikey, val = elem.split('=', 2) output[key][ikey] = val end end output end def eval_container_env(env) env = eval_container_env_split(env) output = {} output = output.update(env['default']) if env.key?('default') output = output.update(env[@stage]) if env.key?(@stage) output end def containers(composer) config['compose']['containers'].map do |name, raw_config| PicsolveDockerBuilder::Composer::Container.new( name, eval_container_config(raw_config), composer ) end end def parse_variables parse_variables_real(@config) end def parse_variables_real(obj) if obj.is_a?(Hash) obj.each do |k, v| obj[k] = parse_variables_real(v) end elsif obj.is_a?(Array) obj.map do |v| parse_variables_real(v) end elsif obj.is_a?(String) Config::VariableObject.replace_string(obj) else obj end end def read @config = default @paths.each do |file| begin yaml = Psych.load_file file @config = @config.deep_merge(yaml) rescue Errno::ENOENT raise "cannot find config at '#{file}'" end end end end end end