Class: Bolt::Inventory::Group2

Inherits:
Object
  • Object
show all
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[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

Instance Method Summary collapse

Constructor Details

#initialize(input, plugins) ⇒ Group2

Returns a new instance of Group2.

Raises:



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
# File 'lib/bolt/inventory/group2.rb', line 21

def initialize(input, plugins)
  @logger = Logging.logger[self]
  @plugins = plugins

  input = resolve_top_level_references(input) if reference?(input)

  raise ValidationError.new("Group does not have a name", nil) unless input.key?('name')

  @name = 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 = resolve_top_level_references(input.fetch('targets', []))

  @unresolved_targets = {}
  @resolved_targets = {}
  @targets = Set.new
  # @target_objects = {}
  @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("Node 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 = resolve_top_level_references(groups)

  @groups = Array(groups).map { |g| Group2.new(g, plugins) }
end

Instance Attribute Details

#groupsObject

Returns the value of attribute groups.



10
11
12
# File 'lib/bolt/inventory/group2.rb', line 10

def groups
  @groups
end

#nameObject

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



217
218
219
# File 'lib/bolt/inventory/group2.rb', line 217

def add_target(target)
  @resolved_targets[target.name] = { 'name' => target.name }
end

#add_target_definition(target) ⇒ Object



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
210
211
212
213
214
215
# File 'lib/bolt/inventory/group2.rb', line 162

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 node
  # names.
  unless target.is_a?(Hash)
    raise ValidationError.new("Node entry must be a Hash, not #{target.class}", @name)
  end

  target['name'] = resolve_references(target['name']) if target.key?('name')
  target['uri'] = resolve_references(target['uri']) if target.key?('uri')
  target['alias'] = 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.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

    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, t_name), @name)
      end
      @aliases[alia] = t_name
    end
  end

  @unresolved_targets[t_name] = target
end

#all_targetsObject

Returns all targets contained within the group, which includes targets from subgroups.



407
408
409
410
411
# File 'lib/bolt/inventory/group2.rb', line 407

def all_targets
  @groups.inject(local_targets) do |acc, g|
    acc.merge(g.all_targets)
  end
end

#collect_groupsObject

Return a mapping of group names to group.



421
422
423
424
425
# File 'lib/bolt/inventory/group2.rb', line 421

def collect_groups
  @groups.inject(name => self) do |acc, g|
    acc.merge(g.collect_groups)
  end
end

#data_merge(data1, data2) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/bolt/inventory/group2.rb', line 221

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'],
    'plugin_hooks' => data1['plugin_hooks'].merge(data2['plugin_hooks']),
    'groups' => data2['groups'] + data1['groups']
  }
end

#group_collect(target_name) ⇒ Object



437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/bolt/inventory/group2.rb', line 437

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_dataObject



397
398
399
# File 'lib/bolt/inventory/group2.rb', line 397

def group_data
  @group_data ||= resolve_data_keys(@input).merge('groups' => [@name])
end

#local_targetsObject

Returns targets contained directly within the group, ignoring subgroups



402
403
404
# File 'lib/bolt/inventory/group2.rb', line 402

def local_targets
  Set.new(@unresolved_targets.keys) + Set.new(@resolved_targets.keys)
end

#reference?(input) ⇒ Boolean

Checks whether a given value is a _plugin reference

Returns:

  • (Boolean)


143
144
145
# File 'lib/bolt/inventory/group2.rb', line 143

def reference?(input)
  input.is_a?(Hash) && input.key?('_plugin')
end

#resolve_data_keys(data, target = nil) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/bolt/inventory/group2.rb', line 359

def resolve_data_keys(data, target = nil)
  result = {
    'config' => resolve_references(data.fetch('config', {})),
    'vars' => resolve_references(data.fetch('vars', {})),
    'facts' => resolve_references(data.fetch('facts', {})),
    'features' => resolve_references(data.fetch('features', [])),
    'plugin_hooks' => 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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/bolt/inventory/group2.rb', line 241

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_aliasesObject

Returns a mapping of aliases to targets contained within the group, which includes subgroups.



414
415
416
417
418
# File 'lib/bolt/inventory/group2.rb', line 414

def target_aliases
  @groups.inject(@aliases) do |acc, g|
    acc.merge(g.target_aliases)
  end
end

#target_collect(target_name) ⇒ Object



427
428
429
430
431
432
433
434
435
# File 'lib/bolt/inventory/group2.rb', line 427

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/bolt/inventory/group2.rb', line 147

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'],
      # 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

Raises:



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
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/bolt/inventory/group2.rb', line 306

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



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/bolt/inventory/group2.rb', line 372

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) || 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 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

Raises:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/bolt/inventory/group2.rb', line 283

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 Metrics/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 reference?(key)
      raise ValidationError.new("Group keys cannot be specified as _plugin references", @name)
    else
      key
    end
  end
end