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