Module: Moose::Inventory::Config
Overview
This Modules manages application-wide configuration options
Instance Attribute Summary collapse
-
#_argv ⇒ Object
readonly
Returns the value of attribute _argv.
-
#_confopts ⇒ Object
readonly
Returns the value of attribute _confopts.
-
#_settings ⇒ Object
readonly
Returns the value of attribute _settings.
Class Method Summary collapse
-
.ansible_args ⇒ Object
———————-.
-
.init(args) ⇒ Object
———————-.
-
.load ⇒ Object
———————- rubocop:disable PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize.
-
.resolve_config_file ⇒ Object
———————-.
-
.symbolize_keys(hash) ⇒ Object
———————-.
-
.top_level_args ⇒ Object
———————-.
-
.top_level_help ⇒ Object
———————-.
Instance Attribute Details
#_argv ⇒ Object (readonly)
Returns the value of attribute _argv.
18 19 20 |
# File 'lib/moose_inventory/config/config.rb', line 18 def _argv @_argv end |
#_confopts ⇒ Object (readonly)
Returns the value of attribute _confopts.
19 20 21 |
# File 'lib/moose_inventory/config/config.rb', line 19 def _confopts @_confopts end |
#_settings ⇒ Object (readonly)
Returns the value of attribute _settings.
20 21 22 |
# File 'lib/moose_inventory/config/config.rb', line 20 def _settings @_settings end |
Class Method Details
.ansible_args ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/moose_inventory/config/config.rb', line 102 def self.ansible_args # rubocop:disable Metrics/AbcSize # # 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' @_confopts[:ansible] = true @_confopts[:format] = 'json' unless @_confopts[:format] =~ /p|pjson|j|json/ @_argv.clear @_argv.concat(['group', 'list']).flatten when '--host' @_confopts[:ansible] = true @_confopts[:format] = 'json' unless @_confopts[:format] =~ /p|pjson|j|json/ host = @_argv[1] @_argv.clear @_argv.concat(['host', 'listvars', "#{host}"]).flatten end end |
.init(args) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/moose_inventory/config/config.rb', line 23 def self.init(args) @_argv = args.dup top_level_help top_level_args ansible_args resolve_config_file load end |
.load ⇒ Object
rubocop:disable PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/moose_inventory/config/config.rb', line 172 def self.load newsets = symbolize_keys(YAML.load_file(@_confopts[:config])) path = @_confopts[:config] # Get the "general" section @_settings[:general] = newsets[:general] @_settings[:general].nil? && fail("Missing 'general' root in #{path}") # Get the config for the correct environment if @_confopts[:env] && !@_confopts[:env].empty? env = @_confopts[:env] @_settings[:config] = newsets[@_confopts[:env].to_sym] else env = @_settings[:general][:defaultenv] (env.nil? || env.empty?) && fail("No defaultenv set in #{path}") @_settings[:config] = newsets[env.to_sym] end @_settings[:config].nil? && fail("Missing '#{env}' root in #{path}") # And now we should have a valid config stuffed into @_options[:config] end |
.resolve_config_file ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/moose_inventory/config/config.rb', line 126 def self.resolve_config_file # rubocop:disable Metrics/AbcSize if ! @_confopts[:config].nil? path = File.(@_confopts[:config]) if File.exist?(path) @_confopts[:config] = path else fail("The configuration file #{path} does not exist") end else possibles = ['./.moose-tools/inventory/config', '~/.moose-tools/inventory/config', '~/local/etc/moose-tools/inventory/config', '/etc/moose-tools/inventory/config' ] possibles.each do |f| file = File.(f) @_confopts[:config] = file if File.exist?(file) end end if @_confopts[:config].nil? fail('No configuration either given or found in standard locations.') end end |
.symbolize_keys(hash) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/moose_inventory/config/config.rb', line 152 def self.symbolize_keys(hash) # rubocop:disable Style/EachWithObject hash.inject({}) do |result, (key, value)| new_key = case key when String then key.to_sym else key end new_value = case value when Hash then symbolize_keys(value) else value end result[new_key] = new_value result end # rubocop:enable Style/EachWithObject end |
.top_level_args ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/moose_inventory/config/config.rb', line 34 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. @_confopts = { env: '', format: 'json', ansible: false, trace: false } # Check for two-part flags %w(config env format).each do |var| @_argv.each_with_index do |val, index| next if val != "--#{var}" @_confopts[var.to_sym] = @_argv[index + 1] 1.downto(0) { |offset| @_argv.delete_at(index + offset) } break end end # Check for one-part flags %w(ansible trace).each do |var| @_argv.each_with_index do |val, index| next if val != "--#{var}" @_confopts[var.to_sym] = true @_argv.delete_at(index) break end end # Sanity # - Ansible output format must be json - pjson is permitted, but yaml is not. if @_confopts[:ansible] == true unless @_confopts[:format] =~ /p|pjson|j|json/ @_confopts[:format] = 'json' end end end |
.top_level_help ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/moose_inventory/config/config.rb', line 87 def self.top_level_help if @_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 end |