blob: 9751a081a1270be0b1067f601136d463949a2ae6 [file] [log] [blame]
Tony Macke4be32f2014-03-11 20:45:25 -04001import os
2import base64
3from collections import defaultdict
4from netaddr import IPAddress, IPNetwork
5from django.db.models import F, Q
6from planetstack.config import Config
7from observer.openstacksyncstep import OpenStackSyncStep
8from core.models.site import SiteDeployments
9from core.models.slice import Slice, SliceDeployments
Tony Mack69f1bc32014-03-12 13:20:34 -040010from core.models.user import UserDeployments
Tony Macke4be32f2014-03-11 20:45:25 -040011from util.logger import Logger, logging
12
13logger = Logger(level=logging.INFO)
14
15class SyncSliceDeployments(OpenStackSyncStep):
16 provides=[Slice, SliceDeployments]
17 requested_interval=0
18
19 def fetch_pending(self):
20 # slice deployments are not visible to users. We must ensure
21 # slices are deployed at all deploymets available to their site.
Tony Macke5968eb2014-03-17 19:19:13 -040022 site_deployments = SiteDeployments.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -040023 site_deploy_lookup = defaultdict(list)
24 for site_deployment in site_deployments:
25 site_deploy_lookup[site_deployment.site].append(site_deployment.deployment)
26
Tony Macke5968eb2014-03-17 19:19:13 -040027 slice_deployments = SliceDeployments.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -040028 slice_deploy_lookup = defaultdict(list)
29 for slice_deployment in slice_deployments:
30 slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment)
31
32 for slice in Slice.objects.all():
33 expected_deployments = site_deploy_lookup[slice.site]
34 for expected_deployment in expected_deployments:
35 if slice not in slice_deploy_lookup or \
36 expected_deployment not in slice_deploy_lookup[slice]:
37 sd = SliceDeployments(slice=slice, deployment=expected_deployment)
38 sd.save()
39
40 # now we can return all slice deployments that need to be enacted
41 return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
42
43 def get_next_subnet(self, deployment=None):
44 # limit ourself to 10.0.x.x for now
45 valid_subnet = lambda net: net.startswith('10.0')
46 driver = self.driver.admin_driver(deployment=deployment)
47 subnets = driver.shell.quantum.list_subnets()['subnets']
48 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
49 if valid_subnet(subnet['cidr'])]
50 ints.sort()
51 last_ip = IPAddress(ints[-1])
52 last_network = IPNetwork(str(last_ip) + "/24")
53 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
54 return next_network
55
56 def sync_record(self, slice_deployment):
57 logger.info("sync'ing slice deployment %s" % slice_deployment)
58 if not slice_deployment.tenant_id:
59 nova_fields = {'tenant_name': slice_deployment.slice.name,
60 'description': slice_deployment.slice.description,
61 'enabled': slice_deployment.slice.enabled}
62 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
63 tenant = driver.create_tenant(**nova_fields)
64 slice_deployment.tenant_id = tenant.id
65
66 # XXX give caller an admin role at the tenant they've created
Tony Mack69f1bc32014-03-12 13:20:34 -040067 deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator,
68 deployment=slice_deployment.deployment)
Tony Macke5968eb2014-03-17 19:19:13 -040069 if not deployment_users:
70 logger.info("slice createor %s has not accout at deployment %s" % (slice_deployment.slice.creator, slice_deployment.deployment.name))
Tony Mack69f1bc32014-03-12 13:20:34 -040071 else:
Tony Mackd1a17e12014-03-19 15:18:15 -040072 deployment_user = deployment_users[0]
Tony Macke5968eb2014-03-17 19:19:13 -040073 # lookup user id at this deployment
74 kuser= driver.shell.keystone.users.find(email=slice_deployment.slice.creator.email)
Tony Macke4be32f2014-03-11 20:45:25 -040075
Tony Mackd1a17e12014-03-19 15:18:15 -040076 # add required roles at the slice's tenant
77 driver.add_user_role(kuser.id, tenant.id, 'admin')
78
Tony Mack69f1bc32014-03-12 13:20:34 -040079 # refresh credentials using this tenant
Tony Mackd1a17e12014-03-19 15:18:15 -040080 client_driver = self.driver.client_driver(caller=deployment_user.user,
81 tenant=tenant.name,
Tony Mack69f1bc32014-03-12 13:20:34 -040082 deployment=slice_deployment.deployment.name)
Tony Macke4be32f2014-03-11 20:45:25 -040083
Tony Mackd1a17e12014-03-19 15:18:15 -040084 # create a public key for the slice creator
85 if deployment_user.user.public_key:
86 keyname = deployment_user.user.email.lower().replace('@', 'AT').replace('.', '') +\
87 slice_deployment.slice.name
88 slice_deployment.keyname = keyname
89 key_fields = {'name': keyname,
90 'public_key': deployment_user.user.public_key}
91 client_driver.create_keypair(**key_fields)
92
Tony Mack69f1bc32014-03-12 13:20:34 -040093 # create network
Tony Macke5968eb2014-03-17 19:19:13 -040094 network = client_driver.create_network(slice_deployment.slice.name)
Tony Mack69f1bc32014-03-12 13:20:34 -040095 slice_deployment.network_id = network['id']
Tony Macke4be32f2014-03-11 20:45:25 -040096
Tony Mack69f1bc32014-03-12 13:20:34 -040097 # create router
Tony Macke5968eb2014-03-17 19:19:13 -040098 router = client_driver.create_router(slice_deployment.slice.name)
Tony Mack69f1bc32014-03-12 13:20:34 -040099 slice_deployment.router_id = router['id']
Tony Macke4be32f2014-03-11 20:45:25 -0400100
Tony Mack69f1bc32014-03-12 13:20:34 -0400101 # create subnet for slice's private network
102 next_subnet = self.get_next_subnet(deployment=slice_deployment.deployment.name)
103 cidr = str(next_subnet.cidr)
104 ip_version = next_subnet.version
105 start = str(next_subnet[2])
106 end = str(next_subnet[-2])
Tony Macke5968eb2014-03-17 19:19:13 -0400107 subnet = client_driver.create_subnet(name=slice_deployment.slice.name,
Tony Mack69f1bc32014-03-12 13:20:34 -0400108 network_id = network['id'],
109 cidr_ip = cidr,
110 ip_version = ip_version,
111 start = start,
112 end = end)
113 slice_deployment.subnet_id = subnet['id']
114 # add subnet as interface to slice's router
115 client_driver.add_router_interface(router['id'], subnet['id'])
116 # add external route
117 client_driver.add_external_route(subnet)
Tony Macke4be32f2014-03-11 20:45:25 -0400118
119
120 if slice_deployment.id and slice_deployment.tenant_id:
121 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
122 driver.update_tenant(slice_deployment.tenant_id,
123 description=slice_deployment.slice.description,
124 enabled=slice_deployment.slice.enabled)
125
126 slice_deployment.save()