check whether vm image exists before running it

This commit is contained in:
ahmadbilalkhalid 2019-07-20 20:06:55 +05:00
parent aaa733d16f
commit a0a30f46ae

27
main.py
View file

@ -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: