Class: Bolt::Inventory::Inventory2

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/inventory/inventory2.rb

Defined Under Namespace

Classes: WildcardError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, config = nil, plugins: nil) ⇒ Inventory2

Returns a new instance of Inventory2.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bolt/inventory/inventory2.rb', line 17

def initialize(data, config = nil, plugins: nil)
  @logger = Logging.logger[self]
  # Config is saved to add config options to targets
  @config = config || Bolt::Config.default
  @data = data || {}
  @groups = Group2.new(@data.merge('name' => 'all'), plugins)
  @plugins = plugins
  @group_lookup = {}
  # The targets hash is the canonical source for all targets in inventory
  @targets = {}
  @groups.resolve_string_targets(@groups.target_aliases, @groups.all_targets)
  collect_groups
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/bolt/inventory/inventory2.rb', line 9

def config
  @config
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



9
10
11
# File 'lib/bolt/inventory/inventory2.rb', line 9

def plugins
  @plugins
end

#targetsObject (readonly)

Returns the value of attribute targets.



9
10
11
# File 'lib/bolt/inventory/inventory2.rb', line 9

def targets
  @targets
end

Instance Method Details

#add_facts(target, new_facts = {}) ⇒ Object



258
259
260
261
262
263
# File 'lib/bolt/inventory/inventory2.rb', line 258

def add_facts(target, new_facts = {})
  @targets[target.name].add_facts(new_facts)
  # rubocop:disable Style/GlobalVars
  $future ? target : facts(target)
  # rubocop:enable Style/GlobalVars
end

#add_to_group(targets, desired_group) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/bolt/inventory/inventory2.rb', line 236

def add_to_group(targets, desired_group)
  if group_names.include?(desired_group)
    targets.each do |target|
      if group_names.include?(target.name)
        raise ValidationError.new("Group #{target.name} conflicts with target of the same name", target.name)
      end
      # Add the inventory copy of the target
      add_target(@groups, @targets[target.name], desired_group)
    end
  else
    raise ValidationError.new("Group #{desired_group} does not exist in inventory", nil)
  end
end

#clear_alia_from_group(group, target_name) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/bolt/inventory/inventory2.rb', line 211

def clear_alia_from_group(group, target_name)
  if group.all_target_names.include?(target_name)
    group.clear_alia(target_name)
  end
  group.groups.each do |grp|
    clear_alia_from_group(grp, target_name)
  end
end

#collect_groupsObject



43
44
45
46
# File 'lib/bolt/inventory/inventory2.rb', line 43

def collect_groups
  # Provide a lookup map for finding a group by name
  @group_lookup = @groups.collect_groups
end

#create_target_from_inventory(target_name) ⇒ Object

Pull in a target definition from the inventory file and evaluate any associated references. This is used when a target is resolved by get_targets.



172
173
174
175
176
177
178
179
180
181
# File 'lib/bolt/inventory/inventory2.rb', line 172

def create_target_from_inventory(target_name)
  target_data = @groups.target_collect(target_name) || { 'uri' => target_name }

  target = Bolt::Inventory::Target.new(target_data, self)
  @targets[target.name] = target

  add_to_group([target], 'all')

  target
end

#create_target_from_plan(data) ⇒ Object

Add a brand new target, overriding any existing target with the same name. This method does not honor target config from the inventory. This is used when Target.new is called from a plan.



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/inventory2.rb', line 186

def create_target_from_plan(data)
  # If target already exists, delete old and replace with new, otherwise add to new to all group
  new_target = Bolt::Inventory::Target.new(data, self)
  existing_target = @targets.key?(new_target.name)
  @targets[new_target.name] = new_target

  if existing_target
    clear_alia_from_group(@groups, new_target.name)
  else
    add_to_group([new_target], 'all')
  end

  if (aliases = new_target.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

    @groups.insert_alia(new_target.name, aliases)
  end

  new_target
end

#data_hashObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bolt/inventory/inventory2.rb', line 75

def data_hash
  {
    data: {},
    target_hash: {
      target_vars: {},
      target_facts: {},
      target_features: {}
    },
    config: @config.transport_data_get
  }
end

#facts(target) ⇒ Object



265
266
267
# File 'lib/bolt/inventory/inventory2.rb', line 265

def facts(target)
  @targets[target.name].facts
end

#features(target) ⇒ Object



273
274
275
# File 'lib/bolt/inventory/inventory2.rb', line 273

def features(target)
  @targets[target.name].features
end

#get_target(target) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/bolt/inventory/inventory2.rb', line 67

def get_target(target)
  target_array = get_targets(target)
  if target_array.count > 1
    raise ValidationError.new("'#{target}' refers to #{target_array.count} targets", nil)
  end
  target_array.first
end

#get_targets(targets) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/bolt/inventory/inventory2.rb', line 58

def get_targets(targets)
  target_array = expand_targets(targets)
  if target_array.is_a? Array
    target_array.flatten.uniq(&:name)
  else
    [target_array]
  end
end

#group_data_for(target_name) ⇒ Object

PRIVATE ####



88
89
90
# File 'lib/bolt/inventory/inventory2.rb', line 88

def group_data_for(target_name)
  @groups.group_collect(target_name)
end

#group_namesObject



48
49
50
# File 'lib/bolt/inventory/inventory2.rb', line 48

def group_names
  @group_lookup.keys
end

#plugin_hooks(target) ⇒ Object



277
278
279
# File 'lib/bolt/inventory/inventory2.rb', line 277

def plugin_hooks(target)
  @targets[target.name].plugin_hooks
end

#remove_from_group(target, desired_group) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/bolt/inventory/inventory2.rb', line 220

def remove_from_group(target, desired_group)
  unless target.length == 1
    raise ValidationError.new("'remove_from_group' expects a single Target, got #{target.length}", nil)
  end

  if desired_group == 'all'
    raise ValidationError.new("Cannot remove Target from Group 'all'", nil)
  end

  if group_names.include?(desired_group)
    remove_target(@groups, @targets[target.first.name], desired_group)
  else
    raise ValidationError.new("Group #{desired_group} does not exist in inventory", nil)
  end
end

#set_config(target, key_or_key_path, value) ⇒ Object



281
282
283
# File 'lib/bolt/inventory/inventory2.rb', line 281

def set_config(target, key_or_key_path, value)
  @targets[target.name].set_config(key_or_key_path, value)
end

#set_feature(target, feature, value = true) ⇒ Object



269
270
271
# File 'lib/bolt/inventory/inventory2.rb', line 269

def set_feature(target, feature, value = true)
  @targets[target.name].set_feature(feature, value)
end

#set_var(target, var_hash) ⇒ Object



250
251
252
# File 'lib/bolt/inventory/inventory2.rb', line 250

def set_var(target, var_hash)
  @targets[target.name].set_var(var_hash)
end

#target_config(target) ⇒ Object



285
286
287
# File 'lib/bolt/inventory/inventory2.rb', line 285

def target_config(target)
  @targets[target.name].config
end

#target_implementation_classObject



39
40
41
# File 'lib/bolt/inventory/inventory2.rb', line 39

def target_implementation_class
  Bolt::Target2
end

#target_namesObject Also known as: node_names



52
53
54
# File 'lib/bolt/inventory/inventory2.rb', line 52

def target_names
  @groups.all_targets
end

#validateObject



31
32
33
# File 'lib/bolt/inventory/inventory2.rb', line 31

def validate
  @groups.validate
end

#vars(target) ⇒ Object



254
255
256
# File 'lib/bolt/inventory/inventory2.rb', line 254

def vars(target)
  @targets[target.name].vars
end

#versionObject



35
36
37
# File 'lib/bolt/inventory/inventory2.rb', line 35

def version
  2
end