Class: SimplyGenius::Atmos::Config

Inherits:
Object
  • Object
show all
Includes:
FileUtils, GemLogger::LoggerSupport
Defined in:
lib/simplygenius/atmos/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(atmos_env, working_group = 'default', root: ENV['ATMOS_ROOT'], config: ENV['ATMOS_CONFIG']) ⇒ Config

Returns a new instance of Config.



22
23
24
25
26
27
28
29
30
# File 'lib/simplygenius/atmos/config.rb', line 22

def initialize(atmos_env, working_group = 'default', root: ENV['ATMOS_ROOT'], config: ENV['ATMOS_CONFIG'])
  @atmos_env = atmos_env
  @working_group = working_group
  @root_dir = File.expand_path(root || Dir.pwd)
  @config_file = config ? File.expand_path(config, root_dir) : File.join(root_dir, "config", "atmos.yml")
  @user_config_file = "~/.atmos.yml"
  @tmp_root = File.join(root_dir, "tmp")
  @included_configs = []
end

Instance Attribute Details

#atmos_envObject

Returns the value of attribute atmos_env.



16
17
18
# File 'lib/simplygenius/atmos/config.rb', line 16

def atmos_env
  @atmos_env
end

#config_fileObject

Returns the value of attribute config_file.



16
17
18
# File 'lib/simplygenius/atmos/config.rb', line 16

def config_file
  @config_file
end

#root_dirObject

Returns the value of attribute root_dir.



16
17
18
# File 'lib/simplygenius/atmos/config.rb', line 16

def root_dir
  @root_dir
end

#tmp_rootObject

Returns the value of attribute tmp_root.



16
17
18
# File 'lib/simplygenius/atmos/config.rb', line 16

def tmp_root
  @tmp_root
end

#user_config_fileObject

Returns the value of attribute user_config_file.



16
17
18
# File 'lib/simplygenius/atmos/config.rb', line 16

def user_config_file
  @user_config_file
end

#working_groupObject

Returns the value of attribute working_group.



16
17
18
# File 'lib/simplygenius/atmos/config.rb', line 16

def working_group
  @working_group
end

Instance Method Details

#[](key) ⇒ Object



36
37
38
39
40
# File 'lib/simplygenius/atmos/config.rb', line 36

def [](key)
  load
  result = @config.notation_get(key)
  return result
end

#[]=(key, value, additive: true) ⇒ Object



42
43
44
45
46
# File 'lib/simplygenius/atmos/config.rb', line 42

def []=(key, value, additive: true)
  load
  result = @config.notation_put(key, value, additive: additive)
  return result
end

#account_hashObject



66
67
68
69
70
71
72
73
# File 'lib/simplygenius/atmos/config.rb', line 66

def 
  load
  environments = @full_config[:environments] || {}
  environments.inject(Hash.new) do |accum, entry|
    accum[entry.first] = entry.last[:account_id]
    accum
  end
end

#add_user_load_path(*paths) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/simplygenius/atmos/config.rb', line 102

def add_user_load_path(*paths)
  load_path = paths + Array(self["atmos.load_path"])
  if load_path.present?
    load_path = load_path.collect { |path| File.expand_path(path) }
    logger.debug("Adding to load path: #{load_path.inspect}")
    $LOAD_PATH.insert(0, *load_path)
  end
end

#all_env_namesObject



61
62
63
64
# File 'lib/simplygenius/atmos/config.rb', line 61

def all_env_names
  load
  (@full_config[:environments] || {}).keys
end

#auth_cache_dirObject



84
85
86
87
88
89
90
91
# File 'lib/simplygenius/atmos/config.rb', line 84

def auth_cache_dir
  @auth_cache_dir ||= begin
    dir = File.join(tmp_dir, 'auth')
    logger.debug("Auth cache dir: #{dir}")
    mkdir_p(dir)
    dir
  end
end

#config_merge(lhs, rhs, debug_state = []) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/simplygenius/atmos/config.rb', line 122

def config_merge(lhs, rhs, debug_state=[])
  result = nil

  return rhs if lhs.nil?
  return lhs if rhs.nil?

  # Warn if user fat fingered config
  if lhs.is_a?(rhs.class) || rhs.is_a?(lhs.class)

    case rhs
    when Hash
      result = lhs.deep_dup

      lhs.each do |k, v|
        if k =~ /^\^(.*)/
          key = k.is_a?(Symbol) ? $1.to_sym : $1
          result[key] = result.delete(k)
        end
      end

      rhs.each do |k, v|
        if k =~ /^\^(.*)/
          key = k.is_a?(Symbol) ? $1.to_sym : $1
          result[key] = v
        else
          result[k] = config_merge(result[k], v, debug_state + [k])
        end
      end
    when Enumerable
      result = lhs + rhs
    else
      result = rhs
    end

  else

    logger.warn("Type mismatch while merging in: #{debug_state.delete_at(0)}")
    logger.warn("Deep merge path: #{debug_state.join(" -> ")}")
    logger.warn("Deep merge LHS (#{lhs.class}): #{lhs.inspect}")
    logger.warn("Deep merge RHS (#{rhs.class}): #{rhs.inspect}")
    result = rhs

  end

  return result
end

#is_atmos_repo?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/simplygenius/atmos/config.rb', line 32

def is_atmos_repo?
  File.exist?(config_file)
end

#plugin_managerObject



57
58
59
# File 'lib/simplygenius/atmos/config.rb', line 57

def plugin_manager
  @plugin_manager ||= PluginManager.new(self["atmos.plugins"])
end

#providerObject



53
54
55
# File 'lib/simplygenius/atmos/config.rb', line 53

def provider
  @provider ||= ProviderFactory.get(self[:provider])
end

#save_user_config_file(data, merge_to_existing: true) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/simplygenius/atmos/config.rb', line 111

def save_user_config_file(data, merge_to_existing: true)
  logger.debug("Saving to user config file (merging=#{merge_to_existing}): #{user_config_file}")

  if merge_to_existing
    existing = load_file(user_config_file, SettingsHash.new)
    data = config_merge(existing, data, ["saving #{user_config_file}"])
  end
  File.write(user_config_file, YAML.dump(data.to_hash))
  File.chmod(0600, user_config_file)
end

#tf_working_dirObject



93
94
95
96
97
98
99
100
# File 'lib/simplygenius/atmos/config.rb', line 93

def tf_working_dir
  @tf_working_dir ||= begin
    dir = File.join(tmp_dir, 'tf', working_group)
    logger.debug("Terraform working dir: #{dir}")
    mkdir_p(dir)
    dir
  end
end

#tmp_dirObject



75
76
77
78
79
80
81
82
# File 'lib/simplygenius/atmos/config.rb', line 75

def tmp_dir
  @tmp_dir ||= begin
    dir = File.join(tmp_root, atmos_env)
    logger.debug("Tmp dir: #{dir}")
    mkdir_p(dir)
    dir
  end
end

#to_hObject



48
49
50
51
# File 'lib/simplygenius/atmos/config.rb', line 48

def to_h
  load
  @config.to_hash
end