diff --git a/README.rst b/README.rst index 1eebb89..87ce690 100644 --- a/README.rst +++ b/README.rst @@ -47,6 +47,13 @@ Build the connection to the cloud deployment and verify it: distractor = os_faults.connect(cloud_config) distractor.verify() +The library can also read configuration from the file specified in +`OS_FAULTS_CONFIG` environment variable or read it from one of default +locations: + * current directory + * ~/.config/os-faults + * /etc/openstack + Make some distraction: .. code-block:: python diff --git a/os_faults/__init__.py b/os_faults/__init__.py index c3ab296..bd82a25 100644 --- a/os_faults/__init__.py +++ b/os_faults/__init__.py @@ -10,8 +10,13 @@ # License for the specific language governing permissions and limitations # under the License. -import pbr.version +import os +import appdirs +import pbr.version +import yaml + +from os_faults.api import error from os_faults.drivers import devstack from os_faults.drivers import fuel from os_faults.drivers import ipmi @@ -21,7 +26,38 @@ __version__ = pbr.version.VersionInfo( 'os_faults').version_string() -def connect(cloud_config): +APPDIRS = appdirs.AppDirs(appname='openstack', appauthor='OpenStack') +UNIX_SITE_CONFIG_HOME = '/etc/openstack' +CONFIG_SEARCH_PATH = [ + os.getcwd(), + APPDIRS.user_config_dir, + UNIX_SITE_CONFIG_HOME, +] +CONFIG_FILES = [ + os.path.join(d, 'os-faults' + s) + for d in CONFIG_SEARCH_PATH + for s in ['.json', '.yaml', '.yml'] +] + + +def _read_config(): + os_faults_config = os.environ.get('OS_FAULTS_CONFIG') + if os_faults_config: + CONFIG_FILES.insert(0, os_faults_config) + + for config_file in CONFIG_FILES: + if os.path.exists(config_file): + with open(config_file) as fd: + return yaml.safe_load(fd.read()) + + msg = 'Config file is not found on any of paths: {}'.format(CONFIG_FILES) + raise error.OSFError(msg) + + +def connect(cloud_config=None): + if not cloud_config: + cloud_config = _read_config() + cloud_management = None cloud_management_params = cloud_config.get('cloud_management') or {} diff --git a/requirements.txt b/requirements.txt index 769a407..7a72d2d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ pbr>=1.6 ansible>=2.0 +appdirs>=1.3.0 # MIT License iso8601>=0.1.9 oslo.i18n>=1.5.0 # Apache-2.0 oslo.log>=1.12.0 # Apache-2.0 @@ -12,4 +13,5 @@ oslo.serialization>=1.10.0 # Apache-2.0 oslo.utils!=2.6.0,>=2.4.0 # Apache-2.0 libvirt-python>=1.2.5 # LGPLv2+ pyghmi>=1.0.3 # Apache-2.0 +PyYAML>=3.1.0 # MIT six>=1.9.0