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