diff options
Diffstat (limited to 'glustolibs-misc/glustolibs/misc/misc_libs.py')
-rwxr-xr-x | glustolibs-misc/glustolibs/misc/misc_libs.py | 193 |
1 files changed, 159 insertions, 34 deletions
diff --git a/glustolibs-misc/glustolibs/misc/misc_libs.py b/glustolibs-misc/glustolibs/misc/misc_libs.py index fb4bb49f1..9f9225929 100755 --- a/glustolibs-misc/glustolibs/misc/misc_libs.py +++ b/glustolibs-misc/glustolibs/misc/misc_libs.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2016 Red Hat, Inc. <http://www.redhat.com> +# Copyright (C) 2015-2020 Red Hat, Inc. <http://www.redhat.com> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -16,14 +16,16 @@ """ Description: Helper module for misc libs. """ -from glusto.core import Glusto as g import os import sys import time +from glusto.core import Glusto as g +from glustolibs.gluster.lib_utils import is_rhel6, is_rhel7 + def create_dirs(list_of_nodes, list_of_dir_paths): - """Creates directories on nodes. + """Create directories on nodes. Args: list_of_nodes (list): Nodes on which dirs has to be created. @@ -54,7 +56,7 @@ def create_dirs(list_of_nodes, list_of_dir_paths): def path_exists(list_of_nodes, list_of_paths): - """check if paths exists on nodes. + """Check if paths exist on nodes. Args: list_of_nodes (list): List of nodes. @@ -87,10 +89,10 @@ def path_exists(list_of_nodes, list_of_paths): def upload_scripts(list_of_nodes, list_of_scripts_abs_path, upload_dir="/usr/share/glustolibs/io/scripts/", user=None): - """Uploads specified scripts to all the nodes. + """Upload specified scripts to all the nodes. Args: - list_of_nodes (list): Nodes on which scripts has to be uploaded. + list_of_nodes (list): Nodes on which scripts have to be uploaded. list_of_scripts_abs_path (list): List of absolute path of all scripts that are to be uploaded from local node. upload_dir (optional[str]): Name of the dir under which @@ -156,7 +158,7 @@ def yum_add_repos(list_of_nodes, list_of_yum_repos): """Add yum repo files on all the nodes. Args: - list_of_nodes (list): Nodes on which yum repo files has to be added. + list_of_nodes (list): Nodes on which yum repo files have to be added. list_of_yum_repos (list): List of yum repos Returns: @@ -189,7 +191,7 @@ def yum_install_packages(list_of_nodes, yum_packages): """Install the specified yum packages on all nodes. Args: - list_of_nodes (list): Nodes on which yum packages has to be installed. + list_of_nodes (list): Nodes on which yum packages have to be installed. yum_packages (list): List of yum packages. Returns: @@ -220,7 +222,7 @@ def yum_remove_packages(list_of_nodes, yum_packages): """Remove the specified yum packages on all nodes. Args: - list_of_nodes (list): Nodes on which yum packages has to be removed. + list_of_nodes (list): Nodes on which yum packages have to be removed. yum_packages (list): List of yum packages. Returns: @@ -251,7 +253,7 @@ def pip_install_packages(list_of_nodes, python_packages): """Install the specified python packages on all the specified nodes. Args: - list_of_nodes (list): Nodes on which python packages has to be + list_of_nodes (list): Nodes on which python packages have to be installed. python_packages (list): List of python packages. @@ -319,7 +321,7 @@ def install_testing_tools(list_of_nodes, testing_tools): def install_arequal(list_of_nodes): - """Installs arequal on nodes in /usr/bin/. + """Install arequal on nodes in /usr/bin/. Args: nodes(list): List of nodes on which arequal-checksum needs to be @@ -339,8 +341,8 @@ def install_arequal(list_of_nodes): list_of_nodes = [list_of_nodes] try: - arequal_repo = (g.config['dependencies']['testing_tools']['arequal'] - ['repo']) + arequal_repo = (g.config['dependencies']['testing_tools'] + ['arequal']['repo']) except KeyError: arequal_repo = ("https://copr.fedorainfracloud.org/coprs/nigelbabu/" "arequal/repo/epel-7/nigelbabu-arequal-epel-7.repo") @@ -377,11 +379,10 @@ def install_arequal(list_of_nodes): def are_nodes_online(nodes): - """ - check whether nodes are online or not + """Check whether nodes are online or not. Args: - nodes ( str|list ) : Node/Nodes to check whether online or not + nodes (str|list): Node(s) to check whether online or not. Returns: tuple : Tuple containing two elements (ret, node_results). @@ -393,7 +394,7 @@ def are_nodes_online(nodes): then the result contains True else False. """ - if isinstance(nodes, str): + if not isinstance(nodes, list): nodes = [nodes] node_results = {} @@ -413,17 +414,15 @@ def are_nodes_online(nodes): def reboot_nodes(nodes): - """ - reboot the nodes and checks whether nodes are offline or not + """Reboot the nodes and check whether nodes are offline or not. Args: - nodes ( str|list ) : Node/Nodes to reboot + nodes (str|list): Node(s) to reboot. Returns: - bool : '_rc' is of type 'bool', True if all nodes - comes offline after reboot. False otherwise. + bool: True if all nodes come offline after reboot. False otherwise. """ - if isinstance(nodes, str): + if not isinstance(nodes, list): nodes = [nodes] cmd = "reboot" @@ -453,12 +452,11 @@ def reboot_nodes(nodes): return _rc -def reboot_nodes_and_wait_to_come_online(nodes, timeout=300): - """ - reboot the node and wait for node to come online +def reboot_nodes_and_wait_to_come_online(nodes, timeout=600): + """Reboot node(s) and wait for it to become online. Args: - nodes ( str|list ) : Node/Nodes to reboot + nodes (str|list): Node(s) to reboot. Kwargs: timeout (int): timeout value in seconds to wait for node @@ -507,11 +505,10 @@ def reboot_nodes_and_wait_to_come_online(nodes, timeout=300): def are_nodes_offline(nodes): - """ - check whether nodes are offline or not + """Check whether nodes are offline or not. Args: - nodes ( str|list ) : Node/Nodes to check whether offline or not + nodes (str|list): Node(s) to check whether offline or not. Returns: tuple : Tuple containing two elements (ret, node_results). @@ -523,7 +520,7 @@ def are_nodes_offline(nodes): then the result contains True else False. """ - if isinstance(nodes, str): + if not isinstance(nodes, list): nodes = [nodes] node_results = {} @@ -543,7 +540,7 @@ def are_nodes_offline(nodes): def drop_caches(hosts): - """Drops Kernel Cache on a list of hosts + """Drop Kernel Cache on a list of hosts (in order to run reads/renames etc on a cold cache). Args: @@ -551,15 +548,143 @@ def drop_caches(hosts): dropped (Servers/ Clients) Returns: - bool : True , post successful completion.Else,False. + bool: True , post successful completion.Else,False. """ cmd = "echo 3 > /proc/sys/vm/drop_caches" results = g.run_parallel(hosts, cmd) _rc = True - for host, ret_values in results.iteritems(): + for host, ret_values in results.items(): retcode, _, _ = ret_values if retcode != 0: g.log.error("Unable to drop cache on host %s", host) _rc = False return _rc + + +def daemon_reload(node): + """Reload the Daemons when unit files are changed. + + Args: + node (str): Node on which daemon has to be reloaded. + + Returns: + bool: True, On successful daemon reload + False, Otherwise + """ + if is_rhel6([node]): + cmd = 'service glusterd reload' + ret, _, _ = g.run(node, cmd) + if ret != 0: + g.log.error("Failed to reload the daemon") + return False + else: + cmd = "systemctl daemon-reload" + ret, _, _ = g.run(node, cmd) + if ret != 0: + g.log.error("Failed to reload the daemon") + return False + return True + + +def git_clone_and_compile(hosts, link, dir_name, compile_option='False'): + """Clone and compile a repo. + + Args: + hosts (list|str): List of hosts where the repo needs to be cloned. + link (str): Link to the repo that needs to be cloned. + dir_name (str): Directory where the repo is to be cloned. + + Kwargs: + compile_option (bool): If this option is set to True + then the cloned repo will be compiled otherwise + the repo is only cloned on the hosts. + + Returns: + bool : True on successfull cloning (and compilation) + False otherwise. + """ + if not isinstance(hosts, list): + hosts = [hosts] + + cmd = "cd /root; git clone %s %s;" % (link, dir_name) + if compile_option: + cmd = cmd + ("cd /root/%s; make" % dir_name) + + for host in hosts: + ret, _, _ = g.run(host, cmd) + if ret: + g.log.error("Cloning/Compiling repo failed on %s" % host) + return False + else: + g.log.info("Successfully cloned/compiled repo on %s" % host) + return True + + +def kill_process(mnode, process_ids='', process_names=''): + """Kills the given set of process running in the specified node + + Args: + mnode (str): Node at which the command has to be executed + process_ids (list|str): List of pid's to be terminated + process_names(list|str): List of Process names to be terminated + + Returns: + bool : True on successful termination of all the processes + False, otherwise + Example: + >>> kill_process("10.70.43.68", process_ids=27664) + True/False + >>> kill_process("10.70.43.68", process_names=["glustershd", + "glusterd"]) + True/False + """ + if process_names: + process_ids = [] + if not isinstance(process_names, list): + process_names = [process_names] + + for process in process_names: + ret, pids, _ = g.run(mnode, + "ps -aef | grep -i '%s' | grep -v 'grep' | " + "awk '{ print $2 }'" % process) + pids = pids.split("\n")[:-1] + if not pids: + g.log.error("Getting pid for process %s failed" % process) + return False + for pid in pids: + if pid: + process_ids.append(pid) + + if process_ids and not isinstance(process_ids, list): + process_ids = [process_ids] + + # Kill process + for pid in process_ids: + ret, _, _ = g.run(mnode, "kill -9 %s" % str(pid)) + if ret: + g.log.error("Failed to kill process with pid %s" % str(pid)) + return False + return True + + +def bring_down_network_interface(mnode, timeout=150): + """Brings the network interface down for a defined time + + Args: + mnode (str): Node at which the interface has to be bought down + timeout (int): Time duration (in secs) for which network has to + be down + + Returns: + network_status(object): Returns a process object + + Example: + >>> bring_down_network_interface("10.70.43.68", timout=100) + """ + interface = "eth0" if is_rhel7(mnode) else "ens3" + cmd = "ifconfig {0} down\nsleep {1}\nifconfig {0} up".format(interface, + timeout) + _, _, _ = g.run(mnode, "echo \"{}\"> 'test.sh'".format(cmd)) + network_status = g.run_async(mnode, "sh test.sh") + return network_status |