Small changes here and there. Typo fix, little bit refactoring
This commit is contained in:
		
					parent
					
						
							
								5fffcd9510
							
						
					
				
			
			
				commit
				
					
						70ec3f832b
					
				
			
		
					 2 changed files with 29 additions and 17 deletions
				
			
		
							
								
								
									
										32
									
								
								main.py
									
										
									
									
									
								
							
							
						
						
									
										32
									
								
								main.py
									
										
									
									
									
								
							| 
						 | 
					@ -36,6 +36,7 @@ logging.basicConfig(
 | 
				
			||||||
    datefmt="%d-%b-%y %H:%M:%S",
 | 
					    datefmt="%d-%b-%y %H:%M:%S",
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@dataclass
 | 
					@dataclass
 | 
				
			||||||
class VM:
 | 
					class VM:
 | 
				
			||||||
    key: str
 | 
					    key: str
 | 
				
			||||||
| 
						 | 
					@ -75,13 +76,11 @@ def need_running_vm(func):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def create_vm(vm):
 | 
					def create_vm(vm):
 | 
				
			||||||
    image = client.get(
 | 
					    image = client.get(f"/v1/image/{vm.value['image_uuid']}", value_in_json=True)
 | 
				
			||||||
        f"/v1/image/{vm.value['image_uuid']}", value_in_json=True
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
    if image:
 | 
					    if image:
 | 
				
			||||||
        logging.debug(image)
 | 
					        logging.debug(image)
 | 
				
			||||||
 | 
					 | 
				
			||||||
        logging.info("Creating New VM...")
 | 
					        logging.info("Creating New VM...")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        _command_to_create = f"rbd clone images/{vm.image_uuid}@protected uservms/{vm.uuid}"
 | 
					        _command_to_create = f"rbd clone images/{vm.image_uuid}@protected uservms/{vm.uuid}"
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            subprocess.call(_command_to_create.split(" "))
 | 
					            subprocess.call(_command_to_create.split(" "))
 | 
				
			||||||
| 
						 | 
					@ -89,16 +88,20 @@ def create_vm(vm):
 | 
				
			||||||
            VM_POOL.put(vm)
 | 
					            VM_POOL.put(vm)
 | 
				
			||||||
        except:
 | 
					        except:
 | 
				
			||||||
            logging.exception("Can't clone image")
 | 
					            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:
 | 
					    try:
 | 
				
			||||||
        user_vms = RBD.ls("uservms")
 | 
					        user_vms = RBD.ls("uservms")
 | 
				
			||||||
    except:
 | 
					    except:
 | 
				
			||||||
        logging.info("Can't access uservms pool")
 | 
					        logging.info("Can't access uservms pool")
 | 
				
			||||||
        return
 | 
					        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"Image file of vm {e.key} does not exists")
 | 
				
			||||||
        logging.info(f"Deleting vm {e.key}")
 | 
					        logging.info(f"Deleting vm {e.key}")
 | 
				
			||||||
        client.client.delete(e.key)
 | 
					        client.client.delete(e.key)
 | 
				
			||||||
| 
						 | 
					@ -181,7 +184,7 @@ def shutdown_vm(e):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def delete_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}")
 | 
					    logging.info(f"Deleting VM {e.key}")
 | 
				
			||||||
    shutdown_vm(e)
 | 
					    shutdown_vm(e)
 | 
				
			||||||
    client.client.delete(e.key)
 | 
					    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)
 | 
					    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)
 | 
					    _vms = VM_POOL.by_host(host.key)
 | 
				
			||||||
    alleged_running_vms = VM_POOL.by_status("RUNNING", _vms)
 | 
					    alleged_running_vms = VM_POOL.by_status("RUNNING", _vms)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -242,13 +245,12 @@ def main():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if e.status == "TIMEOUT":
 | 
					            if e.status == "TIMEOUT":
 | 
				
			||||||
                logging.info("Timeout")
 | 
					                logging.info("Timeout")
 | 
				
			||||||
                maintenence(host)
 | 
					                maintenance(host)
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # TODO: Re-evaluate Migration Design
 | 
				
			||||||
            if hasattr(e, "migration_destination"):
 | 
					            if hasattr(e, "migration_destination"):
 | 
				
			||||||
                e_migration_destination = e.value[
 | 
					                e_migration_destination = e.value["migration_destination"]
 | 
				
			||||||
                    "migration_destination"
 | 
					 | 
				
			||||||
                ]
 | 
					 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                e_migration_destination = ""
 | 
					                e_migration_destination = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -267,8 +269,7 @@ def main():
 | 
				
			||||||
                    resume_vm(e)
 | 
					                    resume_vm(e)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                elif e.status == "REQUESTED_START":
 | 
					                elif e.status == "REQUESTED_START":
 | 
				
			||||||
                    vm_path = f"rbd:uservms/{e.uuid}"
 | 
					                    start_vm(e)
 | 
				
			||||||
                    start_vm(vm_path, e)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                elif e.status == "REQUESTED_SHUTDOWN":
 | 
					                elif e.status == "REQUESTED_SHUTDOWN":
 | 
				
			||||||
                    shutdown_vm(e)
 | 
					                    shutdown_vm(e)
 | 
				
			||||||
| 
						 | 
					@ -276,9 +277,6 @@ def main():
 | 
				
			||||||
                elif e.status == "DELETED":
 | 
					                elif e.status == "DELETED":
 | 
				
			||||||
                    delete_vm(e)
 | 
					                    delete_vm(e)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                # elif e_status == "REQUESTED_MIGRATION":
 | 
					 | 
				
			||||||
                #     if e.value["migration_destination"]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                logging.info(f"Running VMs {running_vms}")
 | 
					                logging.info(f"Running VMs {running_vms}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										14
									
								
								setup.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								setup.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -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")
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue