Class: Bolt::Inventory::Group2
- Inherits:
-
Object
- Object
- Bolt::Inventory::Group2
- Defined in:
- lib/bolt/inventory/group2.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 =
%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::TRANSPORTS.keys.map(&:to_s) + ['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.
- #data_merge(data1, data2) ⇒ Object
- #group_collect(target_name) ⇒ Object
- #group_data ⇒ Object
-
#initialize(input, plugins) ⇒ Group2
constructor
A new instance of Group2.
- #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) ⇒ Group2
Returns a new instance of Group2.
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 |
# File 'lib/bolt/inventory/group2.rb', line 20 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| Group2.new(g, plugins) } end |
Instance Attribute Details
#groups ⇒ Object
Returns the value of attribute groups.
10 11 12 |
# File 'lib/bolt/inventory/group2.rb', line 10 def groups @groups end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/bolt/inventory/group2.rb', line 10 def name @name end |
Instance Method Details
#add_target(target) ⇒ Object
150 151 152 |
# File 'lib/bolt/inventory/group2.rb', line 150 def add_target(target) @resolved_targets[target.name] = { 'name' => target.name } end |
#add_target_definition(target) ⇒ Object
93 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 |
# File 'lib/bolt/inventory/group2.rb', line 93 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 local_targets.include?(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
89 90 91 |
# File 'lib/bolt/inventory/group2.rb', line 89 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.
357 358 359 360 361 |
# File 'lib/bolt/inventory/group2.rb', line 357 def all_targets @groups.inject(local_targets) do |acc, g| acc.merge(g.all_targets) end end |
#clear_alia(target_name) ⇒ Object
165 166 167 |
# File 'lib/bolt/inventory/group2.rb', line 165 def clear_alia(target_name) @aliases.reject! { |_alias, name| name == target_name } end |
#collect_groups ⇒ Object
Return a mapping of group names to group.
371 372 373 374 375 |
# File 'lib/bolt/inventory/group2.rb', line 371 def collect_groups @groups.inject(name => self) do |acc, g| acc.merge(g.collect_groups) end end |
#data_merge(data1, data2) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/bolt/inventory/group2.rb', line 169 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
387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/bolt/inventory/group2.rb', line 387 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 || local_targets.include?(target_name) # Children override the parent data_merge(group_data, child_result) end end |
#group_data ⇒ Object
347 348 349 |
# File 'lib/bolt/inventory/group2.rb', line 347 def group_data @group_data ||= resolve_data_keys(@input).merge('groups' => [@name]) end |
#insert_alia(target_name, aliases) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/bolt/inventory/group2.rb', line 154 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
352 353 354 |
# File 'lib/bolt/inventory/group2.rb', line 352 def local_targets Set.new(@unresolved_targets.keys) + Set.new(@resolved_targets.keys) end |
#remove_target(target) ⇒ Object
145 146 147 148 |
# File 'lib/bolt/inventory/group2.rb', line 145 def remove_target(target) @resolved_targets.delete(target.name) @unresolved_targets.delete(target.name) end |
#resolve_data_keys(data, target = nil) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/bolt/inventory/group2.rb', line 309 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
191 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/group2.rb', line 191 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 local_targets.include?(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 local_targets.include?(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.
364 365 366 367 368 |
# File 'lib/bolt/inventory/group2.rb', line 364 def target_aliases @groups.inject(@aliases) do |acc, g| acc.merge(g.target_aliases) end end |
#target_collect(target_name) ⇒ Object
377 378 379 380 381 382 383 384 385 |
# File 'lib/bolt/inventory/group2.rb', line 377 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bolt/inventory/group2.rb', line 73 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
256 257 258 259 260 261 262 263 264 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 |
# File 'lib/bolt/inventory/group2.rb', line 256 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| begin g.validate(used_group_names, used_target_names, used_aliases) rescue ValidationError => e e.add_parent(@name) raise e end end nil end |
#validate_data_keys(data, target = nil) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/bolt/inventory/group2.rb', line 322 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 |
# File 'lib/bolt/inventory/group2.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 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 |