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/inventory2.rb
Defined Under Namespace
Classes: Group, Group2, Inventory2, ValidationError, WildcardError
Constant Summary
collapse
- ENVIRONMENT_VAR =
'BOLT_INVENTORY'
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, target_vars: {}, target_facts: {}, target_features: {}) ⇒ Inventory
constructor
A new instance of Inventory.
-
#node_names ⇒ Object
-
#set_feature(target, feature, value = true) ⇒ Object
-
#set_var(target, key, value) ⇒ Object
-
#validate ⇒ Object
-
#vars(target) ⇒ Object
Constructor Details
#initialize(data, config = nil, target_vars: {}, target_facts: {}, target_features: {}) ⇒ Inventory
Returns a new instance of Inventory.
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/bolt/inventory.rb', line 74
def initialize(data, config = nil, target_vars: {}, target_facts: {}, target_features: {})
@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
@groups.resolve_aliases(@groups.node_aliases)
collect_groups
end
|
Class Method Details
.create_version(data, config, plugins) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/bolt/inventory.rb', line 62
def self.create_version(data, config, plugins)
version = (data || {}).delete('version') { 1 }
case version
when 1
new(data, config)
when 2
Bolt::Inventory::Inventory2.new(data, config, plugins: plugins)
else
raise ValidationError, "Unsupported version #{version} specified in inventory"
end
end
|
.from_config(config, plugins = nil) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# 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])
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
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/bolt/inventory.rb', line 181
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
138
139
140
141
|
# File 'lib/bolt/inventory.rb', line 138
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
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/bolt/inventory.rb', line 116
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
93
94
95
96
|
# File 'lib/bolt/inventory.rb', line 93
def collect_groups
@group_lookup = @groups.collect_groups
end
|
#data_hash ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/bolt/inventory.rb', line 160
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
143
144
145
|
# File 'lib/bolt/inventory.rb', line 143
def facts(target)
@target_facts[target.name] || {}
end
|
#features(target) ⇒ Object
156
157
158
|
# File 'lib/bolt/inventory.rb', line 156
def features(target)
@target_features[target.name] || Set.new
end
|
#get_targets(targets) ⇒ Object
106
107
108
109
110
111
112
113
114
|
# File 'lib/bolt/inventory.rb', line 106
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
98
99
100
|
# File 'lib/bolt/inventory.rb', line 98
def group_names
@group_lookup.keys
end
|
#node_names ⇒ Object
102
103
104
|
# File 'lib/bolt/inventory.rb', line 102
def node_names
@groups.node_names
end
|
#set_feature(target, feature, value = true) ⇒ Object
147
148
149
150
151
152
153
154
|
# File 'lib/bolt/inventory.rb', line 147
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
129
130
131
132
|
# File 'lib/bolt/inventory.rb', line 129
def set_var(target, key, value)
data = { key => value }
set_vars_from_hash(target.name, data)
end
|
#validate ⇒ Object
89
90
91
|
# File 'lib/bolt/inventory.rb', line 89
def validate
@groups.validate
end
|
#vars(target) ⇒ Object
134
135
136
|
# File 'lib/bolt/inventory.rb', line 134
def vars(target)
@target_vars[target.name] || {}
end
|