Class: Bolt::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/inventory.rb,
lib/bolt/inventory/group.rb,
lib/bolt/inventory/target.rb,
lib/bolt/inventory/inventory.rb

Defined Under Namespace

Classes: Group, Inventory, Target, ValidationError, WildcardError

Constant Summary collapse

ENVIRONMENT_VAR =
'BOLT_INVENTORY'

Class Method Summary collapse

Class Method Details

.create_version(data, config, plugins) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/bolt/inventory.rb', line 68

def self.create_version(data, config, plugins)
  version = (data || {}).delete('version') { 2 }
  case version
  when 2
    Bolt::Inventory::Inventory.new(data, config, plugins: plugins)
  else
    raise ValidationError.new("Unsupported version #{version} specified in inventory", nil)
  end
end

.emptyObject



78
79
80
81
82
# File 'lib/bolt/inventory.rb', line 78

def self.empty
  config = Bolt::Config.default
  plugins = Bolt::Plugin.setup(config, nil, nil, Bolt::Analytics::NoopClient)
  create_version({}, config, plugins)
end

.from_config(config, plugins) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bolt/inventory.rb', line 47

def self.from_config(config, plugins)
  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 = if config.inventoryfile
             Bolt::Util.read_yaml_hash(config.inventoryfile, 'inventory')
           else
             Bolt::Util.read_optional_yaml_hash(config.default_inventoryfile, 'inventory')
           end
  end

  inventory = create_version(data, config, plugins)
  inventory.validate
  inventory
end