Class: Bolt::Inventory::Group
- Inherits:
-
Object
- Object
- Bolt::Inventory::Group
- Defined in:
- lib/bolt/inventory/group.rb
Constant Summary collapse
- NAME_REGEX =
Regex used to validate group names and target aliases.
/\A[a-z0-9_][a-z0-9_-]*\Z/.freeze
- DATA_KEYS =
NOTE: All keys should have a corresponding schema property in schemas/bolt-inventory.schema.json
%w[config facts vars features plugin_hooks].freeze
- TARGET_KEYS =
DATA_KEYS + %w[name alias uri]
- GROUP_KEYS =
DATA_KEYS + %w[name groups targets]
- CONFIG_KEYS =
Bolt::Config::TRANSPORT_CONFIG.keys + ['transport']
Instance Attribute Summary collapse
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #add_target(target) ⇒ Object
- #add_target_definition(target) ⇒ Object
- #all_target_names ⇒ Object
-
#all_targets ⇒ Object
Returns all targets contained within the group, which includes targets from subgroups.
- #clear_alia(target_name) ⇒ Object
-
#collect_groups ⇒ Object
Return a mapping of group names to group.
- #contains_target?(target_name) ⇒ Boolean
- #data_merge(data1, data2) ⇒ Object
- #group_collect(target_name) ⇒ Object
- #group_data ⇒ Object
-
#initialize(input, plugins) ⇒ Group
constructor
A new instance of Group.
- #insert_alia(target_name, aliases) ⇒ Object
-
#local_targets ⇒ Object
Returns targets contained directly within the group, ignoring subgroups.
- #remove_target(target) ⇒ Object
- #resolve_data_keys(data, target = nil) ⇒ Object
- #resolve_string_targets(aliases, known_targets) ⇒ 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
- #validate(used_group_names = Set.new, used_target_names = Set.new, used_aliases = {}) ⇒ Object
- #validate_data_keys(data, target = nil) ⇒ Object
- #validate_group_input(input) ⇒ Object
Constructor Details
#initialize(input, plugins) ⇒ Group
Returns a new instance of Group.
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 |
# File 'lib/bolt/inventory/group.rb', line 21 def initialize(input, plugins) @logger = Logging.logger[self] @plugins = plugins input = @plugins.resolve_top_level_references(input) if @plugins.reference?(input) raise ValidationError.new("Group does not have a name", nil) unless input.key?('name') @name = @plugins.resolve_references(input['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 validate_group_input(input) @input = input validate_data_keys(@input) targets = @plugins.resolve_top_level_references(input.fetch('targets', [])) @unresolved_targets = {} @resolved_targets = {} @aliases = {} @string_targets = [] Array(targets).each do |target| # If target is a string, it can either be trivially defining a target # or it could be a name/alias of a target defined in another group. # We can't tell the difference until all groups have been resolved, # so we store the string on its own here and process it later. if target.is_a?(String) @string_targets << target # Handle plugins at this level so that lookups cannot trigger recursive lookups elsif target.is_a?(Hash) add_target_definition(target) else raise ValidationError.new("Target entry must be a String or Hash, not #{target.class}", @name) end end groups = input.fetch('groups', []) # 'groups' can be a _plugin reference, in which case we want to resolve # it. That can itself return a reference, so we want to keep resolving # them until we have a value. We don't just use resolve_references # though, since that will resolve any nested references and we want to # leave it to the group to do that lazily. groups = @plugins.resolve_top_level_references(groups) @groups = Array(groups).map { |g| Group.new(g, plugins) } end |
Instance Attribute Details
#groups ⇒ Object
Returns the value of attribute groups.
10 11 12 |
# File 'lib/bolt/inventory/group.rb', line 10 def groups @groups end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/bolt/inventory/group.rb', line 10 def name @name end |
Instance Method Details
#add_target(target) ⇒ Object
151 152 153 |
# File 'lib/bolt/inventory/group.rb', line 151 def add_target(target) @resolved_targets[target.name] = { 'name' => target.name } end |
#add_target_definition(target) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/bolt/inventory/group.rb', line 94 def add_target_definition(target) # This check ensures target lookup plugins do not returns bare strings. # Remove it if we decide to allows task plugins to return string Target # names. unless target.is_a?(Hash) raise ValidationError.new("Target entry must be a Hash, not #{target.class}", @name) end target['name'] = @plugins.resolve_references(target['name']) if target.key?('name') target['uri'] = @plugins.resolve_references(target['uri']) if target.key?('uri') target['alias'] = @plugins.resolve_references(target['alias']) if target.key?('alias') t_name = target['name'] || target['uri'] if t_name.nil? || t_name.empty? raise ValidationError.new("No name or uri for target: #{target}", @name) end unless t_name.is_a? String raise ValidationError.new("Target name must be a String, not #{t_name.class}", @name) end unless t_name.ascii_only? raise ValidationError.new("Target name must be ASCII characters: #{target}", @name) end if contains_target?(t_name) @logger.warn("Ignoring duplicate target in #{@name}: #{target}") return end unless (unexpected_keys = target.keys - TARGET_KEYS).empty? msg = "Found unexpected key(s) #{unexpected_keys.join(', ')} in target #{t_name}" @logger.warn(msg) end validate_data_keys(target, t_name) if target.include?('alias') aliases = target['alias'] aliases = [aliases] if aliases.is_a?(String) unless aliases.is_a?(Array) msg = "Alias entry on #{t_name} must be a String or Array, not #{aliases.class}" raise ValidationError.new(msg, @name) end insert_alia(t_name, aliases) end @unresolved_targets[t_name] = target end |
#all_target_names ⇒ Object
90 91 92 |
# File 'lib/bolt/inventory/group.rb', line 90 def all_target_names @unresolved_targets.keys + @resolved_targets.keys end |
#all_targets ⇒ Object
Returns all targets contained within the group, which includes targets from subgroups.
368 369 370 371 372 |
# File 'lib/bolt/inventory/group.rb', line 368 def all_targets @groups.inject(local_targets) do |acc, g| acc.merge(g.all_targets) end end |
#clear_alia(target_name) ⇒ Object
166 167 168 |
# File 'lib/bolt/inventory/group.rb', line 166 def clear_alia(target_name) @aliases.reject! { |_alias, name| name == target_name } end |
#collect_groups ⇒ Object
Return a mapping of group names to group.
382 383 384 385 386 |
# File 'lib/bolt/inventory/group.rb', line 382 def collect_groups @groups.inject(name => self) do |acc, g| acc.merge(g.collect_groups) end end |
#contains_target?(target_name) ⇒ Boolean
363 364 365 |
# File 'lib/bolt/inventory/group.rb', line 363 def contains_target?(target_name) @unresolved_targets.key?(target_name) || @resolved_targets.key?(target_name) end |
#data_merge(data1, data2) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/bolt/inventory/group.rb', line 170 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'], # Collect all aliases across all groups for each target uri 'alias' => [*data1['alias'], *data2['alias']], # 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'], 'plugin_hooks' => data1['plugin_hooks'].merge(data2['plugin_hooks']), 'groups' => data2['groups'] + data1['groups'] } end |
#group_collect(target_name) ⇒ Object
398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/bolt/inventory/group.rb', line 398 def group_collect(target_name) child_data = @groups.map { |group| group.group_collect(target_name) } # Data from earlier groups wins child_result = child_data.inject do |acc, group_data| data_merge(group_data, acc) end # If this group has the target or one of the child groups has the # target, return the data, otherwise return nil if child_result || contains_target?(target_name) # Children override the parent data_merge(group_data, child_result) end end |
#group_data ⇒ Object
354 355 356 |
# File 'lib/bolt/inventory/group.rb', line 354 def group_data @group_data ||= resolve_data_keys(@input).merge('groups' => [@name]) end |
#insert_alia(target_name, aliases) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/bolt/inventory/group.rb', line 155 def insert_alia(target_name, aliases) 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 |
#local_targets ⇒ Object
Returns targets contained directly within the group, ignoring subgroups
359 360 361 |
# File 'lib/bolt/inventory/group.rb', line 359 def local_targets Set.new(@unresolved_targets.keys) + Set.new(@resolved_targets.keys) end |
#remove_target(target) ⇒ Object
146 147 148 149 |
# File 'lib/bolt/inventory/group.rb', line 146 def remove_target(target) @resolved_targets.delete(target.name) @unresolved_targets.delete(target.name) end |
#resolve_data_keys(data, target = nil) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/bolt/inventory/group.rb', line 316 def resolve_data_keys(data, target = nil) result = { 'config' => @plugins.resolve_references(data.fetch('config', {})), 'vars' => @plugins.resolve_references(data.fetch('vars', {})), 'facts' => @plugins.resolve_references(data.fetch('facts', {})), 'features' => @plugins.resolve_references(data.fetch('features', [])), 'plugin_hooks' => @plugins.resolve_references(data.fetch('plugin_hooks', {})) } validate_data_keys(result, target) result['features'] = Set.new(result['features'].flatten) result end |
#resolve_string_targets(aliases, known_targets) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/bolt/inventory/group.rb', line 192 def resolve_string_targets(aliases, known_targets) @string_targets.each do |string_target| # If this is the name of a target defined elsewhere, then insert the # target into this group as just a name. Otherwise, add a new target # with the string as the URI. if known_targets.include?(string_target) @unresolved_targets[string_target] = { 'name' => string_target } # If this is an alias for an existing target, then add it to this group elsif (canonical_name = aliases[string_target]) if contains_target?(canonical_name) @logger.warn("Ignoring duplicate target in #{@name}: #{canonical_name}") else @unresolved_targets[canonical_name] = { 'name' => canonical_name } end # If it's not the name or alias of an existing target, then make a # new target using the string as the URI elsif contains_target?(string_target) @logger.warn("Ignoring duplicate target in #{@name}: #{string_target}") else @unresolved_targets[string_target] = { 'uri' => string_target } end end @groups.each { |g| g.resolve_string_targets(aliases, known_targets) } end |
#target_aliases ⇒ Object
Returns a mapping of aliases to targets contained within the group, which includes subgroups.
375 376 377 378 379 |
# File 'lib/bolt/inventory/group.rb', line 375 def target_aliases @groups.inject(@aliases) do |acc, g| acc.merge(g.target_aliases) end end |
#target_collect(target_name) ⇒ Object
388 389 390 391 392 393 394 395 396 |
# File 'lib/bolt/inventory/group.rb', line 388 def target_collect(target_name) child_data = @groups.map { |group| group.target_collect(target_name) } # Data from earlier groups wins child_result = child_data.inject do |acc, group_data| data_merge(group_data, acc) end # Children override the parent data_merge(target_data(target_name), child_result) end |
#target_data(target_name) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bolt/inventory/group.rb', line 74 def target_data(target_name) if @unresolved_targets.key?(target_name) target = @unresolved_targets.delete(target_name) resolved_data = resolve_data_keys(target, target_name).merge( 'name' => target['name'], 'uri' => target['uri'], 'alias' => target['alias'], # groups come from group_data 'groups' => [] ) @resolved_targets[target_name] = resolved_data else @resolved_targets[target_name] end end |
#validate(used_group_names = Set.new, used_target_names = Set.new, used_aliases = {}) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/bolt/inventory/group.rb', line 265 def validate(used_group_names = Set.new, used_target_names = Set.new, used_aliases = {}) # Test if this group name conflicts with anything used before. raise ValidationError.new("Tried to redefine group #{@name}", @name) if used_group_names.include?(@name) raise ValidationError.new(group_target_conflict(@name), @name) if used_target_names.include?(@name) raise ValidationError.new(group_alias_conflict(@name), @name) if used_aliases.include?(@name) used_group_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. @unresolved_targets.merge(@resolved_targets).each do |t_name, t_data| # Require targets to be parseable as a Target. begin # Catch malformed URI here Bolt::Inventory::Target.parse_uri(t_data['uri']) rescue Bolt::ParseError => e @logger.debug(e) raise ValidationError.new("Invalid target uri #{t_data['uri']}", @name) end raise ValidationError.new(group_target_conflict(t_name), @name) if used_group_names.include?(t_name) if used_aliases.include?(t_name) raise ValidationError.new(alias_target_conflict(t_name), @name) end used_target_names << t_name end @aliases.each do |n, target| raise ValidationError.new(group_alias_conflict(n), @name) if used_group_names.include?(n) if used_target_names.include?(n) raise ValidationError.new(alias_target_conflict(n), @name) end if used_aliases.include?(n) raise ValidationError.new(alias_conflict(n, target, used_aliases[n]), @name) end used_aliases[n] = target end @groups.each do |g| g.validate(used_group_names, used_target_names, used_aliases) rescue ValidationError => e e.add_parent(@name) raise e end nil end |
#validate_data_keys(data, target = nil) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/bolt/inventory/group.rb', line 329 def validate_data_keys(data, target = nil) { 'config' => Hash, 'vars' => Hash, 'facts' => Hash, 'features' => Array, 'plugin_hooks' => Hash }.each do |key, expected_type| next if !data.key?(key) || data[key].is_a?(expected_type) || @plugins.reference?(data[key]) msg = +"Expected #{key} to be of type #{expected_type}, not #{data[key].class}" msg << " for target #{target}" if target raise ValidationError.new(msg, @name) end unless @plugins.reference?(data['config']) unexpected_keys = data.fetch('config', {}).keys - CONFIG_KEYS if unexpected_keys.any? msg = +"Found unexpected key(s) #{unexpected_keys.join(', ')} in config for" msg << " target #{target} in" if target msg << " group #{@name}" @logger.warn(msg) end end end |
#validate_group_input(input) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/bolt/inventory/group.rb', line 233 def validate_group_input(input) raise ValidationError.new("Expected group to be a Hash, not #{input.class}", nil) unless input.is_a?(Hash) # DEPRECATION : remove this before finalization if input.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 Layout/LineLength raise ValidationError.new(msg, @name) end if input.key?('nodes') msg = <<~MSG.chomp Found 'nodes' key in group #{@name}. This looks like a v1 inventory file, which is no longer supported by Bolt. Migrate to a v2 inventory file automatically using 'bolt project migrate'. MSG raise ValidationError.new(msg, nil) end unless (unexpected_keys = input.keys - GROUP_KEYS).empty? msg = "Found unexpected key(s) #{unexpected_keys.join(', ')} in group #{@name}" @logger.warn(msg) end Bolt::Util.walk_keys(input) do |key| if @plugins.reference?(key) raise ValidationError.new("Group keys cannot be specified as _plugin references", @name) else key end end end |