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