37143566413dddf5e4bc5d443a6758ebbe802aa6
[apex.git] / lib / python / apex / inventory.py
1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) and others.
3 #
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 ##############################################################################
9
10 import yaml
11 import json
12
13
14 class Inventory(dict):
15     """
16     This class parses an APEX inventory yaml file into an object. It
17     generates or detects all missing fields for deployment.
18
19     It then collapses one level of identifcation from the object to
20     convert it to a structure that can be dumped into a json file formatted
21     such that Triple-O can read the resulting json as an instackenv.json file.
22     """
23     def __init__(self, source, ha=True, virtual=False):
24         init_dict = {}
25         if type(source) is str:
26             with open(source, 'r') as inventory_file:
27                 yaml_dict = yaml.safe_load(inventory_file)
28             # collapse node identifiers from the structure
29             init_dict['nodes'] = list(map(lambda n: n[1],
30                                           yaml_dict['nodes'].items()))
31         else:
32             # assume input is a dict to build from
33             init_dict = source
34
35         # move ipmi_* to pm_*
36         # make mac a list
37         def munge_nodes(node):
38             node['pm_addr'] = node['ipmi_ip']
39             node['pm_password'] = node['ipmi_pass']
40             node['pm_user'] = node['ipmi_user']
41             node['mac'] = [node['mac_address']]
42
43             for i in ('ipmi_ip', 'ipmi_pass', 'ipmi_user', 'mac_address'):
44                 del i
45
46             return node
47
48         super().__init__({'nodes': list(map(munge_nodes, init_dict['nodes']))})
49
50         # verify number of nodes
51         if ha and len(self['nodes']) < 5:
52             raise InventoryException('You must provide at least 5 '
53                                      'nodes for HA baremetal deployment')
54         elif len(self['nodes']) < 2:
55             raise InventoryException('You must provide at least 2 nodes '
56                                      'for non-HA baremetal deployment${reset}')
57
58         if virtual:
59             self['arch'] = 'x86_64'
60             self['host-ip'] = '192.168.122.1'
61             self['power_manager'] = \
62                 'nova.virt.baremetal.virtual_power_driver.VirtualPowerManager'
63             self['seed-ip'] = ''
64             self['ssh-key'] = 'INSERT_STACK_USER_PRIV_KEY'
65             self['ssh-user'] = 'root'
66
67     def dump_instackenv_json(self):
68         print(json.dumps(dict(self), sort_keys=True, indent=4))
69
70
71 class InventoryException(Exception):
72     def __init__(self, value):
73         self.value = value
74
75     def __str__(self):
76             return self.value