Class: PicsolveDockerBuilder::Helpers::ConfigManager

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/picsolve_docker_builder/helpers/config_manager.rb

Overview

Parses the config file

Instance Method Summary collapse

Methods included from Base

#base_dir, #config_file, #config_path, #create_logger, #default_config, #log, #read_config, #validate_config

Constructor Details

#initialize(path, stage) ⇒ ConfigManager

Returns a new instance of ConfigManager.



10
11
12
13
14
15
16
17
18
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 10

def initialize(path, stage)
  @path = path
  # 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

Instance Method Details

#configObject



24
25
26
27
28
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 24

def config
  return @config unless @config.nil?
  read
  parse_variables
end

#containers(composer) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 57

def containers(composer)
  config['compose']['containers'].map do |name, raw_config|
    PicsolveDockerBuilder::Composer::Container.new(
      name,
      eval_container_config(raw_config),
      composer
    )
  end
end

#defaultObject



20
21
22
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 20

def default
  {}
end

#eval_container_config(config) ⇒ Object



30
31
32
33
34
35
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 30

def eval_container_config(config)
  # eval environment if key exits
  config['environment'] = eval_container_env(config['environment']) \
    if config.key? 'environment'
  config
end

#eval_container_env(env) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 49

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

#eval_container_env_split(env) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 37

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

#parse_variablesObject



67
68
69
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 67

def parse_variables
  parse_variables_real(@config)
end

#parse_variables_real(obj) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 71

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

#readObject



87
88
89
90
91
92
93
94
95
# File 'lib/picsolve_docker_builder/helpers/config_manager.rb', line 87

def read
  @config = default
  begin
    yaml = Psych.load_file @path
    @config = @config.deep_merge(yaml)
  rescue Errno::ENOENT
    raise "cannot find config at '#{path}'"
  end
end