Class: Bolt::Inventory

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

Constructor Details

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

Returns a new instance of Inventory.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bolt/inventory.rb', line 55

def initialize(data, config = nil, target_vars: {}, target_facts: {}, target_features: {})
  @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
  collect_groups
end

Class Method Details

.from_config(config) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bolt/inventory.rb', line 39

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



104
105
106
107
# File 'lib/bolt/inventory.rb', line 104

def add_facts(target, new_facts = {})
  @logger.warn("No facts to add") if new_facts.empty?
  set_facts(target.name, new_facts)
end

#collect_groupsObject



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

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

#data_hashObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bolt/inventory.rb', line 126

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



109
110
111
# File 'lib/bolt/inventory.rb', line 109

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

#features(target) ⇒ Object



122
123
124
# File 'lib/bolt/inventory.rb', line 122

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

#get_targets(targets) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/bolt/inventory.rb', line 85

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



77
78
79
# File 'lib/bolt/inventory.rb', line 77

def group_names
  @group_lookup.keys
end

#node_namesObject



81
82
83
# File 'lib/bolt/inventory.rb', line 81

def node_names
  @groups.node_names
end

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



113
114
115
116
117
118
119
120
# File 'lib/bolt/inventory.rb', line 113

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



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

def set_var(target, key, value)
  data = { key => value }
  set_vars_from_hash(target.name, data)
end

#validateObject



68
69
70
# File 'lib/bolt/inventory.rb', line 68

def validate
  @groups.validate
end

#vars(target) ⇒ Object



100
101
102
# File 'lib/bolt/inventory.rb', line 100

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