1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
15 from apex.common import constants
16 from apex.common import utils
19 class Inventory(dict):
21 This class parses an APEX inventory yaml file into an object. It
22 generates or detects all missing fields for deployment.
24 It then collapses one level of identification from the object to
25 convert it to a structure that can be dumped into a json file formatted
26 such that Triple-O can read the resulting json as an instackenv.json file.
28 def __init__(self, source, ha=True, virtual=False):
30 self.root_device = constants.DEFAULT_ROOT_DEV
31 if isinstance(source, str):
32 with open(source, 'r') as inventory_file:
33 yaml_dict = yaml.safe_load(inventory_file)
34 # collapse node identifiers from the structure
35 init_dict['nodes'] = list(map(lambda n: n[1],
36 yaml_dict['nodes'].items()))
38 # assume input is a dict to build from
43 def munge_nodes(node):
44 node['pm_addr'] = node['ipmi_ip']
45 node['pm_password'] = node['ipmi_pass']
46 node['pm_user'] = node['ipmi_user']
47 node['mac'] = [node['mac_address']]
49 node['cpu'] = node['cpus']
51 # aarch64 is always uefi
52 if 'arch' in node and node['arch'] == 'aarch64':
53 node['capabilities'] += ',boot_mode:uefi'
55 for i in ('ipmi_ip', 'ipmi_pass', 'ipmi_user', 'mac_address',
57 if i == 'disk_device' and 'disk_device' in node.keys():
58 self.root_device = node[i]
65 super().__init__({'nodes': list(map(munge_nodes, init_dict['nodes']))})
67 # verify number of nodes
68 if ha and len(self['nodes']) < 5 and not virtual:
69 raise InventoryException('You must provide at least 5 '
70 'nodes for HA baremetal deployment')
71 elif len(self['nodes']) < 2:
72 raise InventoryException('You must provide at least 2 nodes '
73 'for non-HA baremetal deployment')
76 self['host-ip'] = '192.168.122.1'
77 self['power_manager'] = \
78 'nova.virt.baremetal.virtual_power_driver.VirtualPowerManager'
80 self['ssh-key'] = 'INSERT_STACK_USER_PRIV_KEY'
81 self['ssh-user'] = 'root'
83 def dump_instackenv_json(self):
84 print(json.dumps(dict(self), sort_keys=True, indent=4))
87 class InventoryException(Exception):
88 def __init__(self, value):