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-18 13:43:16 +00:00
|
|
|
import logging
|
2019-07-01 18:59:40 +00:00
|
|
|
from etcd3_wrapper import Etcd3Wrapper
|
|
|
|
|
|
|
|
BASE_PATH = "/var/www"
|
|
|
|
|
2019-07-18 13:43:16 +00:00
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.DEBUG,
|
|
|
|
filename="log.txt",
|
|
|
|
filemode="a",
|
|
|
|
format="%(asctime)s: %(levelname)s - %(message)s",
|
|
|
|
datefmt="%d-%b-%y %H:%M:%S",
|
|
|
|
)
|
|
|
|
|
2019-07-01 18:59:40 +00:00
|
|
|
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:
|
2019-07-18 13:43:16 +00:00
|
|
|
logging.info("Converting it to raw")
|
2019-07-01 18:59:40 +00:00
|
|
|
subprocess.run(["qemu-img", "convert", "-f", "qcow2",
|
2019-07-18 13:43:16 +00:00
|
|
|
"-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")
|
2019-07-01 18:59:40 +00:00
|
|
|
else:
|
2019-07-18 13:43:16 +00:00
|
|
|
logging.info(f"{image_uuid}.raw not found")
|
2019-07-01 18:59:40 +00:00
|
|
|
else:
|
2019-07-18 13:43:16 +00:00
|
|
|
logging.info("not qcow2 format")
|
|
|
|
image.value["status"] = "INVALID_FORMAT"
|
|
|
|
client.put(image.key, json.dumps(image.value))
|
2019-07-01 18:59:40 +00:00
|
|
|
else:
|
2019-07-18 13:43:16 +00:00
|
|
|
logging.info("File does not exists")
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove("image.raw")
|
|
|
|
except OSError:
|
|
|
|
pass
|