2019-11-18 17:39:57 +00:00
|
|
|
from etcd3_wrapper import Etcd3Wrapper
|
|
|
|
|
2019-12-03 10:40:41 +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
|
2019-12-08 11:59:18 +00:00
|
|
|
|
2019-12-07 12:45:01 +00:00
|
|
|
from decouple import Config, RepositoryEnv, RepositoryEmpty
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-12-08 11:59:18 +00:00
|
|
|
# Replacing decouple inline
|
|
|
|
import configparser
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger("ucloud.config")
|
|
|
|
|
|
|
|
|
|
|
|
conf_name = "ucloud.conf"
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-12-07 12:45:01 +00:00
|
|
|
try:
|
2019-12-08 11:59:18 +00:00
|
|
|
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:
|
|
|
|
with open(config_file, "r") as conf_fd:
|
|
|
|
conf.read(conf_fd)
|
2019-12-07 12:45:01 +00:00
|
|
|
except FileNotFoundError:
|
2019-12-08 11:59:18 +00:00
|
|
|
log.warn("Configuration file not found - using defaults")
|
|
|
|
|
|
|
|
|
|
|
|
# Compatibility to old code
|
|
|
|
env_vars = config
|
|
|
|
|
|
|
|
|
|
|
|
# Try importing config, but don't fail if it does not exist
|
|
|
|
# try:
|
|
|
|
# env_vars = Config(RepositoryEnv('/etc/ucloud/ucloud.conf'))
|
|
|
|
# except FileNotFoundError:
|
|
|
|
# env_vars = Config(RepositoryEmpty())
|
2019-12-07 12:45:01 +00:00
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
etcd_wrapper_args = ()
|
2019-11-27 10:35:51 +00:00
|
|
|
etcd_wrapper_kwargs = {
|
|
|
|
'host': env_vars.get('ETCD_URL', 'localhost'),
|
|
|
|
'port': env_vars.get('ETCD_PORT', 2379),
|
|
|
|
'ca_cert': env_vars.get('CA_CERT', None),
|
|
|
|
'cert_cert': env_vars.get('CERT_CERT', None),
|
|
|
|
'cert_key': env_vars.get('CERT_KEY', None)
|
|
|
|
}
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
etcd_client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
|
|
|
|
|
2019-12-07 12:51:50 +00:00
|
|
|
host_pool = HostPool(etcd_client, env_vars.get('HOST_PREFIX', "hosts"))
|
|
|
|
vm_pool = VmPool(etcd_client, env_vars.get('VM_PREFIX', "vms"))
|
|
|
|
request_pool = RequestPool(etcd_client, env_vars.get('REQUEST_PREFIX', "requests"))
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
running_vms = []
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-12-07 12:51:50 +00:00
|
|
|
__storage_backend = env_vars.get("STORAGE_BACKEND", "filesystem")
|
2019-11-25 06:52:36 +00:00
|
|
|
if __storage_backend == "filesystem":
|
2019-12-07 13:10:16 +00:00
|
|
|
image_storage_handler = FileSystemBasedImageStorageHandler(vm_base=env_vars.get("VM_DIR", "/tmp/ucloud-vms"),
|
|
|
|
image_base=env_vars.get("IMAGE_DIR", "/tmp/ucloud-images"))
|
2019-11-25 06:52:36 +00:00
|
|
|
elif __storage_backend == "ceph":
|
|
|
|
image_storage_handler = CEPHBasedImageStorageHandler(vm_base="ssd", image_base="ssd")
|
|
|
|
else:
|
|
|
|
raise Exception("Unknown Image Storage Handler")
|