-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
vagrant is still todo
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'yaml' | ||
require 'delegate' | ||
|
||
class InventoryHelper < SimpleDelegator | ||
Check failure on line 6 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
|
||
def initialize(location) | ||
@location = location | ||
super(refresh) | ||
end | ||
|
||
# Load inventory from location in YAML format | ||
# or generate a default structure | ||
# | ||
# @return [Hash] | ||
def refresh | ||
x = YAML.load_file(@location) if File.file?(@location) | ||
{ 'version' => 2, 'groups' => [] }.merge(x || {}) | ||
end | ||
|
||
# Save inventory to location in yaml format | ||
def save | ||
File.open(@location, 'wb+') { |f| f.write(self.to_yaml) } | ||
Check failure on line 23 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
|
||
end | ||
|
||
# Adds a node to a group specified, if group_name exists in inventory hash. | ||
# | ||
# @param node [Hash] node to add to the group | ||
# @param group [String] group of nodes to limit the search for the node_name in | ||
# @return [Hash] inventory_hash with node added to group if group_name exists in inventory hash. | ||
def add(node, group) | ||
# check if group exists | ||
if self['groups'].any? { |g| g['name'] == group } | ||
self['groups'].each do |g| | ||
g['targets'].push(node) if g['name'] == group | ||
end | ||
else | ||
# add new group | ||
self['groups'].push({ 'name' => group, 'targets' => [node] }) | ||
end | ||
|
||
self | ||
end | ||
|
||
# Lookup a node | ||
# | ||
# @param uri [String] uri of node to find | ||
# @param name [String] name of node to find | ||
# @param group [String] limit search to group | ||
# @return [Hash] inventory target | ||
def lookup(uri=nil, name: nil, group: nil) | ||
Check failure on line 51 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
Check failure on line 51 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)
|
||
value = uri || name | ||
key = uri.nil? ? 'name' : 'uri' | ||
|
||
self['groups'].each do |g| | ||
if (group && group == g['name']) || group.nil? | ||
Check failure on line 56 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
|
||
g['targets'].each do |t| | ||
return t if t[key] == value | ||
end | ||
end | ||
end | ||
|
||
raise "Failed to lookup target for #{key} #{value} in inventory" | ||
end | ||
|
||
# Remove node | ||
# | ||
# @param node [Hash] | ||
# @return [Hash] inventory_hash with node of node_name removed. | ||
def remove(node) | ||
self['groups'].map! do |g| | ||
g['targets'].select! { |target| target != node } | ||
Check failure on line 72 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
|
||
g | ||
end | ||
|
||
self | ||
end | ||
|
||
class << self | ||
attr_accessor :instances | ||
|
||
def open(location = nil) | ||
# Inventory location is an optional task parameter. | ||
location = location.nil? ? Dir.pwd : location | ||
location = if File.directory?(location) | ||
# DEPRECATED: puppet_litmus <= 1.3.0 support | ||
if Gem.loaded_specs['puppet_litmus'] && Gem.loaded_specs['puppet_litmus'].version <= Gem::Version.new('1.3.0') | ||
File.join(location, 'spec', 'fixtures', 'litmus_inventory.yaml') | ||
else | ||
File.join(location, 'inventory.yaml') | ||
end | ||
else | ||
location | ||
end | ||
|
||
@instances ||= Hash.new | ||
Check failure on line 96 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
|
||
@instances[location] = self.new(location) unless @instances.key? location | ||
Check failure on line 97 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
|
||
return @instances[location] | ||
Check failure on line 98 in lib/inventory_helper.rb GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)
|
||
end | ||
end | ||
|
||
protected | ||
|
||
attr_accessor :location | ||
end |