Class: Bolt::Inventory::Inventory2

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Inventory2.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bolt/inventory/inventory2.rb', line 8

def initialize(data, config = 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.resolve_aliases(@groups.node_aliases, @groups.node_names)
  collect_groups
end

Instance Method Details

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



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

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



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

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

#collect_groupsObject



27
28
29
30
# File 'lib/bolt/inventory/inventory2.rb', line 27

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

#create_target(target_name) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/bolt/inventory/inventory2.rb', line 248

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 node was only referred to as a string.
  uri = data['uri']
  uri ||= target_name unless data['name']

  Target.new(uri, name_opt)
end

#data_hashObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bolt/inventory/inventory2.rb', line 94

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



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

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

#features(target) ⇒ Object



90
91
92
# File 'lib/bolt/inventory/inventory2.rb', line 90

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

#get_targets(targets) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/bolt/inventory/inventory2.rb', line 40

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



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

def group_names
  @group_lookup.keys
end

#node_namesObject



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

def node_names
  @groups.node_names
end

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



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

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



63
64
65
66
# File 'lib/bolt/inventory/inventory2.rb', line 63

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

#validateObject



23
24
25
# File 'lib/bolt/inventory/inventory2.rb', line 23

def validate
  @groups.validate
end

#vars(target) ⇒ Object



68
69
70
# File 'lib/bolt/inventory/inventory2.rb', line 68

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