integerate ceph

This commit is contained in:
ahmadbilalkhalid 2019-07-18 18:43:16 +05:00
parent cd1efa16f2
commit 493f1e893d
2 changed files with 45 additions and 13 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
.idea/ .idea/
.vscode/ .vscode/
venv/ venv/
log.txt

56
main.py
View file

@ -1,11 +1,19 @@
import os import os
import json import json
import subprocess import subprocess
import shutil import logging
from etcd3_wrapper import Etcd3Wrapper from etcd3_wrapper import Etcd3Wrapper
BASE_PATH = "/var/www" 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() client = Etcd3Wrapper()
images = list(client.get_prefix("/v1/image/", value_in_json=True)) 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)) 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): if os.path.isfile(image_full_path):
output = subprocess.check_output(["qemu-img", "info", image_full_path]).decode("utf-8") output = subprocess.check_output(["qemu-img", "info", image_full_path]).decode("utf-8")
if "qcow2" in output: if "qcow2" in output:
print("qcow2 format") logging.info("Converting it to raw")
print("Converting it to raw")
subprocess.run(["qemu-img", "convert", "-f", "qcow2", subprocess.run(["qemu-img", "convert", "-f", "qcow2",
"-O", "raw", image_full_path, f"{image_uuid}.raw"]) "-O", "raw", image_full_path, f"image.raw"])
if os.path.isfile(f"{image_uuid}.raw"): if os.path.isfile(f"image.raw"):
shutil.move(f"{image_uuid}.raw", f"/var/vm/{image_uuid}.raw") # shutil.move(f"{image_uuid}.raw", f"/var/vm/{image_uuid}.raw")
# subprocess.run(["rbd", "import", "image.raw", _store_name = image.value["store_name"]
# f"{image.value['store_name']}/{image.key.split('/')[-1]}"]) _image_stores = client.get_prefix("/v1/image_store/", value_in_json=True)
image.value["status"] = "CREATED" _user_image_store = next(filter(lambda s: s.value["name"] == _store_name, _image_stores))
client.put(image.key, json.dumps(image.value)) 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: else:
print(f"{image_uuid}.raw not found") logging.info(f"{image_uuid}.raw not found")
else: else:
print("not qcow2 format") logging.info("not qcow2 format")
image.value["status"] = "INVALID_FORMAT"
client.put(image.key, json.dumps(image.value))
else: else:
print("File does not exists") logging.info("File does not exists")
try:
os.remove("image.raw")
except OSError:
pass