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

@ -21,7 +21,8 @@ import bitmath
from ucloud.common.host import HostStatus
from ucloud.common.vm import VMStatus
from ucloud.config import etcd_client, config, vm_pool, host_pool
from ucloud.shared import shared
from ucloud.settings import settings
from . import helper, logger
from .common_fields import Field, VmUUIDField
from .helper import check_otp, resolve_vm_name
@ -102,14 +103,14 @@ class CreateImageSchema(BaseSchema):
super().__init__(data, fields)
def file_uuid_validation(self):
file_entry = etcd_client.get(os.path.join(config['etcd']['file_prefix'], self.uuid.value))
file_entry = shared.etcd_client.get(os.path.join(settings['etcd']['file_prefix'], self.uuid.value))
if file_entry is None:
self.add_error(
"Image File with uuid '{}' Not Found".format(self.uuid.value)
)
def image_store_name_validation(self):
image_stores = list(etcd_client.get_prefix(config['etcd']['image_store_prefix']))
image_stores = list(shared.etcd_client.get_prefix(settings['etcd']['image_store_prefix']))
image_store = next(
filter(
@ -218,7 +219,7 @@ class CreateVMSchema(OTPSchema):
def image_validation(self):
try:
image_uuid = helper.resolve_image_name(self.image.value, etcd_client)
image_uuid = helper.resolve_image_name(self.image.value, shared.etcd_client)
except Exception as e:
logger.exception("Cannot resolve image name = %s", self.image.value)
self.add_error(str(e))
@ -236,7 +237,7 @@ class CreateVMSchema(OTPSchema):
if _network:
for net in _network:
network = etcd_client.get(os.path.join(config['etcd']['network_prefix'],
network = shared.etcd_client.get(os.path.join(settings['etcd']['network_prefix'],
self.name.value,
net), value_in_json=True)
if not network:
@ -310,7 +311,7 @@ class VMStatusSchema(OTPSchema):
super().__init__(data, fields)
def validation(self):
vm = vm_pool.get(self.uuid.value)
vm = shared.vm_pool.get(self.uuid.value)
if not (
vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
):
@ -343,7 +344,7 @@ class VmActionSchema(OTPSchema):
)
def validation(self):
vm = vm_pool.get(self.uuid.value)
vm = shared.vm_pool.get(self.uuid.value)
if not (
vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
):
@ -383,7 +384,7 @@ class VmMigrationSchema(OTPSchema):
def destination_validation(self):
hostname = self.destination.value
host = next(filter(lambda h: h.hostname == hostname, host_pool.hosts), None)
host = next(filter(lambda h: h.hostname == hostname, shared.host_pool.hosts), None)
if not host:
self.add_error("No Such Host ({}) exists".format(self.destination.value))
elif host.status != HostStatus.alive:
@ -392,7 +393,7 @@ class VmMigrationSchema(OTPSchema):
self.destination.value = host.key
def validation(self):
vm = vm_pool.get(self.uuid.value)
vm = shared.vm_pool.get(self.uuid.value)
if not (
vm.value["owner"] == self.name.value or self.realm.value == "ungleich-admin"
):
@ -401,7 +402,7 @@ class VmMigrationSchema(OTPSchema):
if vm.status != VMStatus.running:
self.add_error("Can't migrate non-running VM")
if vm.hostname == os.path.join(config['etcd']['host_prefix'], self.destination.value):
if vm.hostname == os.path.join(settings['etcd']['host_prefix'], self.destination.value):
self.add_error("Destination host couldn't be same as Source Host")
@ -443,7 +444,7 @@ class CreateNetwork(OTPSchema):
super().__init__(data, fields=fields)
def network_name_validation(self):
network = etcd_client.get(os.path.join(config['etcd']['network_prefix'],
network = shared.etcd_client.get(os.path.join(settings['etcd']['network_prefix'],
self.name.value,
self.network_name.value),
value_in_json=True)