summaryrefslogtreecommitdiffstats
path: root/glustolibs-gluster/glustolibs
diff options
context:
space:
mode:
authorkshithijiyer <kshithij.ki@gmail.com>2019-09-17 15:34:24 +0530
committerkshithijiyer <kshithij.ki@gmail.com>2019-09-17 15:34:24 +0530
commitadb3806f14c32b805a7726ff1b92ffdfbee9c707 (patch)
tree23a687d4ab1daa54a5f271b75f5a4ad76b417bda /glustolibs-gluster/glustolibs
parentea4bdc24ee668fbfca727f8d827e3df00e1f7838 (diff)
[lib] Merging functions to make generic library.
At present there are 2 different functions for prerequisites and create session merging them and adding checks to make the functions more effective when called from a conftest.py file. By doing so we also avoid the possibility of running the same function twice which might lead to problems. For example when commands like: $ gluster system:: execute gsec_create it would make 2 entries in authorized keys files leading to workers getting into faulty state. Note: This patch should only be merged after merging the below mentioned patches: [1] https://review.gluster.org/#/c/glusto-tests/+/23291/ [2] https://review.gluster.org/#/c/glusto-tests/+/23293/ Change-Id: I3f8cda7b589c86de4dab8c66d033b364dd495b9b Signed-off-by: kshithijiyer <kshithij.ki@gmail.com>
Diffstat (limited to 'glustolibs-gluster/glustolibs')
-rw-r--r--glustolibs-gluster/glustolibs/gluster/geo_rep_libs.py365
1 files changed, 181 insertions, 184 deletions
diff --git a/glustolibs-gluster/glustolibs/gluster/geo_rep_libs.py b/glustolibs-gluster/glustolibs/gluster/geo_rep_libs.py
index 2e516cd..17be5e7 100644
--- a/glustolibs-gluster/glustolibs/gluster/geo_rep_libs.py
+++ b/glustolibs-gluster/glustolibs/gluster/geo_rep_libs.py
@@ -15,237 +15,234 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
- Description: Library for gluster geo-replication operations
+ Description: Library for gluster geo-replication operations.
"""
+from time import sleep
from glusto.core import Glusto as g
from glustolibs.gluster.gluster_init import restart_glusterd
+from glustolibs.gluster.peer_ops import is_peer_connected
from glustolibs.gluster.geo_rep_ops import (georep_mountbroker_setup,
georep_mountbroker_add_user,
- georep_mountbroker_status,
georep_create_pem,
georep_create,
georep_set_pem_keys,
georep_config_set)
-from glustolibs.gluster.shared_storage_ops import enable_shared_storage
+from glustolibs.gluster.shared_storage_ops import (enable_shared_storage,
+ is_shared_volume_mounted,
+ check_gluster_shared_volume)
from glustolibs.gluster.lib_utils import (group_add, ssh_copy_id,
ssh_keygen, add_user, set_passwd)
+from glustolibs.gluster.glusterdir import get_dir_contents
-def georep_root_prerequisites(mnode, snode, user, passwd):
- """Takes care of the pre-requisites required for
- setting up a successful geo-rep
- session which includes:
- 1. Check if shared_storage has been successfully created
- 2. Check if shared_storage has been mounted
- 3. Generating the common pem pub file on all the nodes
- on the master
- 4. Establishing passwordless ssh connection to the slave
- Args:
- mnode (str) : The primary master node where the commands are executed
- snode (str) : The primary slave node where the commande are executed
- user (str) : User to be used to setup the geo-rep session
- passwd (str): Password of the specified user.
- Returns:
- bool : True if all the steps are successful, false if there are
- any failures in the middle
-
+def georep_prerequisites(mnode, snode, passwd, user="root", group=None,
+ mntbroker_dir="/var/mountbroker-root"):
"""
- g.log.debug("Enable shared-storage")
- ret = enable_shared_storage(mnode)
- if not ret:
- g.log.error("Failed to create gluster shared storage on "
- "the master cluster")
- return False
-
- g.log.debug("Creating a common pem pub file on all the nodes in "
- "the master to establish passwordless ssh connection "
- "to the slave ")
- ret = ssh_keygen(mnode)
- if not ret:
- g.log.error("Failed to create a common pem pub file")
- return False
+ Sets up all the prerequisites for geo-rep.
- g.log.debug("Establishing passwordless ssh between master and slave")
- ret = ssh_copy_id(mnode, snode, user, passwd)
- if not ret:
- g.log.error("Failed to establish ssh connection")
- return False
-
- g.log.info("Shared storage has been created. "
- "Passwordless ssh between the master and the "
- "slave has been successful!")
- return True
-
-
-def georep_create_root_session(mnode, snode, mastervol, slavevol,
- user=None, force=False):
- """ Create a geo-replication session between the master and
- the slave
Args:
- mnode (str) : The primary master node where the commands are executed
- snode (str) : The primary slave node where the commande are executed
- mastervol (str) : The name of the master volume
- slavevol (str): The name of the slave volume
+ mnode(str): The primary master node where the commands are executed
+ snode(str|list): slave nodes on which setup has to be completed.
+ passwd(str): Password of the specified user.
+
Kwargs:
- user (str): Since it's a root session, user is root or None
- force (bool) : Set to true if session needs to be created with force
- else it remains false as the default option
+ user(str): User to be used to setup the geo-rep session.
+ (Default: root)
+ mntbroker_dir(str): Mountbroker mount directory.
+ (Default: /var/mountbroker-root)
+ group(str): Group under which geo-rep useraccount is setup.
+ (Default: None)
Returns:
bool : True if all the steps are successful, false if there are
any failures in the middle
- """
- g.log.debug("Creating a common pem file on %s", mnode)
- ret, _, _ = georep_create_pem(mnode)
- if not ret:
- g.log.error("Failed to create a common pem file on all the nodes "
- "belonging to the cluster %s ", mnode)
- return False
- g.log.debug("Create geo-rep session from %s to %s", mnode, snode)
- ret, _, _ = georep_create(mnode, mastervol, snode,
- slavevol, user, force)
- if not ret:
- g.log.error("Failed to create geo-rep session")
- return False
+ """
+ # Converting snode to list if string.
+ if snode != list:
+ snode = [snode]
- g.log.debug("Setting up meta-volume on %s", mnode)
- ret, _, _ = georep_config_set(mnode, mastervol, snode, slavevol,
- "use_meta_volume", "True")
+ # Checking and enabling shared storage on master cluster.
+ ret = is_shared_volume_mounted(mnode)
if not ret:
- g.log.error("Failed to set up meta-volume")
- return False
-
- g.log.info("Pem file has been created and the keys have "
- "been pushed to all the slave nodes. The meta-volume "
- "has been successfully configured as well ")
- return True
+ ret = enable_shared_storage(mnode)
+ if not ret:
+ g.log.error("Failed to set cluster"
+ ".enable-shared-storage to enable.")
+ return False
-def georep_nonroot_prerequisites(mnode, snodes, group, user, mntbroker_dir,
- slavevol):
- """ Setup pre-requisites for mountbroker setup
+ # Check volume list to confirm gluster_shared_storage is created
+ ret = check_gluster_shared_volume(mnode)
+ if not ret:
+ g.log.error("gluster_shared_storage volume not"
+ " created even after enabling it.")
+ return False
- Args:
- mnode (str) : Master node on which cmd is to be executed
- snodes (list): List of slave nodes
- group (str): Specifies a group name
- user (str): Specifies a user name
- mntbroker_dir: Mountbroker mount directory
- slavevol (str) The name of the slave volume
- Returns:
- bool: True if all pre-requisite are successful else False
+ # Running prerequisites for non-root user.
+ if user != "root" and group is not None:
- """
- g.log.debug("Enable shared-storage")
- ret = enable_shared_storage(mnode)
- if ret:
- g.log.error("Failed to enable shared storage on %s", mnode)
- return False
+ if len(snode) < 2:
+ g.log.error("A list of all slave nodes is needed for non-root"
+ " setup as every slave node will have a non-root"
+ " user.")
+ return False
- g.log.debug("Create new group: %s on all slave nodes", group)
- if not group_add(snodes, group):
- g.log.error("Creating group: %s on all slave nodes failed", group)
- return False
+ # Creating a group on all slave nodes.
+ ret = group_add(snode, group)
+ if not ret:
+ g.log.error("Creating group: %s on all slave nodes failed.", group)
+ return False
- g.log.debug("Create user: %s in group: %s on all slave nodes", user, group)
- if not add_user(snodes, user, group):
- g.log.error("Creating user: %s in group: %s on all slave nodes "
- "failed", user, group)
- return False
+ # Creating a non-root user on all the nodes.
+ ret = add_user(snode, user, group)
+ if not ret:
+ g.log.error("Creating user: %s in group: %s on all slave nodes "
+ "failed,", user, group)
+ return False
- g.log.debug("Set passwd for user account on slave")
- if not set_passwd(snodes, group, user, "geopasswd"):
- g.log.error("Setting password failed on slaves")
- return False
+ # Setting password for user on all the nodes.
+ ret = set_passwd(snode, user, passwd)
+ if not ret:
+ g.log.error("Setting password failed on slaves")
+ return False
- g.log.debug("Setting up mount broker root directory: %s node: %s",
- mntbroker_dir, snodes[0])
- ret, _, _ = georep_mountbroker_setup(snodes[0], group, mntbroker_dir)
- if ret:
- g.log.error("Setting up of mount broker directory failed: %s node: %s",
- mntbroker_dir, snodes[0])
- return False
+ # Setting up mount broker on first slave node.
+ ret, _, _ = georep_mountbroker_setup(snode[0], group, mntbroker_dir)
+ if ret:
+ g.log.error("Setting up of mount broker directory failed"
+ " on node: %s", snode[0])
+ return False
- g.log.debug("Add volume: %s and user: %s to mountbroker service",
- slavevol, user)
- ret, _, _ = georep_mountbroker_add_user(snodes[0], slavevol, user)
- if ret:
- g.log.error("Add volume: %s and user: %s to mountbroker "
- "service failed", slavevol, user)
- return False
+ # Checking if ssh keys are present.
+ ret = get_dir_contents(mnode, "/root/.ssh/")
+ ssh_keys = ["id_rsa", "id_rsa.pub"]
+ if ssh_keys not in ret:
+ ret = ssh_keygen(mnode)
+ if not ret:
+ g.log.error("Failed to create a common pem pub file.")
+ return False
- g.log.debug("Checking mountbroker status")
- ret, out, _ = georep_mountbroker_status(snodes[0])
+ # Setting up passwordless ssh to primary slave node.
+ ret = ssh_copy_id(mnode, snode[0], passwd, user)
if not ret:
- if "not ok" in out:
- g.log.error("Mountbroker status not ok")
+ g.log.error("Failed to setup passwordless ssh.")
+ return False
+
+ # Checking if pem files else running gsec_create.
+ ret = get_dir_contents(mnode, "/var/lib/glusterd/geo-replication/")
+ list_of_pem_files = [
+ "common_secret.pem.pub", "secret.pem",
+ "tar_ssh.pem", "gsyncd_template.conf",
+ "secret.pem.pub", "tar_ssh.pem.pub"
+ ]
+ if ret != list_of_pem_files:
+ ret, _, _ = georep_create_pem(mnode)
+ if ret:
+ g.log.error("Failed exeucte gluster system:: execute gsec_create.")
return False
- else:
- g.log.error("Mountbroker status command failed")
- return False
+ return True
- g.log.debug("Restart glusterd on all slave nodes")
- if not restart_glusterd(snodes):
- g.log.error("Restarting glusterd failed")
- return False
- g.log.debug("Setup passwordless SSH between %s and %s", mnode, snodes[0])
- if not ssh_keygen(mnode):
- g.log.error("ssh keygen is failed on %s", mnode)
- return False
+def georep_create_session(mnode, snode, mastervol, slavevol,
+ user="root", force=False):
+ """ Create a geo-replication session between the master and
+ the slave.
+ Args:
+ mnode(str): The primary master node where the commands are executed
+ snode(str|list): slave node where the commande are executed
+ mastervol(str): The name of the master volume
+ slavevol(str): The name of the slave volume
+ Kwargs:
+ user (str): User to be used to create geo-rep session.(Default: root)
+ force (bool) : Set to true if session needs to be created with force
+ else it remains false as the default option
- if not ssh_copy_id(mnode, snodes[0], user, "geopasswd"):
- g.log.error("ssh copy-id is failed from %s to %s", mnode, snodes[0])
- return False
+ Returns:
+ bool : True if all the steps are successful, false if there are
+ any failures in the middle
+ """
+ # Converting snode to list if string.
+ if snode != list:
+ snode = [snode]
+
+ # Checking for blank username.
+ if user in ["", " "]:
+ g.log.error("Blank username isn't possible.")
+ return False
+
+ # Setting up root geo-rep session.
+ if user == "root":
+ g.log.debug("Creating root geo-rep session.")
+ ret, _, _ = georep_create(mnode, mastervol, snode[0],
+ slavevol, user, force)
+ if ret:
+ g.log.error("Failed to create geo-rep session")
+ return False
- return True
+ g.log.debug("Setting up meta-volume on %s", mnode)
+ ret, _, _ = georep_config_set(mnode, mastervol, snode[0],
+ slavevol, "use_meta_volume", "True")
+ if ret:
+ g.log.error("Failed to set up meta-volume")
+ return False
+ return True
+ # Setting up non-root geo-rep session.
+ else:
+ # Glusterd has to be restarted on all the slave nodes.
+ if len(snode) < 2:
+ g.log.error("A list of all slave nodes is needed for non-root"
+ " session to restart glusterd on all slaves after"
+ " adding it to mountbroker.")
+ return False
-def georep_create_nonroot_session(mnode, mastervol, snode, slavevol, user,
- force=False):
- """ Create mountbroker/non-root geo-rep session
+ # Adding volume to mountbroker.
+ g.log.debug("Creating a non-root geo-rep session.")
+ ret, _, _ = georep_mountbroker_add_user(snode[0], slavevol, user)
+ if ret:
+ g.log.error("Failed to setup mountbroker.")
+ return False
- Args:
- mnode (str) : Master node for session creation
- mastervol (str) The name of the master volume
- snode (str): Slave node for session creation
- slavevol (str) The name of the slave volume
- user (str): Specifies a user name
- Kwargs:
- force (bool): if master and slave are not of similar
- size
- Returns:
- bool: True if geo-rep session is created successfully
- Else False
+ # Restarting glusterd on all nodes.
+ ret = restart_glusterd(snode)
+ if not ret:
+ g.log.error("Restarting glusterd failed.")
+ return False
- """
+ # Checking if peers are in connected state or not.
+ ret = is_peer_connected(snode[0], snode)
+ if not ret:
- g.log.debug("Create geo-rep pem keys")
- ret, _, _ = georep_create_pem(mnode)
- if ret:
- g.log.error("Failed to create pem keys")
- return False
+ counter = 20
+ while counter > 0:
- g.log.debug("Create geo-rep session")
- ret, _, _ = georep_create(mnode, mastervol, snode, slavevol,
- user, force)
- if ret:
- g.log.error("Failed to create geo-rep session")
- return False
+ ret = is_peer_connected(snode[0], snode)
+ if ret:
+ break
+ sleep(3)
+ counter += 1
- g.log.debug("Copy geo-rep pem keys onto all slave nodes")
- ret, _, _ = georep_set_pem_keys(snode, user, mastervol, slavevol)
- if ret:
- g.log.error("Failed to copy geo-rep pem keys onto all slave nodes")
- return False
+ # Creating a geo-rep session.
+ ret, _, _ = georep_create(mnode, mastervol, snode[0], slavevol,
+ user, force)
+ if ret:
+ g.log.error("Failed to create geo-rep session")
+ return False
- g.log.debug("Enable meta-volume")
- ret, _, _ = georep_config_set(mnode, mastervol, snode, slavevol,
- "use_meta_volume", "true")
- if ret:
- g.log.error("Failed to set meta-volume")
- return False
+ # Setting up pem keys between master and slave node.
+ g.log.debug("Copy geo-rep pem keys onto all slave nodes.")
+ ret, _, _ = georep_set_pem_keys(snode[0], user, mastervol, slavevol)
+ if ret:
+ g.log.error("Failed to copy geo-rep pem keys onto all slave nodes")
+ return False
- return True
+ # Setting use_meta_volume to true.
+ g.log.debug("Enabling meta-volume for master volume.")
+ ret, _, _ = georep_config_set(mnode, mastervol, snode[0], slavevol,
+ "use_meta_volume", "true")
+ if ret:
+ g.log.error("Failed to set meta-volume")
+ return False
+ return True