From c7cbf47421382ef5db5ad8a2f470def52640b21f Mon Sep 17 00:00:00 2001
From: Tim Rozet <trozet@redhat.com>
Date: Thu, 12 Jan 2017 12:27:41 -0500
Subject: Updates ODL Pipeline scripts for CSIT

Changes Include:
 - Change to TripleOInspector which only introspect a
   current Apex deployment and dump yaml config to be bundled with
   snapshots.
 - Add TripleOHelper which consists of all tripleO helper functions.
   Many this are done to virsh so the idea is to have at.
   Some point in time a libvirtHelper, or use another libvirt
   python lib. Thatsway it is a class so that we can inherit
   later on.
 - New argument for passing the SSH private key to use to connect to
   nodes is added to the service utils.
 - Some general clean up and consolidation of logic

JIRA: APEX-363

Change-Id: I792db0fac3f4e81969fe85c05fc298fe5af02537
Signed-off-by: Tim Rozet <trozet@redhat.com>
---
 odl-pipeline/lib/utils/node_manager.py   | 10 ++++--
 odl-pipeline/lib/utils/processutils.py   |  2 +-
 odl-pipeline/lib/utils/service.py        |  6 ++++
 odl-pipeline/lib/utils/shutil.py         |  2 +-
 odl-pipeline/lib/utils/ssh_util.py       |  2 +-
 odl-pipeline/lib/utils/tripleo_helper.py | 53 ++++++++++++++++++++++++++++++++
 odl-pipeline/lib/utils/utils_log.py      |  6 +++-
 odl-pipeline/lib/utils/utils_yaml.py     |  4 +--
 8 files changed, 77 insertions(+), 8 deletions(-)
 create mode 100644 odl-pipeline/lib/utils/tripleo_helper.py

(limited to 'odl-pipeline/lib/utils')

diff --git a/odl-pipeline/lib/utils/node_manager.py b/odl-pipeline/lib/utils/node_manager.py
index d11065f..8a320ed 100755
--- a/odl-pipeline/lib/utils/node_manager.py
+++ b/odl-pipeline/lib/utils/node_manager.py
@@ -17,14 +17,15 @@ class NodeManager(object):
     primary_controller = None
 
     def __init__(self, config=None):
-        if config:
+        if config is not None:
             for (node_name, node_config) in config.iteritems():
                 self.add_node(node_name, node_config)
 
     def add_node(self, node_name, node_config):
         from node import Node
         if not node_config.get('address'):
-            node_config['address'] = self.get_address_of_node(node_name)
+            raise NodeManagerException("IP address missing from node_config:"
+                                       " {}".format(node_config))
         node = Node(node_name, dict=node_config)
         self.env_nodes.append(node)
         self.env_node_dict[node_name] = node
@@ -41,3 +42,8 @@ class NodeManager(object):
         if node not in cls.env_nodes:
             cls.env_nodes.append(node)
         SshUtil.gen_ssh_config(cls.env_nodes)
+
+
+class NodeManagerException(Exception):
+    def __init__(self, value):
+        self.value = value
diff --git a/odl-pipeline/lib/utils/processutils.py b/odl-pipeline/lib/utils/processutils.py
index b5aecb3..2abb88a 100755
--- a/odl-pipeline/lib/utils/processutils.py
+++ b/odl-pipeline/lib/utils/processutils.py
@@ -226,7 +226,7 @@ def execute(cmd, **kwargs):
                     stderr=sanitized_stderr,
                     cmd=(' '.join(cmd)) if isinstance(cmd, list) else cmd)
             (stdout, stderr) = result
-            return (stdout, (stderr, _returncode))
+            return stdout, (stderr, _returncode)
         except ProcessExecutionError:
             raise
         finally:
diff --git a/odl-pipeline/lib/utils/service.py b/odl-pipeline/lib/utils/service.py
index 39cdce5..cf46872 100755
--- a/odl-pipeline/lib/utils/service.py
+++ b/odl-pipeline/lib/utils/service.py
@@ -13,6 +13,7 @@ import argparse
 import traceback
 from utils_log import LOG, LOG_PATH
 from abc import abstractmethod
+from ssh_util import SSH_CONFIG
 
 
 class Service(object):
@@ -31,6 +32,8 @@ class Service(object):
         parser = self._create_cli_parser()
         sys_args = parser.parse_args()
         config = self.read_config(sys_args)
+        if sys_args.ssh_key_file:
+            SSH_CONFIG['ID_RSA_PATH'] = sys_args.ssh_key_file
         self.run(sys_args, config)
 
     @abstractmethod
@@ -49,6 +52,9 @@ class Service(object):
         #                     required=False)
         # parser.add_argument('--boolean', help="",
         #                     required=False, action='store_true')
+        parser.add_argument('--ssh-key-file',
+                            help="SSH private key file to use",
+                            required=False)
         return self.create_cli_parser(parser)
 
     def read_config(self, sys_args):
diff --git a/odl-pipeline/lib/utils/shutil.py b/odl-pipeline/lib/utils/shutil.py
index 40e2aba..5f6d482 100755
--- a/odl-pipeline/lib/utils/shutil.py
+++ b/odl-pipeline/lib/utils/shutil.py
@@ -17,7 +17,7 @@ class shutil():
     classdocs
     '''
     @staticmethod
-    def mkdir_if_not_exsist(path):
+    def mkdir_if_not_exist(path):
         if not path:
             raise Exception('Path should not be empty.')
         putils.execute(["mkdir", "-p", path])
diff --git a/odl-pipeline/lib/utils/ssh_util.py b/odl-pipeline/lib/utils/ssh_util.py
index e70aed3..635a718 100755
--- a/odl-pipeline/lib/utils/ssh_util.py
+++ b/odl-pipeline/lib/utils/ssh_util.py
@@ -33,4 +33,4 @@ class SshUtil(object):
 
     @staticmethod
     def get_id_rsa():
-        return (SSH_CONFIG['ID_RSA_PATH'])
+        return SSH_CONFIG['ID_RSA_PATH']
diff --git a/odl-pipeline/lib/utils/tripleo_helper.py b/odl-pipeline/lib/utils/tripleo_helper.py
new file mode 100644
index 0000000..702e811
--- /dev/null
+++ b/odl-pipeline/lib/utils/tripleo_helper.py
@@ -0,0 +1,53 @@
+import re
+import processutils
+from processutils import execute
+from utils.node import Node
+
+
+class TripleoHelper():
+
+    @staticmethod
+    def find_overcloud_ips():
+        try:
+            res, _ = TripleoHelper.get_undercloud().execute(
+                "'source /home/stack/stackrc; nova list'",
+                shell=True)
+        except processutils.ProcessExecutionError as e:
+            raise TripleOHelperException(
+                "Error unable to issue nova list "
+                "on undercloud.  Please verify "
+                "undercloud is up.  Full error: {"
+                "}".format(e.message))
+        return re.findall('ctlplane=([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', res)
+
+    @staticmethod
+    def get_virtual_node_name_from_mac(mac):
+        vnode_names, _ = execute('virsh list|awk \'{print '
+                                 '$2}\'', shell=True)
+        for node in vnode_names.split('\n'):
+            if 'baremetal' in node:
+                admin_net_mac, _ = execute(
+                    'virsh domiflist %s |grep admin |awk \'{print $5}\''
+                    % node, shell=True)
+                if admin_net_mac.replace('\n', '') == mac:
+                    return node
+        raise Exception('Could not find corresponding virtual node for MAC: %s'
+                        % mac)
+
+    @staticmethod
+    def get_undercloud_ip():
+        out, _ = execute('virsh domifaddr undercloud', shell=True)
+        return re.findall('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', out)[0]
+
+    @staticmethod
+    def get_undercloud():
+        return Node('undercloud', address=TripleoHelper.get_undercloud_ip(),
+                    user='stack')
+
+
+class TripleOHelperException(Exception):
+    def __init__(self, value):
+        self.value = value
+
+    def __str__(self):
+        return self.value
diff --git a/odl-pipeline/lib/utils/utils_log.py b/odl-pipeline/lib/utils/utils_log.py
index e49434c..9d7648f 100755
--- a/odl-pipeline/lib/utils/utils_log.py
+++ b/odl-pipeline/lib/utils/utils_log.py
@@ -11,6 +11,7 @@ import logging
 import datetime
 import os
 import sys
+import types
 
 LOG = logging.getLogger(__name__)
 LOG_LEVEL = logging.DEBUG
@@ -35,7 +36,10 @@ def log_enter_exit(func):
                    'args': args,
                    'kwargs': kwargs})
         start = datetime.datetime.now()
-        ret = func(self, *args, **kwargs)
+        if isinstance(func, types.FunctionType):
+            ret = func(*args, **kwargs)
+        else:
+            ret = func(self, *args, **kwargs)
         end = datetime.datetime.now()
         LOG.debug(("Exiting %(cls)s.%(method)s. "
                    "Spent %(duration)s sec. "
diff --git a/odl-pipeline/lib/utils/utils_yaml.py b/odl-pipeline/lib/utils/utils_yaml.py
index f9513b8..b9357f6 100755
--- a/odl-pipeline/lib/utils/utils_yaml.py
+++ b/odl-pipeline/lib/utils/utils_yaml.py
@@ -12,9 +12,9 @@ import yaml
 
 def write_dict_to_yaml(config, path):
     with open(path, 'w+') as f:
-        yaml.dump(config, f, default_flow_style=False)
+        yaml.safe_dump(config, f, default_flow_style=False)
 
 
 def read_dict_from_yaml(path):
     with open(path, 'r') as f:
-        return yaml.load(f)
+        return yaml.safe_load(f)
-- 
cgit