diff options
author | Michael Chapman <michapma@redhat.com> | 2016-09-08 12:44:39 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@172.30.200.206> | 2016-09-08 12:44:40 +0000 |
commit | dba76d80546f04dc22ddbf4dc70d4cc7206716f0 (patch) | |
tree | deb40a25d7e10bdca4e7b1f3962639bd5725047f /lib/python/apex/inventory.py | |
parent | a7accc442ec95252077d716b229d84ef86cb23ce (diff) | |
parent | 51510a96718c909cbcbf02256c440dbdceab7227 (diff) |
Merge "moving inventory file parsing to python"
Diffstat (limited to 'lib/python/apex/inventory.py')
-rw-r--r-- | lib/python/apex/inventory.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/python/apex/inventory.py b/lib/python/apex/inventory.py new file mode 100644 index 00000000..f4a33b28 --- /dev/null +++ b/lib/python/apex/inventory.py @@ -0,0 +1,76 @@ +############################################################################## +# Copyright (c) 2016 Dan Radez (dradez@redhat.com) and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import yaml +import json + + +class Inventory(dict): + """ + This class parses an APEX inventory yaml file into an object. It + generates or detects all missing fields for deployment. + + It then collapses one level of identifcation from the object to + convert it to a structure that can be dumped into a json file formatted + such that Triple-O can read the resulting json as an instackenv.json file. + """ + def __init__(self, source, ha=True, virtual=False): + init_dict = {} + if type(source) is str: + with open(source, 'r') as network_settings_file: + yaml_dict = yaml.load(network_settings_file) + # collapse node identifiers from the structure + init_dict['nodes'] = list(map(lambda n: n[1], + yaml_dict['nodes'].items())) + else: + # assume input is a dict to build from + init_dict = source + + # move ipmi_* to pm_* + # make mac a list + def munge_nodes(node): + node['pm_addr'] = node['ipmi_ip'] + node['pm_password'] = node['ipmi_pass'] + node['pm_user'] = node['ipmi_user'] + node['mac'] = [node['mac_address']] + + for i in ('ipmi_ip', 'ipmi_pass', 'ipmi_user', 'mac_address'): + del i + + return node + + super().__init__({'nodes': list(map(munge_nodes, init_dict['nodes']))}) + + # verify number of nodes + if ha and len(self['nodes']) < 5: + raise InventoryException('You must provide at least 5 ' + 'nodes for HA baremetal deployment') + elif len(self['nodes']) < 2: + raise InventoryException('You must provide at least 2 nodes ' + 'for non-HA baremetal deployment${reset}') + + if virtual: + self['arch'] = 'x86_64' + self['host-ip'] = '192.168.122.1' + self['power_manager'] = \ + 'nova.virt.baremetal.virtual_power_driver.VirtualPowerManager' + self['seed-ip'] = '' + self['ssh-key'] = 'INSERT_STACK_USER_PRIV_KEY' + self['ssh-user'] = 'root' + + def dump_instackenv_json(self): + print(json.dumps(dict(self), sort_keys=True, indent=4)) + + +class InventoryException(Exception): + def __init__(self, value): + self.value = value + + def __str__(self): + return self.value |