diff options
author | Avra Sengupta <asengupt@redhat.com> | 2015-08-05 13:49:41 +0530 |
---|---|---|
committer | Rajesh Joseph <rjoseph@redhat.com> | 2015-08-23 23:55:47 -0700 |
commit | 6cb78f8b7444ad7f216c976700c28a7d59f6b976 (patch) | |
tree | 4ecf105604172f93150d55850abfbdf997eedba7 | |
parent | 69942154e197d753580b74cc7f8e6bce470ee673 (diff) |
snapshot/scheduler: Check if volume exists before adding/editing schedules
Before adding or editing a scheduler, check if the volume name provided
in the schedule, exists in the cluster or not.
Added return code VOLUME_DOES_NOT_EXIST(17) for the same.
Change-Id: Ia3fe3cc1e1568ddd10f9193bbf40a098f0fe990a
BUG: 1213349
Signed-off-by: Avra Sengupta <asengupt@redhat.com>
Reviewed-on: http://review.gluster.org/11830
Tested-by: NetBSD Build System <jenkins@build.gluster.org>
Reviewed-by: mohammed rafi kc <rkavunga@redhat.com>
Reviewed-by: Rajesh Joseph <rjoseph@redhat.com>
-rwxr-xr-x | extras/snap_scheduler/snap_scheduler.py | 88 |
1 files changed, 67 insertions, 21 deletions
diff --git a/extras/snap_scheduler/snap_scheduler.py b/extras/snap_scheduler/snap_scheduler.py index f9cfc9ee766..af092e2c341 100755 --- a/extras/snap_scheduler/snap_scheduler.py +++ b/extras/snap_scheduler/snap_scheduler.py @@ -53,6 +53,7 @@ INVALID_JOBNAME = 13 INVALID_VOLNAME = 14 INVALID_SCHEDULE = 15 INVALID_ARG = 16 +VOLUME_DOES_NOT_EXIST = 17 def output(msg): print("%s: %s" % (SCRIPT_NAME, msg)) @@ -324,6 +325,34 @@ def update_current_scheduler(data): return ret +def isVolumePresent(volname): + success = False + if volname == "": + log.debug("No volname given") + return success + + cli = ["gluster", + "volume", + "info", + volname] + log.debug("Running command '%s'", " ".join(cli)) + + p = subprocess.Popen(cli, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = p.communicate() + rv = p.returncode + + log.debug("Command '%s' returned '%d'", " ".join(cli), rv) + + if rv: + log.error("Command output:") + log.error(err) + else: + success = True; + + return success + + def add_schedules(jobname, schedule, volname): log.info("Adding snapshot schedules.") ret = load_tasks_from_file() @@ -335,22 +364,31 @@ def add_schedules(jobname, schedule, volname): output(print_str) ret = JOB_ALREADY_EXISTS else: - tasks[jobname] = schedule + ":" + volname - ret = write_tasks_to_file() - if ret == 0: - # Create a LOCK_FILE for the job - job_lockfile = LOCK_FILE_DIR + jobname - try: - f = os.open(job_lockfile, os.O_CREAT | os.O_NONBLOCK, 0644) - os.close(f) - except OSError as (errno, strerror): - log.error("Failed to open %s. Error: %s.", - job_lockfile, strerror) - ret = INTERNAL_ERROR - return ret - log.info("Successfully added snapshot schedule %s" % jobname) - output("Successfully added snapshot schedule") - ret = 0 + if not isVolumePresent(volname): + print_str = ("Volume %s does not exist. Create %s and retry." % + (volname, volname)) + log.error(print_str) + output(print_str) + ret = VOLUME_DOES_NOT_EXIST + else: + tasks[jobname] = schedule + ":" + volname + ret = write_tasks_to_file() + if ret == 0: + # Create a LOCK_FILE for the job + job_lockfile = LOCK_FILE_DIR + jobname + try: + f = os.open(job_lockfile, os.O_CREAT | os.O_NONBLOCK, + 0644) + os.close(f) + except OSError as (errno, strerror): + log.error("Failed to open %s. Error: %s.", + job_lockfile, strerror) + ret = INTERNAL_ERROR + return ret + log.info("Successfully added snapshot schedule %s" % + jobname) + output("Successfully added snapshot schedule") + ret = 0 else: print_str = "Failed to add snapshot schedule. " \ "Error: Failed to load tasks from "+GCRON_ENABLED @@ -401,11 +439,19 @@ def edit_schedules(jobname, schedule, volname): ret = load_tasks_from_file() if ret == 0: if jobname in tasks: - tasks[jobname] = schedule+":"+volname - ret = write_tasks_to_file() - if ret == 0: - log.info("Successfully edited snapshot schedule %s" % jobname) - output("Successfully edited snapshot schedule") + if not isVolumePresent(volname): + print_str = ("Volume %s does not exist. Create %s and retry." % + (volname, volname)) + log.error(print_str) + output(print_str) + ret = VOLUME_DOES_NOT_EXIST + else: + tasks[jobname] = schedule+":"+volname + ret = write_tasks_to_file() + if ret == 0: + log.info("Successfully edited snapshot schedule %s" % + jobname) + output("Successfully edited snapshot schedule") else: print_str = ("Failed to edit %s. Error: No such " "job scheduled" % jobname) |