Adding conditionals dependent on arch
[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 import platform
13
14 from .common import constants
15 from .common import utils
16
17
18 class Inventory(dict):
19     """
20     This class parses an APEX inventory yaml file into an object. It
21     generates or detects all missing fields for deployment.
22
23     It then collapses one level of identification from the object to
24     convert it to a structure that can be dumped into a json file formatted
25     such that Triple-O can read the resulting json as an instackenv.json file.
26     """
27     def __init__(self, source, ha=True, virtual=False):
28         init_dict = {}
29         self.root_device = constants.DEFAULT_ROOT_DEV
30         if isinstance(source, str):
31             with open(source, 'r') as inventory_file:
32                 yaml_dict = yaml.safe_load(inventory_file)
33             # collapse node identifiers from the structure
34             init_dict['nodes'] = list(map(lambda n: n[1],
35                                           yaml_dict['nodes'].items()))
36         else:
37             # assume input is a dict to build from
38             init_dict = source
39
40         # move ipmi_* to pm_*
41         # make mac a list
42         def munge_nodes(node):
43             node['pm_addr'] = node['ipmi_ip']
44             node['pm_password'] = node['ipmi_pass']
45             node['pm_user'] = node['ipmi_user']
46             node['mac'] = [node['mac_address']]
47             if 'cpus' in node:
48                 node['cpu'] = node['cpus']
49
50             for i in ('ipmi_ip', 'ipmi_pass', 'ipmi_user', 'mac_address',
51                       'disk_device'):
52                 if i == 'disk_device' and 'disk_device' in node.keys():
53                     self.root_device = node[i]
54                 else:
55                     continue
56                 del node[i]
57
58             return node
59
60         super().__init__({'nodes': list(map(munge_nodes, init_dict['nodes']))})
61
62         # verify number of nodes
63         if ha and len(self['nodes']) < 5:
64             raise InventoryException('You must provide at least 5 '
65                                      'nodes for HA baremetal deployment')
66         elif len(self['nodes']) < 2:
67             raise InventoryException('You must provide at least 2 nodes '
68                                      'for non-HA baremetal deployment')
69
70         if virtual:
71             self['arch'] = platform.machine()
72             self['host-ip'] = '192.168.122.1'
73             self['power_manager'] = \
74                 'nova.virt.baremetal.virtual_power_driver.VirtualPowerManager'
75             self['seed-ip'] = ''
76             self['ssh-key'] = 'INSERT_STACK_USER_PRIV_KEY'
77             self['ssh-user'] = 'root'
78
79     def dump_instackenv_json(self):
80         print(json.dumps(dict(self), sort_keys=True, indent=4))
81
82     def dump_bash(self, path=None):
83         """
84         Prints settings for bash consumption.
85
86         If optional path is provided, bash string will be written to the file
87         instead of stdout.
88         """
89         bash_str = "{}={}\n".format('root_disk_list', str(self.root_device))
90         utils.write_str(bash_str, path)
91
92
93 class InventoryException(Exception):
94     def __init__(self, value):
95         self.value = value
96
97     def __str__(self):
98             return self.value