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, transport, transports, plugins) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/bolt/inventory.rb', line 73

def self.create_version(data, transport, transports, plugins)
  version = (data || {}).delete('version') { 2 }

  case version
  when 2
    Bolt::Inventory::Inventory.new(data, transport, transports, plugins)
  else
    raise ValidationError.new("Unsupported version #{version} specified in inventory", nil)
  end
end

.emptyObject



84
85
86
87
88
89
# File 'lib/bolt/inventory.rb', line 84

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

  create_version({}, config.transport, config.transports, 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
67
68
69
70
71
# 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

  # Resolve plugin references from transport config
  config.transports.each_value do |t|
    t.resolve(plugins) unless t.resolved?
  end

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