2019-12-21 09:36:55 +00:00
|
|
|
import configparser
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
2019-12-31 10:30:02 +00:00
|
|
|
from uncloud.common.etcd_wrapper import Etcd3Wrapper
|
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(
|
2019-12-31 10:30:02 +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):
|
2019-12-30 09:35:07 +00:00
|
|
|
def __init__(self, config_key="/uncloud/config/"):
|
2019-12-31 10:30:02 +00:00
|
|
|
conf_name = "uncloud.conf"
|
2019-12-30 09:35:07 +00:00
|
|
|
conf_dir = os.environ.get(
|
2019-12-31 10:30:02 +00:00
|
|
|
"UCLOUD_CONF_DIR", os.path.expanduser("~/uncloud/")
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2019-12-22 07:26:48 +00:00
|
|
|
self.config_file = os.path.join(conf_dir, conf_name)
|
2019-12-30 09:35:07 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
self.config_parser = CustomConfigParser(allow_no_value=True)
|
|
|
|
self.config_key = config_key
|
|
|
|
|
|
|
|
self.read_internal_values()
|
2019-12-28 11:35:55 +00:00
|
|
|
try:
|
|
|
|
self.config_parser.read(self.config_file)
|
|
|
|
except Exception as err:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.error("%s", err)
|
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 = {
|
2019-12-30 09:35:07 +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(
|
|
|
|
"{} in config file {}".format(
|
|
|
|
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(
|
|
|
|
"etcd connection not successfull. Please check your config file."
|
|
|
|
"\nDetails: %s\netcd connection parameters: %s",
|
|
|
|
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):
|
2019-12-30 09:35:07 +00:00
|
|
|
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/",
|
|
|
|
}
|
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
|
|
|
|
with open(config_file, "r") as config_file_handle:
|
|
|
|
self.config_parser.read_file(config_file_handle)
|
|
|
|
except FileNotFoundError:
|
2019-12-30 09:35:07 +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)
|
|
|
|
sys.exit("Error occurred while reading configuration file")
|
|
|
|
|
|
|
|
def read_values_from_etcd(self):
|
|
|
|
etcd_client = self.get_etcd_client()
|
2019-12-30 09:35:07 +00:00
|
|
|
config_from_etcd = etcd_client.get(
|
|
|
|
self.config_key, value_in_json=True
|
|
|
|
)
|
2019-12-21 09:36:55 +00:00
|
|
|
if config_from_etcd:
|
|
|
|
self.config_parser.read_dict(config_from_etcd.value)
|
|
|
|
else:
|
2019-12-30 09:35:07 +00:00
|
|
|
raise KeyError(
|
2019-12-31 10:30:02 +00:00
|
|
|
"Key '{}' not found in etcd. Please configure uncloud.".format(
|
2019-12-30 09:35:07 +00:00
|
|
|
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
|
|
|
|
try:
|
|
|
|
self.read_values_from_etcd()
|
|
|
|
except KeyError as e:
|
|
|
|
pass
|
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
return self.config_parser[key]
|
2019-12-22 07:26:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
settings = Settings()
|