From 6046015c3d56067d0eafa3848c59ea333e2a50e7 Mon Sep 17 00:00:00 2001 From: meow Date: Tue, 7 Jan 2020 20:26:10 +0500 Subject: [PATCH] Add base prefix option for uncloud so that we can run independent instance on uncloud --- conf/uncloud.conf | 2 ++ uncloud/common/settings.py | 38 +++++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/conf/uncloud.conf b/conf/uncloud.conf index 9d4358d..9995696 100644 --- a/conf/uncloud.conf +++ b/conf/uncloud.conf @@ -1,6 +1,7 @@ [etcd] url = localhost port = 2379 +prefix = / ca_cert cert_cert cert_key @@ -9,3 +10,4 @@ cert_key name = replace_me realm = replace_me seed = replace_me +api_server = http://localhost:5000 \ No newline at end of file diff --git a/uncloud/common/settings.py b/uncloud/common/settings.py index 0654b9b..7004055 100644 --- a/uncloud/common/settings.py +++ b/uncloud/common/settings.py @@ -4,8 +4,8 @@ import sys import os from datetime import datetime - from uncloud.common.etcd_wrapper import Etcd3Wrapper +from os.path import join as join_path logger = logging.getLogger(__name__) @@ -25,25 +25,28 @@ class CustomConfigParser(configparser.RawConfigParser): class Settings(object): - def __init__(self, config_key='/uncloud/config/'): + def __init__(self): conf_name = 'uncloud.conf' - conf_dir = os.environ.get( - 'UCLOUD_CONF_DIR', os.path.expanduser('~/uncloud/') - ) - self.config_file = os.path.join(conf_dir, conf_name) - self.config_parser = CustomConfigParser(allow_no_value=True) - self.config_key = config_key + conf_dir = os.environ.get('UCLOUD_CONF_DIR', os.path.expanduser('~/uncloud/')) + self.config_file = join_path(conf_dir, conf_name) # this is used to cache config from etcd for 1 minutes. Without this we # would make a lot of requests to etcd which slows down everything. self.last_config_update = datetime.fromtimestamp(0) - self.read_internal_values() + self.config_parser = CustomConfigParser(allow_no_value=True) + self.config_parser.add_section('etcd') + self.config_parser.set('etcd', 'prefix', '/') + + self.config_key = join_path(self['etcd']['prefix'], '/uncloud/config/') + try: self.config_parser.read(self.config_file) except Exception as err: logger.error('%s', err) + self.read_internal_values() + def get_etcd_client(self): args = tuple() try: @@ -75,17 +78,18 @@ class Settings(object): return wrapper def read_internal_values(self): + prefix = self['etcd']['prefix'] self.config_parser.read_dict( { 'etcd': { - 'file_prefix': '/files/', - 'host_prefix': '/hosts/', - 'image_prefix': '/images/', - 'image_store_prefix': '/imagestore/', - 'network_prefix': '/networks/', - 'request_prefix': '/requests/', - 'user_prefix': '/users/', - 'vm_prefix': '/vms/', + 'file_prefix': join_path(prefix, '/files/'), + 'host_prefix': join_path(prefix, '/hosts/'), + 'image_prefix': join_path(prefix, '/images/'), + 'image_store_prefix': join_path(prefix, '/imagestore/'), + 'network_prefix': join_path(prefix, '/networks/'), + 'request_prefix': join_path(prefix, '/requests/'), + 'user_prefix': join_path(prefix, '/users/'), + 'vm_prefix': join_path(prefix, '/vms/'), } } )