Class: Bolt::Inventory
- Inherits:
-
Object
show all
- Defined in:
- lib/bolt/inventory.rb,
lib/bolt/inventory/group.rb
Defined Under Namespace
Classes: Group, 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.
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/bolt/inventory.rb', line 60
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
.from_config(config) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/bolt/inventory.rb', line 44
def self.from_config(config)
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 = new(data, config)
inventory.validate
inventory
end
|
Instance Method Details
#add_facts(target, new_facts = {}) ⇒ Object
124
125
126
127
|
# File 'lib/bolt/inventory.rb', line 124
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
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/bolt/inventory.rb', line 102
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
79
80
81
82
|
# File 'lib/bolt/inventory.rb', line 79
def collect_groups
@group_lookup = @groups.collect_groups
end
|
#data_hash ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/bolt/inventory.rb', line 146
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
129
130
131
|
# File 'lib/bolt/inventory.rb', line 129
def facts(target)
@target_facts[target.name] || {}
end
|
#features(target) ⇒ Object
142
143
144
|
# File 'lib/bolt/inventory.rb', line 142
def features(target)
@target_features[target.name] || Set.new
end
|
#get_targets(targets) ⇒ Object
92
93
94
95
96
97
98
99
100
|
# File 'lib/bolt/inventory.rb', line 92
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
84
85
86
|
# File 'lib/bolt/inventory.rb', line 84
def group_names
@group_lookup.keys
end
|
#node_names ⇒ Object
88
89
90
|
# File 'lib/bolt/inventory.rb', line 88
def node_names
@groups.node_names
end
|
#set_feature(target, feature, value = true) ⇒ Object
133
134
135
136
137
138
139
140
|
# File 'lib/bolt/inventory.rb', line 133
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
115
116
117
118
|
# File 'lib/bolt/inventory.rb', line 115
def set_var(target, key, value)
data = { key => value }
set_vars_from_hash(target.name, data)
end
|
#validate ⇒ Object
75
76
77
|
# File 'lib/bolt/inventory.rb', line 75
def validate
@groups.validate
end
|
#vars(target) ⇒ Object
120
121
122
|
# File 'lib/bolt/inventory.rb', line 120
def vars(target)
@target_vars[target.name] || {}
end
|