Class: SimplyGenius::Atmos::Config
- Inherits:
-
Object
- Object
- SimplyGenius::Atmos::Config
- Includes:
- FileUtils, GemLogger::LoggerSupport
- Defined in:
- lib/simplygenius/atmos/config.rb
Instance Attribute Summary collapse
-
#atmos_env ⇒ Object
Returns the value of attribute atmos_env.
-
#config_file ⇒ Object
Returns the value of attribute config_file.
-
#root_dir ⇒ Object
Returns the value of attribute root_dir.
-
#tmp_root ⇒ Object
Returns the value of attribute tmp_root.
-
#user_config_file ⇒ Object
Returns the value of attribute user_config_file.
-
#working_group ⇒ Object
Returns the value of attribute working_group.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value, additive: true) ⇒ Object
- #account_hash ⇒ Object
- #add_user_load_path(*paths) ⇒ Object
- #all_env_names ⇒ Object
- #auth_cache_dir ⇒ Object
- #config_merge(lhs, rhs, debug_state = []) ⇒ Object
-
#initialize(atmos_env, working_group = 'default', root: ENV['ATMOS_ROOT'], config: ENV['ATMOS_CONFIG']) ⇒ Config
constructor
A new instance of Config.
- #is_atmos_repo? ⇒ Boolean
- #plugin_manager ⇒ Object
- #provider ⇒ Object
- #save_user_config_file(data, merge_to_existing: true) ⇒ Object
- #tf_working_dir ⇒ Object
- #tmp_dir ⇒ Object
- #to_h ⇒ Object
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.(root || Dir.pwd) @config_file = config ? File.(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_env ⇒ Object
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_file ⇒ Object
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_dir ⇒ Object
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_root ⇒ Object
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_file ⇒ Object
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_group ⇒ Object
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_hash ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/simplygenius/atmos/config.rb', line 66 def account_hash 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.(path) } logger.debug("Adding to load path: #{load_path.inspect}") $LOAD_PATH.insert(0, *load_path) end end |
#all_env_names ⇒ Object
61 62 63 64 |
# File 'lib/simplygenius/atmos/config.rb', line 61 def all_env_names load (@full_config[:environments] || {}).keys end |
#auth_cache_dir ⇒ Object
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
32 33 34 |
# File 'lib/simplygenius/atmos/config.rb', line 32 def is_atmos_repo? File.exist?(config_file) end |
#plugin_manager ⇒ Object
57 58 59 |
# File 'lib/simplygenius/atmos/config.rb', line 57 def plugin_manager @plugin_manager ||= PluginManager.new(self["atmos.plugins"]) end |
#provider ⇒ Object
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_dir ⇒ Object
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_dir ⇒ Object
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_h ⇒ Object
48 49 50 51 |
# File 'lib/simplygenius/atmos/config.rb', line 48 def to_h load @config.to_hash end |