summaryrefslogtreecommitdiffstats
path: root/apex/inventory/inventory.py
diff options
context:
space:
mode:
authorTim Rozet <trozet@redhat.com>2017-06-25 21:25:36 -0400
committerTim Rozet <trozet@redhat.com>2017-08-23 08:59:54 -0400
commitf4d388ea508ba00771e43a219ac64e0d430b73bd (patch)
tree4f61a89664474154c3d6f7adecfbb0396617199c /apex/inventory/inventory.py
parent807fad268c90649f2901c5f5c4cdeb788a0308e0 (diff)
Migrates Apex to Python
Removes all bash libraries and converts almost all of the code to a mixture of Python and Ansible. utils.sh and clean.sh still exist. clean.sh will be migrated fully to clean.py in another patch. The Apex Python package is now built into the opnfv-apex-common RPM. To install locally do 'pip3 install .'. To deploy: opnfv-deploy -d <file> -n <file> --image-dir /root/apex/.build -v --debug Non-python files (THT yaml, settings files, ansible playbooks) are all installed into /usr/share/opnfv-apex/. The RPM will copy settings files into /etc/opnfv-apex/. JIRA: APEX-317 Change-Id: I3232f0329bcd13bce5a28da6a8c9c84d0b048024 Signed-off-by: Tim Rozet <trozet@redhat.com>
Diffstat (limited to 'apex/inventory/inventory.py')
-rw-r--r--apex/inventory/inventory.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/apex/inventory/inventory.py b/apex/inventory/inventory.py
new file mode 100644
index 00000000..dd731a83
--- /dev/null
+++ b/apex/inventory/inventory.py
@@ -0,0 +1,89 @@
+##############################################################################
+# 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 json
+import platform
+
+import yaml
+
+from apex.common import constants
+from apex.common import utils
+
+
+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 identification 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 = {}
+ self.root_device = constants.DEFAULT_ROOT_DEV
+ if isinstance(source, str):
+ with open(source, 'r') as inventory_file:
+ yaml_dict = yaml.safe_load(inventory_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']]
+ if 'cpus' in node:
+ node['cpu'] = node['cpus']
+
+ for i in ('ipmi_ip', 'ipmi_pass', 'ipmi_user', 'mac_address',
+ 'disk_device'):
+ if i == 'disk_device' and 'disk_device' in node.keys():
+ self.root_device = node[i]
+ else:
+ continue
+ del node[i]
+
+ return node
+
+ super().__init__({'nodes': list(map(munge_nodes, init_dict['nodes']))})
+
+ # verify number of nodes
+ if ha and len(self['nodes']) < 5 and not virtual:
+ 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')
+
+ if virtual:
+ self['arch'] = platform.machine()
+ 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