From: Satheesh Rajendran <[email protected]>
Signed-off-by: Satheesh Rajendran <[email protected]> --- virttest/gluster.py | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 134 insertions(+), 0 deletions(-) create mode 100644 virttest/gluster.py diff --git a/virttest/gluster.py b/virttest/gluster.py new file mode 100644 index 0000000..0163f57 --- /dev/null +++ b/virttest/gluster.py @@ -0,0 +1,134 @@ +""" +GlusterFS Support +This file has the functions that helps +* To create/check gluster volume. +* To start/check gluster services. +* To create gluster uri which can be used as disk image file path. +""" +import logging, os, re, commands + + +def glusterd_start(): + """ + Check for glusterd status + """ + cmd = "service glusterd status" + (status, output) = commands.getstatusoutput(cmd) + if 'stopped' in output: + cmd = "service glusterd start" + (status, output) = commands.getstatusoutput(cmd) + if status: + logging.error(output) + + +def is_gluster_vol_started(vol_name): + """ + Returns if the volume is started, if not send false + """ + cmd = "gluster volume info %s" % vol_name + (status, vol_info) = commands.getstatusoutput(cmd) + if not status: + volume_status = re.findall(r'Status: (\S+)', vol_info) + if 'Started' in volume_status: + return True + else: + return False + else: + logging.error("Gluster volume info not available, %s", vol_info) + return False + + +def gluster_vol_start(vol_name): + """ + Starts the volume if it is stopped + """ + # Check if the volume is stopped, if then start + if not is_gluster_vol_started(vol_name): + cmd = "gluster volume start %s" % vol_name + (status, output) = commands.getstatusoutput(cmd) + if not status: + return True + else: + logging.error("Glsuter Volume Start %s", output) + return False + else: + return True + + +def is_gluster_vol_avail(vol_name): + """ + Returns if the volume already available + """ + cmd = "gluster volume info" + (status, output) = commands.getstatusoutput(cmd) + if not status: + volume_name = re.findall(r'Volume Name: (%s)\n' % vol_name, output) + if volume_name: + if not is_gluster_vol_started(vol_name): + if gluster_vol_start(vol_name): + return True + return True + else: + return False + else: + logging.error("Not able to check on volume info %s", output) + return False + + +def gluster_brick_create(brick_path): + """ + Creates brick + """ + if not os.path.isdir(brick_path): + try: + os.mkdir(brick_path) + return True + except OSError, details: + logging.error("Not able to create brick folder %s", details) + + +def gluster_vol_create(vol_name, hostname, brick_path): + """ + Gluster Volume Creation + """ + # Create a brick + gluster_brick_create(brick_path) + + cmd = "gluster volume create %s %s:/%s" % (vol_name, hostname, + brick_path) + (status, output) = commands.getstatusoutput(cmd) + if not status: + return True + else: + logging.error("Gluster Volume Creation %s", output) + return False + + +def create_gluster_uri(params): + """ + Find/create gluster volume + """ + vol_name = params.get("gluster_volume_name") + brick_path = params.get("gluster_brick") + hostname = commands.getoutput("hostname") + + # Start the gluster dameon, if not started + glusterd_start() + # Check for the volume is already present, if not create one. + if not is_gluster_vol_avail(vol_name): + gluster_vol_create(vol_name, hostname, brick_path) + + # Building gluster uri + gluster_uri = "gluster://%s:0/%s/" % (hostname, vol_name) + return gluster_uri + + +def get_image_filename(params, image_name, image_format): + """ + Form the image file name using gluster uri + """ + + img_name = image_name.split('/')[-1] + gluster_uri = create_gluster_uri(params) + image_filename = "%s%s.%s" % (gluster_uri, img_name, image_format) + return image_filename -- 1.7.1 _______________________________________________ Virt-test-devel mailing list [email protected] https://www.redhat.com/mailman/listinfo/virt-test-devel
