Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from collections import defaultdict |
| 4 | from netaddr import IPAddress, IPNetwork |
| 5 | from django.db.models import F, Q |
| 6 | from planetstack.config import Config |
| 7 | from observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | 91463d9 | 2014-06-19 20:42:03 -0400 | [diff] [blame] | 8 | from core.models.site import Deployment, SiteDeployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 9 | from core.models.slice import Slice, SliceDeployments |
Tony Mack | 91463d9 | 2014-06-19 20:42:03 -0400 | [diff] [blame] | 10 | from core.models.userdeployments import UserDeployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 11 | from util.logger import Logger, logging |
| 12 | |
| 13 | logger = Logger(level=logging.INFO) |
| 14 | |
| 15 | class SyncSliceDeployments(OpenStackSyncStep): |
Tony Mack | b84fccc | 2014-04-12 10:28:52 -0400 | [diff] [blame] | 16 | provides=[SliceDeployments] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 17 | requested_interval=0 |
| 18 | |
Sapan Bhatia | d97caf3 | 2014-07-23 09:53:53 -0400 | [diff] [blame] | 19 | def fetch_pending(self, deleted): |
| 20 | if (deleted): |
| 21 | return SliceDeployments.deleted_objects.all() |
| 22 | else: |
Sapan Bhatia | d97caf3 | 2014-07-23 09:53:53 -0400 | [diff] [blame] | 23 | return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 24 | |
| 25 | def get_next_subnet(self, deployment=None): |
| 26 | # limit ourself to 10.0.x.x for now |
| 27 | valid_subnet = lambda net: net.startswith('10.0') |
| 28 | driver = self.driver.admin_driver(deployment=deployment) |
| 29 | subnets = driver.shell.quantum.list_subnets()['subnets'] |
| 30 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 31 | if valid_subnet(subnet['cidr'])] |
| 32 | ints.sort() |
Tony Mack | 6042aaf | 2014-04-07 19:45:46 -0400 | [diff] [blame] | 33 | if ints: |
| 34 | last_ip = IPAddress(ints[-1]) |
| 35 | else: |
| 36 | last_ip = IPAddress('10.0.0.1') |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 37 | last_ip = IPAddress(ints[-1]) |
| 38 | last_network = IPNetwork(str(last_ip) + "/24") |
| 39 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 40 | return next_network |
| 41 | |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 42 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 43 | def sync_record(self, slice_deployment): |
| 44 | logger.info("sync'ing slice deployment %s" % slice_deployment) |
Scott Baker | 1e10f06 | 2014-09-04 15:13:40 -0700 | [diff] [blame] | 45 | |
| 46 | if not slice_deployment.deployment.admin_user: |
| 47 | logger.info("deployment %r has no admin_user, skipping" % slice_deployment.deployment) |
| 48 | return |
| 49 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 50 | if not slice_deployment.tenant_id: |
| 51 | nova_fields = {'tenant_name': slice_deployment.slice.name, |
| 52 | 'description': slice_deployment.slice.description, |
| 53 | 'enabled': slice_deployment.slice.enabled} |
| 54 | driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name) |
| 55 | tenant = driver.create_tenant(**nova_fields) |
| 56 | slice_deployment.tenant_id = tenant.id |
| 57 | |
| 58 | # XXX give caller an admin role at the tenant they've created |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 59 | deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator, |
| 60 | deployment=slice_deployment.deployment) |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 61 | if not deployment_users: |
| 62 | logger.info("slice createor %s has not accout at deployment %s" % (slice_deployment.slice.creator, slice_deployment.deployment.name)) |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 63 | else: |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 64 | deployment_user = deployment_users[0] |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 65 | # lookup user id at this deployment |
| 66 | kuser= driver.shell.keystone.users.find(email=slice_deployment.slice.creator.email) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 67 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 68 | # add required roles at the slice's tenant |
| 69 | driver.add_user_role(kuser.id, tenant.id, 'admin') |
| 70 | |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 71 | # refresh credentials using this tenant |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 72 | client_driver = self.driver.client_driver(caller=deployment_user.user, |
| 73 | tenant=tenant.name, |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 74 | deployment=slice_deployment.deployment.name) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 75 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 76 | |
| 77 | if slice_deployment.id and slice_deployment.tenant_id: |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 78 | # update existing tenant |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 79 | driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name) |
| 80 | driver.update_tenant(slice_deployment.tenant_id, |
| 81 | description=slice_deployment.slice.description, |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 82 | enabled=slice_deployment.slice.enabled) |
| 83 | |
| 84 | if slice_deployment.tenant_id: |
| 85 | # update slice/tenant quota |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [diff] [blame] | 86 | driver = self.driver.client_driver(deployment=slice_deployment.deployment.name, tenant=slice_deployment.slice.name) |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 87 | driver.shell.nova.quotas.update(tenant_id=slice_deployment.tenant_id, instances=int(slice_deployment.slice.max_slivers)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 88 | |
| 89 | slice_deployment.save() |
Sapan Bhatia | d97caf3 | 2014-07-23 09:53:53 -0400 | [diff] [blame] | 90 | |
| 91 | |
| 92 | def delete_record(self, slice_deployment): |
| 93 | user = User.objects.get(id=slice_deployment.slice.creator.id) |
| 94 | driver = OpenStackDriver().admin_driver(deployment=slice_deployment.deployment.name) |
| 95 | client_driver = driver.client_driver(caller=user, |
| 96 | tenant=slice_deployment.slice.name, |
| 97 | deployment=slice_deployment.deployment.name) |
| 98 | |
| 99 | if slice_deployment.router_id and slice_deployment.subnet_id: |
| 100 | client_driver.delete_router_interface(slice_deployment.router_id, slice_deployment.subnet_id) |
| 101 | if slice_deployment.subnet_id: |
| 102 | client_driver.delete_subnet(slice_deployment.subnet_id) |
| 103 | if slice_deployment.router_id: |
| 104 | client_driver.delete_router(slice_deployment.router_id) |
| 105 | if slice_deployment.network_id: |
| 106 | client_driver.delete_network(slice_deployment.network_id) |
| 107 | if slice_deployment.tenant_id: |
| 108 | driver.delete_tenant(slice_deployment.tenant_id) |
| 109 | # delete external route |
| 110 | #subnet = None |
| 111 | #subnets = client_driver.shell.quantum.list_subnets()['subnets'] |
| 112 | #for snet in subnets: |
| 113 | # if snet['id'] == slice_deployment.subnet_id: |
| 114 | # subnet = snet |
| 115 | #if subnet: |
| 116 | # driver.delete_external_route(subnet) |