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