Class: Bolt::Inventory
- Inherits:
-
Object
show all
- Defined in:
- lib/bolt/inventory.rb,
lib/bolt/inventory/group.rb,
lib/bolt/inventory/group2.rb,
lib/bolt/inventory/target.rb,
lib/bolt/inventory/inventory2.rb
Defined Under Namespace
Classes: Group, Group2, Inventory2, Target, ValidationError, WildcardError
Constant Summary
collapse
- ENVIRONMENT_VAR =
'BOLT_INVENTORY'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_facts(target, new_facts = {}) ⇒ Object
-
#add_to_group(targets, desired_group) ⇒ Object
-
#collect_groups ⇒ 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: {}) ⇒ Inventory
constructor
A new instance of Inventory.
-
#node_names ⇒ Object
-
#plugin_hooks(target) ⇒ Object
-
#remove_from_group(target, desired_group) ⇒ Object
-
#set_feature(target, feature, value = true) ⇒ Object
-
#set_var(target, var_hash) ⇒ Object
-
#target_alias(target) ⇒ Object
-
#validate ⇒ Object
-
#vars(target) ⇒ Object
-
#version ⇒ Object
Constructor Details
#initialize(data, config = nil, plugins: nil, target_vars: {}, target_facts: {}, target_features: {}, target_plugin_hooks: {}) ⇒ Inventory
Returns a new instance of Inventory.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/bolt/inventory.rb', line 77
def initialize(data, config = nil, plugins: nil, target_vars: {},
target_facts: {}, target_features: {}, target_plugin_hooks: {})
@logger = Logging.logger[self]
@config = config || Bolt::Config.default
@data = data ||= {}
@groups = Group.new(data.merge('name' => 'all'))
@group_lookup = {}
@target_vars = target_vars
@target_facts = target_facts
@target_features = target_features
@plugins = plugins
@target_plugin_hooks = target_plugin_hooks
@groups.resolve_aliases(@groups.node_aliases)
collect_groups
deprecation unless @data.empty?
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
75
76
77
|
# File 'lib/bolt/inventory.rb', line 75
def config
@config
end
|
#plugins ⇒ Object
Returns the value of attribute plugins.
75
76
77
|
# File 'lib/bolt/inventory.rb', line 75
def plugins
@plugins
end
|
Class Method Details
.create_version(data, config, plugins) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/bolt/inventory.rb', line 63
def self.create_version(data, config, plugins)
version = (data || {}).delete('version') { 1 }
case version
when 1
new(data, config, plugins: plugins)
when 2
Bolt::Inventory::Inventory2.new(data, config, plugins: plugins)
else
raise ValidationError.new("Unsupported version #{version} specified in inventory", nil)
end
end
|
.from_config(config, plugins = nil) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/bolt/inventory.rb', line 46
def self.from_config(config, plugins = nil)
if ENV.include?(ENVIRONMENT_VAR)
begin
data = YAML.safe_load(ENV[ENVIRONMENT_VAR])
raise Bolt::ParseError, "Could not parse inventory from $#{ENVIRONMENT_VAR}" unless data.is_a?(Hash)
rescue Psych::Exception
raise Bolt::ParseError, "Could not parse inventory from $#{ENVIRONMENT_VAR}"
end
else
data = Bolt::Util.read_config_file(config.inventoryfile, config.default_inventoryfile, 'inventory')
end
inventory = create_version(data, config, plugins)
inventory.validate
inventory
end
|
.localhost_defaults(data) ⇒ Object
TODO: Possibly refactor this once inventory v2 is more stable
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/bolt/inventory.rb', line 235
def self.localhost_defaults(data)
defaults = {
'config' => {
'transport' => 'local',
'local' => { 'interpreters' => { '.rb' => RbConfig.ruby } }
},
'features' => ['puppet-agent']
}
data = Bolt::Util.deep_merge(defaults, data)
data['features'] += ['puppet-agent'] if data['features'].empty?
data
end
|
Instance Method Details
#add_facts(target, new_facts = {}) ⇒ Object
181
182
183
184
185
186
187
|
# File 'lib/bolt/inventory.rb', line 181
def add_facts(target, new_facts = {})
@logger.warn("No facts to add") if new_facts.empty?
facts = set_facts(target.name, new_facts)
$future ? target : facts
end
|
#add_to_group(targets, desired_group) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/bolt/inventory.rb', line 143
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_groups ⇒ Object
116
117
118
119
|
# File 'lib/bolt/inventory.rb', line 116
def collect_groups
@group_lookup = @groups.collect_groups
end
|
#data_hash ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/bolt/inventory.rb', line 214
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
189
190
191
|
# File 'lib/bolt/inventory.rb', line 189
def facts(target)
@target_facts[target.name] || {}
end
|
#features(target) ⇒ Object
202
203
204
|
# File 'lib/bolt/inventory.rb', line 202
def features(target)
@target_features[target.name] || Set.new
end
|
#get_targets(targets) ⇒ Object
133
134
135
136
137
138
139
140
141
|
# File 'lib/bolt/inventory.rb', line 133
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_names ⇒ Object
121
122
123
|
# File 'lib/bolt/inventory.rb', line 121
def group_names
@group_lookup.keys
end
|
#node_names ⇒ Object
125
126
127
|
# File 'lib/bolt/inventory.rb', line 125
def node_names
@groups.node_names
end
|
#plugin_hooks(target) ⇒ Object
129
130
131
|
# File 'lib/bolt/inventory.rb', line 129
def plugin_hooks(target)
@target_plugin_hooks[target.name] || {}
end
|
#remove_from_group(target, desired_group) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/bolt/inventory.rb', line 156
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)
invalidate_group_cache!(target.first)
remove_node(@groups, target.first, desired_group)
else
raise ValidationError.new("Group #{desired_group} does not exist in inventory", nil)
end
end
|
#set_feature(target, feature, value = true) ⇒ Object
193
194
195
196
197
198
199
200
|
# File 'lib/bolt/inventory.rb', line 193
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, var_hash) ⇒ Object
173
174
175
|
# File 'lib/bolt/inventory.rb', line 173
def set_var(target, var_hash)
set_vars_from_hash(target.name, var_hash)
end
|
#target_alias(target) ⇒ Object
206
207
208
209
210
211
212
|
# File 'lib/bolt/inventory.rb', line 206
def target_alias(target)
@groups.node_aliases.each_with_object([]) do |(alia, name), acc|
if target.name == name
acc << alia
end
end.uniq
end
|
#validate ⇒ Object
97
98
99
|
# File 'lib/bolt/inventory.rb', line 97
def validate
@groups.validate
end
|
#vars(target) ⇒ Object
177
178
179
|
# File 'lib/bolt/inventory.rb', line 177
def vars(target)
@target_vars[target.name] || {}
end
|
#version ⇒ Object
101
102
103
|
# File 'lib/bolt/inventory.rb', line 101
def version
1
end
|