uncloud-mravi/ucloud/config.py

53 lines
1.7 KiB
Python

import configparser
import os
import logging
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
from ucloud.common.etcd_wrapper import Etcd3Wrapper
log = logging.getLogger('ucloud.config')
conf_name = 'ucloud.conf'
conf_dir = os.environ.get('UCLOUD_CONF_DIR', '/etc/ucloud')
config_file = os.path.join(conf_dir, conf_name)
config = configparser.ConfigParser(allow_no_value=True)
if os.access(config_file, os.R_OK):
config.read(config_file)
else:
log.warning('Configuration file not found - using defaults')
etcd_wrapper_args = ()
etcd_wrapper_kwargs = {
'host': config['etcd']['url'],
'port': config['etcd']['port'],
'ca_cert': config['etcd']['ca_cert'],
'cert_cert': config['etcd']['cert_cert'],
'cert_key': config['etcd']['cert_key']
}
etcd_client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
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 = []
__storage_backend = config['storage']['backend']
if __storage_backend == 'filesystem':
image_storage_handler = FileSystemBasedImageStorageHandler(
vm_base=config['storage']['vm_dir'],
image_base=config['storage']['image_dir']
)
elif __storage_backend == 'ceph':
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')