blob: 21ec9ed8bfe395aa18736e0db051abd72fc214f8 [file] [log] [blame]
Matteo Scandolof0441032017-08-08 13:05:26 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
gongysh6c0804c2018-01-27 16:38:28 +080016import types
Scott Bakerb63ea792016-08-11 10:24:48 -070017import commands
18import hashlib
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070019from xosconfig import Config
Scott Bakeraf599eb2017-03-21 12:43:26 -070020from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -070021
22try:
Scott Baker04a37f52016-08-11 10:52:21 -070023 from synchronizers.openstack.client import OpenStackClient
Scott Bakerb63ea792016-08-11 10:24:48 -070024 has_openstack = True
25except:
26 has_openstack = False
27
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070028manager_enabled = Config.get("nova.enabled")
Scott Bakerb63ea792016-08-11 10:24:48 -070029
gongysh6c0804c2018-01-27 16:38:28 +080030
31def _findall(self, **kwargs):
32 if 'id' in kwargs:
33 return [self.get(id)]
34 return self.list(**kwargs)
35
36
37def extend_v3_attr(keystone, base_attr, **kwargs):
38 if 'v2.0' != keystone.version:
39 base_attr.update(kwargs)
40
41
Scott Bakerb63ea792016-08-11 10:24:48 -070042class OpenStackDriver:
43
44 def __init__(self, config = None, client=None):
Scott Bakerb63ea792016-08-11 10:24:48 -070045
46 if client:
47 self.shell = client
gongysh6c0804c2018-01-27 16:38:28 +080048 if 'v2.0' != self.shell.keystone.version:
49 self.shell.keystone.projects.findall = types.MethodType(
50 _findall, self.shell.keystone.projects)
51 self.shell.keystone.tenants = self.shell.keystone.projects
52 self.shell.keystone.roles.findall = types.MethodType(
53 _findall, self.shell.keystone.roles)
54 self.shell.keystone.users.findall = types.MethodType(
55 _findall, self.shell.keystone.users)
Scott Bakerb63ea792016-08-11 10:24:48 -070056
57 self.enabled = manager_enabled
58 self.has_openstack = has_openstack
59 self.controller = None
60 self.admin_user = None
61
62 def client_driver(self, caller=None, tenant=None, controller=None):
63 if caller:
64 auth = {'username': caller.email,
65 'password': hashlib.md5(caller.password).hexdigest()[:6],
66 'tenant': tenant}
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070067 client = OpenStackClient(controller=controller, cacert=Config.get("nova.ca_ssl_cert"), **auth)
Scott Bakerb63ea792016-08-11 10:24:48 -070068 else:
69 admin_driver = self.admin_driver(tenant=tenant, controller=controller)
70 client = OpenStackClient(tenant=tenant, controller=admin_driver.controller)
71
72 driver = OpenStackDriver(client=client)
73 #driver.admin_user = admin_driver.admin_user
74 #driver.controller = admin_driver.controller
75 return driver
76
77 def admin_driver(self, tenant=None, controller=None):
78 if isinstance(controller, int):
79 controller = Controller.objects.get(id=controller.id)
80 if not tenant:
81 tenant = controller.admin_tenant
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070082 client = OpenStackClient(tenant=tenant, controller=controller, cacert=Config.get("nova.ca_ssl_cert"))
Scott Bakerb63ea792016-08-11 10:24:48 -070083 driver = OpenStackDriver(client=client)
84 driver.admin_user = client.keystone.users.find(name=controller.admin_user)
85 driver.controller = controller
86 return driver
87
88 def create_role(self, name):
89 roles = self.shell.keystone.roles.findall(name=name)
90 roles_title = self.shell.keystone.roles.findall(name=name.title())
91 roles_found = roles + roles_title
92 if not roles_found:
93 role = self.shell.keystone.roles.create(name)
94 else:
95 role = roles_found[0]
96 return role
97
98 def delete_role(self, filter):
99 roles = self.shell.keystone.roles.findall(**filter)
100 for role in roles:
101 self.shell.keystone.roles.delete(role)
102 return 1
103
gongysh6c0804c2018-01-27 16:38:28 +0800104 def create_tenant(self, tenant_name, enabled, description, domain_id='default'):
Scott Bakerb63ea792016-08-11 10:24:48 -0700105 """Create keystone tenant. Suggested fields: name, description, enabled"""
Scott Bakerb63ea792016-08-11 10:24:48 -0700106 if not tenants:
107 fields = {'tenant_name': tenant_name, 'enabled': enabled,
108 'description': description}
gongysh6c0804c2018-01-27 16:38:28 +0800109 extend_v3_attr(self.shell.keystone, fields, domain_id=domain_id)
Scott Bakerb63ea792016-08-11 10:24:48 -0700110 tenant = self.shell.keystone.tenants.create(**fields)
111 else:
112 tenant = tenants[0]
113
114 # always give the admin user the admin role to any tenant created
115 # by the driver.
116 self.add_user_role(self.admin_user.id, tenant.id, 'admin')
117 return tenant
118
119 def update_tenant(self, id, **kwds):
120 return self.shell.keystone.tenants.update(id, **kwds)
121
122 def delete_tenant(self, id):
123 # FIXME: nova_db is commented out in clients.py, throws errors.
124 # Commenting this out for the time being until actually fixed
125
126 #ctx = self.shell.nova_db.ctx
127 tenants = self.shell.keystone.tenants.findall(id=id)
128 for tenant in tenants:
129 # nova does not automatically delete the tenant's instances
130 # so we manually delete instances before deleting the tenant
131 #instances = self.shell.nova_db.instance_get_all_by_filters(ctx,
132 # {'project_id': tenant.id}, 'id', 'asc')
133 #client = OpenStackClient(tenant=tenant.name)
134 #driver = OpenStackDriver(client=client)
135 #for instance in instances:
136 # driver.destroy_instance(instance.id)
137 self.shell.keystone.tenants.delete(tenant)
138 return 1
139
gongysh6c0804c2018-01-27 16:38:28 +0800140
141 def create_user(self, name, email, password, enabled, domain_id='default'):
Scott Bakerb63ea792016-08-11 10:24:48 -0700142 users = self.shell.keystone.users.findall(email=email)
143 if not users:
144 fields = {'name': name, 'email': email, 'password': password,
145 'enabled': enabled}
gongysh6c0804c2018-01-27 16:38:28 +0800146 extend_v3_attr(self.shell.keystone, fields, domain_id=domain_id)
Scott Bakerb63ea792016-08-11 10:24:48 -0700147 user = self.shell.keystone.users.create(**fields)
148 else:
149 user = users[0]
150 return user
151
152 def delete_user(self, id):
153 users = self.shell.keystone.users.findall(id=id)
154 for user in users:
155 # delete users keys
gongysh6c0804c2018-01-27 16:38:28 +0800156 if 'v2.0' == self.shell.keystone.version:
157 keys = self.shell.nova.keypairs.findall()
158 else:
159 keys = self.shell.nova.keypairs.findall(user_id=id)
Scott Bakerb63ea792016-08-11 10:24:48 -0700160 for key in keys:
161 self.shell.nova.keypairs.delete(key)
162 self.shell.keystone.users.delete(user)
163 return 1
164
165 def get_admin_role(self):
166 role = None
167 for admin_role_name in ['admin', 'Admin']:
168 roles = self.shell.keystone.roles.findall(name=admin_role_name)
169 if roles:
170 role = roles[0]
171 break
172 return role
173
174 def add_user_role(self, kuser_id, tenant_id, role_name):
175 user = self.shell.keystone.users.find(id=kuser_id)
176 tenant = self.shell.keystone.tenants.find(id=tenant_id)
177 # admin role can be lowercase or title. Look for both
178 role = None
179 if role_name.lower() == 'admin':
180 role = self.get_admin_role()
181 else:
182 # look up non admin role or force exception when admin role isnt found
183 role = self.shell.keystone.roles.find(name=role_name)
184
185 role_found = False
186 user_roles = user.list_roles(tenant.id)
187 for user_role in user_roles:
188 if user_role.name == role.name:
189 role_found = True
190 if not role_found:
191 tenant.add_user(user, role)
192
193 return 1
194
195 def delete_user_role(self, kuser_id, tenant_id, role_name):
196 user = self.shell.keystone.users.find(id=kuser_id)
197 tenant = self.shell.keystone.tenants.find(id=tenant_id)
198 # admin role can be lowercase or title. Look for both
199 role = None
200 if role_name.lower() == 'admin':
201 role = self.get_admin_role()
202 else:
203 # look up non admin role or force exception when admin role isnt found
204 role = self.shell.keystone.roles.find(name=role_name)
205
206 role_found = False
207 user_roles = user.list_roles(tenant.id)
208 for user_role in user_roles:
209 if user_role.name == role.name:
210 role_found = True
211 if role_found:
212 tenant.remove_user(user, role)
213
214 return 1
215
216 def update_user(self, id, fields):
217 if 'password' in fields:
218 self.shell.keystone.users.update_password(id, fields['password'])
219 if 'enabled' in fields:
220 self.shell.keystone.users.update_enabled(id, fields['enabled'])
221 return 1
222
223 def create_router(self, name, set_gateway=True):
224 routers = self.shell.neutron.list_routers(name=name)['routers']
225 if routers:
226 router = routers[0]
227 else:
228 router = self.shell.neutron.create_router({'router': {'name': name}})['router']
229 # add router to external network
230 if set_gateway:
231 nets = self.shell.neutron.list_networks()['networks']
232 for net in nets:
233 if net['router:external'] == True:
234 self.shell.neutron.add_gateway_router(router['id'],
235 {'network_id': net['id']})
236
237 return router
238
239 def delete_router(self, id):
240 routers = self.shell.neutron.list_routers(id=id)['routers']
241 for router in routers:
242 self.shell.neutron.delete_router(router['id'])
243 # remove router form external network
244 #nets = self.shell.neutron.list_networks()['networks']
245 #for net in nets:
246 # if net['router:external'] == True:
247 # self.shell.neutron.remove_gateway_router(router['id'])
248
249 def add_router_interface(self, router_id, subnet_id):
250 router = self.shell.neutron.show_router(router_id)['router']
251 subnet = self.shell.neutron.show_subnet(subnet_id)['subnet']
252 if router and subnet:
253 self.shell.neutron.add_interface_router(router_id, {'subnet_id': subnet_id})
254
255 def delete_router_interface(self, router_id, subnet_id):
256 router = self.shell.neutron.show_router(router_id)
257 subnet = self.shell.neutron.show_subnet(subnet_id)
258 if router and subnet:
259 self.shell.neutron.remove_interface_router(router_id, {'subnet_id': subnet_id})
260
261 def create_network(self, name, shared=False):
262 nets = self.shell.neutron.list_networks(name=name)['networks']
263 if nets:
264 net = nets[0]
265 else:
266 net = self.shell.neutron.create_network({'network': {'name': name, 'shared': shared}})['network']
267 return net
268
269 def delete_network(self, id):
270 nets = self.shell.neutron.list_networks()['networks']
271 for net in nets:
272 if net['id'] == id:
273 # delete_all ports
274 self.delete_network_ports(net['id'])
275 # delete all subnets:
276 for subnet_id in net['subnets']:
277 self.delete_subnet(subnet_id)
278 self.shell.neutron.delete_network(net['id'])
279 return 1
280
281 def delete_network_ports(self, network_id):
282 ports = self.shell.neutron.list_ports()['ports']
283 for port in ports:
284 if port['network_id'] == network_id:
285 self.shell.neutron.delete_port(port['id'])
286 return 1
287
288 def delete_subnet_ports(self, subnet_id):
289 ports = self.shell.neutron.list_ports()['ports']
290 for port in ports:
291 delete = False
292 for fixed_ip in port['fixed_ips']:
293 if fixed_ip['subnet_id'] == subnet_id:
294 delete=True
295 break
296 if delete:
297 self.shell.neutron.delete_port(port['id'])
298 return 1
299
300 def create_subnet(self, name, network_id, cidr_ip, ip_version, start, end):
301 #nets = self.shell.neutron.list_networks(name=network_name)['networks']
302 #if not nets:
303 # raise Exception, "No such network: %s" % network_name
304 #net = nets[0]
305
306 subnet = None
307 subnets = self.shell.neutron.list_subnets()['subnets']
308 for snet in subnets:
309 if snet['cidr'] == cidr_ip and snet['network_id'] == network_id:
310 subnet = snet
311
312 if not subnet:
313 # HACK: Add metadata route -- Neutron does not reliably supply this
314 metadata_ip = cidr_ip.replace("0/24", "3")
315
316 allocation_pools = [{'start': start, 'end': end}]
317 subnet = {'subnet': {'name': name,
318 'network_id': network_id,
319 'ip_version': ip_version,
320 'cidr': cidr_ip,
321 #'dns_nameservers': ['8.8.8.8', '8.8.4.4'],
322 'host_routes': [{'destination':'169.254.169.254/32','nexthop':metadata_ip}],
323 'gateway_ip': None,
324 'allocation_pools': allocation_pools}}
325 subnet = self.shell.neutron.create_subnet(subnet)['subnet']
326 # self.add_external_route(subnet)
327
328 return subnet
329
330 def update_subnet(self, id, fields):
331 return self.shell.neutron.update_subnet(id, fields)
332
333 def delete_subnet(self, id):
334 #return self.shell.neutron.delete_subnet(id=id)
335 # inefficient but fault tolerant
336 subnets = self.shell.neutron.list_subnets()['subnets']
337 for subnet in subnets:
338 if subnet['id'] == id:
339 self.delete_subnet_ports(subnet['id'])
340 self.shell.neutron.delete_subnet(id)
341 self.delete_external_route(subnet)
342 return 1
343
344 def get_external_routes(self):
345 status, output = commands.getstatusoutput('route')
346 routes = output.split('\n')[3:]
347 return routes
348
349 def add_external_route(self, subnet, routes=[]):
350 if not routes:
351 routes = self.get_external_routes()
352
353 ports = self.shell.neutron.list_ports()['ports']
354
355 gw_ip = subnet['gateway_ip']
356 subnet_id = subnet['id']
357
358 # 1. Find the port associated with the subnet's gateway
359 # 2. Find the router associated with that port
360 # 3. Find the port associated with this router and on the external net
361 # 4. Set up route to the subnet through the port from step 3
362 ip_address = None
363 for port in ports:
364 for fixed_ip in port['fixed_ips']:
365 if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip:
366 gw_port = port
367 router_id = gw_port['device_id']
368 router = self.shell.neutron.show_router(router_id)['router']
369 if router and router.get('external_gateway_info'):
370 ext_net = router['external_gateway_info']['network_id']
371 for port in ports:
372 if port['device_id'] == router_id and port['network_id'] == ext_net:
373 ip_address = port['fixed_ips'][0]['ip_address']
374
375 if ip_address:
376 # check if external route already exists
377 route_exists = False
378 if routes:
379 for route in routes:
380 if subnet['cidr'] in route and ip_address in route:
381 route_exists = True
382 if not route_exists:
383 cmd = "route add -net %s dev br-ex gw %s" % (subnet['cidr'], ip_address)
384 s, o = commands.getstatusoutput(cmd)
385 #print cmd, "\n", s, o
386
387 return 1
388
389 def delete_external_route(self, subnet):
390 ports = self.shell.neutron.list_ports()['ports']
391
392 gw_ip = subnet['gateway_ip']
393 subnet_id = subnet['id']
394
395 # 1. Find the port associated with the subnet's gateway
396 # 2. Find the router associated with that port
397 # 3. Find the port associated with this router and on the external net
398 # 4. Set up route to the subnet through the port from step 3
399 ip_address = None
400 for port in ports:
401 for fixed_ip in port['fixed_ips']:
402 if fixed_ip['subnet_id'] == subnet_id and fixed_ip['ip_address'] == gw_ip:
403 gw_port = port
404 router_id = gw_port['device_id']
405 router = self.shell.neutron.show_router(router_id)['router']
406 ext_net = router['external_gateway_info']['network_id']
407 for port in ports:
408 if port['device_id'] == router_id and port['network_id'] == ext_net:
409 ip_address = port['fixed_ips'][0]['ip_address']
410
411 if ip_address:
412 cmd = "route delete -net %s" % (subnet['cidr'])
413 commands.getstatusoutput(cmd)
414
415 return 1
416
417 def create_keypair(self, name, public_key):
418 keys = self.shell.nova.keypairs.findall(name=name)
419 if keys:
420 key = keys[0]
421 # update key
422 if key.public_key != public_key:
423 self.delete_keypair(key.id)
424 key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
425 else:
426 key = self.shell.nova.keypairs.create(name=name, public_key=public_key)
427 return key
428
429 def delete_keypair(self, id):
430 keys = self.shell.nova.keypairs.findall(id=id)
431 for key in keys:
432 self.shell.nova.keypairs.delete(key)
433 return 1
434
435 def get_private_networks(self, tenant=None):
436 if not tenant:
437 tenant = self.shell.nova.tenant
438 tenant = self.shell.keystone.tenants.find(name=tenant)
439 search_opts = {"tenant_id": tenant.id, "shared": False}
440 private_networks = self.shell.neutron.list_networks(**search_opts)
441 return private_networks
442
443 def get_shared_networks(self):
444 search_opts = {"shared": True}
445 shared_networks = self.shell.neutron.list_networks(**search_opts)
446 return shared_networks
447
448 def get_network_subnet(self, network_id):
449 subnet_id = None
450 subnet = None
451 if network_id:
452 os_networks = self.shell.neutron.list_networks(id=network_id)["networks"]
453 if os_networks:
454 os_network = os_networks[0]
455 if os_network['subnets']:
456 subnet_id = os_network['subnets'][0]
457 os_subnets = self.shell.neutron.list_subnets(id=subnet_id)['subnets']
458 if os_subnets:
459 subnet = os_subnets[0]['cidr']
460
461 return (subnet_id, subnet)
462
463 def spawn_instance(self, name, key_name=None, availability_zone=None, hostname=None, image_id=None, security_group=None, pubkeys=[], nics=None, metadata=None, userdata=None, flavor_name=None):
464 if not flavor_name:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700465 flavor_name = Config.get("nova.default_flavor")
Scott Bakerb63ea792016-08-11 10:24:48 -0700466
467 flavor = self.shell.nova.flavors.find(name=flavor_name)
468
469 if not security_group:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700470 security_group = Config.get("nova.default_security_group")
Scott Bakerb63ea792016-08-11 10:24:48 -0700471
472 files = {}
473 #if pubkeys:
474 # files["/root/.ssh/authorized_keys"] = "\n".join(pubkeys).encode('base64')
475 hints = {}
476
477 # determine availability zone and compute host
478 availability_zone_filter = None
479 if availability_zone is None or not availability_zone:
480 availability_zone_filter = 'nova'
481 else:
482 availability_zone_filter = availability_zone
483 if hostname:
484 availability_zone_filter += ':%s' % hostname
485
486 server = self.shell.nova.servers.create(
487 name=name,
488 key_name = key_name,
489 flavor=flavor.id,
490 image=image_id,
491 security_group = security_group,
492 #files = files,
493 scheduler_hints=hints,
494 availability_zone=availability_zone_filter,
495 nics=nics,
496 networks=nics,
497 meta=metadata,
498 userdata=userdata)
499 return server
500
501 def destroy_instance(self, id):
502 if (self.shell.nova.tenant=="admin"):
503 # findall() is implemented as a list() followed by a python search of the
504 # list. Since findall() doesn't accept "all_tenants", we do this using
505 # list() ourselves. This allows us to delete an instance as admin.
506 servers = self.shell.nova.servers.list(search_opts={"all_tenants": True})
507 else:
508 servers = self.shell.nova.servers.list()
509 for server in servers:
510 if server.id == id:
511 result=self.shell.nova.servers.delete(server)
512
513 def update_instance_metadata(self, id, metadata):
514 servers = self.shell.nova.servers.findall(id=id)
515 for server in servers:
516 self.shell.nova.servers.set_meta(server, metadata)
517 # note: set_meta() returns a broken Server() object. Don't try to
518 # print it in the shell or it will fail in __repr__.
519
520 def delete_instance_metadata(self, id, metadata):
521 # note: metadata is a dict. Only the keys matter, not the values.
522 servers = self.shell.nova.servers.findall(id=id)
523 for server in servers:
524 self.shell.nova.servers.delete_meta(server, metadata)
525