Refactoring, Removal of most global vars, config default path is ~/ucloud/

This commit is contained in:
ahmadbilalkhalid 2019-12-22 12:26:48 +05:00
commit 04993e4106
23 changed files with 673 additions and 726 deletions

View file

@ -5,7 +5,8 @@ import sys
from os.path import isdir
from os.path import join as join_path
from ucloud.config import etcd_client, config, image_storage_handler
from ucloud.settings import settings
from ucloud.shared import shared
from ucloud.imagescanner import logger
@ -22,9 +23,9 @@ def qemu_img_type(path):
def check():
""" check whether settings are sane, refuse to start if they aren't """
if config['storage']['storage_backend'] == 'filesystem' and not isdir(config['storage']['image_dir']):
if settings['storage']['storage_backend'] == 'filesystem' and not isdir(settings['storage']['image_dir']):
sys.exit("You have set STORAGE_BACKEND to filesystem, but "
"{} does not exist. Refusing to start".format(config['storage']['image_dir'])
"{} does not exist. Refusing to start".format(settings['storage']['image_dir'])
)
try:
@ -36,7 +37,7 @@ def check():
def main():
# We want to get images entries that requests images to be created
images = etcd_client.get_prefix(config['etcd']['image_prefix'], value_in_json=True)
images = shared.etcd_client.get_prefix(settings['etcd']['image_prefix'], value_in_json=True)
images_to_be_created = list(filter(lambda im: im.value['status'] == 'TO_BE_CREATED', images))
for image in images_to_be_created:
@ -45,9 +46,9 @@ def main():
image_owner = image.value['owner']
image_filename = image.value['filename']
image_store_name = image.value['store_name']
image_full_path = join_path(config['storage']['file_dir'], image_owner, image_filename)
image_full_path = join_path(settings['storage']['file_dir'], image_owner, image_filename)
image_stores = etcd_client.get_prefix(config['etcd']['image_store_prefix'],
image_stores = shared.etcd_client.get_prefix(settings['etcd']['image_store_prefix'],
value_in_json=True)
user_image_store = next(filter(
lambda s, store_name=image_store_name: s.value["name"] == store_name,
@ -71,18 +72,18 @@ def main():
logger.exception(e)
else:
# Import and Protect
r_status = image_storage_handler.import_image(src="image.raw",
r_status = shared.storage_handler.import_image(src="image.raw",
dest=image_uuid,
protect=True)
if r_status:
# Everything is successfully done
image.value["status"] = "CREATED"
etcd_client.put(image.key, json.dumps(image.value))
shared.etcd_client.put(image.key, json.dumps(image.value))
else:
# The user provided image is either not found or of invalid format
image.value["status"] = "INVALID_IMAGE"
etcd_client.put(image.key, json.dumps(image.value))
shared.etcd_client.put(image.key, json.dumps(image.value))
try:
os.remove("image.raw")