Class: Bolt::Inventory::Group2
- Inherits:
-
Object
- Object
- Bolt::Inventory::Group2
- Defined in:
- lib/bolt/inventory/group2.rb
Constant Summary collapse
- NAME_REGEX =
THESE are duplicates with the old groups for now. Regex used to validate group names and target aliases.
/\A[a-z0-9_][a-z0-9_-]*\Z/.freeze
- DATA_KEYS =
%w[name config facts vars features].freeze
- NODE_KEYS =
DATA_KEYS + %w[alias uri]
- GROUP_KEYS =
DATA_KEYS + %w[groups targets]
- CONFIG_KEYS =
Bolt::TRANSPORTS.keys.map(&:to_s) + ['transport']
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#config ⇒ Object
Returns the value of attribute config.
-
#facts ⇒ Object
Returns the value of attribute facts.
-
#features ⇒ Object
Returns the value of attribute features.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#name ⇒ Object
Returns the value of attribute name.
-
#name_or_alias ⇒ Object
Returns the value of attribute name_or_alias.
-
#rest ⇒ Object
Returns the value of attribute rest.
-
#targets ⇒ Object
Returns the value of attribute targets.
-
#vars ⇒ Object
Returns the value of attribute vars.
Instance Method Summary collapse
- #add_target(target) ⇒ Object
-
#collect_groups ⇒ Object
Return a mapping of group names to group.
-
#data_for(target_name) ⇒ Object
The data functions below expect and return nil or a hash of the schema { ‘config’ => Hash , ‘vars’ => Hash, ‘facts’ => Hash, ‘features’ => Array, groups => Array }.
- #data_merge(data1, data2) ⇒ Object
- #empty_data ⇒ Object
- #group_collect(target_name) ⇒ Object
- #group_data ⇒ Object
-
#initialize(data, plugins) ⇒ Group2
constructor
A new instance of Group2.
- #lookup_targets(lookup) ⇒ Object
- #resolve_aliases(aliases, target_names) ⇒ Object
-
#target_aliases ⇒ Object
Returns a mapping of aliases to targets contained within the group, which includes subgroups.
- #target_collect(target_name) ⇒ Object
- #target_data(target_name) ⇒ Object
-
#target_names ⇒ Object
Returns all targets contained within the group, which includes targets from subgroups.
- #validate(used_names = Set.new, target_names = Set.new, aliased = {}, depth = 0) ⇒ Object
Constructor Details
#initialize(data, plugins) ⇒ Group2
Returns a new instance of Group2.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 |
# File 'lib/bolt/inventory/group2.rb', line 19 def initialize(data, plugins) @logger = Logging.logger[self] raise ValidationError.new("Expected group to be a Hash, not #{data.class}", nil) unless data.is_a?(Hash) raise ValidationError.new("Cannot set group with plugin", nil) if data.key?('_plugin') raise ValidationError.new("Group does not have a name", nil) unless data.key?('name') @plugins = plugins %w[name vars features facts].each do |key| validate_config_plugin(data[key], key, nil) end @name = data['name'] raise ValidationError.new("Group name must be a String, not #{@name.inspect}", nil) unless @name.is_a?(String) raise ValidationError.new("Invalid group name #{@name}", @name) unless @name =~ NAME_REGEX # DEPRECATION : remove this before finalization if data.key?('target-lookups') msg = "'target-lookups' are no longer a separate key. Merge 'target-lookups' and 'targets' lists and replace 'plugin' with '_plugin'" # rubocop:disable Metrics/LineLength raise ValidationError.new(msg, @name) end unless (unexpected_keys = data.keys - GROUP_KEYS).empty? msg = "Found unexpected key(s) #{unexpected_keys.join(', ')} in group #{@name}" @logger.warn(msg) end @vars = fetch_value(data, 'vars', Hash) @facts = fetch_value(data, 'facts', Hash) @features = fetch_value(data, 'features', Array) @config = config_only_plugin(fetch_value(data, 'config', Hash)) unless (unexpected_keys = @config.keys - CONFIG_KEYS).empty? msg = "Found unexpected key(s) #{unexpected_keys.join(', ')} in config for group #{@name}" @logger.warn(msg) end targets = fetch_value(data, 'targets', Array) groups = fetch_value(data, 'groups', Array) @targets = {} @aliases = {} @name_or_alias = [] targets.each do |target| # If target is a string, it can refer to either a target name or # alias. Which can't be determined until all groups have been # resolved, and requires a depth-first traversal to categorize them. if target.is_a?(String) @name_or_alias << target # Handle plugins at this level so that lookups cannot trigger recursive lookups elsif target.is_a?(Hash) if target.include?('_plugin') lookup_targets(target) else add_target(target) end else raise ValidationError.new("Node entry must be a String or Hash, not #{target.class}", @name) end end @groups = groups.map { |g| Group2.new(g, plugins) } end |
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def aliases @aliases end |
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def config @config end |
#facts ⇒ Object
Returns the value of attribute facts.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def facts @facts end |
#features ⇒ Object
Returns the value of attribute features.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def features @features end |
#groups ⇒ Object
Returns the value of attribute groups.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def groups @groups end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def name @name end |
#name_or_alias ⇒ Object
Returns the value of attribute name_or_alias.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def name_or_alias @name_or_alias end |
#rest ⇒ Object
Returns the value of attribute rest.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def rest @rest end |
#targets ⇒ Object
Returns the value of attribute targets.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def targets @targets end |
#vars ⇒ Object
Returns the value of attribute vars.
8 9 10 |
# File 'lib/bolt/inventory/group2.rb', line 8 def vars @vars end |
Instance Method Details
#add_target(target) ⇒ Object
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/bolt/inventory/group2.rb', line 145 def add_target(target) # This check ensures target lookup plugins do not returns bare strings. # Remove it if we decide to allows task plugins to return string node # names. unless target.is_a?(Hash) raise ValidationError.new("Node entry must be a Hash, not #{target.class}", @name) end # This check prevents plugins from returning plugins raise ValidationError.new("Cannot set target with plugin", @name) if target.key?('_plugin') target.each do |k, v| next if k == 'config' validate_config_plugin(v, k, @name) end target['name'] ||= target['uri'] if target['name'].nil? || target['name'].empty? raise ValidationError.new("No name or uri for target: #{target}", @name) end if @targets.include?(target['name']) @logger.warn("Ignoring duplicate target in #{@name}: #{target}") return end raise ValidationError.new("Node #{target} does not have a name", @name) unless target['name'] @targets[target['name']] = target unless (unexpected_keys = target.keys - NODE_KEYS).empty? msg = "Found unexpected key(s) #{unexpected_keys.join(', ')} in target #{target['name']}" @logger.warn(msg) end unless target['config'].nil? || target['config'].is_a?(Hash) raise ValidationError.new("Invalid configuration for target: #{target['name']}", @name) end config_keys = target['config']&.keys || [] unless (unexpected_keys = config_keys - CONFIG_KEYS).empty? msg = "Found unexpected key(s) #{unexpected_keys.join(', ')} in config for target #{target['name']}" @logger.warn(msg) end target['config'] = config_only_plugin(target['config']) unless target.include?('alias') return end aliases = target['alias'] aliases = [aliases] if aliases.is_a?(String) unless aliases.is_a?(Array) msg = "Alias entry on #{target['name']} must be a String or Array, not #{aliases.class}" raise ValidationError.new(msg, @name) end aliases.each do |alia| raise ValidationError.new("Invalid alias #{alia}", @name) unless alia =~ NAME_REGEX if (found = @aliases[alia]) raise ValidationError.new(alias_conflict(alia, found, target['name']), @name) end @aliases[alia] = target['name'] end end |
#collect_groups ⇒ Object
Return a mapping of group names to group.
385 386 387 388 389 |
# File 'lib/bolt/inventory/group2.rb', line 385 def collect_groups @groups.inject(name => self) do |acc, g| acc.merge(g.collect_groups) end end |
#data_for(target_name) ⇒ Object
The data functions below expect and return nil or a hash of the schema { ‘config’ => Hash , ‘vars’ => Hash, ‘facts’ => Hash, ‘features’ => Array, groups => Array }
350 351 352 |
# File 'lib/bolt/inventory/group2.rb', line 350 def data_for(target_name) data_merge(group_collect(target_name), target_collect(target_name)) end |
#data_merge(data1, data2) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/bolt/inventory/group2.rb', line 228 def data_merge(data1, data2) if data2.nil? || data1.nil? return data2 || data1 end { 'config' => Bolt::Util.deep_merge(data1['config'], data2['config']), 'name' => data1['name'] || data2['name'], 'uri' => data1['uri'] || data2['uri'], # Shallow merge instead of deep merge so that vars with a hash value # are assigned a new hash, rather than merging the existing value # with the value meant to replace it 'vars' => data1['vars'].merge(data2['vars']), 'facts' => Bolt::Util.deep_merge(data1['facts'], data2['facts']), 'features' => data1['features'] | data2['features'], 'groups' => data2['groups'] + data1['groups'] } end |
#empty_data ⇒ Object
362 363 364 365 366 367 368 |
# File 'lib/bolt/inventory/group2.rb', line 362 def empty_data { 'config' => {}, 'vars' => {}, 'facts' => {}, 'features' => [], 'groups' => [] } end |
#group_collect(target_name) ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/bolt/inventory/group2.rb', line 407 def group_collect(target_name) data = @groups.inject(nil) do |acc, g| if (d = g.data_for(target_name)) data_merge(d, acc) else acc end end if data data_merge(group_data, data) elsif @targets.include?(target_name) group_data end end |
#group_data ⇒ Object
354 355 356 357 358 359 360 |
# File 'lib/bolt/inventory/group2.rb', line 354 def group_data { 'config' => @config, 'vars' => @vars, 'facts' => @facts, 'features' => @features, 'groups' => [@name] } end |
#lookup_targets(lookup) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/bolt/inventory/group2.rb', line 211 def lookup_targets(lookup) begin hook = @plugins.get_hook(lookup['_plugin'], :inventory_targets) rescue Bolt::Plugin::PluginError => e raise ValidationError.new(e., @name) end begin targets = hook.call(lookup) rescue StandardError => e loc = "inventory_targets in #{@name}" raise Bolt::Plugin::PluginError::ExecutionError.new(e., lookup['_plugin'], loc) end targets.each { |target| add_target(target) } end |
#resolve_aliases(aliases, target_names) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/bolt/inventory/group2.rb', line 255 def resolve_aliases(aliases, target_names) @name_or_alias.each do |name_or_alias| # If an alias is found, insert the name into this group. Otherwise use the name as a new target's uri. if target_names.include?(name_or_alias) @targets[name_or_alias] = { 'name' => name_or_alias } elsif (target_name = aliases[name_or_alias]) if @targets.include?(target_name) @logger.warn("Ignoring duplicate target in #{@name}: #{target_name}") else @targets[target_name] = { 'name' => target_name } end else target_name = name_or_alias if @targets.include?(target_name) @logger.warn("Ignoring duplicate target in #{@name}: #{target_name}") else @targets[target_name] = { 'uri' => target_name } end end end @groups.each { |g| g.resolve_aliases(aliases, target_names) } end |
#target_aliases ⇒ Object
Returns a mapping of aliases to targets contained within the group, which includes subgroups.
378 379 380 381 382 |
# File 'lib/bolt/inventory/group2.rb', line 378 def target_aliases @groups.inject(@aliases) do |acc, g| acc.merge(g.target_aliases) end end |
#target_collect(target_name) ⇒ Object
396 397 398 399 400 401 402 403 404 405 |
# File 'lib/bolt/inventory/group2.rb', line 396 def target_collect(target_name) data = @groups.inject(nil) do |acc, g| if (d = g.target_collect(target_name)) data_merge(d, acc) else acc end end data_merge(target_data(target_name), data) end |
#target_data(target_name) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/bolt/inventory/group2.rb', line 131 def target_data(target_name) if (data = @targets[target_name]) { 'config' => data['config'] || {}, 'vars' => data['vars'] || {}, 'facts' => data['facts'] || {}, 'features' => data['features'] || [], # This allows us to determine if a target was found? 'name' => data['name'] || nil, 'uri' => data['uri'] || nil, # groups come from group_data 'groups' => [] } end end |
#target_names ⇒ Object
Returns all targets contained within the group, which includes targets from subgroups.
371 372 373 374 375 |
# File 'lib/bolt/inventory/group2.rb', line 371 def target_names @groups.inject(local_target_names) do |acc, g| acc.merge(g.target_names) end end |
#validate(used_names = Set.new, target_names = Set.new, aliased = {}, depth = 0) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/bolt/inventory/group2.rb', line 296 def validate(used_names = Set.new, target_names = Set.new, aliased = {}, depth = 0) # Test if this group name conflicts with anything used before. raise ValidationError.new("Tried to redefine group #{@name}", @name) if used_names.include?(@name) raise ValidationError.new(group_target_conflict(@name), @name) if target_names.include?(@name) raise ValidationError.new(group_alias_conflict(@name), @name) if aliased.include?(@name) used_names << @name # Collect target names and aliases into a list used to validate that subgroups don't conflict. # Used names validate that previously used group names don't conflict with new target names/aliases. @targets.each_key do |n| # Require targets to be parseable as a Target. begin Target.new(n) rescue Bolt::ParseError => e @logger.debug(e) raise ValidationError.new("Invalid target name #{n}", @name) end raise ValidationError.new(group_target_conflict(n), @name) if used_names.include?(n) if aliased.include?(n) raise ValidationError.new(alias_target_conflict(n), @name) end target_names << n end @aliases.each do |n, target| raise ValidationError.new(group_alias_conflict(n), @name) if used_names.include?(n) if target_names.include?(n) raise ValidationError.new(alias_target_conflict(n), @name) end if aliased.include?(n) raise ValidationError.new(alias_conflict(n, target, aliased[n]), @name) end aliased[n] = target end @groups.each do |g| begin g.validate(used_names, target_names, aliased, depth + 1) rescue ValidationError => e e.add_parent(@name) raise e end end nil end |