uncloud-mravi/ucloud/config.py

58 lines
1.9 KiB
Python
Raw Normal View History

from etcd3_wrapper import Etcd3Wrapper
from ucloud.common.host import HostPool
from ucloud.common.request import RequestPool
from ucloud.common.vm import VmPool
from ucloud.common.storage_handlers import FileSystemBasedImageStorageHandler, CEPHBasedImageStorageHandler
# Replacing decouple inline
import configparser
import os
import os.path
import logging
log = logging.getLogger("ucloud.config")
conf_name = "ucloud.conf"
try:
conf_dir = os.environ["UCLOUD_CONF_DIR"]
except KeyError:
conf_dir = "/etc/ucloud"
config_file = os.path.join(conf_dir, conf_name)
config = configparser.ConfigParser()
try:
2019-12-08 13:15:36 +00:00
config.read(config_file)
except FileNotFoundError:
log.warn("Configuration file not found - using defaults")
etcd_wrapper_args = ()
2019-11-27 10:35:51 +00:00
etcd_wrapper_kwargs = {
2019-12-08 12:41:42 +00:00
'host': config['etcd']['ETCD_URL'],
'port': config['etcd']['ETCD_PORT'],
'ca_cert': config['etcd']['CA_CERT'],
'cert_cert': config['etcd']['CERT_CERT'],
'cert_key': config['etcd']['CERT_KEY']
2019-11-27 10:35:51 +00:00
}
etcd_client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
2019-12-08 12:41:42 +00:00
host_pool = HostPool(etcd_client, config['etcd']['HOST_PREFIX'])
vm_pool = VmPool(etcd_client, config['etcd']['VM_PREFIX'])
request_pool = RequestPool(etcd_client, config['etcd']['REQUEST_PREFIX'])
running_vms = []
2019-12-08 12:41:42 +00:00
__storage_backend = config['storage']["STORAGE_BACKEND"]
if __storage_backend == "filesystem":
2019-12-08 12:41:42 +00:00
image_storage_handler = FileSystemBasedImageStorageHandler(vm_base=config['storage']["VM_DIR"],
image_base=config['storage']["IMAGE_DIR"])
elif __storage_backend == "ceph":
2019-12-08 12:41:42 +00:00
image_storage_handler = CEPHBasedImageStorageHandler(vm_base=config['storage']["CEPH_VM_POOL"],
image_base=config['storage']["CEPH_IMAGE_POOL"])
else:
raise Exception("Unknown Image Storage Handler")