From e04dd165c33a21d2674f8084f824cf41a300c088 Mon Sep 17 00:00:00 2001 From: kshithijiyer Date: Fri, 23 Aug 2019 12:13:07 +0530 Subject: [lib] Adding group_add,ssh_keygen,ssh_copy_id, set_passwd to lib_utils. Adding the following functions tot lib_utils.py: 1.ssh_keygen(): Creates a pair of ssh private and public key if not present. 2.set_passwd(): Sets password for a given username. 3.ssh_copy_id(): Copies the default ssh public key. 4.group_add(): Creates a group in all the servers. Modifying add_user() to create and add user to a specific group. Also modifying the code in add_user() to take list of servers. Change-Id: I58cdef967293204073aa912fa98adb9606f6885d Signed-off-by: kshithijiyer --- glustolibs-gluster/glustolibs/gluster/lib_utils.py | 142 +++++++++++++++++++-- 1 file changed, 130 insertions(+), 12 deletions(-) (limited to 'glustolibs-gluster') diff --git a/glustolibs-gluster/glustolibs/gluster/lib_utils.py b/glustolibs-gluster/glustolibs/gluster/lib_utils.py index 121efb8ca..61498176d 100755 --- a/glustolibs-gluster/glustolibs/gluster/lib_utils.py +++ b/glustolibs-gluster/glustolibs/gluster/lib_utils.py @@ -954,30 +954,47 @@ def get_size_of_mountpoint(node, mount_point): return out -def add_user(host, uname): +def add_user(servers, username, group=None): """ - Add user with default home directory + Add user with default home directory + Args: - host (str): hostname/ip of the system - uname (str): username - Returns always True - """ + servers(list|str): hostname/ip of the system + username(str): username of the user to be created. + Kwargs: + group(str): Group name to which user is to be + added.(Default:None) - command = "useradd -m %s -d /home/%s" % (uname, uname) - ret, _, err = g.run(host, command) - if 'already exists' in err: - g.log.warn("User %s is already exists", uname) + Returns: + bool : True if user add is successful on all servers. + False otherwise. + """ + # Checking if group is given or not. + if not group: + cmd = "useradd -m %s -d /home/%s" % (username, username) else: - g.log.info("User %s is created successfully", uname) + cmd = "useradd -G %s %s" % (group, username) + + if servers != list: + servers = [servers] + + results = g.run_parallel(servers, cmd) + for server, ret_value in list(results.items()): + retcode, _, err = ret_value + if retcode != 0 and "already exists" not in err: + g.log.error("Unable to add user on %s", server) + return False return True def del_user(host, uname): """ - Delete user with home directory + Delete user with home directory + Args: host (str): hostname/ip of the system uname (str): username + Return always True """ command = "userdel -r %s" % (uname) @@ -987,3 +1004,104 @@ def del_user(host, uname): else: g.log.info("User %s successfully deleted", uname) return True + + +def group_add(servers, groupname): + """ + Creates a group in all the servers. + + Args: + servers(list|str): Nodes on which cmd is to be executed. + groupname(str): Name of the group to be created. + + Returns: + bool: True if add group is successful on all servers. + False otherwise. + + """ + if servers != list: + servers = [servers] + + cmd = "groupadd %s" % groupname + results = g.run_parallel(servers, cmd) + + for server, ret_value in list(results.items()): + retcode, _, err = ret_value + if retcode != 0 and "already exists" not in err: + g.log.error("Unable to add group %s on server %s", + groupname, server) + return False + return True + + +def ssh_keygen(mnode): + """ + Creates a pair of ssh private and public key if not present + + Args: + mnode (str): Node on which cmd is to be executed + Returns: + bool : True if ssh-keygen is successful on all servers. + False otherwise. It also returns True if ssh key + is already present + + """ + cmd = 'echo -e "n" | ssh-keygen -f ~/.ssh/id_rsa -q -N ""' + ret, out, _ = g.run(mnode, cmd) + if ret and "already exists" not in out: + return False + return True + + +def ssh_copy_id(mnode, tonode, passwd, username="root"): + """ + Copies the default ssh public key onto tonode's + authorized_keys file. + + Args: + mnode (str): Node on which cmd is to be executed + tonode (str): Node to which ssh key is to be copied + passwd (str): passwd of the user of tonode + Kwargs: + username (str): username of tonode(Default:root) + + Returns: + bool: True if ssh-copy-id is successful to tonode. + False otherwise. It also returns True if ssh key + is already present + + """ + cmd = ('sshpass -p "%s" ssh-copy-id -o StrictHostKeyChecking=no %s@%s' % + (passwd, username, tonode)) + ret, _, _ = g.run(mnode, cmd) + if ret: + return False + return True + + +def set_passwd(servers, username, passwd): + """ + Sets password for a given username. + + Args: + servers(list|str): list of nodes on which cmd is to be executed. + username(str): username of user for which password is to be set. + passwd(str): Password to be set. + + Returns: + bool : True if password set is successful on all servers. + False otherwise. + + """ + if servers != list: + servers = [servers] + cmd = "echo %s:%s | chpasswd" % (username, passwd) + results = g.run_parallel(servers, cmd) + + for server, ret_value in list(results.items()): + retcode, _, _ = ret_value + if retcode != 0: + g.log.error("Unable to set passwd for user %s on %s", + username, server) + return False + return True -- cgit