| import os |
| import base64 |
| from django.db.models import F, Q |
| from planetstack.config import Config |
| from observer.openstacksyncstep import OpenStackSyncStep |
| from core.models.network import * |
| |
| class SyncNetworks(OpenStackSyncStep): |
| provides=[Network] |
| requested_interval = 0 |
| |
| def fetch_pending(self): |
| return Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| |
| def save_network(self, network): |
| if not network.network_id: |
| if network.template.sharedNetworkName: |
| network.network_id = network.template.sharedNetworkId |
| (network.subnet_id, network.subnet) = self.driver.get_network_subnet(network.network_id) |
| else: |
| network_name = network.name |
| |
| # create network |
| os_network = self.driver.create_network(network_name, shared=True) |
| network.network_id = os_network['id'] |
| |
| # create router |
| router = self.driver.create_router(network_name) |
| network.router_id = router['id'] |
| |
| # create subnet |
| next_subnet = self.get_next_subnet() |
| cidr = str(next_subnet.cidr) |
| ip_version = next_subnet.version |
| start = str(next_subnet[2]) |
| end = str(next_subnet[-2]) |
| subnet = self.driver.create_subnet(name=network_name, |
| network_id = network.network_id, |
| cidr_ip = cidr, |
| ip_version = ip_version, |
| start = start, |
| end = end) |
| network.subnet = cidr |
| network.subnet_id = subnet['id'] |
| # add subnet as interface to slice's router |
| self.driver.add_router_interface(router['id'], subnet['id']) |
| # add external route |
| self.driver.add_external_route(subnet) |
| |
| def sync_record(self, site): |
| if network.owner and network.owner.creator: |
| try: |
| # update manager context |
| real_driver = self.driver |
| self.driver = self.driver.client_driver(network.owner.creator, network.owner.name) |
| self.save_network(network) |
| self.driver = real_driver |
| logger.info("saved network: %s" % (network)) |
| except Exception,e: |
| logger.log_exc("save network failed: %s" % network) |
| raise e |
| |