Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 1 | import ConfigParser |
| 2 | |
| 3 | |
| 4 | class NovaConfig(object): |
| 5 | """Provides configuration information for connecting to Nova.""" |
| 6 | |
| 7 | def __init__(self, conf): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame^] | 8 | """Initialize a Nova-specific configuration object""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 9 | self.conf = conf |
| 10 | |
| 11 | def get(self, item_name, default_value): |
| 12 | try: |
| 13 | return self.conf.get("nova", item_name) |
| 14 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
| 15 | return default_value |
| 16 | |
| 17 | @property |
| 18 | def auth_url(self): |
| 19 | """URL used to authenticate. Defaults to 127.0.0.1.""" |
| 20 | return self.get("auth_url", "127.0.0.1") |
| 21 | |
| 22 | @property |
| 23 | def username(self): |
| 24 | """Username to use for Nova API requests. Defaults to 'admin'.""" |
| 25 | return self.get("user", "admin") |
| 26 | |
| 27 | @property |
| 28 | def tenant_name(self): |
| 29 | """Tenant name to use for Nova API requests. Defaults to 'admin'.""" |
| 30 | return self.get("tenant_name", "admin") |
| 31 | |
| 32 | @property |
| 33 | def api_key(self): |
| 34 | """API key to use when authenticating. Defaults to 'admin_key'.""" |
| 35 | return self.get("api_key", "admin_key") |
| 36 | |
| 37 | @property |
| 38 | def build_interval(self): |
| 39 | """Time in seconds between build status checks.""" |
| 40 | return float(self.get("build_interval", 10)) |
| 41 | |
| 42 | @property |
| 43 | def ssh_timeout(self): |
| 44 | """Timeout in seconds to use when connecting via ssh.""" |
| 45 | return float(self.get("ssh_timeout", 300)) |
| 46 | |
| 47 | @property |
| 48 | def build_timeout(self): |
| 49 | """Timeout in seconds to wait for an entity to build.""" |
| 50 | return float(self.get("build_timeout", 300)) |
| 51 | |
| 52 | |
| 53 | class EnvironmentConfig(object): |
| 54 | def __init__(self, conf): |
| 55 | """Initialize a Environment-specific configuration object.""" |
| 56 | self.conf = conf |
| 57 | |
| 58 | def get(self, item_name, default_value): |
| 59 | try: |
| 60 | return self.conf.get("environment", item_name) |
| 61 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
| 62 | return default_value |
| 63 | |
| 64 | @property |
| 65 | def image_ref(self): |
| 66 | """Valid imageRef to use """ |
| 67 | return self.get("image_ref", 3) |
| 68 | |
| 69 | @property |
| 70 | def image_ref_alt(self): |
| 71 | """Valid imageRef to rebuild images with""" |
| 72 | return self.get("image_ref_alt", 3) |
| 73 | |
| 74 | @property |
| 75 | def flavor_ref(self): |
| 76 | """Valid flavorRef to use""" |
| 77 | return int(self.get("flavor_ref", 1)) |
| 78 | |
| 79 | @property |
| 80 | def flavor_ref_alt(self): |
| 81 | """Valid flavorRef to resize images with""" |
| 82 | return self.get("flavor_ref_alt", 2) |
| 83 | |
| 84 | @property |
| 85 | def resize_available(self): |
| 86 | """ Does the test environment support resizing """ |
| 87 | return self.get("resize_available", 'false') != 'false' |
| 88 | |
| 89 | @property |
| 90 | def create_image_enabled(self): |
| 91 | """ Does the test environment support resizing """ |
| 92 | return self.get("create_image_enabled", 'false') != 'false' |
| 93 | |
| 94 | @property |
| 95 | def authentication(self): |
| 96 | """ What auth method does the environment use (basic|keystone) """ |
| 97 | return self.get("authentication", 'keystone') |
| 98 | |
| 99 | |
| 100 | class StormConfig(object): |
| 101 | """Provides OpenStack configuration information.""" |
| 102 | |
| 103 | _path = "etc/storm.conf" |
| 104 | |
| 105 | def __init__(self, path=None): |
| 106 | """Initialize a configuration from a path.""" |
| 107 | self._conf = self.load_config(self._path) |
| 108 | self.nova = NovaConfig(self._conf) |
| 109 | self.env = EnvironmentConfig(self._conf) |
| 110 | |
| 111 | def load_config(self, path=None): |
| 112 | """Read configuration from given path and return a config object.""" |
| 113 | config = ConfigParser.SafeConfigParser() |
| 114 | config.read(path) |
| 115 | return config |