From 03d40b81352f1f9de985f2cf598ac6c18be13b69 Mon Sep 17 00:00:00 2001 From: "jose.lausuch" Date: Wed, 16 Nov 2016 10:54:23 +0100 Subject: Move installer adapters to the new modules directory JIRA: RELENG-149 JIRA: RELENG-161 Now that we have a modules directory in place, let's move all the shared python code to this location. We need to create a setup.py to install these modules in the system so that we don't have to modify the pythonpath. Change-Id: I7e608ce6e1e6b3d111405bc789cdb2ff7fcb49c3 Signed-off-by: jose.lausuch --- modules/README.rst | 12 ++ modules/Readme.txt | 12 -- modules/__init.py__ | 0 modules/installer_adapters/__init.py__ | 0 modules/opnfv/__init__.py | 0 .../opnfv/installer_adapters/InstallerHandler.py | 78 +++++++ modules/opnfv/installer_adapters/__init__.py | 0 .../opnfv/installer_adapters/apex/ApexAdapter.py | 32 +++ modules/opnfv/installer_adapters/apex/__init__.py | 0 .../installer_adapters/compass/CompassAdapter.py | 32 +++ .../opnfv/installer_adapters/compass/__init__.py | 0 .../opnfv/installer_adapters/fuel/FuelAdapter.py | 236 +++++++++++++++++++++ modules/opnfv/installer_adapters/fuel/__init__.py | 0 modules/opnfv/installer_adapters/fuel/example.py | 22 ++ .../opnfv/installer_adapters/joid/JoidAdapter.py | 32 +++ modules/opnfv/installer_adapters/joid/__init__.py | 0 modules/opnfv/utils/OPNFVLogger.py | 51 +++++ modules/opnfv/utils/SSHUtils.py | 121 +++++++++++ modules/opnfv/utils/__init__.py | 0 modules/utils/__init.py__ | 0 utils/installer-adapter/ApexAdapter.py | 32 --- utils/installer-adapter/CompassAdapter.py | 32 --- utils/installer-adapter/FuelAdapter.py | 236 --------------------- utils/installer-adapter/InstallerHandler.py | 78 ------- utils/installer-adapter/JoidAdapter.py | 32 --- utils/installer-adapter/RelengLogger.py | 51 ----- utils/installer-adapter/SSHUtils.py | 120 ----------- utils/installer-adapter/__init__.py | 0 utils/installer-adapter/example.py | 22 -- 29 files changed, 616 insertions(+), 615 deletions(-) create mode 100644 modules/README.rst delete mode 100644 modules/Readme.txt delete mode 100644 modules/__init.py__ delete mode 100644 modules/installer_adapters/__init.py__ create mode 100644 modules/opnfv/__init__.py create mode 100644 modules/opnfv/installer_adapters/InstallerHandler.py create mode 100644 modules/opnfv/installer_adapters/__init__.py create mode 100644 modules/opnfv/installer_adapters/apex/ApexAdapter.py create mode 100644 modules/opnfv/installer_adapters/apex/__init__.py create mode 100644 modules/opnfv/installer_adapters/compass/CompassAdapter.py create mode 100644 modules/opnfv/installer_adapters/compass/__init__.py create mode 100644 modules/opnfv/installer_adapters/fuel/FuelAdapter.py create mode 100644 modules/opnfv/installer_adapters/fuel/__init__.py create mode 100644 modules/opnfv/installer_adapters/fuel/example.py create mode 100644 modules/opnfv/installer_adapters/joid/JoidAdapter.py create mode 100644 modules/opnfv/installer_adapters/joid/__init__.py create mode 100644 modules/opnfv/utils/OPNFVLogger.py create mode 100644 modules/opnfv/utils/SSHUtils.py create mode 100644 modules/opnfv/utils/__init__.py delete mode 100644 modules/utils/__init.py__ delete mode 100644 utils/installer-adapter/ApexAdapter.py delete mode 100644 utils/installer-adapter/CompassAdapter.py delete mode 100644 utils/installer-adapter/FuelAdapter.py delete mode 100644 utils/installer-adapter/InstallerHandler.py delete mode 100644 utils/installer-adapter/JoidAdapter.py delete mode 100644 utils/installer-adapter/RelengLogger.py delete mode 100644 utils/installer-adapter/SSHUtils.py delete mode 100644 utils/installer-adapter/__init__.py delete mode 100644 utils/installer-adapter/example.py diff --git a/modules/README.rst b/modules/README.rst new file mode 100644 index 000000000..7c91eca5a --- /dev/null +++ b/modules/README.rst @@ -0,0 +1,12 @@ + +This directory may be used to add new tools that might be useful for any +project in OPNFV. This tools must be python based and shall be imported +as follows: + + from opnfv.utils import SSHUtils + from opnfv.utils import RelengLogger + ... + +For further information about how to use this modules directory, contact: + fatih.degirmenci@ericsson.com + jose.lausuch@ericsson.com diff --git a/modules/Readme.txt b/modules/Readme.txt deleted file mode 100644 index 32becc21e..000000000 --- a/modules/Readme.txt +++ /dev/null @@ -1,12 +0,0 @@ - -This directory may be used to add new tools that might be useful for any -project in OPNFV. This tools must be python based and shall be imported -as follows: - - from releng.modules.utils import SSHUtils - from releng.modules.utils import RelengLogger - ... - -For further information about how to use this modules directory, contact: - fatih.degirmenci@ericsson.com - jose.lausuch@ericsson.com diff --git a/modules/__init.py__ b/modules/__init.py__ deleted file mode 100644 index e69de29bb..000000000 diff --git a/modules/installer_adapters/__init.py__ b/modules/installer_adapters/__init.py__ deleted file mode 100644 index e69de29bb..000000000 diff --git a/modules/opnfv/__init__.py b/modules/opnfv/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/modules/opnfv/installer_adapters/InstallerHandler.py b/modules/opnfv/installer_adapters/InstallerHandler.py new file mode 100644 index 000000000..b81b806ca --- /dev/null +++ b/modules/opnfv/installer_adapters/InstallerHandler.py @@ -0,0 +1,78 @@ +############################################################################## +# Copyright (c) 2015 Ericsson AB and others. +# Author: Jose Lausuch (jose.lausuch@ericsson.com) +# 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 +############################################################################## + +from FuelAdapter import FuelAdapter +from ApexAdapter import ApexAdapter +from CompassAdapter import CompassAdapter +from JoidAdapter import JoidAdapter + + +INSTALLERS = ["fuel", "apex", "compass", "joid"] + + +class InstallerHandler: + + def __init__(self, + installer, + installer_ip, + installer_user, + installer_pwd=None): + self.installer = installer.lower() + self.installer_ip = installer_ip + self.installer_user = installer_user + self.installer_pwd = installer_pwd + + if self.installer == INSTALLERS[0]: + self.InstallerAdapter = FuelAdapter(self.installer_ip, + self.installer_user, + self.installer_pwd) + elif self.installer == INSTALLERS[1]: + self.InstallerAdapter = ApexAdapter(self.installer_ip) + elif self.installer == INSTALLERS[2]: + self.InstallerAdapter = CompassAdapter(self.installer_ip) + elif self.installer == INSTALLERS[3]: + self.InstallerAdapter = JoidAdapter(self.installer_ip) + else: + print("Installer %s is not valid. " + "Please use one of the followings: %s" + % (self.installer, INSTALLERS)) + exit(1) + + def get_deployment_info(self): + return self.InstallerAdapter.get_deployment_info() + + def get_nodes(self, options=None): + return self.InstallerAdapter.get_nodes(options=options) + + def get_controller_ips(self, options=None): + return self.InstallerAdapter.get_controller_ips(options=options) + + def get_compute_ips(self, options=None): + return self.InstallerAdapter.get_compute_ips(options=options) + + def get_file_from_installer(self, + remote_path, + local_path, + options=None): + return self.InstallerAdapter.get_file_from_installer(remote_path, + local_path, + options=options) + + def get_file_from_controller(self, + remote_path, + local_path, + ip=None, + options=None): + return self.InstallerAdapter.get_file_from_controller(remote_path, + local_path, + ip=ip, + options=options) + + def get_all(self): + pass diff --git a/modules/opnfv/installer_adapters/__init__.py b/modules/opnfv/installer_adapters/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/modules/opnfv/installer_adapters/apex/ApexAdapter.py b/modules/opnfv/installer_adapters/apex/ApexAdapter.py new file mode 100644 index 000000000..17a27b10a --- /dev/null +++ b/modules/opnfv/installer_adapters/apex/ApexAdapter.py @@ -0,0 +1,32 @@ +############################################################################## +# Copyright (c) 2016 Ericsson AB and others. +# Author: Jose Lausuch (jose.lausuch@ericsson.com) +# 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 +############################################################################## + + +class ApexAdapter: + + def __init__(self, installer_ip): + self.installer_ip = installer_ip + + def get_deployment_info(self): + pass + + def get_nodes(self): + pass + + def get_controller_ips(self): + pass + + def get_compute_ips(self): + pass + + def get_file_from_installer(self, origin, target, options=None): + pass + + def get_file_from_controller(self, origin, target, ip=None, options=None): + pass diff --git a/modules/opnfv/installer_adapters/apex/__init__.py b/modules/opnfv/installer_adapters/apex/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/modules/opnfv/installer_adapters/compass/CompassAdapter.py b/modules/opnfv/installer_adapters/compass/CompassAdapter.py new file mode 100644 index 000000000..47cbc646d --- /dev/null +++ b/modules/opnfv/installer_adapters/compass/CompassAdapter.py @@ -0,0 +1,32 @@ +############################################################################## +# Copyright (c) 2016 Ericsson AB and others. +# Author: Jose Lausuch (jose.lausuch@ericsson.com) +# 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 +############################################################################## + + +class CompassAdapter: + + def __init__(self, installer_ip): + self.installer_ip = installer_ip + + def get_deployment_info(self): + pass + + def get_nodes(self): + pass + + def get_controller_ips(self): + pass + + def get_compute_ips(self): + pass + + def get_file_from_installer(self, origin, target, options=None): + pass + + def get_file_from_controller(self, origin, target, ip=None, options=None): + pass diff --git a/modules/opnfv/installer_adapters/compass/__init__.py b/modules/opnfv/installer_adapters/compass/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/modules/opnfv/installer_adapters/fuel/FuelAdapter.py b/modules/opnfv/installer_adapters/fuel/FuelAdapter.py new file mode 100644 index 000000000..95b2ab673 --- /dev/null +++ b/modules/opnfv/installer_adapters/fuel/FuelAdapter.py @@ -0,0 +1,236 @@ +############################################################################## +# Copyright (c) 2016 Ericsson AB and others. +# Author: Jose Lausuch (jose.lausuch@ericsson.com) +# George Paraskevopoulos (geopar@intracom-telecom.com) +# 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 opnfv.modules.utils.SSHUtils as ssh_utils +import opnfv.modules.utils.OPNFVLogger as logger + + +class FuelAdapter: + + def __init__(self, installer_ip, user="root", password="r00tme"): + self.installer_ip = installer_ip + self.installer_user = user + self.installer_password = password + self.installer_connection = ssh_utils.get_ssh_client( + installer_ip, + self.installer_user, + password=self.installer_password) + self.logger = logger.Logger("FuelHandler").getLogger() + + def runcmd_fuel_installer(self, cmd): + _, stdout, stderr = (self + .installer_connection + .exec_command(cmd)) + error = stderr.readlines() + if len(error) > 0: + self.logger.error("error %s" % ''.join(error)) + return error + output = ''.join(stdout.readlines()) + return output + + def runcmd_fuel_nodes(self): + return self.runcmd_fuel_installer('fuel nodes') + + def runcmd_fuel_env(self): + return self.runcmd_fuel_installer('fuel env') + + def get_clusters(self): + environments = [] + output = self.runcmd_fuel_env() + lines = output.rsplit('\n') + if len(lines) < 2: + self.logger.infp("No environments found in the deployment.") + return None + else: + fields = lines[0].rsplit(' | ') + + index_id = -1 + index_status = -1 + index_name = -1 + index_release_id = -1 + + for i in range(0, len(fields) - 1): + if "id" in fields[i]: + index_id = i + elif "status" in fields[i]: + index_status = i + elif "name" in fields[i]: + index_name = i + elif "release_id" in fields[i]: + index_release_id = i + + # order env info + for i in range(2, len(lines) - 1): + fields = lines[i].rsplit(' | ') + dict = {"id": fields[index_id].strip(), + "status": fields[index_status].strip(), + "name": fields[index_name].strip(), + "release_id": fields[index_release_id].strip()} + environments.append(dict) + + return environments + + def get_nodes(self, options=None): + nodes = [] + output = self.runcmd_fuel_nodes() + lines = output.rsplit('\n') + if len(lines) < 2: + self.logger.info("No nodes found in the deployment.") + return None + else: + # get fields indexes + fields = lines[0].rsplit(' | ') + + index_id = -1 + index_status = -1 + index_name = -1 + index_cluster = -1 + index_ip = -1 + index_mac = -1 + index_roles = -1 + index_online = -1 + + for i in range(0, len(fields) - 1): + if "id" in fields[i]: + index_id = i + elif "status" in fields[i]: + index_status = i + elif "name" in fields[i]: + index_name = i + elif "cluster" in fields[i]: + index_cluster = i + elif "ip" in fields[i]: + index_ip = i + elif "mac" in fields[i]: + index_mac = i + elif "roles " in fields[i]: + index_roles = i + elif "online" in fields[i]: + index_online = i + + # order nodes info + for i in range(2, len(lines) - 1): + fields = lines[i].rsplit(' | ') + dict = {"id": fields[index_id].strip(), + "status": fields[index_status].strip(), + "name": fields[index_name].strip(), + "cluster": fields[index_cluster].strip(), + "ip": fields[index_ip].strip(), + "mac": fields[index_mac].strip(), + "roles": fields[index_roles].strip(), + "online": fields[index_online].strip()} + if options and options['cluster']: + if fields[index_cluster].strip() == options['cluster']: + nodes.append(dict) + else: + nodes.append(dict) + + return nodes + + def get_controller_ips(self, options): + nodes = self.get_nodes(options=options) + controllers = [] + for node in nodes: + if "controller" in node["roles"]: + controllers.append(node['ip']) + return controllers + + def get_compute_ips(self, options=None): + nodes = self.get_nodes(options=options) + computes = [] + for node in nodes: + if "compute" in node["roles"]: + computes.append(node['ip']) + return computes + + def get_deployment_info(self): + str = "Deployment details:\n" + str += "\tInstaller: Fuel\n" + str += "\tScenario: Unknown\n" + sdn = "None" + clusters = self.get_clusters() + str += "\tN.Clusters: %s\n" % len(clusters) + for cluster in clusters: + cluster_dic = {'cluster': cluster['id']} + str += "\tCluster info:\n" + str += "\t ID: %s\n" % cluster['id'] + str += "\t NAME: %s\n" % cluster['name'] + str += "\t STATUS: %s\n" % cluster['status'] + nodes = self.get_nodes(options=cluster_dic) + num_nodes = len(nodes) + for node in nodes: + if "opendaylight" in node['roles']: + sdn = "OpenDaylight" + elif "onos" in node['roles']: + sdn = "ONOS" + num_controllers = len( + self.get_controller_ips(options=cluster_dic)) + num_computes = len(self.get_compute_ips(options=cluster_dic)) + ha = False + if num_controllers > 1: + ha = True + + str += "\t HA: %s\n" % ha + str += "\t NUM.NODES: %s\n" % num_nodes + str += "\t CONTROLLERS: %s\n" % num_controllers + str += "\t COMPUTES: %s\n" % num_computes + str += "\t SDN CONTR.: %s\n\n" % sdn + str += self.runcmd_fuel_nodes() + return str + + def get_file_from_installer(self, remote_path, local_path, options=None): + self.logger.debug("Fetching %s from %s" % + (remote_path, self.installer_ip)) + get_file_result = ssh_utils.get_file(self.installer_connection, + remote_path, + local_path) + if get_file_result is None: + self.logger.error("SFTP failed to retrieve the file.") + return 1 + self.logger.info("%s successfully copied from Fuel to %s" % + (remote_path, local_path)) + + def get_file_from_controller(self, + remote_path, + local_path, + ip=None, + user='root', + options=None): + if ip is None: + controllers = self.get_controller_ips(options=options) + if len(controllers) == 0: + self.logger.info("No controllers found in the deployment.") + return 1 + else: + target_ip = controllers[0] + else: + target_ip = ip + + installer_jumphost = { + 'ip': self.installer_ip, + 'username': self.installer_user, + 'password': self.installer_password + } + controller_conn = ssh_utils.get_ssh_client( + target_ip, + user, + jumphost=installer_jumphost) + + self.logger.debug("Fetching %s from %s" % + (remote_path, target_ip)) + + get_file_result = ssh_utils.get_file(controller_conn, + remote_path, + local_path) + if get_file_result is None: + self.logger.error("SFTP failed to retrieve the file.") + return 1 + self.logger.info("%s successfully copied from %s to %s" % + (remote_path, target_ip, local_path)) diff --git a/modules/opnfv/installer_adapters/fuel/__init__.py b/modules/opnfv/installer_adapters/fuel/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/modules/opnfv/installer_adapters/fuel/example.py b/modules/opnfv/installer_adapters/fuel/example.py new file mode 100644 index 000000000..804d79c3d --- /dev/null +++ b/modules/opnfv/installer_adapters/fuel/example.py @@ -0,0 +1,22 @@ +# This is an example of usage of this Tool +# Author: Jose Lausuch (jose.lausuch@ericsson.com) + +from InstallerHandler import InstallerHandler + +fuel_handler = InstallerHandler(installer='fuel', + installer_ip='10.20.0.2', + installer_user='root', + installer_pwd='r00tme') +print("Nodes in cluster 1:\n%s\n" % + fuel_handler.get_nodes(options={'cluster': '1'})) +print("Nodes in cluster 2:\n%s\n" % + fuel_handler.get_nodes(options={'cluster': '2'})) +print("Nodes:\n%s\n" % fuel_handler.get_nodes()) +print("Controller nodes:\n%s\n" % fuel_handler.get_controller_ips()) +print("Compute nodes:\n%s\n" % fuel_handler.get_compute_ips()) +print("\n%s\n" % fuel_handler.get_deployment_info()) +fuel_handler.get_file_from_installer('/root/deploy/dea.yaml', './dea.yaml') +fuel_handler.get_file_from_controller( + '/etc/neutron/neutron.conf', './neutron.conf') +fuel_handler.get_file_from_controller( + '/root/openrc', './openrc') diff --git a/modules/opnfv/installer_adapters/joid/JoidAdapter.py b/modules/opnfv/installer_adapters/joid/JoidAdapter.py new file mode 100644 index 000000000..be8c2ebac --- /dev/null +++ b/modules/opnfv/installer_adapters/joid/JoidAdapter.py @@ -0,0 +1,32 @@ +############################################################################## +# Copyright (c) 2016 Ericsson AB and others. +# Author: Jose Lausuch (jose.lausuch@ericsson.com) +# 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 +############################################################################## + + +class JoidAdapter: + + def __init__(self, installer_ip): + self.installer_ip = installer_ip + + def get_deployment_info(self): + pass + + def get_nodes(self): + pass + + def get_controller_ips(self): + pass + + def get_compute_ips(self): + pass + + def get_file_from_installer(self, origin, target, options=None): + pass + + def get_file_from_controller(self, origin, target, ip=None, options=None): + pass diff --git a/modules/opnfv/installer_adapters/joid/__init__.py b/modules/opnfv/installer_adapters/joid/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/modules/opnfv/utils/OPNFVLogger.py b/modules/opnfv/utils/OPNFVLogger.py new file mode 100644 index 000000000..6fa4ef2e2 --- /dev/null +++ b/modules/opnfv/utils/OPNFVLogger.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# +# jose.lausuch@ericsson.com +# 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 +# +# Logging levels: +# Level Numeric value +# CRITICAL 50 +# ERROR 40 +# WARNING 30 +# INFO 20 +# DEBUG 10 +# NOTSET 0 +# +# Usage: +# import RelengLogger as rl +# logger = fl.Logger("script_name").getLogger() +# logger.info("message to be shown with - INFO - ") +# logger.debug("message to be shown with - DEBUG -") + +import logging + + +class Logger: + + def __init__(self, logger_name, level="INFO"): + + self.logger = logging.getLogger(logger_name) + self.logger.propagate = 0 + self.logger.setLevel(logging.DEBUG) + + ch = logging.StreamHandler() + formatter = logging.Formatter('%(asctime)s - %(name)s - ' + '%(levelname)s - %(message)s') + ch.setFormatter(formatter) + if level.lower() == "debug": + ch.setLevel(logging.DEBUG) + else: + ch.setLevel(logging.INFO) + self.logger.addHandler(ch) + + hdlr = logging.FileHandler('/tmp/releng.log') + hdlr.setFormatter(formatter) + hdlr.setLevel(logging.DEBUG) + self.logger.addHandler(hdlr) + + def getLogger(self): + return self.logger diff --git a/modules/opnfv/utils/SSHUtils.py b/modules/opnfv/utils/SSHUtils.py new file mode 100644 index 000000000..2f48addbf --- /dev/null +++ b/modules/opnfv/utils/SSHUtils.py @@ -0,0 +1,121 @@ +############################################################################## +# Copyright (c) 2015 Ericsson AB and others. +# Authors: George Paraskevopoulos (geopar@intracom-telecom.com) +# Jose Lausuch (jose.lausuch@ericsson.com) +# 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 paramiko +import opnfv.modules.utils.OPNFVLogger as OPNFVLogger +import os + +logger = OPNFVLogger.Logger('SSHUtils').getLogger() + + +def get_ssh_client(hostname, username, password=None, jumphost=None): + client = None + try: + if jumphost is None: + client = paramiko.SSHClient() + else: + client = JumpHostHopClient() + client.configure_jump_host(jumphost['ip'], + jumphost['username'], + jumphost['password']) + + if client is None: + raise Exception('Could not connect to client') + + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + client.connect(hostname, + username=username, + password=password) + return client + except Exception, e: + logger.error(e) + return None + + +def get_file(ssh_conn, src, dest): + try: + sftp = ssh_conn.open_sftp() + sftp.get(src, dest) + return True + except Exception, e: + logger.error("Error [get_file(ssh_conn, '%s', '%s']: %s" % + (src, dest, e)) + return None + + +def put_file(ssh_conn, src, dest): + try: + sftp = ssh_conn.open_sftp() + sftp.put(src, dest) + return True + except Exception, e: + logger.error("Error [put_file(ssh_conn, '%s', '%s']: %s" % + (src, dest, e)) + return None + + +class JumpHostHopClient(paramiko.SSHClient): + ''' + Connect to a remote server using a jumphost hop + ''' + + def __init__(self, *args, **kwargs): + self.logger = rl.Logger("JumpHostHopClient").getLogger() + self.jumphost_ssh = None + self.jumphost_transport = None + self.jumphost_channel = None + self.jumphost_ip = None + self.jumphost_ssh_key = None + self.local_ssh_key = os.path.join(os.getcwd(), 'id_rsa') + super(JumpHostHopClient, self).__init__(*args, **kwargs) + + def configure_jump_host(self, jh_ip, jh_user, jh_pass, + jh_ssh_key='/root/.ssh/id_rsa'): + self.jumphost_ip = jh_ip + self.jumphost_ssh_key = jh_ssh_key + self.jumphost_ssh = paramiko.SSHClient() + self.jumphost_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.jumphost_ssh.connect(jh_ip, + username=jh_user, + password=jh_pass) + self.jumphost_transport = self.jumphost_ssh.get_transport() + + def connect(self, hostname, port=22, username='root', password=None, + pkey=None, key_filename=None, timeout=None, allow_agent=True, + look_for_keys=True, compress=False, sock=None, gss_auth=False, + gss_kex=False, gss_deleg_creds=True, gss_host=None, + banner_timeout=None): + try: + if self.jumphost_ssh is None: + raise Exception('You must configure the jump ' + 'host before calling connect') + + get_file_res = get_file(self.jumphost_ssh, + self.jumphost_ssh_key, + self.local_ssh_key) + if get_file_res is None: + raise Exception('Could\'t fetch SSH key from jump host') + jumphost_key = (paramiko.RSAKey + .from_private_key_file(self.local_ssh_key)) + + self.jumphost_channel = self.jumphost_transport.open_channel( + "direct-tcpip", + (hostname, 22), + (self.jumphost_ip, 22)) + + self.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + super(JumpHostHopClient, self).connect(hostname, + username=username, + pkey=jumphost_key, + sock=self.jumphost_channel) + os.remove(self.local_ssh_key) + except Exception, e: + self.logger.error(e) diff --git a/modules/opnfv/utils/__init__.py b/modules/opnfv/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/modules/utils/__init.py__ b/modules/utils/__init.py__ deleted file mode 100644 index e69de29bb..000000000 diff --git a/utils/installer-adapter/ApexAdapter.py b/utils/installer-adapter/ApexAdapter.py deleted file mode 100644 index 17a27b10a..000000000 --- a/utils/installer-adapter/ApexAdapter.py +++ /dev/null @@ -1,32 +0,0 @@ -############################################################################## -# Copyright (c) 2016 Ericsson AB and others. -# Author: Jose Lausuch (jose.lausuch@ericsson.com) -# 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 -############################################################################## - - -class ApexAdapter: - - def __init__(self, installer_ip): - self.installer_ip = installer_ip - - def get_deployment_info(self): - pass - - def get_nodes(self): - pass - - def get_controller_ips(self): - pass - - def get_compute_ips(self): - pass - - def get_file_from_installer(self, origin, target, options=None): - pass - - def get_file_from_controller(self, origin, target, ip=None, options=None): - pass diff --git a/utils/installer-adapter/CompassAdapter.py b/utils/installer-adapter/CompassAdapter.py deleted file mode 100644 index 47cbc646d..000000000 --- a/utils/installer-adapter/CompassAdapter.py +++ /dev/null @@ -1,32 +0,0 @@ -############################################################################## -# Copyright (c) 2016 Ericsson AB and others. -# Author: Jose Lausuch (jose.lausuch@ericsson.com) -# 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 -############################################################################## - - -class CompassAdapter: - - def __init__(self, installer_ip): - self.installer_ip = installer_ip - - def get_deployment_info(self): - pass - - def get_nodes(self): - pass - - def get_controller_ips(self): - pass - - def get_compute_ips(self): - pass - - def get_file_from_installer(self, origin, target, options=None): - pass - - def get_file_from_controller(self, origin, target, ip=None, options=None): - pass diff --git a/utils/installer-adapter/FuelAdapter.py b/utils/installer-adapter/FuelAdapter.py deleted file mode 100644 index 672fd5175..000000000 --- a/utils/installer-adapter/FuelAdapter.py +++ /dev/null @@ -1,236 +0,0 @@ -############################################################################## -# Copyright (c) 2016 Ericsson AB and others. -# Author: Jose Lausuch (jose.lausuch@ericsson.com) -# George Paraskevopoulos (geopar@intracom-telecom.com) -# 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 SSHUtils as ssh_utils -import RelengLogger as rl - - -class FuelAdapter: - - def __init__(self, installer_ip, user="root", password="r00tme"): - self.installer_ip = installer_ip - self.installer_user = user - self.installer_password = password - self.installer_connection = ssh_utils.get_ssh_client( - installer_ip, - self.installer_user, - password=self.installer_password) - self.logger = rl.Logger("Handler").getLogger() - - def runcmd_fuel_installer(self, cmd): - _, stdout, stderr = (self - .installer_connection - .exec_command(cmd)) - error = stderr.readlines() - if len(error) > 0: - self.logger.error("error %s" % ''.join(error)) - return error - output = ''.join(stdout.readlines()) - return output - - def runcmd_fuel_nodes(self): - return self.runcmd_fuel_installer('fuel nodes') - - def runcmd_fuel_env(self): - return self.runcmd_fuel_installer('fuel env') - - def get_clusters(self): - environments = [] - output = self.runcmd_fuel_env() - lines = output.rsplit('\n') - if len(lines) < 2: - self.logger.infp("No environments found in the deployment.") - return None - else: - fields = lines[0].rsplit(' | ') - - index_id = -1 - index_status = -1 - index_name = -1 - index_release_id = -1 - - for i in range(0, len(fields) - 1): - if "id" in fields[i]: - index_id = i - elif "status" in fields[i]: - index_status = i - elif "name" in fields[i]: - index_name = i - elif "release_id" in fields[i]: - index_release_id = i - - # order env info - for i in range(2, len(lines) - 1): - fields = lines[i].rsplit(' | ') - dict = {"id": fields[index_id].strip(), - "status": fields[index_status].strip(), - "name": fields[index_name].strip(), - "release_id": fields[index_release_id].strip()} - environments.append(dict) - - return environments - - def get_nodes(self, options=None): - nodes = [] - output = self.runcmd_fuel_nodes() - lines = output.rsplit('\n') - if len(lines) < 2: - self.logger.info("No nodes found in the deployment.") - return None - else: - # get fields indexes - fields = lines[0].rsplit(' | ') - - index_id = -1 - index_status = -1 - index_name = -1 - index_cluster = -1 - index_ip = -1 - index_mac = -1 - index_roles = -1 - index_online = -1 - - for i in range(0, len(fields) - 1): - if "id" in fields[i]: - index_id = i - elif "status" in fields[i]: - index_status = i - elif "name" in fields[i]: - index_name = i - elif "cluster" in fields[i]: - index_cluster = i - elif "ip" in fields[i]: - index_ip = i - elif "mac" in fields[i]: - index_mac = i - elif "roles " in fields[i]: - index_roles = i - elif "online" in fields[i]: - index_online = i - - # order nodes info - for i in range(2, len(lines) - 1): - fields = lines[i].rsplit(' | ') - dict = {"id": fields[index_id].strip(), - "status": fields[index_status].strip(), - "name": fields[index_name].strip(), - "cluster": fields[index_cluster].strip(), - "ip": fields[index_ip].strip(), - "mac": fields[index_mac].strip(), - "roles": fields[index_roles].strip(), - "online": fields[index_online].strip()} - if options and options['cluster']: - if fields[index_cluster].strip() == options['cluster']: - nodes.append(dict) - else: - nodes.append(dict) - - return nodes - - def get_controller_ips(self, options): - nodes = self.get_nodes(options=options) - controllers = [] - for node in nodes: - if "controller" in node["roles"]: - controllers.append(node['ip']) - return controllers - - def get_compute_ips(self, options=None): - nodes = self.get_nodes(options=options) - computes = [] - for node in nodes: - if "compute" in node["roles"]: - computes.append(node['ip']) - return computes - - def get_deployment_info(self): - str = "Deployment details:\n" - str += "\tInstaller: Fuel\n" - str += "\tScenario: Unknown\n" - sdn = "None" - clusters = self.get_clusters() - str += "\tN.Clusters: %s\n" % len(clusters) - for cluster in clusters: - cluster_dic = {'cluster': cluster['id']} - str += "\tCluster info:\n" - str += "\t ID: %s\n" % cluster['id'] - str += "\t NAME: %s\n" % cluster['name'] - str += "\t STATUS: %s\n" % cluster['status'] - nodes = self.get_nodes(options=cluster_dic) - num_nodes = len(nodes) - for node in nodes: - if "opendaylight" in node['roles']: - sdn = "OpenDaylight" - elif "onos" in node['roles']: - sdn = "ONOS" - num_controllers = len( - self.get_controller_ips(options=cluster_dic)) - num_computes = len(self.get_compute_ips(options=cluster_dic)) - ha = False - if num_controllers > 1: - ha = True - - str += "\t HA: %s\n" % ha - str += "\t NUM.NODES: %s\n" % num_nodes - str += "\t CONTROLLERS: %s\n" % num_controllers - str += "\t COMPUTES: %s\n" % num_computes - str += "\t SDN CONTR.: %s\n\n" % sdn - str += self.runcmd_fuel_nodes() - return str - - def get_file_from_installer(self, remote_path, local_path, options=None): - self.logger.debug("Fetching %s from %s" % - (remote_path, self.installer_ip)) - get_file_result = ssh_utils.get_file(self.installer_connection, - remote_path, - local_path) - if get_file_result is None: - self.logger.error("SFTP failed to retrieve the file.") - return 1 - self.logger.info("%s successfully copied from Fuel to %s" % - (remote_path, local_path)) - - def get_file_from_controller(self, - remote_path, - local_path, - ip=None, - user='root', - options=None): - if ip is None: - controllers = self.get_controller_ips(options=options) - if len(controllers) == 0: - self.logger.info("No controllers found in the deployment.") - return 1 - else: - target_ip = controllers[0] - else: - target_ip = ip - - installer_jumphost = { - 'ip': self.installer_ip, - 'username': self.installer_user, - 'password': self.installer_password - } - controller_conn = ssh_utils.get_ssh_client( - target_ip, - user, - jumphost=installer_jumphost) - - self.logger.debug("Fetching %s from %s" % - (remote_path, target_ip)) - - get_file_result = ssh_utils.get_file(controller_conn, - remote_path, - local_path) - if get_file_result is None: - self.logger.error("SFTP failed to retrieve the file.") - return 1 - self.logger.info("%s successfully copied from %s to %s" % - (remote_path, target_ip, local_path)) diff --git a/utils/installer-adapter/InstallerHandler.py b/utils/installer-adapter/InstallerHandler.py deleted file mode 100644 index b81b806ca..000000000 --- a/utils/installer-adapter/InstallerHandler.py +++ /dev/null @@ -1,78 +0,0 @@ -############################################################################## -# Copyright (c) 2015 Ericsson AB and others. -# Author: Jose Lausuch (jose.lausuch@ericsson.com) -# 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 -############################################################################## - -from FuelAdapter import FuelAdapter -from ApexAdapter import ApexAdapter -from CompassAdapter import CompassAdapter -from JoidAdapter import JoidAdapter - - -INSTALLERS = ["fuel", "apex", "compass", "joid"] - - -class InstallerHandler: - - def __init__(self, - installer, - installer_ip, - installer_user, - installer_pwd=None): - self.installer = installer.lower() - self.installer_ip = installer_ip - self.installer_user = installer_user - self.installer_pwd = installer_pwd - - if self.installer == INSTALLERS[0]: - self.InstallerAdapter = FuelAdapter(self.installer_ip, - self.installer_user, - self.installer_pwd) - elif self.installer == INSTALLERS[1]: - self.InstallerAdapter = ApexAdapter(self.installer_ip) - elif self.installer == INSTALLERS[2]: - self.InstallerAdapter = CompassAdapter(self.installer_ip) - elif self.installer == INSTALLERS[3]: - self.InstallerAdapter = JoidAdapter(self.installer_ip) - else: - print("Installer %s is not valid. " - "Please use one of the followings: %s" - % (self.installer, INSTALLERS)) - exit(1) - - def get_deployment_info(self): - return self.InstallerAdapter.get_deployment_info() - - def get_nodes(self, options=None): - return self.InstallerAdapter.get_nodes(options=options) - - def get_controller_ips(self, options=None): - return self.InstallerAdapter.get_controller_ips(options=options) - - def get_compute_ips(self, options=None): - return self.InstallerAdapter.get_compute_ips(options=options) - - def get_file_from_installer(self, - remote_path, - local_path, - options=None): - return self.InstallerAdapter.get_file_from_installer(remote_path, - local_path, - options=options) - - def get_file_from_controller(self, - remote_path, - local_path, - ip=None, - options=None): - return self.InstallerAdapter.get_file_from_controller(remote_path, - local_path, - ip=ip, - options=options) - - def get_all(self): - pass diff --git a/utils/installer-adapter/JoidAdapter.py b/utils/installer-adapter/JoidAdapter.py deleted file mode 100644 index be8c2ebac..000000000 --- a/utils/installer-adapter/JoidAdapter.py +++ /dev/null @@ -1,32 +0,0 @@ -############################################################################## -# Copyright (c) 2016 Ericsson AB and others. -# Author: Jose Lausuch (jose.lausuch@ericsson.com) -# 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 -############################################################################## - - -class JoidAdapter: - - def __init__(self, installer_ip): - self.installer_ip = installer_ip - - def get_deployment_info(self): - pass - - def get_nodes(self): - pass - - def get_controller_ips(self): - pass - - def get_compute_ips(self): - pass - - def get_file_from_installer(self, origin, target, options=None): - pass - - def get_file_from_controller(self, origin, target, ip=None, options=None): - pass diff --git a/utils/installer-adapter/RelengLogger.py b/utils/installer-adapter/RelengLogger.py deleted file mode 100644 index 6fa4ef2e2..000000000 --- a/utils/installer-adapter/RelengLogger.py +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env python -# -# jose.lausuch@ericsson.com -# 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 -# -# Logging levels: -# Level Numeric value -# CRITICAL 50 -# ERROR 40 -# WARNING 30 -# INFO 20 -# DEBUG 10 -# NOTSET 0 -# -# Usage: -# import RelengLogger as rl -# logger = fl.Logger("script_name").getLogger() -# logger.info("message to be shown with - INFO - ") -# logger.debug("message to be shown with - DEBUG -") - -import logging - - -class Logger: - - def __init__(self, logger_name, level="INFO"): - - self.logger = logging.getLogger(logger_name) - self.logger.propagate = 0 - self.logger.setLevel(logging.DEBUG) - - ch = logging.StreamHandler() - formatter = logging.Formatter('%(asctime)s - %(name)s - ' - '%(levelname)s - %(message)s') - ch.setFormatter(formatter) - if level.lower() == "debug": - ch.setLevel(logging.DEBUG) - else: - ch.setLevel(logging.INFO) - self.logger.addHandler(ch) - - hdlr = logging.FileHandler('/tmp/releng.log') - hdlr.setFormatter(formatter) - hdlr.setLevel(logging.DEBUG) - self.logger.addHandler(hdlr) - - def getLogger(self): - return self.logger diff --git a/utils/installer-adapter/SSHUtils.py b/utils/installer-adapter/SSHUtils.py deleted file mode 100644 index c93888694..000000000 --- a/utils/installer-adapter/SSHUtils.py +++ /dev/null @@ -1,120 +0,0 @@ -############################################################################## -# Copyright (c) 2015 Ericsson AB and others. -# Authors: George Paraskevopoulos (geopar@intracom-telecom.com) -# Jose Lausuch (jose.lausuch@ericsson.com) -# 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 paramiko -import RelengLogger as rl -import os - -logger = rl.Logger('SSHUtils').getLogger() - - -def get_ssh_client(hostname, username, password=None, jumphost=None): - client = None - try: - if jumphost is None: - client = paramiko.SSHClient() - else: - client = JumpHostHopClient() - client.configure_jump_host(jumphost['ip'], - jumphost['username'], - jumphost['password']) - - if client is None: - raise Exception('Could not connect to client') - - client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - client.connect(hostname, - username=username, - password=password) - return client - except Exception, e: - logger.error(e) - return None - - -def get_file(ssh_conn, src, dest): - try: - sftp = ssh_conn.open_sftp() - sftp.get(src, dest) - return True - except Exception, e: - logger.error("Error [get_file(ssh_conn, '%s', '%s']: %s" % - (src, dest, e)) - return None - - -def put_file(ssh_conn, src, dest): - try: - sftp = ssh_conn.open_sftp() - sftp.put(src, dest) - return True - except Exception, e: - logger.error("Error [put_file(ssh_conn, '%s', '%s']: %s" % - (src, dest, e)) - return None - - -class JumpHostHopClient(paramiko.SSHClient): - ''' - Connect to a remote server using a jumphost hop - ''' - def __init__(self, *args, **kwargs): - self.logger = rl.Logger("JumpHostHopClient").getLogger() - self.jumphost_ssh = None - self.jumphost_transport = None - self.jumphost_channel = None - self.jumphost_ip = None - self.jumphost_ssh_key = None - self.local_ssh_key = os.path.join(os.getcwd(), 'id_rsa') - super(JumpHostHopClient, self).__init__(*args, **kwargs) - - def configure_jump_host(self, jh_ip, jh_user, jh_pass, - jh_ssh_key='/root/.ssh/id_rsa'): - self.jumphost_ip = jh_ip - self.jumphost_ssh_key = jh_ssh_key - self.jumphost_ssh = paramiko.SSHClient() - self.jumphost_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - self.jumphost_ssh.connect(jh_ip, - username=jh_user, - password=jh_pass) - self.jumphost_transport = self.jumphost_ssh.get_transport() - - def connect(self, hostname, port=22, username='root', password=None, - pkey=None, key_filename=None, timeout=None, allow_agent=True, - look_for_keys=True, compress=False, sock=None, gss_auth=False, - gss_kex=False, gss_deleg_creds=True, gss_host=None, - banner_timeout=None): - try: - if self.jumphost_ssh is None: - raise Exception('You must configure the jump ' - 'host before calling connect') - - get_file_res = get_file(self.jumphost_ssh, - self.jumphost_ssh_key, - self.local_ssh_key) - if get_file_res is None: - raise Exception('Could\'t fetch SSH key from jump host') - jumphost_key = (paramiko.RSAKey - .from_private_key_file(self.local_ssh_key)) - - self.jumphost_channel = self.jumphost_transport.open_channel( - "direct-tcpip", - (hostname, 22), - (self.jumphost_ip, 22)) - - self.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - super(JumpHostHopClient, self).connect(hostname, - username=username, - pkey=jumphost_key, - sock=self.jumphost_channel) - os.remove(self.local_ssh_key) - except Exception, e: - self.logger.error(e) diff --git a/utils/installer-adapter/__init__.py b/utils/installer-adapter/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/utils/installer-adapter/example.py b/utils/installer-adapter/example.py deleted file mode 100644 index 804d79c3d..000000000 --- a/utils/installer-adapter/example.py +++ /dev/null @@ -1,22 +0,0 @@ -# This is an example of usage of this Tool -# Author: Jose Lausuch (jose.lausuch@ericsson.com) - -from InstallerHandler import InstallerHandler - -fuel_handler = InstallerHandler(installer='fuel', - installer_ip='10.20.0.2', - installer_user='root', - installer_pwd='r00tme') -print("Nodes in cluster 1:\n%s\n" % - fuel_handler.get_nodes(options={'cluster': '1'})) -print("Nodes in cluster 2:\n%s\n" % - fuel_handler.get_nodes(options={'cluster': '2'})) -print("Nodes:\n%s\n" % fuel_handler.get_nodes()) -print("Controller nodes:\n%s\n" % fuel_handler.get_controller_ips()) -print("Compute nodes:\n%s\n" % fuel_handler.get_compute_ips()) -print("\n%s\n" % fuel_handler.get_deployment_info()) -fuel_handler.get_file_from_installer('/root/deploy/dea.yaml', './dea.yaml') -fuel_handler.get_file_from_controller( - '/etc/neutron/neutron.conf', './neutron.conf') -fuel_handler.get_file_from_controller( - '/root/openrc', './openrc') -- cgit 1.2.3-korg