import os
import json
import subprocess
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))

for image in images_to_be_created:
    image_uuid = image.key.split("/")[-1]
    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:
            logging.info("Converting it to raw")
            subprocess.run(["qemu-img", "convert", "-f", "qcow2",
                            "-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:
                logging.info(f"{image_uuid}.raw not found")
        else:
            logging.info("not qcow2 format")
            image.value["status"] = "INVALID_FORMAT"
            client.put(image.key, json.dumps(image.value))
    else:
        logging.info("File does not exists")
    
    try:
        os.remove("image.raw")
    except OSError:
        pass