On 02/27/2013 07:54 AM, [email protected] wrote:
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)

I generally dislike usage of the commands API, considering we have APIs to do that in autotest, so what about converting all use of the command APIs to utils.run or utils.system?

+    if 'stopped' in output:
+        cmd = "service glusterd start"
+        (status, output) = commands.getstatusoutput(cmd)
+        if status:
+            logging.error(output)

Second remark: In virt-tests we try as much as possible to avoid the need of root, so compulsorily try to start the daemon should be avoided. I'd say checking for uid with os.getuid, and if not 0 (root), log message telling that the current credentials are not enough.

Now, the second reason why this might go wrong - the lack of the glusterfs-server package. I'd recommend a function to check for the presence of that package installed, before we blindly try to check service status or start services. You could use the software_manager API to check for the presence of the given package, and if it's not present, try to install it given the current credentials would allow so.

+
+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

It seems better to make the brick path data_dir.get_tmp_dir() instead of /tmp.

+
+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]

^ os.path.basename(image_name) works just as well as the code above

+    gluster_uri = create_gluster_uri(params)
+    image_filename = "%s%s.%s" % (gluster_uri, img_name, image_format)
+    return image_filename


_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel

Reply via email to