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