diff --git a/.gitignore b/.gitignore index 66c92a4..bd33a60 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea/ .vscode/ venv/ +log.txt + diff --git a/main.py b/main.py index ad667c3..15c4bf6 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,19 @@ import os import json import subprocess -import shutil +import logging from etcd3_wrapper import Etcd3Wrapper BASE_PATH = "/var/www" +logging.basicConfig( + level=logging.DEBUG, + filename="log.txt", + filemode="a", + format="%(asctime)s: %(levelname)s - %(message)s", + datefmt="%d-%b-%y %H:%M:%S", +) + 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)) @@ -16,19 +24,41 @@ for image in images_to_be_created: 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") + logging.info("Converting it to raw") subprocess.run(["qemu-img", "convert", "-f", "qcow2", - "-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)) + "-O", "raw", image_full_path, f"image.raw"]) + if os.path.isfile(f"image.raw"): + # shutil.move(f"{image_uuid}.raw", f"/var/vm/{image_uuid}.raw") + _store_name = image.value["store_name"] + _image_stores = client.get_prefix("/v1/image_store/", value_in_json=True) + _user_image_store = next(filter(lambda s: s.value["name"] == _store_name, _image_stores)) + if _user_image_store: + _image_store_pool = _user_image_store.value["attributes"]["pool"] + rc = subprocess.check_call(["rbd", "import", "image.raw", + f"{_image_store_pool}/{image_uuid}"]) + if rc == 0: + _snapshot_creation_command = f"rbd snap create {_image_store_pool}/{image_uuid}@protected" + subprocess.check_call(_snapshot_creation_command.split(" ")) + + _snapshot_protect_command = f"rbd snap protect {_image_store_pool}/{image_uuid}@protected" + subprocess.check_call(_snapshot_protect_command.split(" ")) + + image.value["status"] = "CREATED" + client.put(image.key, json.dumps(image.value)) + else: + logging.info(f"Some error occurred while creating image {image_uuid}") + else: + logging.info(f"Image store {_user_image_store} not found") else: - print(f"{image_uuid}.raw not found") + logging.info(f"{image_uuid}.raw not found") else: - print("not qcow2 format") + logging.info("not qcow2 format") + image.value["status"] = "INVALID_FORMAT" + client.put(image.key, json.dumps(image.value)) else: - print("File does not exists") \ No newline at end of file + logging.info("File does not exists") + + try: + os.remove("image.raw") + except OSError: + pass \ No newline at end of file