Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 3 | from django.db.models import F, Q |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 5 | from observer.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.network import * |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 7 | |
| 8 | class SyncNetworks(OpenStackSyncStep): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 9 | provides=[Network] |
| 10 | requested_interval = 0 |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 11 | |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 12 | def fetch_pending(self): |
| 13 | return Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 14 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 15 | def save_network(self, network): |
| 16 | if not network.network_id: |
| 17 | if network.template.sharedNetworkName: |
| 18 | network.network_id = network.template.sharedNetworkId |
| 19 | (network.subnet_id, network.subnet) = self.driver.get_network_subnet(network.network_id) |
| 20 | else: |
| 21 | network_name = network.name |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 22 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 23 | # create network |
| 24 | os_network = self.driver.create_network(network_name, shared=True) |
| 25 | network.network_id = os_network['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 26 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 27 | # create router |
| 28 | router = self.driver.create_router(network_name) |
| 29 | network.router_id = router['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 30 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 31 | # create subnet |
| 32 | next_subnet = self.get_next_subnet() |
| 33 | cidr = str(next_subnet.cidr) |
| 34 | ip_version = next_subnet.version |
| 35 | start = str(next_subnet[2]) |
| 36 | end = str(next_subnet[-2]) |
| 37 | subnet = self.driver.create_subnet(name=network_name, |
| 38 | network_id = network.network_id, |
| 39 | cidr_ip = cidr, |
| 40 | ip_version = ip_version, |
| 41 | start = start, |
| 42 | end = end) |
| 43 | network.subnet = cidr |
| 44 | network.subnet_id = subnet['id'] |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 45 | # add subnet as interface to slice's router |
| 46 | self.driver.add_router_interface(router['id'], subnet['id']) |
| 47 | # add external route |
| 48 | self.driver.add_external_route(subnet) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 49 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 50 | def sync_record(self, site): |
| 51 | if network.owner and network.owner.creator: |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 52 | try: |
| 53 | # update manager context |
| 54 | real_driver = self.driver |
| 55 | self.driver = self.driver.client_driver(network.owner.creator, network.owner.name) |
| 56 | self.save_network(network) |
| 57 | self.driver = real_driver |
| 58 | logger.info("saved network: %s" % (network)) |
| 59 | except Exception,e: |
| 60 | logger.log_exc("save network failed: %s" % network) |
| 61 | raise e |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 62 | |