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.



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

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.



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

def atmos_env
  @atmos_env
end

#config_fileObject

Returns the value of attribute config_file.



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

def config_file
  @config_file
end

#root_dirObject

Returns the value of attribute root_dir.



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

def root_dir
  @root_dir
end

#tmp_rootObject

Returns the value of attribute tmp_root.



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

def tmp_root
  @tmp_root
end

#user_config_fileObject

Returns the value of attribute user_config_file.



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

def user_config_file
  @user_config_file
end

#working_groupObject

Returns the value of attribute working_group.



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

def working_group
  @working_group
end

Instance Method Details

#[](key) ⇒ Object



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

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

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



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

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

#account_hashObject



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

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



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

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



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

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

#auth_cache_dirObject



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

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



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
168
169
170
171
172
173
174
175
# File 'lib/simplygenius/atmos/config.rb', line 124

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

      rhs.each do |k, v|
        new_state = debug_state + [k]

        # The load sequence could have multiple overrides and its not
        # obvious to the user which are lhs vs rhs, so rather than having
        # one seem to win arbitrarily, we collect them all additively
        # under their key, then at the end of the load process we push the
        # override back as a replacement for its base key in the
        # finalize_merge method. This means that if one has multiple
        # overrides for the same key (not usually what one wants), those
        # overrides get merged together additively before replacing the
        # base.  Thus the need for the log messages below.
        #
        if k =~ /^\^/
          logger.debug { "Override seen at #{new_state.join(" -> ")}" }
          logger.warn { "Multiple overrides on a single key seen at #{new_state.join(" -> ")}" } if result.has_key?(k)
        end

        result[k] = config_merge(result[k], v, new_state)
      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)


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

def is_atmos_repo?
  File.exist?(config_file)
end

#plugin_managerObject



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

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

#providerObject



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

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

#save_user_config_file(data, merge_to_existing: true) ⇒ Object



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

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}"])
    data = finalize_merge(data)
  end
  File.write(user_config_file, YAML.dump(data.to_hash))
  File.chmod(0600, user_config_file)
end

#tf_working_dirObject



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

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



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

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



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

def to_h
  load
  @config.to_hash
end