blob: cb1d52a95121935dedea05a44c01a4adf526b9d1 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04004from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
6from core.models.network import *
Andy Bavier49801322013-11-18 20:08:05 -05007from util.logger import Logger, logging
8
9logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040010
11class SyncNetworks(OpenStackSyncStep):
Tony Mackce79de02013-09-24 10:12:33 -040012 provides=[Network]
13 requested_interval = 0
Sapan Bhatia24836f12013-08-27 10:16:05 -040014
Tony Mack66646d52013-09-24 21:47:12 -040015 def fetch_pending(self):
16 return Network.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
17
Tony Mackce79de02013-09-24 10:12:33 -040018 def save_network(self, network):
Andy Bavier6bba5352013-11-21 15:36:47 -050019 if not network.network_id and network.template.sharedNetworkName:
Tony Mackce79de02013-09-24 10:12:33 -040020 network.network_id = network.template.sharedNetworkId
Sapan Bhatia24836f12013-08-27 10:16:05 -040021
Andy Bavier6bba5352013-11-21 15:36:47 -050022 if not network.network_id:
23 network_name = network.name
Sapan Bhatia24836f12013-08-27 10:16:05 -040024
Andy Bavier6bba5352013-11-21 15:36:47 -050025 # create network
26 os_network = self.driver.create_network(network_name, shared=True)
27 network.network_id = os_network['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040028
Andy Bavier6bba5352013-11-21 15:36:47 -050029 # create router
30 router = self.driver.create_router(network_name)
31 network.router_id = router['id']
32
33 # create subnet
34 next_subnet = self.get_next_subnet()
35 cidr = str(next_subnet.cidr)
36 ip_version = next_subnet.version
37 start = str(next_subnet[2])
38 end = str(next_subnet[-2])
39 subnet = self.driver.create_subnet(name=network_name,
40 network_id = network.network_id,
41 cidr_ip = cidr,
42 ip_version = ip_version,
43 start = start,
44 end = end)
45 network.subnet = cidr
46 network.subnet_id = subnet['id']
47 # add subnet as interface to slice's router
48 self.driver.add_router_interface(router['id'], subnet['id'])
49 # add external route
50 self.driver.add_external_route(subnet)
51 logger.info("created private subnet (%s) for network: %s" % (cidr, network))
52 else:
53 (network.subnet_id, network.subnet) = self.driver.get_network_subnet(network.network_id)
54 logger.info("sync'ed subnet (%s) for network: %s" % (network.subnet, network))
55 network.save()
Sapan Bhatia24836f12013-08-27 10:16:05 -040056
Andy Bavier49801322013-11-18 20:08:05 -050057 def sync_record(self, network):
Tony Mackce79de02013-09-24 10:12:33 -040058 if network.owner and network.owner.creator:
Tony Mack66646d52013-09-24 21:47:12 -040059 try:
60 # update manager context
61 real_driver = self.driver
62 self.driver = self.driver.client_driver(network.owner.creator, network.owner.name)
63 self.save_network(network)
64 self.driver = real_driver
65 logger.info("saved network: %s" % (network))
66 except Exception,e:
67 logger.log_exc("save network failed: %s" % network)
68 raise e
Sapan Bhatia24836f12013-08-27 10:16:05 -040069