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] [etcd]
url = localhost url = localhost
port = 2379 port = 2379
prefix = /
ca_cert ca_cert
cert_cert cert_cert
cert_key cert_key
@ -9,3 +10,4 @@ cert_key
name = replace_me name = replace_me
realm = replace_me realm = replace_me
seed = replace_me seed = replace_me
api_server = http://localhost:5000

View file

@ -4,8 +4,8 @@ import sys
import os import os
from datetime import datetime from datetime import datetime
from uncloud.common.etcd_wrapper import Etcd3Wrapper from uncloud.common.etcd_wrapper import Etcd3Wrapper
from os.path import join as join_path
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,25 +25,28 @@ class CustomConfigParser(configparser.RawConfigParser):
class Settings(object): class Settings(object):
def __init__(self, config_key='/uncloud/config/'): def __init__(self):
conf_name = 'uncloud.conf' conf_name = 'uncloud.conf'
conf_dir = os.environ.get( conf_dir = os.environ.get('UCLOUD_CONF_DIR', os.path.expanduser('~/uncloud/'))
'UCLOUD_CONF_DIR', os.path.expanduser('~/uncloud/') self.config_file = join_path(conf_dir, conf_name)
)
self.config_file = os.path.join(conf_dir, conf_name)
self.config_parser = CustomConfigParser(allow_no_value=True)
self.config_key = config_key
# this is used to cache config from etcd for 1 minutes. Without this we # 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. # would make a lot of requests to etcd which slows down everything.
self.last_config_update = datetime.fromtimestamp(0) 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: try:
self.config_parser.read(self.config_file) self.config_parser.read(self.config_file)
except Exception as err: except Exception as err:
logger.error('%s', err) logger.error('%s', err)
self.read_internal_values()
def get_etcd_client(self): def get_etcd_client(self):
args = tuple() args = tuple()
try: try:
@ -75,17 +78,18 @@ class Settings(object):
return wrapper return wrapper
def read_internal_values(self): def read_internal_values(self):
prefix = self['etcd']['prefix']
self.config_parser.read_dict( self.config_parser.read_dict(
{ {
'etcd': { 'etcd': {
'file_prefix': '/files/', 'file_prefix': join_path(prefix, '/files/'),
'host_prefix': '/hosts/', 'host_prefix': join_path(prefix, '/hosts/'),
'image_prefix': '/images/', 'image_prefix': join_path(prefix, '/images/'),
'image_store_prefix': '/imagestore/', 'image_store_prefix': join_path(prefix, '/imagestore/'),
'network_prefix': '/networks/', 'network_prefix': join_path(prefix, '/networks/'),
'request_prefix': '/requests/', 'request_prefix': join_path(prefix, '/requests/'),
'user_prefix': '/users/', 'user_prefix': join_path(prefix, '/users/'),
'vm_prefix': '/vms/', 'vm_prefix': join_path(prefix, '/vms/'),
} }
} }
) )