Class: Bolt::Inventory::Inventory2
- Inherits:
-
Object
- Object
- Bolt::Inventory::Inventory2
- Defined in:
- lib/bolt/inventory/inventory2.rb
Defined Under Namespace
Classes: WildcardError
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
Instance Method Summary collapse
- #add_facts(target, new_facts = {}) ⇒ Object
- #add_to_group(targets, desired_group) ⇒ Object
- #collect_groups ⇒ Object
- #create_target(target_name) ⇒ Object
- #data_hash ⇒ Object
- #facts(target) ⇒ Object
- #features(target) ⇒ Object
- #get_targets(targets) ⇒ Object
- #group_names ⇒ Object
-
#initialize(data, config = nil, plugins: nil, target_vars: {}, target_facts: {}, target_features: {}, target_plugin_hooks: {}) ⇒ Inventory2
constructor
A new instance of Inventory2.
- #plugin_hooks(target) ⇒ Object
- #set_feature(target, feature, value = true) ⇒ Object
- #set_var(target, key, value) ⇒ Object
- #target_names ⇒ Object (also: #node_names)
- #validate ⇒ Object
- #vars(target) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(data, config = nil, plugins: nil, target_vars: {}, target_facts: {}, target_features: {}, target_plugin_hooks: {}) ⇒ Inventory2
Returns a new instance of Inventory2.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bolt/inventory/inventory2.rb', line 17 def initialize(data, config = nil, plugins: nil, target_vars: {}, target_facts: {}, target_features: {}, target_plugin_hooks: {}) @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 = {} @target_vars = target_vars @target_facts = target_facts @target_features = target_features @target_plugin_hooks = target_plugin_hooks @groups.resolve_aliases(@groups.target_aliases, @groups.target_names) collect_groups end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/bolt/inventory/inventory2.rb', line 15 def config @config end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
15 16 17 |
# File 'lib/bolt/inventory/inventory2.rb', line 15 def plugins @plugins end |
Instance Method Details
#add_facts(target, new_facts = {}) ⇒ Object
90 91 92 93 |
# File 'lib/bolt/inventory/inventory2.rb', line 90 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
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bolt/inventory/inventory2.rb', line 68 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_groups ⇒ Object
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(target_name) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/bolt/inventory/inventory2.rb', line 307 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_hash ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/bolt/inventory/inventory2.rb', line 116 def data_hash { data: {}, target_hash: { target_vars: {}, target_facts: {}, target_features: {} }, config: @config.transport_data_get } end |
#facts(target) ⇒ Object
95 96 97 |
# File 'lib/bolt/inventory/inventory2.rb', line 95 def facts(target) @target_facts[target.name] || {} end |
#features(target) ⇒ Object
108 109 110 |
# File 'lib/bolt/inventory/inventory2.rb', line 108 def features(target) @target_features[target.name] || Set.new end |
#get_targets(targets) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/bolt/inventory/inventory2.rb', line 58 def get_targets(targets) targets = (targets) targets = if targets.is_a? Array targets.flatten.uniq(&:name) else [targets] end targets.map { |t| update_target(t) } end |
#group_names ⇒ Object
48 49 50 |
# File 'lib/bolt/inventory/inventory2.rb', line 48 def group_names @group_lookup.keys end |
#plugin_hooks(target) ⇒ Object
112 113 114 |
# File 'lib/bolt/inventory/inventory2.rb', line 112 def plugin_hooks(target) @target_plugin_hooks[target.name] || {} end |
#set_feature(target, feature, value = true) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/bolt/inventory/inventory2.rb', line 99 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
81 82 83 84 |
# File 'lib/bolt/inventory/inventory2.rb', line 81 def set_var(target, key, value) data = { key => value } set_vars_from_hash(target.name, data) end |
#target_names ⇒ Object Also known as: node_names
52 53 54 |
# File 'lib/bolt/inventory/inventory2.rb', line 52 def target_names @groups.target_names end |
#validate ⇒ Object
35 36 37 |
# File 'lib/bolt/inventory/inventory2.rb', line 35 def validate @groups.validate end |
#vars(target) ⇒ Object
86 87 88 |
# File 'lib/bolt/inventory/inventory2.rb', line 86 def vars(target) @target_vars[target.name] || {} end |
#version ⇒ Object
39 40 41 |
# File 'lib/bolt/inventory/inventory2.rb', line 39 def version 2 end |