2019-12-21 09:36:55 +00:00
|
|
|
import configparser
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
from datetime import datetime
|
2019-12-31 10:30:02 +00:00
|
|
|
from uncloud.common.etcd_wrapper import Etcd3Wrapper
|
2020-01-07 15:26:10 +00:00
|
|
|
from os.path import join as join_path
|
2019-12-21 09:36:55 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomConfigParser(configparser.RawConfigParser):
|
|
|
|
def __getitem__(self, key):
|
|
|
|
try:
|
|
|
|
result = super().__getitem__(key)
|
|
|
|
except KeyError as err:
|
2019-12-23 07:58:04 +00:00
|
|
|
raise KeyError(
|
2020-01-05 15:20:00 +00:00
|
|
|
'Key \'{}\' not found in configuration. Make sure you configure uncloud.'.format(
|
2019-12-30 09:35:07 +00:00
|
|
|
key
|
|
|
|
)
|
2019-12-23 07:58:04 +00:00
|
|
|
) from err
|
2019-12-21 09:36:55 +00:00
|
|
|
else:
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class Settings(object):
|
2020-01-07 15:26:10 +00:00
|
|
|
def __init__(self):
|
2020-01-05 15:20:00 +00:00
|
|
|
conf_name = 'uncloud.conf'
|
2020-01-07 15:26:10 +00:00
|
|
|
conf_dir = os.environ.get('UCLOUD_CONF_DIR', os.path.expanduser('~/uncloud/'))
|
|
|
|
self.config_file = join_path(conf_dir, conf_name)
|
2019-12-21 09:36:55 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
# 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)
|
|
|
|
|
2020-01-07 15:26:10 +00:00
|
|
|
self.config_parser = CustomConfigParser(allow_no_value=True)
|
|
|
|
self.config_parser.add_section('etcd')
|
2020-01-07 16:45:11 +00:00
|
|
|
self.config_parser.set('etcd', 'base_prefix', '/')
|
2020-01-07 15:26:10 +00:00
|
|
|
|
2019-12-28 11:35:55 +00:00
|
|
|
try:
|
|
|
|
self.config_parser.read(self.config_file)
|
|
|
|
except Exception as err:
|
2020-01-05 15:20:00 +00:00
|
|
|
logger.error('%s', err)
|
2019-12-21 09:36:55 +00:00
|
|
|
|
2020-01-07 16:45:11 +00:00
|
|
|
self.config_key = join_path(self['etcd']['base_prefix'] + 'uncloud/config/')
|
|
|
|
|
2020-01-07 15:26:10 +00:00
|
|
|
self.read_internal_values()
|
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
def get_etcd_client(self):
|
2019-12-22 07:26:48 +00:00
|
|
|
args = tuple()
|
|
|
|
try:
|
|
|
|
kwargs = {
|
2020-01-05 15:20:00 +00:00
|
|
|
'host': self.config_parser.get('etcd', 'url'),
|
|
|
|
'port': self.config_parser.get('etcd', 'port'),
|
|
|
|
'ca_cert': self.config_parser.get('etcd', 'ca_cert'),
|
|
|
|
'cert_cert': self.config_parser.get('etcd', 'cert_cert'),
|
|
|
|
'cert_key': self.config_parser.get('etcd', 'cert_key'),
|
2019-12-22 07:26:48 +00:00
|
|
|
}
|
|
|
|
except configparser.Error as err:
|
2019-12-30 09:35:07 +00:00
|
|
|
raise configparser.Error(
|
2020-01-05 15:20:00 +00:00
|
|
|
'{} in config file {}'.format(
|
2019-12-30 09:35:07 +00:00
|
|
|
err.message, self.config_file
|
|
|
|
)
|
|
|
|
) from err
|
2019-12-29 18:14:39 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
wrapper = Etcd3Wrapper(*args, **kwargs)
|
|
|
|
except Exception as err:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.error(
|
2020-01-05 15:20:00 +00:00
|
|
|
'etcd connection not successfull. Please check your config file.'
|
|
|
|
'\nDetails: %s\netcd connection parameters: %s',
|
2019-12-30 09:35:07 +00:00
|
|
|
err,
|
|
|
|
kwargs,
|
|
|
|
)
|
2019-12-29 18:14:39 +00:00
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
return wrapper
|
2019-12-30 09:35:07 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
def read_internal_values(self):
|
2020-01-07 16:45:11 +00:00
|
|
|
base_prefix = self['etcd']['base_prefix']
|
2019-12-30 09:35:07 +00:00
|
|
|
self.config_parser.read_dict(
|
|
|
|
{
|
2020-01-05 15:20:00 +00:00
|
|
|
'etcd': {
|
2020-01-07 16:45:11 +00:00
|
|
|
'file_prefix': join_path(base_prefix, 'files/'),
|
|
|
|
'host_prefix': join_path(base_prefix, 'hosts/'),
|
|
|
|
'image_prefix': join_path(base_prefix, 'images/'),
|
|
|
|
'image_store_prefix': join_path(base_prefix, 'imagestore/'),
|
|
|
|
'network_prefix': join_path(base_prefix, 'networks/'),
|
|
|
|
'request_prefix': join_path(base_prefix, 'requests/'),
|
|
|
|
'user_prefix': join_path(base_prefix, 'users/'),
|
|
|
|
'vm_prefix': join_path(base_prefix, 'vms/'),
|
2020-01-07 17:18:13 +00:00
|
|
|
'vxlan_counter': join_path(base_prefix, 'counters/vxlan'),
|
|
|
|
'tap_counter': join_path(base_prefix, 'counters/tap')
|
2019-12-30 09:35:07 +00:00
|
|
|
}
|
2019-12-21 09:36:55 +00:00
|
|
|
}
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2019-12-21 09:36:55 +00:00
|
|
|
|
|
|
|
def read_config_file_values(self, config_file):
|
|
|
|
try:
|
|
|
|
# Trying to read configuration file
|
2020-01-10 11:39:40 +00:00
|
|
|
with open(config_file) as config_file_handle:
|
2019-12-21 09:36:55 +00:00
|
|
|
self.config_parser.read_file(config_file_handle)
|
|
|
|
except FileNotFoundError:
|
2020-01-10 11:39:40 +00:00
|
|
|
sys.exit('Configuration file {} not found!'.format(config_file))
|
2019-12-21 09:36:55 +00:00
|
|
|
except Exception as err:
|
|
|
|
logger.exception(err)
|
2020-01-05 15:20:00 +00:00
|
|
|
sys.exit('Error occurred while reading configuration file')
|
2019-12-21 09:36:55 +00:00
|
|
|
|
|
|
|
def read_values_from_etcd(self):
|
|
|
|
etcd_client = self.get_etcd_client()
|
2020-01-03 13:38:59 +00:00
|
|
|
if (datetime.utcnow() - self.last_config_update).total_seconds() > 60:
|
|
|
|
config_from_etcd = etcd_client.get(self.config_key, value_in_json=True)
|
|
|
|
if config_from_etcd:
|
|
|
|
self.config_parser.read_dict(config_from_etcd.value)
|
|
|
|
self.last_config_update = datetime.utcnow()
|
|
|
|
else:
|
2020-01-05 15:20:00 +00:00
|
|
|
raise KeyError('Key \'{}\' not found in etcd. Please configure uncloud.'.format(self.config_key))
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
def __getitem__(self, key):
|
2019-12-31 12:50:56 +00:00
|
|
|
# Allow failing to read from etcd if we have
|
|
|
|
# it locally
|
2020-01-03 13:38:59 +00:00
|
|
|
if key not in self.config_parser.sections():
|
|
|
|
try:
|
|
|
|
self.read_values_from_etcd()
|
2020-01-05 15:20:00 +00:00
|
|
|
except KeyError:
|
2020-01-03 13:38:59 +00:00
|
|
|
pass
|
2019-12-21 09:36:55 +00:00
|
|
|
return self.config_parser[key]
|
2019-12-22 07:26:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
settings = Settings()
|