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



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

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



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

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

  create_version({}, config.transport, config.transports, plugins)
end

.from_config(config, plugins) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bolt/inventory.rb', line 48

def self.from_config(config, plugins)
  logger = Logging.logger[self]

  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)
      logger.debug("Loaded inventory from environment variable #{ENVIRONMENT_VAR}")
    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
             i = Bolt::Util.read_optional_yaml_hash(config.default_inventoryfile, 'inventory')
             logger.debug("Loaded inventory from #{config.default_inventoryfile}") if i
             i
           end
    # This avoids rubocop complaining about identical conditionals
    logger.debug("Loaded inventory from #{config.inventoryfile}") if config.inventoryfile
  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