initial code added
This commit is contained in:
		
				commit
				
					
						ba2063ed9c
					
				
			
		
					 6 changed files with 150 additions and 0 deletions
				
			
		
							
								
								
									
										30
									
								
								main.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								main.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,30 @@
 | 
			
		|||
import os
 | 
			
		||||
import subprocess
 | 
			
		||||
 | 
			
		||||
from etcd3_wrapper import Etcd3Wrapper
 | 
			
		||||
 | 
			
		||||
BASE_PATH = "/var/www"
 | 
			
		||||
 | 
			
		||||
client = Etcd3Wrapper()
 | 
			
		||||
images = list(client.get_prefix("/v1/image/", value_in_json=True))
 | 
			
		||||
images_to_be_created = list(filter(lambda e: e.value["status"] == "TO_BE_CREATED", images))
 | 
			
		||||
 | 
			
		||||
for image in images_to_be_created:
 | 
			
		||||
    image_full_path = f"{BASE_PATH}/{image.value['owner']}/{image.value['filename']}"
 | 
			
		||||
    if os.path.isfile(image_full_path):
 | 
			
		||||
        output = subprocess.check_output(["qemu-img", "info", image_full_path]).decode("utf-8")
 | 
			
		||||
        if "qcow2" in output:
 | 
			
		||||
            print("qcow2 format")
 | 
			
		||||
            print("Converting it to raw")
 | 
			
		||||
            subprocess.run(["qemu-img", "convert", "-f", "qcow2",
 | 
			
		||||
                            "-O", "raw", image_full_path, "image.raw"])
 | 
			
		||||
            if os.path.isfile("image.raw"):
 | 
			
		||||
                subprocess.run(["rbd", "import", "image.raw",
 | 
			
		||||
                               f"{image.value['store_name']}/{image.key.split('/')[-1]}"])
 | 
			
		||||
                os.remove("image.raw")
 | 
			
		||||
            else:
 | 
			
		||||
                print("image.raw not found")
 | 
			
		||||
        else:
 | 
			
		||||
            print("not qcow2 format")
 | 
			
		||||
    else:
 | 
			
		||||
        print("File does not exists")
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue