From a0a30f46aebce96609d551ccc9fd8d1356bbe502 Mon Sep 17 00:00:00 2001 From: Ahmad Bilal Khalid Date: Sat, 20 Jul 2019 20:06:55 +0500 Subject: [PATCH] check whether vm image exists before running it --- main.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index fe69e11..2c0086f 100644 --- a/main.py +++ b/main.py @@ -33,6 +33,20 @@ class VM: vm: qmp.QEMUMachine +class RBD(object): + @staticmethod + def ls(pool): + output = "" + try: + output = subprocess.check_output( + ["rbd", "ls", pool], stderr=subprocess.PIPE + ).decode("utf-8").strip() + except subprocess.CalledProcessError as e: + raise Exception(e.stderr) + return output.split("\n") + + + logging.basicConfig( level=logging.DEBUG, filename="log.txt", @@ -99,12 +113,13 @@ def start_vm(vm_path, e): # uservms pool then checkwhether the e.key.split("/").pop() # exists in rbd_ls(uservms_pool) - # if not os.path.isfile(vm_path): - # logging.info(f"Image file of vm {e.key} does not exists") - # logging.info(f"Setting vm {e.key} status to DELETED") - # e.value["status"] = "DELETED" - # client.put(e.key, json.dumps(e.value)) - # return + if not vm_path.split("/")[-1] in RBD.ls("uservms") + logging.info(f"Image file of vm {e.key} does not exists") + logging.info(f"Setting vm {e.key} status to DELETED") + e.value["status"] = "DELETED" + client.put(e.key, json.dumps(e.value)) + return + _vm = get_vm(running_vms, e.key) if _vm: