Use FuelClientSettings everywhere

This patch moves all default settings from the code
to the default single settings file. And modifies
recently introduced settings manager to be able to
also update default values from /etc/fuel/client/config.yaml
file and from environment varables

DocImpact
Blueprint: re-thinking-fuel-client
Change-Id: Ib73b0494cf30e75099aa7a0552745cd4adc3a07e
This commit is contained in:
Roman Prykhodchenko
2015-01-24 19:44:55 +01:00
parent 61431ed16f
commit 1ae57d6781
3 changed files with 54 additions and 33 deletions

View File

@@ -14,14 +14,14 @@
import json
import logging
import os
import requests
from keystoneclient.v2_0 import client as auth_client
from six.moves.urllib import parse as urlparse
import yaml
from keystoneclient.v2_0 import client as auth_client
from fuelclient.cli.error import exceptions_decorator
from fuelclient import fuelclient_settings
from fuelclient.logs import NullHandler
@@ -36,28 +36,17 @@ class Client(object):
"""
def __init__(self):
conf = fuelclient_settings.get_settings()
self.debug = False
path_to_config = "/etc/fuel/client/config.yaml"
defaults = {
"SERVER_ADDRESS": "127.0.0.1",
"LISTEN_PORT": "8000",
"KEYSTONE_USER": "admin",
"KEYSTONE_PASS": "admin",
}
if os.access(path_to_config, os.R_OK):
with open(path_to_config, "r") as fh:
config = yaml.load(fh.read())
defaults.update(config)
defaults.update(os.environ)
self.root = "http://{SERVER_ADDRESS}:{LISTEN_PORT}".format(**defaults)
self.keystone_base = (
"http://{SERVER_ADDRESS}:{LISTEN_PORT}/keystone/v2.0"
.format(**defaults)
)
self.api_root = self.root + "/api/v1/"
self.ostf_root = self.root + "/ostf/"
self.user = defaults["KEYSTONE_USER"]
self.password = defaults["KEYSTONE_PASS"]
self.root = "http://{server}:{port}".format(server=conf.SERVER_ADDRESS,
port=conf.LISTEN_PORT)
self.keystone_base = urlparse.urljoin(self.root, "/keystone/v2.0")
self.api_root = urlparse.urljoin(self.root, "/api/v1/")
self.ostf_root = urlparse.urljoin(self.root, "/ostf/")
self.user = conf.KEYSTONE_USER
self.password = conf.KEYSTONE_PASS
self._keystone_client = None
self._auth_required = None

View File

@@ -15,7 +15,9 @@
# under the License.
import os
import sys
import six
import yaml
from fuelclient.cli import error
@@ -28,14 +30,15 @@ class FuelClientSettings(object):
"""Represents a model of Fuel Clients settings
Default settigs file are distributed with the source code in
the <DIST_DIR>/fuelclient_settings.yaml.
the <DIST_DIR>/fuelclient_settings.yaml. Those settings can be
overriden by /etc/fuel/client/config.yaml file.
User-specific settings may be stored in any YAML-formatted file
the path to which should be supplied via the FUELCLIENT_CUSTOM_SETTINGS
environment variable. Custom settins override the default ones.
NOTE: This is not to be confused with the API client settings which
requires a different configuration file.
Top level values may also be set as environment variables, e.g.
export SERVER_PORT=8080.
"""
def __init__(self):
@@ -45,12 +48,24 @@ class FuelClientSettings(object):
project_path = os.path.dirname(__file__)
project_settings_file = os.path.join(project_path,
'fuelclient_settings.yaml')
settings_files.append(project_settings_file)
external_default_settings = '/etc/fuel/client/config.yaml'
# Check whether a user specified a custom settings file
test_config = os.environ.get('FUELCLIENT_CUSTOM_SETTINGS')
if test_config:
settings_files.append(test_config)
# NOTE(romcheg): when external default settings file is removed
# this deprecation warning should be removed as well.
if os.path.exists(external_default_settings):
six.print_('DEPRECATION WARNING: file {0} is found and will be '
'used as a source for settings. However, it deprecated '
'and will not be used by default in the ongoing '
'version of '
'python-fuelclient.'.format(external_default_settings),
file=sys.stderr)
self._add_file_if_exists(project_settings_file, settings_files)
self._add_file_if_exists(external_default_settings, settings_files)
# Add a custom settings file specified by user
self._add_file_if_exists(os.environ.get('FUELCLIENT_CUSTOM_SETTINGS'),
settings_files)
self.config = {}
for sf in settings_files:
@@ -62,12 +77,23 @@ class FuelClientSettings(object):
raise error.SettingsException(msg)
self._update_from_env()
def _add_file_if_exists(self, path_to_file, file_list):
if path_to_file and os.access(path_to_file, os.R_OK):
file_list.append(path_to_file)
def _update_from_file(self, path):
with open(path, 'r') as custom_config:
self.config.update(
yaml.load(custom_config.read())
)
def _update_from_env(self):
for k in six.iterkeys(self.config):
if k in os.environ:
self.config[k] = os.environ[k]
def dump(self):
return yaml.dump(self.config)

View File

@@ -1,4 +1,10 @@
# performance tests settings
# Connection settings
SERVER_ADDRESS: "127.0.0.1"
LISTEN_PORT: "8000"
KEYSTONE_USER: "admin"
KEYSTONE_PASS: "admin"
# Performance tests settings
PERFORMANCE_PROFILING_TESTS: 0
PERF_TESTS_PATHS:
perf_tests_base: "/tmp/fuelclient_performance_tests/tests/"