summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs/gluster/lib_utils.py
diff options
context:
space:
mode:
authorkshithijiyer <kshithij.ki@gmail.com>2019-08-23 12:13:07 +0530
committerBala Konda Reddy M <bmekala@redhat.com>2019-09-17 07:00:41 +0000
commite04dd165c33a21d2674f8084f824cf41a300c088 (patch)
tree0b3080dc726cf69dedb77e16b80884bf17ba776e /glustolibs-gluster/glustolibs/gluster/lib_utils.py
parentfee2d2d2b242c2423a8fa8c6f79db8d99082ac3e (diff)
[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 <kshithij.ki@gmail.com>
Diffstat (limited to 'glustolibs-gluster/glustolibs/gluster/lib_utils.py')
-rwxr-xr-xglustolibs-gluster/glustolibs/gluster/lib_utils.py142
1 files changed, 130 insertions, 12 deletions
diff --git a/glustolibs-gluster/glustolibs/gluster/lib_utils.py b/glustolibs-gluster/glustolibs/gluster/lib_utils.py
index 121efb8..6149817 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