Class: Bolt::Inventory

Inherits:
Object
  • 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

Constructor Details

#initialize(data, config = nil, plugins: nil, target_vars: {}, target_facts: {}, target_features: {}, target_plugin_hooks: {}) ⇒ Inventory

Returns a new instance of Inventory.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bolt/inventory.rb', line 76

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 = 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
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



74
75
76
# File 'lib/bolt/inventory.rb', line 74

def config
  @config
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



74
75
76
# File 'lib/bolt/inventory.rb', line 74

def plugins
  @plugins
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, plugins: plugins)
  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



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/bolt/inventory.rb', line 196

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



150
151
152
153
154
155
156
# File 'lib/bolt/inventory.rb', line 150

def add_facts(target, new_facts = {})
  @logger.warn("No facts to add") if new_facts.empty?
  facts = set_facts(target.name, new_facts)
  # rubocop:disable Style/GlobalVars
  $future ? target : facts
  # rubocop:enable Style/GlobalVars
end

#add_to_group(targets, desired_group) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bolt/inventory.rb', line 129

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_groupsObject



102
103
104
105
# File 'lib/bolt/inventory.rb', line 102

def collect_groups
  # Provide a lookup map for finding a group by name
  @group_lookup = @groups.collect_groups
end

#data_hashObject



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/bolt/inventory.rb', line 175

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



158
159
160
# File 'lib/bolt/inventory.rb', line 158

def facts(target)
  @target_facts[target.name] || {}
end

#features(target) ⇒ Object



171
172
173
# File 'lib/bolt/inventory.rb', line 171

def features(target)
  @target_features[target.name] || Set.new
end

#get_targets(targets) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/bolt/inventory.rb', line 119

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_namesObject



107
108
109
# File 'lib/bolt/inventory.rb', line 107

def group_names
  @group_lookup.keys
end

#node_namesObject



111
112
113
# File 'lib/bolt/inventory.rb', line 111

def node_names
  @groups.node_names
end

#plugin_hooks(target) ⇒ Object



115
116
117
# File 'lib/bolt/inventory.rb', line 115

def plugin_hooks(target)
  @target_plugin_hooks[target.name] || {}
end

#set_feature(target, feature, value = true) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/bolt/inventory.rb', line 162

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



142
143
144
# File 'lib/bolt/inventory.rb', line 142

def set_var(target, var_hash)
  set_vars_from_hash(target.name, var_hash)
end

#validateObject



94
95
96
# File 'lib/bolt/inventory.rb', line 94

def validate
  @groups.validate
end

#vars(target) ⇒ Object



146
147
148
# File 'lib/bolt/inventory.rb', line 146

def vars(target)
  @target_vars[target.name] || {}
end

#versionObject



98
99
100
# File 'lib/bolt/inventory.rb', line 98

def version
  1
end