Add base prefix option for uncloud so that we can run independent instance on uncloud

This commit is contained in:
ahmadbilalkhalid 2020-01-07 20:26:10 +05:00
parent b4292615de
commit 6046015c3d
2 changed files with 23 additions and 17 deletions

View file

@ -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

View file

@ -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/'),
}
}
)