Просмотр исходного кода

Add an option to read provider config values from a file

An INI-style file can be placed in ~/.cloudbridge or /etc/cloudbridge
with configuration values for cloud providers. For example:

[aws]
ec2_region_name = 'eu-central-1'

For the time being, only a single section can be defined per provider
and it must match the provider ID (i.e., aws or openstack).
Enis Afgan 9 лет назад
Родитель
Сommit
9b19e2d6c8

+ 21 - 8
cloudbridge/cloud/base/provider.py

@@ -1,7 +1,10 @@
-"""
-Base implementation of a provider interface
-"""
+"""Base implementation of a provider interface."""
 import os
+try:
+    from config_config_parser import SafeConfigParser
+except ImportError:  # Python 2
+    from ConfigParser import SafeConfigParser
+from os.path import expanduser
 
 from cloudbridge.cloud.interfaces import CloudProvider
 from cloudbridge.cloud.interfaces.resources import Configuration
@@ -10,6 +13,12 @@ DEFAULT_RESULT_LIMIT = 50
 DEFAULT_WAIT_TIMEOUT = 600
 DEFAULT_WAIT_INTERVAL = 5
 
+# By default, use two locations for CloudBridge configuration
+CloudBridgeConfigPath = '/etc/cloudbridge.ini'
+CloudBridgeConfigLocations = [CloudBridgeConfigPath]
+UserConfigPath = os.path.join(expanduser('~'), '.cloudbridge')
+CloudBridgeConfigLocations.append(UserConfigPath)
+
 
 class BaseConfiguration(Configuration):
 
@@ -61,6 +70,8 @@ class BaseCloudProvider(CloudProvider):
 
     def __init__(self, config):
         self._config = BaseConfiguration(config)
+        self._config_parser = SafeConfigParser()
+        self._config_parser.read(CloudBridgeConfigLocations)
 
     @property
     def config(self):
@@ -100,9 +111,11 @@ class BaseCloudProvider(CloudProvider):
 
         :return: a configuration value for the supplied ``key``
         """
-        if isinstance(self.config, dict):
+        if isinstance(self.config, dict) and self.config.get(key):
             return self.config.get(key, default_value)
-        else:
-            return getattr(self.config, key) if hasattr(
-                self.config, key) and getattr(self.config, key) else \
-                default_value
+        elif hasattr(self.config, key) and getattr(self.config, key):
+            return getattr(self.config, key)
+        elif (self._config_parser.has_option(self.PROVIDER_ID, key) and
+              self._config_parser.get(self.PROVIDER_ID, key)):
+            return self._config_parser.get(self.PROVIDER_ID, key)
+        return default_value

+ 1 - 1
cloudbridge/cloud/providers/openstack/provider.py

@@ -52,7 +52,7 @@ class OpenStackCloudProvider(BaseCloudProvider):
             'os_identity_api_version',
             os.environ.get('OS_IDENTITY_API_VERSION', None))
         # Allow individual service connections to have their own values but
-        # default to a the ones defined above.
+        # default to the ones defined above.
         self.swift_username = self._get_config_value(
             'os_swift_username',
             os.environ.get('OS_SWIFT_USERNAME', self.username))