Module: Moose::Inventory::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/moose_inventory/config/config.rb

Overview

This Modules manages application-wide configuration options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#_argvObject (readonly)

Returns the value of attribute _argv.



23
24
25
# File 'lib/moose_inventory/config/config.rb', line 23

def _argv
  @_argv
end

#_confoptsObject (readonly)

Returns the value of attribute _confopts.



23
24
25
# File 'lib/moose_inventory/config/config.rb', line 23

def _confopts
  @_confopts
end

#_settingsObject (readonly)

Returns the value of attribute _settings.



23
24
25
# File 'lib/moose_inventory/config/config.rb', line 23

def _settings
  @_settings
end

Class Method Details

._runtime_optionsObject



73
74
75
# File 'lib/moose_inventory/config/config.rb', line 73

def self._runtime_options
  @runtime_options
end

.ansible?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/moose_inventory/config/config.rb', line 61

def self.ansible?
  runtime_options.ansible? || @_confopts[:ansible] == true
end

.ansible_argsObject




121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/moose_inventory/config/config.rb', line 121

def self.ansible_args
  #
  # See http://docs.ansible.com/developing_inventory.html for Ansible specs
  # for dynamic inventory sources

  # --list            => group list
  # --host HOSTNAME  => host getvars HOSTNAME

  case @_argv[0]
  when '--list'
    apply_ansible_alias!(%w[group list])
  when '--host'
    host = @_argv[1]
    apply_ansible_alias!(['host', 'listvars', host.to_s])
  end
end

.application_argsObject



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

def self.application_args
  runtime_options.argv
end

.apply_ansible_alias!(argv) ⇒ Object



251
252
253
254
255
# File 'lib/moose_inventory/config/config.rb', line 251

def self.apply_ansible_alias!(argv)
  @_confopts[:ansible] = true
  normalize_ansible_format!
  @_argv = argv
end

.build_runtime_optionsObject



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/moose_inventory/config/config.rb', line 261

def self.build_runtime_options
  Moose::Inventory::RuntimeOptions.new(
    argv: @_argv.dup,
    config: @_confopts[:config],
    env: @_confopts[:env],
    format: @_confopts[:format],
    flags: {
      ansible: @_confopts[:ansible],
      trace: @_confopts[:trace]
    }
  )
end

.db_settingsObject



69
70
71
# File 'lib/moose_inventory/config/config.rb', line 69

def self.db_settings
  @_settings[:config][:db]
end

.default_confoptsObject



45
46
47
# File 'lib/moose_inventory/config/config.rb', line 45

def self.default_confopts
  { env: '', format: 'json', ansible: false, trace: false }
end

.extract_boolean_flag(flag) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/moose_inventory/config/config.rb', line 236

def self.extract_boolean_flag(flag)
  index = @_argv.index("--#{flag}")
  return if index.nil?

  @_confopts[flag.to_sym] = true
  @_argv.delete_at(index)
end

.extract_boolean_flags(flags) ⇒ Object



232
233
234
# File 'lib/moose_inventory/config/config.rb', line 232

def self.extract_boolean_flags(flags)
  flags.each { |flag| extract_boolean_flag(flag) }
end

.extract_value_flag(flag) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'lib/moose_inventory/config/config.rb', line 221

def self.extract_value_flag(flag)
  index = @_argv.index("--#{flag}")
  return if index.nil?

  value = @_argv[index + 1]
  raise("Expected a value after --#{flag}") if value.nil? || value.start_with?('--')

  @_confopts[flag.to_sym] = value
  @_argv.slice!(index, 2)
end

.extract_value_flags(flags) ⇒ Object



217
218
219
# File 'lib/moose_inventory/config/config.rb', line 217

def self.extract_value_flags(flags)
  flags.each { |flag| extract_value_flag(flag) }
end

.fetch_general_settings(newsets, path) ⇒ Object



178
179
180
181
182
# File 'lib/moose_inventory/config/config.rb', line 178

def self.fetch_general_settings(newsets, path)
  general = newsets[:general]
  general.nil? && raise("Missing 'general' root in #{path}")
  general
end

.find_default_config_fileObject



204
205
206
207
208
# File 'lib/moose_inventory/config/config.rb', line 204

def self.find_default_config_file
  standard_config_paths.map { |path| File.expand_path(path) }.find do |path|
    File.exist?(path)
  end
end

.init(args) ⇒ Object




26
27
28
29
30
31
32
33
34
35
36
# File 'lib/moose_inventory/config/config.rb', line 26

def self.init(args)
  reset_runtime_state
  @_argv = args.dup

  top_level_help
  top_level_args
  ansible_args
  resolve_config_file
  load
  refresh_runtime_options
end

.loadObject




158
159
160
161
162
163
164
165
166
167
# File 'lib/moose_inventory/config/config.rb', line 158

def self.load
  newsets = load_config_file(@_confopts[:config])
  path = @_confopts[:config]

  @_settings[:general] = fetch_general_settings(newsets, path)

  env, settings = resolve_environment_settings(newsets, path)
  @_settings[:config] = settings
  @_settings[:config].nil? && raise("Missing '#{env}' root in #{path}")
end

.load_config_file(path) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/moose_inventory/config/config.rb', line 169

def self.load_config_file(path)
  symbolize_keys(YAML.safe_load_file(
                   path,
                   aliases: false,
                   permitted_classes: [],
                   permitted_symbols: []
                 ))
end

.normalize_ansible_format!Object



244
245
246
247
248
249
# File 'lib/moose_inventory/config/config.rb', line 244

def self.normalize_ansible_format!
  return unless @_confopts[:ansible] == true
  return if @_confopts[:format] =~ /p|pjson|j|json/

  @_confopts[:format] = 'json'
end

.output_formatObject



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

def self.output_format
  runtime_options.output_format
end

.refresh_runtime_optionsObject



257
258
259
# File 'lib/moose_inventory/config/config.rb', line 257

def self.refresh_runtime_options
  @runtime_options = build_runtime_options
end

.reset_runtime_stateObject



38
39
40
41
42
43
# File 'lib/moose_inventory/config/config.rb', line 38

def self.reset_runtime_state
  @_argv = []
  @_confopts = default_confopts
  @_settings = {}
  @runtime_options = nil
end

.resolve_config_fileObject




139
140
141
142
143
144
145
146
147
148
# File 'lib/moose_inventory/config/config.rb', line 139

def self.resolve_config_file
  explicit_path = @_confopts[:config]
  @_confopts[:config] = if explicit_path.nil?
                          find_default_config_file
                        else
                          validated_config_path(explicit_path)
                        end

  raise('No configuration either given or found in standard locations.') if @_confopts[:config].nil?
end

.resolve_environment_settings(newsets, path) ⇒ Object



184
185
186
187
# File 'lib/moose_inventory/config/config.rb', line 184

def self.resolve_environment_settings(newsets, path)
  env = selected_environment(newsets, path)
  [env, newsets[env.to_sym]]
end

.runtime_optionsObject



49
50
51
# File 'lib/moose_inventory/config/config.rb', line 49

def self.runtime_options
  @runtime_options ||= build_runtime_options
end

.selected_environment(newsets, path) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/moose_inventory/config/config.rb', line 189

def self.selected_environment(newsets, path)
  return @_confopts[:env] if @_confopts[:env] && !@_confopts[:env].empty?

  env = newsets.dig(:general, :defaultenv)
  (env.nil? || env.empty?) && raise("No defaultenv set in #{path}")
  env
end

.standard_config_pathsObject



197
198
199
200
201
202
# File 'lib/moose_inventory/config/config.rb', line 197

def self.standard_config_paths
  ['./.moose-tools/inventory/config',
   '~/.moose-tools/inventory/config',
   '~/local/etc/moose-tools/inventory/config',
   '/etc/moose-tools/inventory/config']
end

.symbolize_key(key) ⇒ Object



274
275
276
# File 'lib/moose_inventory/config/config.rb', line 274

def self.symbolize_key(key)
  key.is_a?(String) ? key.to_sym : key
end

.symbolize_keys(hash) ⇒ Object




151
152
153
154
155
# File 'lib/moose_inventory/config/config.rb', line 151

def self.symbolize_keys(hash)
  hash.each_with_object({}) do |(key, value), result|
    result[symbolize_key(key)] = value.is_a?(Hash) ? symbolize_keys(value) : value
  end
end

.top_level_argsObject




78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/moose_inventory/config/config.rb', line 78

def self.top_level_args
  # The following top-level flags affect the global configuration.
  #
  # --ansible      => forces ansible mode in relevant responders
  #                   Default is not use to use ansible mode
  #
  # --config FILE  => sets the configuration file to be used.
  #                   Default is to search standard locations.
  #
  # --env ENV      => sets the section to be used as the configuration.
  #                   Defaults to "", which forces the use of the
  #                   defaultenv parameter from the general section of
  #                   the config file.
  #
  # --format FORMAT=> See formatter for supported types.
  #                   Defaults to json.
  #
  # -- trace       => Enable more complete exceptions for db transactions
  #                   Default is not to trace.

  extract_value_flags(%w[config env format])
  extract_boolean_flags(%w[ansible trace])
  normalize_ansible_format!
end

.top_level_helpObject




104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/moose_inventory/config/config.rb', line 104

def self.top_level_help
  return unless @_argv[0] == 'help'

  puts 'Global flags:'
  printf '  %-31s %-10s', '--ansible', "# Force Ansible mode (automatically set when using ansible flags)\n"
  printf '  %-31s %-10s', '--config FILE', "# Specifies a configuration file to use\n"
  printf '  %-31s %-10s', '--env ENV', "# Specifies the environment section of the config to use\n"
  printf '  %-31s %-10s', '--format yaml|json|pjson',
         "# Format for the output of 'get', 'list', and 'listvars' subcommands\n"
  printf '  %-31s %-10s', '--trace', "# Enable more complete exception dumps for database transactions\n"
  puts "\nAnsible flags:"
  printf '  %-31s %-10s', '--host HOSTNAME',
         "# Retrieves host variables for the specified host (alias for 'host listvars HOSTNAME')\n"
  printf '  %-31s %-10s', '--list', "# Retrieves the list of groups (alias for 'group list')\n\n"
end

.trace_enabled?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/moose_inventory/config/config.rb', line 65

def self.trace_enabled?
  runtime_options.trace? || @_confopts[:trace] == true
end

.validated_config_path(path) ⇒ Object



210
211
212
213
214
215
# File 'lib/moose_inventory/config/config.rb', line 210

def self.validated_config_path(path)
  expanded = File.expand_path(path)
  raise("The configuration file #{expanded} does not exist") unless File.exist?(expanded)

  expanded
end