Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 1 | import urlparse |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 2 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 3 | try: |
| 4 | from keystoneauth1.identity import v2 as keystoneauth_v2 |
| 5 | from keystoneauth1 import session as keystone_session |
| 6 | from keystoneclient.v2_0 import client as keystone_client |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 7 | # from glance import client as glance_client |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 8 | import glanceclient |
| 9 | from novaclient.v2 import client as nova_client |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 10 | from neutronclient.v2_0 import client as neutron_client |
| 11 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 12 | has_openstack = True |
| 13 | except: |
| 14 | has_openstack = False |
| 15 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 16 | from xosconfig import Config |
| 17 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 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 |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 25 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 26 | return wrapper |
| 27 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 28 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 29 | def parse_novarc(filename): |
| 30 | opts = {} |
| 31 | f = open(filename, 'r') |
| 32 | for line in f: |
| 33 | try: |
| 34 | line = line.replace('export', '').strip() |
| 35 | parts = line.split('=') |
| 36 | if len(parts) > 1: |
| 37 | value = parts[1].replace("\'", "") |
| 38 | value = value.replace('\"', '') |
| 39 | opts[parts[0]] = value |
| 40 | except: |
| 41 | pass |
| 42 | f.close() |
| 43 | return opts |
| 44 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 45 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 46 | class Client: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 47 | def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None, |
| 48 | cacert=None, admin=True, *args, **kwds): |
| 49 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 50 | self.has_openstack = has_openstack |
| 51 | self.url = controller.auth_url |
| 52 | if admin: |
| 53 | self.username = controller.admin_user |
| 54 | self.password = controller.admin_password |
| 55 | self.tenant = controller.admin_tenant |
| 56 | else: |
| 57 | self.username = None |
| 58 | self.password = None |
| 59 | self.tenant = None |
| 60 | |
| 61 | if username: |
| 62 | self.username = username |
| 63 | if password: |
| 64 | self.password = password |
| 65 | if tenant: |
| 66 | self.tenant = tenant |
| 67 | if url: |
| 68 | self.url = url |
| 69 | if token: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 70 | self.token = token |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 71 | if endpoint: |
| 72 | self.endpoint = endpoint |
| 73 | |
| 74 | if cacert: |
| 75 | self.cacert = cacert |
| 76 | else: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 77 | self.cacert = Config.get("nova.ca_ssl_cert") |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 78 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 79 | |
| 80 | class KeystoneClient(Client): |
| 81 | def __init__(self, *args, **kwds): |
| 82 | Client.__init__(self, *args, **kwds) |
| 83 | if has_openstack: |
| 84 | auth = keystoneauth_v2.Password(username=self.username, |
| 85 | password=self.password, |
| 86 | tenant_name=self.tenant, |
| 87 | auth_url=self.url, |
| 88 | ) |
| 89 | sess = keystone_session.Session(auth=auth, verify=self.cacert, ) |
| 90 | self.client = keystone_client.Client(session=sess) |
| 91 | |
| 92 | @require_enabled |
| 93 | def connect(self, *args, **kwds): |
| 94 | self.__init__(*args, **kwds) |
| 95 | |
| 96 | @require_enabled |
| 97 | def __getattr__(self, name): |
| 98 | return getattr(self.client, name) |
| 99 | |
| 100 | |
| 101 | class Glance(Client): |
| 102 | def __init__(self, *args, **kwds): |
| 103 | Client.__init__(self, *args, **kwds) |
| 104 | if has_openstack: |
| 105 | self.client = glanceclient.get_client(host='0.0.0.0', |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 106 | username=self.username, |
| 107 | password=self.password, |
| 108 | tenant=self.tenant, |
| 109 | auth_url=self.url, |
| 110 | cacert=self.cacert |
| 111 | ) |
| 112 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 113 | @require_enabled |
| 114 | def __getattr__(self, name): |
| 115 | return getattr(self.client, name) |
| 116 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 117 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 118 | class GlanceClient(Client): |
| 119 | def __init__(self, version, endpoint, token, cacert=None, *args, **kwds): |
| 120 | Client.__init__(self, *args, **kwds) |
| 121 | if has_openstack: |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 122 | self.client = glanceclient.Client(version, |
| 123 | endpoint=endpoint, |
| 124 | token=token, |
| 125 | cacert=cacert |
| 126 | ) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 127 | |
| 128 | @require_enabled |
| 129 | def __getattr__(self, name): |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 130 | return getattr(self.client, name) |
| 131 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 132 | |
| 133 | class NovaClient(Client): |
| 134 | def __init__(self, *args, **kwds): |
| 135 | Client.__init__(self, *args, **kwds) |
| 136 | if has_openstack: |
| 137 | self.client = nova_client.client.Client( |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 138 | "2", |
| 139 | username=self.username, |
| 140 | api_key=self.password, |
| 141 | project_id=self.tenant, |
| 142 | auth_url=self.url, |
| 143 | region_name='', |
| 144 | extensions=[], |
| 145 | service_type='compute', |
| 146 | service_name='', |
| 147 | cacert=self.cacert |
| 148 | ) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 149 | |
| 150 | @require_enabled |
| 151 | def connect(self, *args, **kwds): |
| 152 | self.__init__(*args, **kwds) |
| 153 | |
| 154 | @require_enabled |
| 155 | def __getattr__(self, name): |
| 156 | return getattr(self.client, name) |
| 157 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 158 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 159 | class NovaDB(Client): |
| 160 | def __init__(self, *args, **kwds): |
| 161 | Client.__init__(self, *args, **kwds) |
| 162 | if has_openstack: |
| 163 | self.ctx = get_admin_context() |
| 164 | nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf']) |
| 165 | self.client = nova_db_api |
| 166 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 167 | @require_enabled |
| 168 | def connect(self, *args, **kwds): |
| 169 | self.__init__(*args, **kwds) |
| 170 | |
| 171 | @require_enabled |
| 172 | def __getattr__(self, name): |
| 173 | return getattr(self.client, name) |
| 174 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 175 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 176 | class NeutronClient(Client): |
| 177 | def __init__(self, *args, **kwds): |
| 178 | Client.__init__(self, *args, **kwds) |
| 179 | if has_openstack: |
| 180 | self.client = neutron_client.Client(username=self.username, |
| 181 | password=self.password, |
| 182 | tenant_name=self.tenant, |
| 183 | auth_url=self.url, |
| 184 | ca_cert=self.cacert |
| 185 | ) |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 186 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 187 | @require_enabled |
| 188 | def connect(self, *args, **kwds): |
| 189 | self.__init__(*args, **kwds) |
| 190 | |
| 191 | @require_enabled |
| 192 | def __getattr__(self, name): |
| 193 | return getattr(self.client, name) |
| 194 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 195 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 196 | class OpenStackClient: |
| 197 | """ |
| 198 | A simple native shell to the openstack backend services. |
| 199 | This class can receive all nova calls to the underlying testbed |
| 200 | """ |
| 201 | |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 202 | def __init__(self, *args, **kwds): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 203 | # instantiate managers |
| 204 | self.keystone = KeystoneClient(*args, **kwds) |
| 205 | url_parsed = urlparse.urlparse(self.keystone.url) |
| 206 | hostname = url_parsed.netloc.split(':')[0] |
Matteo Scandolo | ceccb1f | 2017-06-05 10:35:44 -0700 | [diff] [blame] | 207 | token = self.keystone.client.tokens.authenticate(username=self.keystone.username, |
| 208 | password=self.keystone.password, |
| 209 | tenant_name=self.keystone.tenant) |
| 210 | # glance_endpoint = self.keystone.client.service_catalog.url_for(service_type='image', endpoint_type='publicURL') |
| 211 | # self.glanceclient = GlanceClient('1', endpoint=glance_endpoint, token=token.id, **kwds) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 212 | self.nova = NovaClient(*args, **kwds) |
| 213 | # self.nova_db = NovaDB(*args, **kwds) |
| 214 | self.neutron = NeutronClient(*args, **kwds) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 215 | |
| 216 | @require_enabled |
| 217 | def connect(self, *args, **kwds): |
| 218 | self.__init__(*args, **kwds) |
| 219 | |
| 220 | @require_enabled |
| 221 | def authenticate(self): |
| 222 | return self.keystone.authenticate() |