diff options
-rw-r--r-- | cns-libs/cnslibs/common/cns_libs.py | 415 | ||||
-rw-r--r-- | cns-libs/cnslibs/common/docker_libs.py | 91 | ||||
-rw-r--r-- | cns-libs/cnslibs/common/heketi_libs.py | 71 | ||||
-rw-r--r-- | tests/cns_tests_sample_config.yml | 11 |
4 files changed, 11 insertions, 577 deletions
diff --git a/cns-libs/cnslibs/common/cns_libs.py b/cns-libs/cnslibs/common/cns_libs.py index 370e05e3..68951784 100644 --- a/cns-libs/cnslibs/common/cns_libs.py +++ b/cns-libs/cnslibs/common/cns_libs.py @@ -1,397 +1,15 @@ +from glusto.core import Glusto as g +import yaml + from cnslibs.common.exceptions import ( ExecutionError, NotSupportedException) -from cnslibs.common.openshift_ops import ( - get_ocp_gluster_pod_names, - oc_rsh, - oc_version) -from cnslibs.common.waiter import Waiter -from glusto.core import Glusto as g -import yaml +from cnslibs.common.openshift_ops import oc_version MASTER_CONFIG_FILEPATH = "/etc/origin/master/master-config.yaml" -def edit_master_config_file(hostname, routingconfig_subdomain): - ''' - This function edits the /etc/origin/master/master-config.yaml file - Args: - hostname (str): hostname on which want to edit - the master-config.yaml file - routingconfig_subdomain (str): routing config subdomain url - ex: cloudapps.mystorage.com - Returns: - bool: True if successful, - otherwise False - ''' - try: - conn = g.rpyc_get_connection(hostname, user="root") - if conn is None: - g.log.error("Failed to get rpyc connection of node %s" - % hostname) - return False - with conn.builtin.open(MASTER_CONFIG_FILEPATH, 'r') as f: - data = yaml.load(f) - add_allow = 'AllowAllPasswordIdentityProvider' - data['oauthConfig']['identityProviders'][0]['provider'][ - 'kind'] = add_allow - data['routingConfig']['subdomain'] = routingconfig_subdomain - with conn.builtin.open(MASTER_CONFIG_FILEPATH, 'w+') as f: - yaml.dump(data, f, default_flow_style=False) - except Exception as err: - raise ExecutionError("failed to edit master-config.yaml file " - "%s on %s" % (err, hostname)) - finally: - g.rpyc_close_connection(hostname, user="root") - - g.log.info("successfully edited master-config.yaml file %s" % hostname) - return True - - -def setup_router(hostname, router_name, timeout=1200, wait_step=60): - ''' - This function sets up router - Args: - hostname (str): hostname on which we need to - setup router - router_name (str): router name - timeout (int): timeout value, - default value is 1200 sec - wait_step( int): wait step, - default value is 60 sec - Returns: - bool: True if successful, - otherwise False - ''' - cmd = ("oc get pods | grep '%s'| grep -v deploy | " - "awk '{print $3}'" % router_name) - ret, out, err = g.run(hostname, cmd, "root") - if ret != 0: - g.log.error("failed to execute cmd %s" % cmd) - return False - output = out.strip().split("\n")[0] - if "No resources found" in output or output == "": - g.log.info("%s not present creating it" % router_name) - cmd = "oadm policy add-scc-to-user privileged -z router" - ret, out, err = g.run(hostname, cmd, "root") - if ret != 0: - g.log.error("failed to execute cmd %s" % cmd) - return False - cmd = "oadm policy add-scc-to-user privileged -z default" - ret, out, err = g.run(hostname, cmd, "root") - if ret != 0: - g.log.error("failed to execute cmd %s" % cmd) - return False - cmd = "oadm router %s --replicas=1" % router_name - ret, out, err = g.run(hostname, cmd, "root") - if ret != 0: - g.log.error("failed to execute cmd %s" % cmd) - return False - router_flag = False - for w in Waiter(timeout, wait_step): - cmd = "oc get pods | grep '%s'| awk '{print $3}'" % router_name - ret, out, err = g.run(hostname, cmd, "root") - if ret != 0: - g.log.error("failed to execute cmd %s" % cmd) - break - status = out.strip().split("\n")[0].strip() - if status == "ContainerCreating" or status == "Pending": - g.log.info("container creating for router %s sleeping for" - " %s seconds" % (router_name, wait_step)) - continue - elif status == "Running": - router_flag = True - g.log.info("router %s is up and running" % router_name) - return router_flag - elif status == "Error": - g.log.error("error while setting up router %s" % ( - router_name)) - return router_flag - else: - g.log.error("%s router pod has different status - " - "%s" % (router_name, status)) - return router_flag - if w.expired: - g.log.error("failed to setup '%s' router in " - "%s seconds" % (router_name, timeout)) - return False - else: - g.log.info("%s already present" % router_name) - return True - - -def update_router_ip_dnsmasq_conf(hostname, router_name, router_domain): - ''' - This function updates the router-ip in /etc/dnsmasq.conf file - Args: - hostname (str): hostname on which we need to - edit dnsmaq.conf file - router_name (str): router name to find its ip - Returns: - bool: True if successful, - otherwise False - ''' - cmd = ("oc get pods -o wide | grep '%s'| grep -v deploy | " - "awk '{print $6}' | cut -d ':' -f 1") % router_name - ret, out, err = g.run(hostname, cmd, "root") - if ret != 0: - g.log.error("failed to execute cmd %s" % cmd) - return False - router_ip = out.strip().split("\n")[0].strip() - data_to_write = "address=/.%s/%s" % (router_domain, router_ip) - try: - conn = g.rpyc_get_connection(hostname, user="root") - if conn is None: - g.log.error("Failed to get rpyc connection of node %s" - % hostname) - return False - - update_flag = False - for line in conn.modules.fileinput.input( - '/etc/dnsmasq.conf', inplace=True): - if router_domain in line: - conn.modules.sys.stdout.write(line.replace(line, - data_to_write)) - update_flag = True - else: - conn.modules.sys.stdout.write(line) - if not update_flag: - with conn.builtin.open('/etc/dnsmasq.conf', 'a+') as f: - f.write(data_to_write + '\n') - except Exception as err: - g.log.error("failed to update router-ip in dnsmasq.conf %s" % err) - return False - finally: - g.rpyc_close_connection(hostname, user="root") - g.log.info("sucessfully updated router-ip in dnsmasq.conf") - return True - - -def update_nameserver_resolv_conf(hostname, position="first_line"): - ''' - This function updates namserver 127.0.0.1 - at first line in /etc/resolv.conf - Args: - hostname (str): hostname on which we need to - edit resolv.conf - position (str): where to add nameserver - ex: EOF, it defaults to first line - Returns: - bool: True if successful, - otherwise False - ''' - try: - conn = g.rpyc_get_connection(hostname, user="root") - if conn is None: - g.log.error("Failed to get rpyc connection of node %s" - % hostname) - return False - - if position == "EOF": - update_flag = False - with conn.builtin.open("/etc/resolv.conf", "r+") as f: - for line in f: - if "nameserver" in line and "127.0.0.1" in line: - update_flag = True - break - if not update_flag: - f.write("nameserver 127.0.0.1\n") - else: - for linenum, line in enumerate(conn.modules.fileinput.input( - '/etc/resolv.conf', inplace=True)): - if linenum == 0 and "127.0.0.1" not in line: - conn.modules.sys.stdout.write("nameserver 127.0.0.1\n") - conn.modules.sys.stdout.write(line) - except Exception as err: - g.log.error("failed to update nameserver in resolv.conf %s" % err) - return False - finally: - g.rpyc_close_connection(hostname, user="root") - g.log.info("sucessfully updated namserver in resolv.conf") - return True - - -def edit_multipath_conf_file(hostname): - ''' - This function edits the /etc/multipath.conf - Args: - hostname (str): hostname on which we want to edit - the /etc/multipath.conf file - Returns: - bool: True if successful, - otherwise False - ''' - try: - conn = g.rpyc_get_connection(hostname, user="root") - if conn is None: - g.log.error("Failed to get rpyc connection of node %s" - % hostname) - return False - - edit_flag = False - file1 = conn.builtin.open("/etc/multipath.conf", "r+") - for line1 in file1.readlines(): - if "LIO iSCSI" in line1: - g.log.info("/etc/multipath.conf file already " - "edited on %s" % hostname) - edit_flag = True - if not edit_flag: - file1 = conn.builtin.open("/etc/multipath.conf", "a+") - with open("cnslibs/common/sample-multipath.txt") as file2: - for line2 in file2: - file1.write(line2) - except Exception as err: - g.log.error("failed to edit /etc/multipath.conf file %s on %s" % - (err, hostname)) - return False - finally: - g.rpyc_close_connection(hostname, user="root") - g.log.info("successfully edited /etc/multipath.conf file %s" % hostname) - return True - - -def edit_iptables_cns(hostname): - ''' - This function edits the iptables file to open the ports - Args: - hostname (str): hostname on which we need to edit - the iptables - Returns: - bool: True if successful, - otherwise False - ''' - try: - conn = g.rpyc_get_connection(hostname, user="root") - if conn is None: - g.log.error("Failed to get rpyc connection of node %s" % hostname) - return False - - filter_flag = False - file_data = "" - data_to_add = "\n".join([ - "-A OS_FIREWALL_ALLOW -p tcp -m state --state NEW -m %s" % line - for line in ("tcp --dport 24007 -j ACCEPT", - "tcp --dport 24008 -j ACCEPT", - "tcp --dport 2222 -j ACCEPT", - "multiport --dports 49152:49664 -j ACCEPT", - "tcp --dport 24010 -j ACCEPT", - "tcp --dport 3260 -j ACCEPT", - "tcp --dport 111 -j ACCEPT") - ]) + "\n" - with conn.builtin.open("/etc/sysconfig/iptables", "r+") as f: - for line in f.readlines(): - if "--dport 3260" in line: - g.log.info("Iptables is already edited on %s" % hostname) - return True - if "*filter" in line: - filter_flag = True - elif "COMMIT" in line and filter_flag is True: - file_data += data_to_add - filter_flag = False - file_data += "%s" % line - with conn.builtin.open("/etc/sysconfig/iptables", "w") as f: - f.write(file_data) - g.log.info("successfully edited iptables on %s" % hostname) - return True - except Exception as err: - g.log.error("failed to edit iptables on %s err %s" % (hostname, err)) - return False - finally: - g.rpyc_close_connection(hostname, user="root") - - -def enable_kernel_module(hostname, module_name): - ''' - This function enables kernel modules required for CNS - Args: - hostname (str): hostname on which we want to - enable kernel modules - module_name (str): name of the module - ex: dm_thin_pool - Returns: - bool: True if successfull or already running, - False otherwise - ''' - cmd = "lsmod | grep %s" % module_name - ret, out, err = g.run(hostname, cmd, "root") - if ret == 0: - g.log.info("%s module is already enabled on %s" - % (module_name, hostname)) - else: - cmd = "modprobe %s" % module_name - ret, out, err = g.run(hostname, cmd, "root") - if ret == 0: - g.log.info("%s module enabled on %s" - % (module_name, hostname)) - else: - g.log.error("failed to enable %s module on %s" - % (module_name, hostname)) - return False - cmd = "echo %s > /etc/modules-load.d/%s.conf" % ( - module_name, module_name) - ret, out, err = g.run(hostname, cmd, "root") - if ret == 0: - g.log.info("created %s.conf" % module_name) - else: - g.log.error("failed to %s.conf" % module_name) - - return True - - -def start_service(hostname, service): - ''' - This function starts service by its name - Args: - hostname (str): hostname on which we want - to start service - Returns: - bool: True if successfull or already running, - False otherwise - ''' - cmd = "systemctl status %s" % service - ret, out, err = g.run(hostname, cmd, "root") - if ret == 0: - g.log.info("%s service is already running on %s" - % (service, hostname)) - return True - cmd = "systemctl start %s" % service - ret, out, err = g.run(hostname, cmd, "root") - if ret == 0: - g.log.info("successfully started %s service on %s" - % (service, hostname)) - return True - g.log.error("failed to start %s service on %s" - % (service, hostname)) - return False - - -def start_rpcbind_service(hostname): - ''' - This function starts the rpcbind service - Args: - hostname (str): hostname on which we want to start - rpcbind service - Returns: - bool: True if successfull or already running, - False otherwise - ''' - return start_service(hostname, 'rpcbind') - - -def start_gluster_blockd_service(hostname): - ''' - This function starts the gluster-blockd service - Args: - hostname (str): hostname on which we want to start - gluster-blocks service - Returns: - bool: True if successfull or already running, - False otherwise - ''' - return start_service(hostname, 'gluster-blockd') - - def validate_multipath_pod(hostname, podname, hacount): ''' This function validates multipath for given app-pod @@ -443,31 +61,6 @@ def validate_multipath_pod(hostname, podname, hacount): return True -def validate_gluster_blockd_service_gluster_pod(hostname): - ''' - This function validates if gluster-blockd service is - running on all gluster-pods - Args: - hostname (str): OCP master node name - Returns: - bool: True if service is running on all gluster-pods, - otherwise False - ''' - gluster_pod_list = get_ocp_gluster_pod_names(hostname) - g.log.info("gluster_pod_list -> %s" % gluster_pod_list) - for pod in gluster_pod_list: - cmd = "systemctl status gluster-blockd" - ret, out, err = oc_rsh(hostname, pod, cmd) - if ret != 0: - g.log.error("failed to execute cmd %s on %s out: " - "%s err: %s" % ( - cmd, hostname, out, err)) - return False - g.log.info("gluster-blockd service is running on all " - "gluster-pods %s" % gluster_pod_list) - return True - - def enable_pvc_resize(master_node): ''' This function edits the /etc/origin/master/master-config.yaml diff --git a/cns-libs/cnslibs/common/docker_libs.py b/cns-libs/cnslibs/common/docker_libs.py deleted file mode 100644 index c47f8b77..00000000 --- a/cns-libs/cnslibs/common/docker_libs.py +++ /dev/null @@ -1,91 +0,0 @@ -from glusto.core import Glusto as g - - -DOCKER_FILE_PATH = "/etc/sysconfig/docker" - - -def _docker_update_registry(hostname, registry, registry_type): - ''' - This function updates docker registry - Args: - hostname (str): hostname on which want to setup - the docker - registry (str): add regsitry url that needs to be added - in docker file. - ex: "ADD_REGISTRY='--add-registry registry.access.stage.redhat.com'" - registry_type (str): type of registry - ex: add or insecure - ''' - try: - conn = g.rpyc_get_connection(hostname, user="root") - if conn is None: - g.log.error("Failed to get rpyc connection of node %s" - % hostname) - return False - - if not conn.modules.os.path.exists(DOCKER_FILE_PATH): - g.log.error("Unable to locate %s in node %s" - % (DOCKER_FILE_PATH, hostname)) - return False - - registry_flag = False - lookup_str = "%s_REGISTRY=" % registry_type.upper() - for line in conn.modules.fileinput.input( - DOCKER_FILE_PATH, inplace=True): - if lookup_str in line: - registry_flag = True - if registry not in line: - line = line if "#" in line else "#" + line - conn.modules.sys.stdout.write(line) - conn.modules.sys.stdout.write(registry) - continue - conn.modules.sys.stdout.write(line) - - if not registry_flag: - with conn.builtin.open(DOCKER_FILE_PATH, 'a+') as docker_file: - docker_file.write(registry + '\n') - - except Exception as err: - g.log.error("failed to edit docker file with %s-registry " - "%s on %s" % (registry_type, err, hostname)) - return False - finally: - g.rpyc_close_connection(hostname, user="root") - - g.log.info("Sucessfully edited docker file with %s-registry " - "on %s" % (registry_type, hostname)) - return True - - -def docker_add_registry(hostname, registry_url): - ''' - This function edits /etc/sysconfig/docker file with ADD_REGISTRY - Args: - hostname (str): hostname on which want to setup - the docker - registry_url (str): add regsitry url that needs to be added - in docker file. - ex: "ADD_REGISTRY='--add-registry registry.access.stage.redhat.com'" - Returns: - bool: True if successful, - otherwise False - ''' - return _docker_update_registry(hostname, registry_url, 'add') - - -def docker_insecure_registry(hostname, registry_url): - ''' - This function edits /etc/sysconfig/docker file with INSECURE_REGISTRY - Args: - hostname (str): hostname on which want to setup - the docker - registry_url (str): insecure registry url that needs to be added - in docker file. - ex: "INSECURE_REGISTRY= - '--insecure-registry registry.access.stage.redhat.com'" - Returns: - bool: True if successful, - otherwise False - - ''' - return _docker_update_registry(hostname, registry_url, 'insecure') diff --git a/cns-libs/cnslibs/common/heketi_libs.py b/cns-libs/cnslibs/common/heketi_libs.py index 4a001adc..3ebe91ee 100644 --- a/cns-libs/cnslibs/common/heketi_libs.py +++ b/cns-libs/cnslibs/common/heketi_libs.py @@ -1,18 +1,11 @@ -#!/usr/bin/python - -""" - Description: Library for heketi BaseClass. -""" +from collections import OrderedDict +import datetime +import unittest from glusto.core import Glusto as g -import unittest -import datetime -from collections import OrderedDict + from cnslibs.common.exceptions import ExecutionError, ConfigError -from cnslibs.common.heketi_ops import (setup_heketi_ssh_key, - modify_heketi_executor, - export_heketi_cli_server, - hello_heketi, +from cnslibs.common.heketi_ops import (hello_heketi, heketi_volume_delete, heketi_blockvolume_delete) from cnslibs.common.openshift_ops import (oc_login, switch_oc_project, @@ -41,9 +34,6 @@ class HeketiBaseClass(unittest.TestCase): cls.ocp_master_node = cls.ocp_master_nodes[0] cls.deployment_type = g.config['cns']['deployment_type'] - cls.executor = g.config['cns']['executor'] - cls.executor_user = g.config['cns']['executor_user'] - cls.executor_port = g.config['cns']['executor_port'] heketi_config = g.config['cns']['heketi_config'] cls.heketi_dc_name = heketi_config['heketi_dc_name'] @@ -159,54 +149,3 @@ class HeketiBaseClass(unittest.TestCase): super(HeketiBaseClass, cls).tearDownClass() msg = "Teardownclass: %s : %s" % (cls.__name__, cls.glustotest_run_id) g.log.info(msg) - - -class HeketiClientSetupBaseClass(HeketiBaseClass): - """ - Class to setup heketi ssh keys, modify heketi executor and to export - heketi cli server. - """ - - @classmethod - def setUpClass(cls): - """ - setUpClass of HeketiClientSetupBaseClass - """ - - super(HeketiClientSetupBaseClass, cls).setUpClass() - - if cls.deployment_type == "crs_heketi_outside_openshift": - # setup heketi ssh keys, if setup is not done already - conn = g.rpyc_get_connection(cls.heketi_client_node, user="root") - if conn is None: - raise ConfigError("Failed to get rpyc connection of heketi " - "node %s" % cls.heket_client_node) - - if conn.modules.os.path.exists(cls.heketi_ssh_key): - g.log.info("Heketi ssh key is already generated") - else: - if not setup_heketi_ssh_key(cls.heketi_client_node, - cls.gluster_servers, - heketi_ssh_key=cls.heketi_ssh_key): - raise ConfigError("heketi ssh key setup failed on %s" - % cls.heketi_client_node) - - g.rpyc_close_connection(cls.heketi_client_node, user="root") - - # Modifies heketi executor - if not modify_heketi_executor(cls.heketi_client_node, cls.executor, - cls.heketi_ssh_key, - cls.executor_user, - int(cls.executor_port)): - raise ExecutionError("Failed to modify heketi executor on %s" - % cls.heketi_client_node) - - # Exports heketi cli server - heketi_url = cls.heketi_server_url - if not export_heketi_cli_server( - cls.heketi_client_node, - heketi_cli_server=heketi_url, - heketi_cli_user=cls.heketi_cli_user, - heketi_cli_key=cls.heketi_cli_key): - raise ExecutionError("Failed to export heketi cli server on %s" - % cls.heketi_client_node) diff --git a/tests/cns_tests_sample_config.yml b/tests/cns_tests_sample_config.yml index 4e1c7919..8d4a73b1 100644 --- a/tests/cns_tests_sample_config.yml +++ b/tests/cns_tests_sample_config.yml @@ -38,19 +38,12 @@ gluster_servers: cns: setup: - routing_config: "cloudapps.mystorage.com" - insecure_registry: "INSECURE_REGISTRY='--insecure-registry registry.access.redhat.com" - add_registry: "ADD_REGISTRY='--add-registry registry.access.redhat.com" cns_project_name: "storage-project" cns_username: "test-admin" cns_password: - # 'deployment_type' can be crs_heketi_outside_openshift|crs_heketi_inside_openshift|cns - deployment_type: - # 'executor' can be ssh|kube|mock - executor: ssh - executor_user: root - executor_port: 22 + # 'deployment_type' can be cns|crs + deployment_type: 'cns' trusted_storage_pool_list: - [gluster_server1, gluster_server2] heketi_config: |