Class: Bolt::Inventory::Inventory2

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

Defined Under Namespace

Classes: WildcardError

Instance Method Summary collapse

Constructor Details

#initialize(data, config = nil, plugins: nil, target_vars: {}, target_facts: {}, target_features: {}) ⇒ Inventory2

Returns a new instance of Inventory2.



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

def initialize(data, config = nil, plugins: nil, target_vars: {}, target_facts: {}, target_features: {})
  @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'))
  @group_lookup = {}
  @target_vars = target_vars
  @target_facts = target_facts
  @target_features = target_features

  @groups.lookup_targets(plugins)
  @groups.resolve_aliases(@groups.target_aliases, @groups.target_names)
  collect_groups
end

Instance Method Details

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



82
83
84
85
# File 'lib/bolt/inventory/inventory2.rb', line 82

def add_facts(target, new_facts = {})
  @logger.warn("No facts to add") if new_facts.empty?
  set_facts(target.name, new_facts)
end

#add_to_group(targets, desired_group) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bolt/inventory/inventory2.rb', line 60

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_target(@groups, target, desired_group)
    end
  else
    raise ValidationError.new("Group #{desired_group} does not exist in inventory", nil)
  end
end

#collect_groupsObject



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

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

#create_target(target_name) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/bolt/inventory/inventory2.rb', line 261

def create_target(target_name)
  data = @groups.data_for(target_name) || {}
  name_opt = {}
  name_opt['name'] = data['name'] if data['name']

  # If there is no name then this target was only referred to as a string.
  uri = data['uri']
  uri ||= target_name unless data['name']

  Target.new(uri, name_opt)
end

#data_hashObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bolt/inventory/inventory2.rb', line 104

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

#facts(target) ⇒ Object



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

def facts(target)
  @target_facts[target.name] || {}
end

#features(target) ⇒ Object



100
101
102
# File 'lib/bolt/inventory/inventory2.rb', line 100

def features(target)
  @target_features[target.name] || Set.new
end

#get_targets(targets) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/bolt/inventory/inventory2.rb', line 50

def get_targets(targets)
  targets = expand_targets(targets)
  targets = if targets.is_a? Array
              targets.flatten.uniq(&:name)
            else
              [targets]
            end
  targets.map { |t| update_target(t) }
end

#group_namesObject



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

def group_names
  @group_lookup.keys
end

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



91
92
93
94
95
96
97
98
# File 'lib/bolt/inventory/inventory2.rb', line 91

def set_feature(target, feature, value = true)
  @target_features[target.name] ||= Set.new
  if value
    @target_features[target.name] << feature
  else
    @target_features[target.name].delete(feature)
  end
end

#set_var(target, key, value) ⇒ Object



73
74
75
76
# File 'lib/bolt/inventory/inventory2.rb', line 73

def set_var(target, key, value)
  data = { key => value }
  set_vars_from_hash(target.name, data)
end

#target_namesObject Also known as: node_names



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

def target_names
  @groups.target_names
end

#validateObject



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

def validate
  @groups.validate
end

#vars(target) ⇒ Object



78
79
80
# File 'lib/bolt/inventory/inventory2.rb', line 78

def vars(target)
  @target_vars[target.name] || {}
end