From 70ec3f832b5cf74a0713e128f9c9cf370f64f196 Mon Sep 17 00:00:00 2001
From: Ahmed Bilal Khalid <ahmedbilal96@gmail.com>
Date: Tue, 30 Jul 2019 20:05:19 +0500
Subject: [PATCH] Small changes here and there. Typo fix, little bit
 refactoring

---
 main.py  | 32 +++++++++++++++-----------------
 setup.py | 14 ++++++++++++++
 2 files changed, 29 insertions(+), 17 deletions(-)
 create mode 100644 setup.py

diff --git a/main.py b/main.py
index aba2562..4f0af69 100644
--- a/main.py
+++ b/main.py
@@ -36,6 +36,7 @@ logging.basicConfig(
     datefmt="%d-%b-%y %H:%M:%S",
 )
 
+
 @dataclass
 class VM:
     key: str
@@ -75,13 +76,11 @@ def need_running_vm(func):
 
 
 def create_vm(vm):
-    image = client.get(
-        f"/v1/image/{vm.value['image_uuid']}", value_in_json=True
-    )
+    image = client.get(f"/v1/image/{vm.value['image_uuid']}", value_in_json=True)
     if image:
         logging.debug(image)
-
         logging.info("Creating New VM...")
+
         _command_to_create = f"rbd clone images/{vm.image_uuid}@protected uservms/{vm.uuid}"
         try:
             subprocess.call(_command_to_create.split(" "))
@@ -89,16 +88,20 @@ def create_vm(vm):
             VM_POOL.put(vm)
         except:
             logging.exception("Can't clone image")
+    else:
+        logging.info(f"Image not found for {vm.image_uuid}")
 
 
-def start_vm(vm_path, e):
+def start_vm(e):
+    vm_path = f"rbd:uservms/{e.uuid}"
+
     try:
         user_vms = RBD.ls("uservms")
     except:
         logging.info("Can't access uservms pool")
         return
 
-    if not vm_path.split("/")[-1] in user_vms:
+    if e.uuid not in user_vms:
         logging.info(f"Image file of vm {e.key} does not exists")
         logging.info(f"Deleting vm {e.key}")
         client.client.delete(e.key)
@@ -181,7 +184,7 @@ def shutdown_vm(e):
 
 
 def delete_vm(e):
-    # FIXME: Implementation Obseleted after CEPH Integeration
+    # TODO: Delete VM Image From CEPH
     logging.info(f"Deleting VM {e.key}")
     shutdown_vm(e)
     client.client.delete(e.key)
@@ -191,7 +194,7 @@ def get_vm(vm_list: list, vm_key) -> Union[VM, None]:
     return next((vm for vm in vm_list if vm.key == vm_key), None)
 
 
-def maintenence(host):
+def maintenance(host):
     _vms = VM_POOL.by_host(host.key)
     alleged_running_vms = VM_POOL.by_status("RUNNING", _vms)
 
@@ -242,13 +245,12 @@ def main():
 
             if e.status == "TIMEOUT":
                 logging.info("Timeout")
-                maintenence(host)
+                maintenance(host)
                 continue
 
+            # TODO: Re-evaluate Migration Design
             if hasattr(e, "migration_destination"):
-                e_migration_destination = e.value[
-                    "migration_destination"
-                ]
+                e_migration_destination = e.value["migration_destination"]
             else:
                 e_migration_destination = ""
 
@@ -267,8 +269,7 @@ def main():
                     resume_vm(e)
 
                 elif e.status == "REQUESTED_START":
-                    vm_path = f"rbd:uservms/{e.uuid}"
-                    start_vm(vm_path, e)
+                    start_vm(e)
 
                 elif e.status == "REQUESTED_SHUTDOWN":
                     shutdown_vm(e)
@@ -276,9 +277,6 @@ def main():
                 elif e.status == "DELETED":
                     delete_vm(e)
 
-                # elif e_status == "REQUESTED_MIGRATION":
-                #     if e.value["migration_destination"]
-
                 logging.info(f"Running VMs {running_vms}")
 
 
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..a6bd87d
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,14 @@
+import argparse
+import os
+
+argparser = argparse.ArgumentParser()
+argparser.add_argument("--production", action="store_true")
+argparser.add_argument("--development", action="store_true")
+args = argparser.parse_args()
+
+if args.development:
+    if not os.path.exists("etcd3_wrapper"):
+        os.symlink("../etcd3_wrapper", "etcd3_wrapper")
+
+    if not os.path.exists("ucloud_common"):
+        os.symlink("../ucloud_common", "ucloud_common")