Class: Bolt::Inventory::Inventory
- Inherits:
-
Object
- Object
- Bolt::Inventory::Inventory
- Defined in:
- lib/bolt/inventory/inventory.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.
-
#targets ⇒ Object
readonly
Returns the value of attribute targets.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Class Method Summary collapse
Instance Method Summary collapse
- #add_facts(target, new_facts = {}) ⇒ Object
- #add_to_group(targets, desired_group) ⇒ Object
- #clear_alia_from_group(group, target_name) ⇒ Object
- #collect_groups ⇒ Object
-
#create_target_from_hash(data) ⇒ Object
Add a brand new target, overriding any existing target with the same name.
-
#create_target_from_inventory(target_name) ⇒ Object
Pull in a target definition from the inventory file and evaluate any associated references.
- #facts(target) ⇒ Object
- #features(target) ⇒ Object
- #get_target(target) ⇒ Object
- #get_targets(targets) ⇒ Object
-
#group_data_for(target_name) ⇒ Object
PRIVATE ####.
- #group_names ⇒ Object
- #group_names_for(target_name) ⇒ Object
-
#initialize(data, transport, transports, plugins) ⇒ Inventory
constructor
TODO: Pass transport config instead of config object.
- #plugin_hooks(target) ⇒ Object
- #remove_from_group(target, desired_group) ⇒ Object
- #resources(target) ⇒ Object
- #set_config(target, key_or_key_path, value) ⇒ Object
- #set_feature(target, feature, value = true) ⇒ Object
- #set_var(target, var_hash) ⇒ Object
- #target_config(target) ⇒ Object
- #target_implementation_class ⇒ Object
- #target_names ⇒ Object (also: #node_names)
- #transport_data_get ⇒ Object
- #validate ⇒ Object
- #validate_target_from_hash(target) ⇒ Object
- #vars(target) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(data, transport, transports, plugins) ⇒ Inventory
TODO: Pass transport config instead of config object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bolt/inventory/inventory.rb', line 18 def initialize(data, transport, transports, plugins) @logger = Logging.logger[self] @data = data || {} @transport = transport @config = transports @plugins = plugins @groups = Group.new(@data.merge('name' => 'all'), plugins) @group_lookup = {} @targets = {} @groups.resolve_string_targets(@groups.target_aliases, @groups.all_targets) collect_groups end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/bolt/inventory/inventory.rb', line 9 def config @config end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
9 10 11 |
# File 'lib/bolt/inventory/inventory.rb', line 9 def plugins @plugins end |
#targets ⇒ Object (readonly)
Returns the value of attribute targets.
9 10 11 |
# File 'lib/bolt/inventory/inventory.rb', line 9 def targets @targets end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
9 10 11 |
# File 'lib/bolt/inventory/inventory.rb', line 9 def transport @transport end |
Class Method Details
.localhost_defaults(data) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/bolt/inventory/inventory.rb', line 81 def self.localhost_defaults(data) defaults = { 'config' => { 'transport' => 'local', 'local' => { 'interpreters' => { '.rb' => RbConfig.ruby } } }, 'features' => ['puppet-agent'] } data = Bolt::Util.deep_merge(defaults, data) # If features is an empty array deep_merge won't add the puppet-agent data['features'] += ['puppet-agent'] if data['features'].empty? data end |
Instance Method Details
#add_facts(target, new_facts = {}) ⇒ Object
294 295 296 297 |
# File 'lib/bolt/inventory/inventory.rb', line 294 def add_facts(target, new_facts = {}) @targets[target.name].add_facts(new_facts) target end |
#add_to_group(targets, desired_group) ⇒ Object
271 272 273 274 275 276 277 278 279 280 |
# File 'lib/bolt/inventory/inventory.rb', line 271 def add_to_group(targets, desired_group) if group_names.include?(desired_group) targets.each do |target| # 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
246 247 248 249 250 251 252 253 |
# File 'lib/bolt/inventory/inventory.rb', line 246 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_groups ⇒ Object
45 46 47 48 |
# File 'lib/bolt/inventory/inventory.rb', line 45 def collect_groups # Provide a lookup map for finding a group by name @group_lookup = @groups.collect_groups end |
#create_target_from_hash(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 or with a data hash.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/bolt/inventory/inventory.rb', line 193 def create_target_from_hash(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) validate_target_from_hash(new_target) @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 new_target.target_alias @groups.insert_alia(new_target.name, Array(new_target.target_alias)) end new_target 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.
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/bolt/inventory/inventory.rb', line 179 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 |
#facts(target) ⇒ Object
299 300 301 |
# File 'lib/bolt/inventory/inventory.rb', line 299 def facts(target) @targets[target.name].facts end |
#features(target) ⇒ Object
307 308 309 |
# File 'lib/bolt/inventory/inventory.rb', line 307 def features(target) @targets[target.name].features end |
#get_target(target) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/bolt/inventory/inventory.rb', line 73 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
64 65 66 67 68 69 70 71 |
# File 'lib/bolt/inventory/inventory.rb', line 64 def get_targets(targets) target_array = (targets) if target_array.is_a? Array target_array.flatten.uniq(&:name) else [target_array] end end |
#group_data_for(target_name) ⇒ Object
PRIVATE ####
96 97 98 |
# File 'lib/bolt/inventory/inventory.rb', line 96 def group_data_for(target_name) @groups.group_collect(target_name) end |
#group_names ⇒ Object
50 51 52 |
# File 'lib/bolt/inventory/inventory.rb', line 50 def group_names @group_lookup.keys end |
#group_names_for(target_name) ⇒ Object
54 55 56 |
# File 'lib/bolt/inventory/inventory.rb', line 54 def group_names_for(target_name) group_data_for(target_name).fetch('groups', []) end |
#plugin_hooks(target) ⇒ Object
311 312 313 |
# File 'lib/bolt/inventory/inventory.rb', line 311 def plugin_hooks(target) @targets[target.name].plugin_hooks end |
#remove_from_group(target, desired_group) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/bolt/inventory/inventory.rb', line 255 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 |
#resources(target) ⇒ Object
323 324 325 |
# File 'lib/bolt/inventory/inventory.rb', line 323 def resources(target) @targets[target.name].resources end |
#set_config(target, key_or_key_path, value) ⇒ Object
315 316 317 |
# File 'lib/bolt/inventory/inventory.rb', line 315 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
303 304 305 |
# File 'lib/bolt/inventory/inventory.rb', line 303 def set_feature(target, feature, value = true) @targets[target.name].set_feature(feature, value) end |
#set_var(target, var_hash) ⇒ Object
286 287 288 |
# File 'lib/bolt/inventory/inventory.rb', line 286 def set_var(target, var_hash) @targets[target.name].set_var(var_hash) end |
#target_config(target) ⇒ Object
319 320 321 |
# File 'lib/bolt/inventory/inventory.rb', line 319 def target_config(target) @targets[target.name].config end |
#target_implementation_class ⇒ Object
41 42 43 |
# File 'lib/bolt/inventory/inventory.rb', line 41 def target_implementation_class Bolt::Target end |
#target_names ⇒ Object Also known as: node_names
58 59 60 |
# File 'lib/bolt/inventory/inventory.rb', line 58 def target_names @groups.all_targets end |
#transport_data_get ⇒ Object
282 283 284 |
# File 'lib/bolt/inventory/inventory.rb', line 282 def transport_data_get { transport: transport, transports: config.transform_values(&:to_h) } end |
#validate ⇒ Object
33 34 35 |
# File 'lib/bolt/inventory/inventory.rb', line 33 def validate @groups.validate end |
#validate_target_from_hash(target) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/bolt/inventory/inventory.rb', line 214 def validate_target_from_hash(target) groups = Set.new(group_names) targets = target_names # Make sure there are no group name conflicts if groups.include?(target.name) raise ValidationError.new("Target name #{target.name} conflicts with group of the same name", nil) end # Validate any aliases if (aliases = target.target_alias) unless aliases.is_a?(Array) || aliases.is_a?(String) msg = "Alias entry on #{t_name} must be a String or Array, not #{aliases.class}" raise ValidationError.new(msg, @name) end end # Make sure there are no conflicts with the new target aliases used_aliases = @groups.target_aliases Array(target.target_alias).each do |alia| if groups.include?(alia) raise ValidationError.new("Alias #{alia} conflicts with group of the same name", nil) elsif targets.include?(alia) raise ValidationError.new("Alias #{alia} conflicts with target of the same name", nil) elsif used_aliases[alia] && used_aliases[alia] != target.name raise ValidationError.new( "Alias #{alia} refers to multiple targets: #{used_aliases[alia]} and #{target.name}", nil ) end end end |
#vars(target) ⇒ Object
290 291 292 |
# File 'lib/bolt/inventory/inventory.rb', line 290 def vars(target) @targets[target.name].vars end |
#version ⇒ Object
37 38 39 |
# File 'lib/bolt/inventory/inventory.rb', line 37 def version 2 end |