From 070ab84efa4904c254b8e21861916c00d9b4c3d1 Mon Sep 17 00:00:00 2001 From: Stratos Psomadakis Date: Thu, 29 Mar 2012 17:34:59 +0300 Subject: [PATCH] Initial import of the ganeti extstorage vlmc scripts --- xseg/tools/ext_scripts/attach | 126 ++++++++++++++++++++++++++++++++ xseg/tools/ext_scripts/create | 91 +++++++++++++++++++++++ xseg/tools/ext_scripts/detach | 107 +++++++++++++++++++++++++++ xseg/tools/ext_scripts/grow | 84 +++++++++++++++++++++ xseg/tools/ext_scripts/parameters.list | 1 + xseg/tools/ext_scripts/remove | 76 +++++++++++++++++++ xseg/tools/ext_scripts/verify | 3 + 7 files changed, 488 insertions(+) create mode 100755 xseg/tools/ext_scripts/attach create mode 100755 xseg/tools/ext_scripts/create create mode 100755 xseg/tools/ext_scripts/detach create mode 100755 xseg/tools/ext_scripts/grow create mode 100644 xseg/tools/ext_scripts/parameters.list create mode 100755 xseg/tools/ext_scripts/remove create mode 100755 xseg/tools/ext_scripts/verify diff --git a/xseg/tools/ext_scripts/attach b/xseg/tools/ext_scripts/attach new file mode 100755 index 0000000..a916011 --- /dev/null +++ b/xseg/tools/ext_scripts/attach @@ -0,0 +1,126 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011 Greek Research and Technology Network +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +"""Map an existing rbd Image to a block device + +This program maps an existing rbd Image to a block device +e.g. /dev/rbd{X} and returns the device path. If the mapping +already exists, it returns the corresponding device path. + +It takes it's input from environment variables. Specifically the +following variables should be present: + + - VOL_NAME: The name of the Image to map + - POOL: The RADOS pool in which the Image resides + +Returns the block device path (which maps the Image) as a string +upon success, or 1 upon failure + +""" + +import os +import sys +import re + +from ganeti import utils + + +def ReadEnv(): + """Read the mandatory enviromental variables + + """ + name = os.getenv("VOL_NAME") + if name is None: + sys.stderr.write('The environment variable VOL_NAME is missing.\n') + return None +# pool = os.getenv("POOL") + pool = "rbd" + if pool is None: + sys.stderr.write('The environment variable POOL is missing.\n') + return None + + return (name, pool) + + +def main(): + env = ReadEnv() + if env is None: + sys.stderr.write('Wrong environment. Aborting...\n') + return 1 + + name, pool = env + + # Check if the mapping already exists + cmd1 = ["/root/archip/xseg/tools/vlmc", "showmapped"] + result = utils.RunCmd(cmd1) + if result.failed: + sys.stderr.write("rbd showmapped failed (%s): %s" % + (result.fail_reason, result.output)) + return 1 + else: + cmd2 = "echo '%s' | grep %s" % (result.output, name) + result = utils.RunCmd(cmd2) + if not result.failed: + # The mapping already exists. + # Parse the result and return the rbd device + try: + rbd_dev = re.search("(/dev/xsegbd\d+)", result.output).group(1) + except: + sys.stderr.write("The rbd mapping exists but couldn't parse " + "the result of rbd showmapped to find the " + "corresponding rbd block device (/dev/rbd*)") + return 1 + + #return str(rbd_dev) + sys.stdout.write("%s" % str(rbd_dev)) + sys.exit(0) + + else: + # The mapping doesn't exist. Create it + cmd = ["/root/archip/xseg/tools/vlmc", "map", "-p", "%s" % pool, "%s" % name] + result = utils.RunCmd(cmd) + if result.failed: + sys.stderr.write("rbd map failed (%s): %s" % + (result.fail_reason, result.output)) + return 1 + # Use rbd showmapped again to find the rbd device + # the image was mapped to + cmd3 = "/root/archip/xseg/tools/vlmc showmapped | grep %s" % name + result = utils.RunCmd(cmd3) + if result.failed: + sys.stderr.write("Can't find mapped device. " + "rbd showmapped failed (%s): %s" % + (result.fail_reason, result.output)) + return 1 + try: + rbd_dev = re.search("(/dev/xsegbd\d+)", result.output).group(1) + except: + sys.stderr.write("The rbd mapping was created succesfully, but " + "couldn't parse the result of rbd showmapped to " + "find the corresponding rbd block device " + "(/dev/rbd*)") + return 1 + + # The device was successfully mapped. Return it + #return str(rbd_dev) + sys.stdout.write("%s" % str(rbd_dev)) + sys.exit(0) + +if __name__ == "__main__": + sys.exit(main()) diff --git a/xseg/tools/ext_scripts/create b/xseg/tools/ext_scripts/create new file mode 100755 index 0000000..080586b --- /dev/null +++ b/xseg/tools/ext_scripts/create @@ -0,0 +1,91 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011 Greek Research and Technology Network +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +"""Create a new rbd Image inside a RADOS cluster + +This program creates a new rbd Image file inside a RADOS cluster. +It takes it's input from environment variables. Specifically the +following variables should be present: + + - VOL_NAME: The name of the new Image file + - POOL: The RADOS pool in which to put the Image + - VOL_SIZE: The size of the new Image (in megabytes) + +Returns O after successfull creation, 1 on failure + +""" + +import os +import sys + +from ganeti import utils + + +def ReadEnv(): + """Read the mandatory enviromental variables + + """ + name = os.getenv("VOL_NAME") + if name is None: + sys.stderr.write('The environment variable VOL_NAME is missing.\n') + sys.exit(1) +# pool = os.getenv("POOL") + pool = "rbd" + if pool is None: + sys.stderr.write('The environment variable POOL is missing.\n') + sys.exit(1) + size = os.getenv("VOL_SIZE") + if size is None: + sys.stderr.write('The environment variable VOL_SIZE is missing.\n') + sys.exit(1) + test_param = os.getenv("EXTP_TEST") + if test_param: + sys.stderr.write('parameter test = %s\n' % test_param) + origin = os.getenv("EXTP_ORIGIN") + + return (name, pool, size, origin) + + +def main(): + sys.stderr.write('Creation started...\n') + env = ReadEnv() + if env is None: + sys.stderr.write('Wrong environment. Aborting...\n') + sys.exit(1) + + rbd_name, rbd_pool, size, origin = env + + sys.stderr.write('name: %s, pool: %s, size: %s\n' % (rbd_name, rbd_pool, size)) + cmd = ["/root/archip/xseg/tools/vlmc", "create", "-p", "%s" % rbd_pool, + "%s" % rbd_name, "--size", "%s" % size] + if origin: + cmd.extend(["--snap", origin]) + sys.stderr.write('Before RunCmd') + result = utils.RunCmd(cmd) + sys.stderr.write('After RunCmd') + if result.failed: + sys.stderr.write('rbd creation failed (%s): %s\n' % + (result.fail_reason, result.output)) + sys.exit(1) + + sys.exit(0) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/xseg/tools/ext_scripts/detach b/xseg/tools/ext_scripts/detach new file mode 100755 index 0000000..98a5656 --- /dev/null +++ b/xseg/tools/ext_scripts/detach @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011 Greek Research and Technology Network +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +"""Unmap an rbd device from the Image it is mapped to + +This program unmaps an rbd device from the Image it is mapped to. +It is idempotent if the mapping doesn't exist at all. + +It takes it's input from environment variables. Specifically the +following variables should be present: + + - VOL_NAME: The name of the Image file to which the device points + - POOL: The RADOS pool in which the Image resides + +Returns O upon successfull unmapping, 1 upon failure + +""" + +import os +import sys +import re + +from ganeti import utils + + +def ReadEnv(): + """Read the mandatory enviromental variables + + """ + name = os.getenv("VOL_NAME") + if name is None: + sys.stderr.write('The environment variable VOL_NAME is missing.\n') + return None +# pool = os.getenv("POOL") + pool = "rbd" + if pool is None: + sys.stderr.write('The environment variable POOL is missing.\n') + return None + + return (name, pool) + + +def main(): + env = ReadEnv() + if env is None: + sys.stderr.write('Wrong environment. Aborting...\n') + return 1 + + name, pool = env + + # Check if the mapping already exists + cmd1 = ["/root/archip/xseg/tools/vlmc", "showmapped"] + result = utils.RunCmd(cmd1) + if result.failed: + sys.stderr.write("/root/archip/xseg/tools/vlmc showmapped failed (%s): %s" % + (result.fail_reason, result.output)) + return 1 + else: + cmd2 = "echo '%s' | grep %s" % (result.output, name) + result = utils.RunCmd(cmd2) + if not result.failed: + # The mapping already exists. + # Parse the result to find the rbd device + try: + rbd_dev = re.search("(/dev/xsegbd\d+)", result.output).group(1) + except: + sys.stderr.write("The rbd mapping exists but couldn't parse " + "the result of rbd showmapped to find the " + "corresponding rbd block device (/dev/rbd*)") + return 1 + + # Unmap the rbd device + cmd = ["/root/archip/xseg/tools/vlmc", "unmap", "%s" % rbd_dev] + result = utils.RunCmd(cmd) + if result.failed: + sys.stderr.write("rbd unmap failed (%s): %s" % + (result.fail_reason, result.output)) + return 1 + + # Succesfull unmapping + return 0 + + else: + # The mapping doesn't exist. Do nothing + # ! Here there is a chance that echo or grep actually failed. + # TODO: Zero that chance + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/xseg/tools/ext_scripts/grow b/xseg/tools/ext_scripts/grow new file mode 100755 index 0000000..2f3e479 --- /dev/null +++ b/xseg/tools/ext_scripts/grow @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011 Greek Research and Technology Network +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +"""Grow an existing rbd Image + +This program grows an existing rbd Image which resides in a +RADOS cluster + +It takes it's input from environment variables. Specifically the +following variables should be present: + + - VOL_NAME: The name of the Image to grow + - POOL: The RADOS pool in which the Image resides + - VOL_SIZE: The current Image's size + - VOL_NEW_SIZE: The new size of the Image after it's grown + +Returns 0 upon successful grow, 1 upon failure + +""" + +import os +import sys + +from ganeti import utils + + +def ReadEnv(): + """Read the mandatory enviromental variables + + """ + name = os.getenv("VOL_NAME") + if name is None: + sys.stderr.write('The environment variable VOL_NAME is missing.\n') + return None +# pool = os.getenv("POOL") + pool = "rbd" + if pool is None: + sys.stderr.write('The environment variable POOL is missing.\n') + return None + new_size = os.getenv("VOL_NEW_SIZE") + if new_size is None: + sys.stderr.write('The environment variable VOL_NEW_SIZE is missing.\n') + return None + + return (name, pool, new_size) + + +def main(): + env = ReadEnv() + if env is None: + sys.stderr.write('Wrong environment. Aborting...\n') + return 1 + + rbd_name, rbd_pool, new_size = env + + cmd = ["/root/archip/xseg/tools/vlmc", "resize", "-p", "%s" % rbd_pool, "%s" % rbd_name, + "--size", "%s" % new_size] + result = utils.RunCmd(cmd) + if result.failed: + sys.stderr.write('rbd resize failed (%s): %s\n' % + (result.fail_reason, result.output)) + return 1 + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/xseg/tools/ext_scripts/parameters.list b/xseg/tools/ext_scripts/parameters.list new file mode 100644 index 0000000..156e737 --- /dev/null +++ b/xseg/tools/ext_scripts/parameters.list @@ -0,0 +1 @@ +origin Snapshot to create the image from diff --git a/xseg/tools/ext_scripts/remove b/xseg/tools/ext_scripts/remove new file mode 100755 index 0000000..bc32e33 --- /dev/null +++ b/xseg/tools/ext_scripts/remove @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# +# Copyright (C) 2011 Greek Research and Technology Network +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +"""Remove an existing rbd Image from a RADOS cluster + +This program removes an rbd Image file from a RADOS cluster. +It takes it's input from environment variables. Specifically the +following variables should be present: + + - VOL_NAME: The name of the Image file to remove + - POOL: The RADOS pool in which the Image resides + +Returns O upon successfull removal, 1 upon failure + +""" + +import os +import sys + +from ganeti import utils + + +def ReadEnv(): + """Read the mandatory enviromental variables + + """ + name = os.getenv("VOL_NAME") + if name is None: + sys.stderr.write('The environment variable VOL_NAME is missing.\n') + return None +# pool = os.getenv("POOL") + pool = "rbd" + if pool is None: + sys.stderr.write('The environment variable POOL is missing.\n') + return None + + return (name, pool) + + +def main(): + env = ReadEnv() + if env is None: + sys.stderr.write('Wrong environment. Aborting...\n') + return 1 + + rbd_name, rbd_pool = env + + cmd = ["/root/archip/xseg/tools/vlmc", "rm", "-p", "%s" % rbd_pool, "%s" % rbd_name] + result = utils.RunCmd(cmd) + if result.failed: + sys.stderr.write("Can't remove Image %s from cluster with rbd rm: " + "%s - %s" % + (rbd_name, result.fail_reason, result.output)) + return 1 + + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/xseg/tools/ext_scripts/verify b/xseg/tools/ext_scripts/verify new file mode 100755 index 0000000..625db0a --- /dev/null +++ b/xseg/tools/ext_scripts/verify @@ -0,0 +1,3 @@ +!#/bin/bash + +exit 0 -- 1.7.10.4