Module: PuppetLitmus::InventoryManipulation

Included in:
PuppetLitmus, PuppetLitmus
Defined in:
lib/puppet_litmus/inventory_manipulation.rb

Overview

helper functions for manipulating and reading a bolt inventory file

Instance Method Summary collapse

Instance Method Details

#add_feature_to_group(inventory_hash, feature_name, group_name) ⇒ Object

Adds a feature to the group specified/

group_name [String] group of nodes to limit the search for the group_name in

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • feature_name (String)

    feature to locate in the group

Returns:

  • inventory.yaml file with feature added to group.

  • (Hash)

    inventory_hash with feature added to group if group_name exists in inventory hash.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 172

def add_feature_to_group(inventory_hash, feature_name, group_name)
  i = 0
  inventory_hash['groups'].each do |group|
    if group['name'] == group_name
      if group['features'].nil? == true
        group = group.merge('features' => [])
      end
      group['features'].push feature_name unless group['features'].include?(feature_name)
      inventory_hash['groups'][i] = group
    end
    i += 1
  end
  inventory_hash
end

#add_feature_to_node(inventory_hash, feature_name, node_name) ⇒ Object

Adds a feature to the node specified/

node_name [String] node of nodes to limit the search for the node_name in

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • feature_name (String)

    feature to locate in the node

Returns:

  • inventory.yaml file with feature added to node.

  • (Hash)

    inventory_hash with feature added to node if node_name exists in inventory hash.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 210

def add_feature_to_node(inventory_hash, feature_name, node_name)
  group_index = 0
  inventory_hash['groups'].each do |group|
    node_index = 0
    group['targets'].each do |node|
      if node['uri'] == node_name
        if node['features'].nil? == true
          node = node.merge('features' => [])
        end
        node['features'].push feature_name unless node['features'].include?(feature_name)
        inventory_hash['groups'][group_index]['targets'][node_index] = node
      end
      node_index += 1
    end
    group_index += 1
  end
  inventory_hash
end

#add_node_to_group(inventory_hash, node, group_name) ⇒ Hash

Adds a node to a group specified, if group_name exists in inventory hash.

group_name [String] group of nodes to limit the search for the node_name in

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • node (Hash)

    node to add to the group

Returns:

  • (Hash)

    inventory_hash with node added to group if group_name exists in inventory hash.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 137

def add_node_to_group(inventory_hash, node, group_name)
  # check if group exists
  if inventory_hash['groups'].any? { |g| g['name'] == group_name }
    inventory_hash['groups'].each do |group|
      if group['name'] == group_name
        group['targets'].push node
      end
    end
  else
    # add new group
    group = { 'name' => group_name, 'targets' => [node] }
    inventory_hash['groups'].push group
  end
  inventory_hash
end

#add_platform_field(inventory_hash, node_name) ⇒ Object

Add the ‘litmus.platform` with platform information for the target

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • node_name (String)

    node of nodes to limit the search for the node_name in



260
261
262
263
264
265
266
267
268
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 260

def add_platform_field(inventory_hash, node_name)
  facts = begin
    facts_from_node(inventory_hash, node_name)
  rescue StandardError => e
    warn e
    {}
  end
  Honeycomb.current_span.add_field('litmus.platform', facts&.dig('platform'))
end

#config_from_node(inventory_hash, node_name) ⇒ Hash

Finds a config hash in the inventory hash by searching for a node name.

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • node_name (String)

    node to locate in the group

Returns:

  • (Hash)

    config for node of name node_name



88
89
90
91
92
93
94
95
96
97
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 88

def config_from_node(inventory_hash, node_name)
  inventory_hash['groups'].each do |group|
    group['targets'].each do |node|
      if node['uri'] == node_name
        return node['config']
      end
    end
  end
  raise "No config was found for #{node_name}"
end

#facts_from_node(inventory_hash, node_name) ⇒ Hash

Finds a facts hash in the inventory hash by searching for a node name.

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • node_name (String)

    node to locate in the group

Returns:

  • (Hash)

    facts for node of name node_name



104
105
106
107
108
109
110
111
112
113
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 104

def facts_from_node(inventory_hash, node_name)
  inventory_hash['groups'].each do |group|
    group['targets'].each do |node|
      if node['uri'] == node_name
        return node['facts']
      end
    end
  end
  raise "No facts were found for #{node_name}"
end

#find_targets(inventory_hash, targets) ⇒ Array

Finds targets to perform operations on from an inventory hash.

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • targets (Array)

Returns:

  • (Array)

    array of targets.



48
49
50
51
52
53
54
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 48

def find_targets(inventory_hash, targets)
  if targets.nil?
    inventory_hash.to_s.scan(%r{uri"=>"(\S*)"}).flatten
  else
    [targets]
  end
end

#inventory_hash_from_inventory_file(inventory_full_path = nil) ⇒ Hash

Creates an inventory hash from the inventory.yaml.

Parameters:

  • inventory_full_path (String) (defaults to: nil)

    path to the inventory.yaml file

Returns:

  • (Hash)

    hash of the inventory.yaml file.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 11

def inventory_hash_from_inventory_file(inventory_full_path = nil)
  require 'yaml'
  inventory_full_path = if inventory_full_path.nil?
                          "#{Dir.pwd}/spec/fixtures/litmus_inventory.yaml"
                        else
                          inventory_full_path
                        end
  raise "There is no inventory file at '#{inventory_full_path}'." unless File.exist?(inventory_full_path)

  YAML.load_file(inventory_full_path)
end

#localhost_inventory_hashHash

Provide a default hash for executing against localhost

Returns:

  • (Hash)

    inventory.yaml hash containing only an entry for localhost



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 26

def localhost_inventory_hash
  {
    'groups' => [
      {
        'name' => 'local',
        'targets' => [
          {
            'uri' => 'litmus_localhost',
            'config' => { 'transport' => 'local' },
            'feature' => 'puppet-agent',
          },
        ],
      },
    ],
  }
end

#remove_feature_from_group(inventory_hash, feature_name, group_name) ⇒ Object

Removes a feature from the group specified/

group_name [String] group of nodes to limit the search for the group_name in

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • feature_name (String)

    feature to locate in the group

Returns:

  • inventory.yaml file with feature removed from the group.

  • (Hash)

    inventory_hash with feature added to group if group_name exists in inventory hash.



194
195
196
197
198
199
200
201
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 194

def remove_feature_from_group(inventory_hash, feature_name, group_name)
  inventory_hash['groups'].each do |group|
    if group['name'] == group_name && group['features'].nil? != true
      group['features'].delete(feature_name)
    end
  end
  inventory_hash
end

#remove_feature_from_node(inventory_hash, feature_name, node_name) ⇒ Object

Removes a feature from the node specified/

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • feature_name (String)

    feature to locate in the node

  • node_name (String)

    node of nodes to limit the search for the node_name in

Returns:

  • inventory.yaml file with feature removed from the node.

  • (Hash)

    inventory_hash with feature added to node if node_name exists in inventory hash.



236
237
238
239
240
241
242
243
244
245
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 236

def remove_feature_from_node(inventory_hash, feature_name, node_name)
  inventory_hash['groups'].each do |group|
    group['targets'].each do |node|
      if node['uri'] == node_name && node['features'].nil? != true
        node['features'].delete(feature_name)
      end
    end
  end
  inventory_hash
end

#remove_node(inventory_hash, node_name) ⇒ Hash

Removes named node from a group inside an inventory_hash.

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • node_name (String)

    node to locate in the group

Returns:

  • (Hash)

    inventory_hash with node of node_name removed.



158
159
160
161
162
163
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 158

def remove_node(inventory_hash, node_name)
  inventory_hash['groups'].each do |group|
    group['targets'].delete_if { |i| i['uri'] == node_name }
  end
  inventory_hash
end

#target_in_group(inventory_hash, node_name, group_name) ⇒ Boolean

Determines if a node_name exists in a group in the inventory_hash.

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • node_name (String)

    node to locate in the group

  • group_name (String)

    group of nodes to limit the search for the node_name in

Returns:

  • (Boolean)

    true if node_name exists in group_name.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 62

def target_in_group(inventory_hash, node_name, group_name)
  exists = false
  inventory_hash['groups'].each do |group|
    next unless group['name'] == group_name

    group['targets'].each do |node|
      exists = true if node['uri'] == node_name
    end
  end
  exists
end

#target_in_inventory?(inventory_hash, node_name) ⇒ Boolean

Determines if a node_name exists in the inventory_hash.

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • node_name (String)

    node to locate in the group

Returns:

  • (Boolean)

    true if node_name exists in the inventory_hash.



79
80
81
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 79

def target_in_inventory?(inventory_hash, node_name)
  find_targets(inventory_hash, nil).include?(node_name)
end

#vars_from_node(inventory_hash, node_name) ⇒ Hash

Finds a var hash in the inventory hash by searching for a node name.

Parameters:

  • inventory_hash (Hash)

    hash of the inventory.yaml file

  • node_name (String)

    node to locate in the group

Returns:

  • (Hash)

    vars for node of name node_name



120
121
122
123
124
125
126
127
128
129
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 120

def vars_from_node(inventory_hash, node_name)
  inventory_hash['groups'].each do |group|
    group['targets'].each do |node|
      if node['uri'] == node_name
        return node['vars']
      end
    end
  end
  {}
end

#write_to_inventory_file(inventory_hash, inventory_full_path) ⇒ Object

Write inventory_hash to inventory_yaml file/

@param inventory_full_path [String] path to the inventory.yaml file
@param inventory_hash [Hash] hash of the inventory.yaml file
@return inventory.yaml file with feature added to group.


252
253
254
# File 'lib/puppet_litmus/inventory_manipulation.rb', line 252

def write_to_inventory_file(inventory_hash, inventory_full_path)
  File.open(inventory_full_path, 'wb+') { |f| f.write(inventory_hash.to_yaml) }
end