2019-12-08 11:59:18 +00:00
|
|
|
import configparser
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
2019-12-14 15:23:31 +00:00
|
|
|
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
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-12-14 15:23:31 +00:00
|
|
|
log = logging.getLogger('ucloud.config')
|
2019-12-08 11:59:18 +00:00
|
|
|
|
2019-12-14 15:23:31 +00:00
|
|
|
conf_name = 'ucloud.conf'
|
|
|
|
conf_dir = os.environ.get('UCLOUD_CONF_DIR', '/etc/ucloud')
|
2019-12-08 11:59:18 +00:00
|
|
|
config_file = os.path.join(conf_dir, conf_name)
|
|
|
|
|
2019-12-14 15:23:31 +00:00
|
|
|
config = configparser.ConfigParser(allow_no_value=True)
|
2019-12-08 11:59:18 +00:00
|
|
|
|
2019-12-14 15:23:31 +00:00
|
|
|
if os.access(config_file, os.R_OK):
|
2019-12-08 13:15:36 +00:00
|
|
|
config.read(config_file)
|
2019-12-14 15:23:31 +00:00
|
|
|
else:
|
|
|
|
log.warning('Configuration file not found - using defaults')
|
2019-12-08 13:55:26 +00:00
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
etcd_wrapper_args = ()
|
2019-11-27 10:35:51 +00:00
|
|
|
etcd_wrapper_kwargs = {
|
2019-12-14 15:23:31 +00:00
|
|
|
'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']
|
2019-11-27 10:35:51 +00:00
|
|
|
}
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
etcd_client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
|
|
|
|
|
2019-12-14 15:23:31 +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'])
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
running_vms = []
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-12-14 15:23:31 +00:00
|
|
|
__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']
|
|
|
|
)
|
2019-11-25 06:52:36 +00:00
|
|
|
else:
|
2019-12-14 15:23:31 +00:00
|
|
|
raise Exception('Unknown Image Storage Handler')
|