Module: Beaker::TaskHelper::Inventory

Defined in:
lib/beaker-task_helper/inventory.rb

Instance Method Summary collapse

Instance Method Details

#add_node(node, group_name, groups) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/beaker-task_helper/inventory.rb', line 11

def add_node(node, group_name, groups)
  if group_name =~ %r{\A[a-z0-9_]+\Z}
    group = groups.find { |g| g['name'] == group_name }
    unless group
      group = { 'name' => group_name, 'nodes' => [] }
      groups << group
    end
    group['nodes'] << node
  else
    puts "invalid group name #{group_name} skipping"
  end
end

#hosts_to_inventoryObject

This attempts to make a bolt inventory hash from beakers hosts roles should be targetable by bolt as groups



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
72
73
74
75
76
# File 'lib/beaker-task_helper/inventory.rb', line 8

def hosts_to_inventory
  groups = []

  def add_node(node, group_name, groups)
    if group_name =~ %r{\A[a-z0-9_]+\Z}
      group = groups.find { |g| g['name'] == group_name }
      unless group
        group = { 'name' => group_name, 'nodes' => [] }
        groups << group
      end
      group['nodes'] << node
    else
      puts "invalid group name #{group_name} skipping"
    end
  end

  nodes = hosts.map do |host|
    # Make sure nodes with IPs have unique target names
    node_name = host[:ip] ? "#{host[:ip]}?n=#{host.hostname}" : host.hostname

    if host[:platform] =~ %r{windows}
      config = { 'transport' => 'winrm',
                 'winrm' => { 'ssl' => false,
                              'user' => host[:user],
                              'password' => ENV['BEAKER_password'] } }
    else
      config = { 'transport' => 'ssh',
                 'ssh' => { 'host-key-check' => false } }
      %i[password user port].each do |k|
        config['ssh'][k.to_s] = host[:ssh][k] if host[:ssh][k]
      end

      case host[:hypervisor]
      when 'docker'
        nil
      when 'vagrant'
        key = nil
        keys = host.connection.instance_variable_get(:@ssh).options[:keys]
        key = keys.first if keys
        config['ssh']['private-key'] = key if key
      when 'vmpooler', 'abs'
        key = nil
        keys = host[:ssh][:keys]
        key = keys.first if keys
        config['ssh']['private-key'] = key if key
      else
        raise "Can't generate inventory for platform #{host[:platform]} hypervisor #{host[:hypervisor]}"
      end
    end

    # make alias groups for each role
    host[:roles].each do |role|
      add_node(node_name, role, groups)
    end

    {
      'name' => node_name,
      'config' => config
    }
  end

  { 'nodes' => nodes,
    'groups' => groups,
    'config' => {
      'ssh' => {
        'host-key-check' => false
      }
    } }
end