check whether vm image exists before running it
This commit is contained in:
		
					parent
					
						
							
								aaa733d16f
							
						
					
				
			
			
				commit
				
					
						a0a30f46ae
					
				
			
		
					 1 changed files with 21 additions and 6 deletions
				
			
		
							
								
								
									
										27
									
								
								main.py
									
										
									
									
									
								
							
							
						
						
									
										27
									
								
								main.py
									
										
									
									
									
								
							| 
						 | 
					@ -33,6 +33,20 @@ class VM:
 | 
				
			||||||
    vm: qmp.QEMUMachine
 | 
					    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(
 | 
					logging.basicConfig(
 | 
				
			||||||
    level=logging.DEBUG,
 | 
					    level=logging.DEBUG,
 | 
				
			||||||
    filename="log.txt",
 | 
					    filename="log.txt",
 | 
				
			||||||
| 
						 | 
					@ -99,12 +113,13 @@ def start_vm(vm_path, e):
 | 
				
			||||||
    #        uservms pool then checkwhether the e.key.split("/").pop()
 | 
					    #        uservms pool then checkwhether the e.key.split("/").pop()
 | 
				
			||||||
    #        exists in rbd_ls(uservms_pool)
 | 
					    #        exists in rbd_ls(uservms_pool)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # if not os.path.isfile(vm_path):
 | 
					    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"Image file of vm {e.key} does not exists")
 | 
				
			||||||
    #     logging.info(f"Setting vm {e.key} status to DELETED")
 | 
					        logging.info(f"Setting vm {e.key} status to DELETED")
 | 
				
			||||||
    #     e.value["status"] = "DELETED"
 | 
					        e.value["status"] = "DELETED"
 | 
				
			||||||
    #     client.put(e.key, json.dumps(e.value))
 | 
					        client.put(e.key, json.dumps(e.value))
 | 
				
			||||||
    #     return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _vm = get_vm(running_vms, e.key)
 | 
					    _vm = get_vm(running_vms, e.key)
 | 
				
			||||||
    if _vm:
 | 
					    if _vm:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue