ucloud-image-scanner/main.py

34 lines
1.4 KiB
Python
Raw Normal View History

2019-07-01 18:59:40 +00:00
import os
2019-07-03 13:08:22 +00:00
import json
2019-07-01 18:59:40 +00:00
import subprocess
2019-07-03 13:08:22 +00:00
import shutil
2019-07-01 18:59:40 +00:00
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:
2019-07-03 13:08:22 +00:00
image_uuid = image.key.split("/")[-1]
2019-07-01 18:59:40 +00:00
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",
2019-07-03 13:08:22 +00:00
"-O", "raw", image_full_path, f"{image_uuid}.raw"])
if os.path.isfile(f"{image_uuid}.raw"):
shutil.move(f"{image_uuid}.raw", f"/var/vm/{image_uuid}.raw")
# subprocess.run(["rbd", "import", "image.raw",
# f"{image.value['store_name']}/{image.key.split('/')[-1]}"])
image.value["status"] = "CREATED"
client.put(image.key, json.dumps(image.value))
2019-07-01 18:59:40 +00:00
else:
2019-07-03 13:08:22 +00:00
print(f"{image_uuid}.raw not found")
2019-07-01 18:59:40 +00:00
else:
print("not qcow2 format")
else:
print("File does not exists")