Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 3 | from netaddr import IPAddress, IPNetwork |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 4 | from django.db.models import F, Q |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 5 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 6 | from observer.openstacksyncstep import OpenStackSyncStep |
| 7 | from core.models.slice import Slice |
Andy Bavier | 5e243e1 | 2013-10-18 14:11:00 -0400 | [diff] [blame] | 8 | from util.logger import Logger, logging |
| 9 | |
Andy Bavier | 33584c5 | 2013-10-28 15:17:16 -0400 | [diff] [blame] | 10 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 11 | |
| 12 | class SyncSlices(OpenStackSyncStep): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 13 | provides=[Slice] |
| 14 | requested_interval=0 |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 15 | |
| 16 | def fetch_pending(self): |
| 17 | return Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 18 | |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 19 | def get_next_subnet(self): |
| 20 | # limit ourself to 10.0.x.x for now |
| 21 | valid_subnet = lambda net: net.startswith('10.0') |
| 22 | subnets = self.driver.shell.quantum.list_subnets()['subnets'] |
| 23 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 24 | if valid_subnet(subnet['cidr'])] |
| 25 | ints.sort() |
| 26 | last_ip = IPAddress(ints[-1]) |
| 27 | last_network = IPNetwork(str(last_ip) + "/24") |
| 28 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
Andy Bavier | 5e243e1 | 2013-10-18 14:11:00 -0400 | [diff] [blame] | 29 | return next_network |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 30 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 31 | def sync_record(self, slice): |
Andy Bavier | 5e243e1 | 2013-10-18 14:11:00 -0400 | [diff] [blame] | 32 | logger.info("sync'ing slice %s" % slice.name) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 33 | if not slice.tenant_id: |
| 34 | nova_fields = {'tenant_name': slice.name, |
| 35 | 'description': slice.description, |
| 36 | 'enabled': slice.enabled} |
| 37 | tenant = self.driver.create_tenant(**nova_fields) |
| 38 | slice.tenant_id = tenant.id |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 39 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 40 | # XXX give caller an admin role at the tenant they've created |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 41 | self.driver.add_user_role(slice.creator.kuser_id, tenant.id, 'admin') |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 42 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 43 | # refresh credentials using this tenant |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 44 | client_driver = self.driver.client_driver(tenant=tenant.name) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 45 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 46 | # create network |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 47 | network = client_driver.create_network(slice.name) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 48 | slice.network_id = network['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 49 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 50 | # create router |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 51 | router = client_driver.create_router(slice.name) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 52 | slice.router_id = router['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 53 | |
Andy Bavier | 33584c5 | 2013-10-28 15:17:16 -0400 | [diff] [blame] | 54 | # create subnet for slice's private network |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 55 | next_subnet = self.get_next_subnet() |
| 56 | cidr = str(next_subnet.cidr) |
| 57 | ip_version = next_subnet.version |
| 58 | start = str(next_subnet[2]) |
| 59 | end = str(next_subnet[-2]) |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 60 | subnet = client_driver.create_subnet(name=slice.name, |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 61 | network_id = network['id'], |
| 62 | cidr_ip = cidr, |
| 63 | ip_version = ip_version, |
| 64 | start = start, |
| 65 | end = end) |
| 66 | slice.subnet_id = subnet['id'] |
| 67 | # add subnet as interface to slice's router |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 68 | client_driver.add_router_interface(router['id'], subnet['id']) |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 69 | # add external route |
Tony Mack | 0ae3fe3 | 2013-10-08 08:47:53 -0400 | [diff] [blame] | 70 | client_driver.add_external_route(subnet) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 71 | |
| 72 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 73 | if slice.id and slice.tenant_id: |
Andy Bavier | 606bf0e | 2013-10-18 15:11:29 -0400 | [diff] [blame] | 74 | self.driver.update_tenant(slice.tenant_id, |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 75 | description=slice.description, |
| 76 | enabled=slice.enabled) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 77 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 78 | slice.save() |