Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 1 | import ConfigParser |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame^] | 2 | import logging |
| 3 | import os |
| 4 | |
| 5 | LOG = logging.getLogger(__name__) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class NovaConfig(object): |
| 9 | """Provides configuration information for connecting to Nova.""" |
| 10 | |
| 11 | def __init__(self, conf): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 12 | """Initialize a Nova-specific configuration object""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 13 | self.conf = conf |
| 14 | |
| 15 | def get(self, item_name, default_value): |
| 16 | try: |
| 17 | return self.conf.get("nova", item_name) |
| 18 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
| 19 | return default_value |
| 20 | |
| 21 | @property |
Rohit Karajgi | e1b050d | 2011-12-02 16:13:18 -0800 | [diff] [blame] | 22 | def host(self): |
| 23 | """Host IP for making Nova API requests. Defaults to '127.0.0.1'.""" |
| 24 | return self.get("host", "127.0.1") |
| 25 | |
| 26 | @property |
| 27 | def port(self): |
| 28 | """Listen port of the Nova service.""" |
| 29 | return self.get("port", "8773") |
| 30 | |
| 31 | @property |
| 32 | def apiVer(self): |
| 33 | """Version of the API""" |
| 34 | return self.get("apiVer", "v1.1") |
| 35 | |
| 36 | @property |
| 37 | def path(self): |
| 38 | """Path of API request""" |
| 39 | return self.get("path", "/") |
| 40 | |
| 41 | def params(self): |
| 42 | """Parameters to be passed with the API request""" |
| 43 | return self.get("params", "") |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 44 | |
| 45 | @property |
| 46 | def username(self): |
| 47 | """Username to use for Nova API requests. Defaults to 'admin'.""" |
| 48 | return self.get("user", "admin") |
| 49 | |
| 50 | @property |
| 51 | def tenant_name(self): |
| 52 | """Tenant name to use for Nova API requests. Defaults to 'admin'.""" |
| 53 | return self.get("tenant_name", "admin") |
| 54 | |
| 55 | @property |
| 56 | def api_key(self): |
| 57 | """API key to use when authenticating. Defaults to 'admin_key'.""" |
| 58 | return self.get("api_key", "admin_key") |
| 59 | |
| 60 | @property |
| 61 | def build_interval(self): |
| 62 | """Time in seconds between build status checks.""" |
| 63 | return float(self.get("build_interval", 10)) |
| 64 | |
| 65 | @property |
| 66 | def ssh_timeout(self): |
| 67 | """Timeout in seconds to use when connecting via ssh.""" |
| 68 | return float(self.get("ssh_timeout", 300)) |
| 69 | |
| 70 | @property |
| 71 | def build_timeout(self): |
| 72 | """Timeout in seconds to wait for an entity to build.""" |
| 73 | return float(self.get("build_timeout", 300)) |
| 74 | |
| 75 | |
| 76 | class EnvironmentConfig(object): |
| 77 | def __init__(self, conf): |
| 78 | """Initialize a Environment-specific configuration object.""" |
| 79 | self.conf = conf |
| 80 | |
| 81 | def get(self, item_name, default_value): |
| 82 | try: |
| 83 | return self.conf.get("environment", item_name) |
| 84 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
| 85 | return default_value |
| 86 | |
| 87 | @property |
| 88 | def image_ref(self): |
| 89 | """Valid imageRef to use """ |
| 90 | return self.get("image_ref", 3) |
| 91 | |
| 92 | @property |
| 93 | def image_ref_alt(self): |
| 94 | """Valid imageRef to rebuild images with""" |
| 95 | return self.get("image_ref_alt", 3) |
| 96 | |
| 97 | @property |
| 98 | def flavor_ref(self): |
| 99 | """Valid flavorRef to use""" |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 100 | return self.get("flavor_ref", 1) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 101 | |
| 102 | @property |
| 103 | def flavor_ref_alt(self): |
| 104 | """Valid flavorRef to resize images with""" |
| 105 | return self.get("flavor_ref_alt", 2) |
| 106 | |
| 107 | @property |
| 108 | def resize_available(self): |
| 109 | """ Does the test environment support resizing """ |
| 110 | return self.get("resize_available", 'false') != 'false' |
| 111 | |
| 112 | @property |
| 113 | def create_image_enabled(self): |
| 114 | """ Does the test environment support resizing """ |
| 115 | return self.get("create_image_enabled", 'false') != 'false' |
| 116 | |
| 117 | @property |
| 118 | def authentication(self): |
| 119 | """ What auth method does the environment use (basic|keystone) """ |
| 120 | return self.get("authentication", 'keystone') |
| 121 | |
| 122 | |
| 123 | class StormConfig(object): |
| 124 | """Provides OpenStack configuration information.""" |
| 125 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame^] | 126 | def __init__(self, conf_dir, conf_file): |
| 127 | """ |
| 128 | Initialize a configuration from a conf directory and conf file. |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 129 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame^] | 130 | :param conf_dir: Directory to look for config files |
| 131 | :param conf_file: Name of config file to use |
| 132 | """ |
| 133 | path = os.path.join(conf_dir, conf_file) |
| 134 | |
| 135 | if not os.path.exists(path): |
| 136 | msg = "Config file %(path)s not found" % locals() |
| 137 | raise RuntimeError(msg) |
| 138 | |
| 139 | self._conf = self.load_config(path) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 140 | self.nova = NovaConfig(self._conf) |
| 141 | self.env = EnvironmentConfig(self._conf) |
| 142 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame^] | 143 | def load_config(self, path): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 144 | """Read configuration from given path and return a config object.""" |
| 145 | config = ConfigParser.SafeConfigParser() |
| 146 | config.read(path) |
| 147 | return config |