Class: Bolt::Inventory

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

Defined Under Namespace

Modules: Options Classes: Group, Inventory, Target, ValidationError, WildcardError

Constant Summary collapse

ENVIRONMENT_VAR =
'BOLT_INVENTORY'

Constants included from Options

Options::DEFINITIONS, Options::OPTIONS

Class Method Summary collapse

Class Method Details

.create_version(data, transport, transports, plugins, source = nil) ⇒ Object


102
103
104
105
106
107
108
109
110
111
# File 'lib/bolt/inventory.rb', line 102

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

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

.emptyObject


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

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

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

.from_config(config, plugins) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bolt/inventory.rb', line 66

def self.from_config(config, plugins)
  logger = Bolt::Logger.logger(self)

  if ENV.include?(ENVIRONMENT_VAR)
    begin
      source = ENVIRONMENT_VAR
      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
  elsif config.inventoryfile
    source = config.inventoryfile
    data = Bolt::Util.read_yaml_hash(config.inventoryfile, 'inventory')
    logger.debug("Loaded inventory from #{config.inventoryfile}")
  else
    source = config.default_inventoryfile
    data = Bolt::Util.read_optional_yaml_hash(config.default_inventoryfile, 'inventory')

    if config.default_inventoryfile.exist?
      logger.debug("Loaded inventory from #{config.default_inventoryfile}")
    else
      source = nil
      logger.debug("Tried to load inventory from #{config.default_inventoryfile}, but the file does not exist")
    end
  end

  Bolt::Validator.new.tap do |validator|
    validator.validate(data, schema, source)
    validator.warnings.each { |warning| Bolt::Logger.warn(warning[:id], warning[:msg]) }
  end

  create_version(data, config.transport, config.transports, plugins, source)
end

.schemaObject

Builds the schema used by the validator.


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

def self.schema
  schema = {
    type:        Hash,
    properties:  OPTIONS.map { |opt| [opt, _ref: opt] }.to_h,
    definitions: DEFINITIONS,
    _plugin:     true
  }

  schema[:definitions]['config'][:properties] = Bolt::Config.transport_definitions
  schema
end