Tony Mack | f8a1a61 | 2014-05-06 23:42:40 -0400 | [diff] [blame] | 1 | import urlparse |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 2 | try: |
| 3 | from keystoneclient.v2_0 import client as keystone_client |
| 4 | from glance import client as glance_client |
Tony Mack | f8a1a61 | 2014-05-06 23:42:40 -0400 | [diff] [blame] | 5 | import glanceclient |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 6 | from novaclient.v1_1 import client as nova_client |
| 7 | from quantumclient.v2_0 import client as quantum_client |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 8 | from nova.db.sqlalchemy import api as nova_db_api |
| 9 | from nova.context import get_admin_context |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 10 | from keystone.common.sql import core |
| 11 | core.CONF(args=[], project='keystone', default_config_files=['/etc/keystone/keystone.conf']) |
| 12 | from keystone.identity.backends.sql import Metadata |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 13 | has_openstack = True |
| 14 | except: |
| 15 | has_openstack = False |
| 16 | |
| 17 | from planetstack.config import Config |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 18 | from deployment_auth import deployment_auth |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 19 | |
| 20 | def require_enabled(callable): |
| 21 | def wrapper(*args, **kwds): |
| 22 | if has_openstack: |
| 23 | return callable(*args, **kwds) |
| 24 | else: |
| 25 | return None |
| 26 | return wrapper |
| 27 | |
| 28 | def parse_novarc(filename): |
| 29 | opts = {} |
| 30 | f = open(filename, 'r') |
| 31 | for line in f: |
| 32 | try: |
| 33 | line = line.replace('export', '').strip() |
| 34 | parts = line.split('=') |
| 35 | if len(parts) > 1: |
| 36 | value = parts[1].replace("\'", "") |
| 37 | value = value.replace('\"', '') |
| 38 | opts[parts[0]] = value |
| 39 | except: |
| 40 | pass |
| 41 | f.close() |
| 42 | return opts |
| 43 | |
| 44 | class Client: |
Tony Mack | 49e839c | 2014-04-07 19:49:01 -0400 | [diff] [blame] | 45 | def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, admin=True, *args, **kwds): |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 46 | |
| 47 | |
| 48 | if not deployment or deployment not in deployment_auth: |
| 49 | auth = deployment_auth['default'] |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 50 | else: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 51 | auth = deployment_auth[deployment] |
| 52 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 53 | self.has_openstack = has_openstack |
Tony Mack | 49e839c | 2014-04-07 19:49:01 -0400 | [diff] [blame] | 54 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 55 | self.url = auth['url'] |
Tony Mack | 49e839c | 2014-04-07 19:49:01 -0400 | [diff] [blame] | 56 | if admin: |
| 57 | self.username = auth['user'] |
| 58 | self.password = auth['password'] |
| 59 | self.tenant = auth['tenant'] |
| 60 | self.endpoint = auth['endpoint'] |
| 61 | self.token = auth['token'] |
| 62 | else: |
| 63 | self.username = None |
| 64 | self.password = None |
| 65 | self.tenant = None |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 66 | |
| 67 | if username: |
| 68 | self.username = username |
| 69 | if password: |
| 70 | self.password = password |
| 71 | if tenant: |
| 72 | self.tenant = tenant |
| 73 | if url: |
| 74 | self.url = url |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 75 | if token: |
| 76 | self.token = token |
| 77 | if endpoint: |
| 78 | self.endpoint = endpoint |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 79 | |
Tony Mack | 976d774 | 2014-03-18 22:00:52 -0400 | [diff] [blame] | 80 | #if '@' in self.username: |
| 81 | # self.username = self.username[:self.username.index('@')] |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 82 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 83 | class KeystoneDB: |
| 84 | @require_enabled |
| 85 | def get_session(self): |
| 86 | return core.Base().get_session() |
| 87 | |
| 88 | @require_enabled |
| 89 | def get_metadata(self): |
| 90 | session = self.get_session() |
| 91 | return session.query(Metadata).all() |
| 92 | |
| 93 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 94 | class KeystoneClient(Client): |
| 95 | def __init__(self, *args, **kwds): |
| 96 | Client.__init__(self, *args, **kwds) |
| 97 | if has_openstack: |
| 98 | self.client = keystone_client.Client(username=self.username, |
| 99 | password=self.password, |
| 100 | tenant_name=self.tenant, |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 101 | auth_url=self.url, |
| 102 | endpoint=self.endpoint, |
| 103 | token=self.token |
| 104 | ) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 105 | |
| 106 | @require_enabled |
| 107 | def connect(self, *args, **kwds): |
| 108 | self.__init__(*args, **kwds) |
| 109 | |
| 110 | @require_enabled |
| 111 | def __getattr__(self, name): |
| 112 | return getattr(self.client, name) |
| 113 | |
| 114 | |
| 115 | class GlanceClient(Client): |
| 116 | def __init__(self, *args, **kwds): |
| 117 | Client.__init__(self, *args, **kwds) |
| 118 | if has_openstack: |
| 119 | self.client = glance_client.get_client(host='0.0.0.0', |
| 120 | username=self.username, |
| 121 | password=self.password, |
| 122 | tenant=self.tenant, |
| 123 | auth_url=self.url) |
| 124 | @require_enabled |
| 125 | def __getattr__(self, name): |
| 126 | return getattr(self.client, name) |
| 127 | |
Tony Mack | f8a1a61 | 2014-05-06 23:42:40 -0400 | [diff] [blame] | 128 | class GlanceClientNew(Client): |
| 129 | def __init__(self, version, endpoint, token, *args, **kwds): |
| 130 | Client.__init__(self, *args, **kwds) |
| 131 | if has_openstack: |
| 132 | self.client = glanceclient.Client(version, endpoint=endpoint, token=token) |
| 133 | |
| 134 | @require_enabled |
| 135 | def __getattr__(self, name): |
| 136 | return getattr(self.client, name) |
| 137 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 138 | class NovaClient(Client): |
| 139 | def __init__(self, *args, **kwds): |
| 140 | Client.__init__(self, *args, **kwds) |
| 141 | if has_openstack: |
| 142 | self.client = nova_client.Client(username=self.username, |
| 143 | api_key=self.password, |
| 144 | project_id=self.tenant, |
| 145 | auth_url=self.url, |
| 146 | region_name='', |
| 147 | extensions=[], |
| 148 | service_type='compute', |
| 149 | service_name='', |
| 150 | ) |
| 151 | |
| 152 | @require_enabled |
| 153 | def connect(self, *args, **kwds): |
| 154 | self.__init__(*args, **kwds) |
| 155 | |
| 156 | @require_enabled |
| 157 | def __getattr__(self, name): |
| 158 | return getattr(self.client, name) |
| 159 | |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 160 | class NovaDB(Client): |
| 161 | def __init__(self, *args, **kwds): |
| 162 | Client.__init__(self, *args, **kwds) |
| 163 | if has_openstack: |
| 164 | self.ctx = get_admin_context() |
Tony Mack | 7b0dad0 | 2013-06-10 13:42:21 -0400 | [diff] [blame] | 165 | nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf']) |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 166 | self.client = nova_db_api |
| 167 | |
| 168 | |
| 169 | @require_enabled |
| 170 | def connect(self, *args, **kwds): |
| 171 | self.__init__(*args, **kwds) |
| 172 | |
| 173 | @require_enabled |
| 174 | def __getattr__(self, name): |
| 175 | return getattr(self.client, name) |
| 176 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 177 | class QuantumClient(Client): |
| 178 | def __init__(self, *args, **kwds): |
| 179 | Client.__init__(self, *args, **kwds) |
| 180 | if has_openstack: |
| 181 | self.client = quantum_client.Client(username=self.username, |
| 182 | password=self.password, |
| 183 | tenant_name=self.tenant, |
| 184 | auth_url=self.url) |
| 185 | @require_enabled |
| 186 | def connect(self, *args, **kwds): |
| 187 | self.__init__(*args, **kwds) |
| 188 | |
| 189 | @require_enabled |
| 190 | def __getattr__(self, name): |
| 191 | return getattr(self.client, name) |
| 192 | |
| 193 | class OpenStackClient: |
| 194 | """ |
| 195 | A simple native shell to the openstack backend services. |
| 196 | This class can receive all nova calls to the underlying testbed |
| 197 | """ |
| 198 | |
| 199 | def __init__ ( self, *args, **kwds) : |
| 200 | # instantiate managers |
| 201 | self.keystone = KeystoneClient(*args, **kwds) |
Tony Mack | f8a1a61 | 2014-05-06 23:42:40 -0400 | [diff] [blame] | 202 | url_parsed = urlparse.urlparse(self.keystone.url) |
| 203 | hostname = url_parsed.netloc.split(':')[0] |
| 204 | token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 205 | self.keystone_db = KeystoneDB() |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 206 | self.glance = GlanceClient(*args, **kwds) |
Tony Mack | f8a1a61 | 2014-05-06 23:42:40 -0400 | [diff] [blame] | 207 | |
| 208 | self.glanceclient = GlanceClientNew('1', endpoint='http://%s:9292' % hostname, token=token.id) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 209 | self.nova = NovaClient(*args, **kwds) |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 210 | self.nova_db = NovaDB(*args, **kwds) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 211 | self.quantum = QuantumClient(*args, **kwds) |
Tony Mack | f8a1a61 | 2014-05-06 23:42:40 -0400 | [diff] [blame] | 212 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 213 | |
| 214 | @require_enabled |
| 215 | def connect(self, *args, **kwds): |
| 216 | self.__init__(*args, **kwds) |
| 217 | |
| 218 | @require_enabled |
| 219 | def authenticate(self): |
| 220 | return self.keystone.authenticate() |