blob: 2254e7be175797938ebde58c00446ab40d1cabe8 [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
Tony Mack6042aaf2014-04-07 19:45:46 -040032 all_deployments = Deployment.objects.filter()
Tony Macke4be32f2014-03-11 20:45:25 -040033 for slice in Slice.objects.all():
Tony Mack6042aaf2014-04-07 19:45:46 -040034 # slices are added to all deployments for now
35 expected_deployments = all_deployments
36 #expected_deployments = site_deploy_lookup[slice.site]
Tony Macke4be32f2014-03-11 20:45:25 -040037 for expected_deployment in expected_deployments:
38 if slice not in slice_deploy_lookup or \
39 expected_deployment not in slice_deploy_lookup[slice]:
40 sd = SliceDeployments(slice=slice, deployment=expected_deployment)
41 sd.save()
42
43 # now we can return all slice deployments that need to be enacted
44 return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
45
46 def get_next_subnet(self, deployment=None):
47 # limit ourself to 10.0.x.x for now
48 valid_subnet = lambda net: net.startswith('10.0')
49 driver = self.driver.admin_driver(deployment=deployment)
50 subnets = driver.shell.quantum.list_subnets()['subnets']
51 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
52 if valid_subnet(subnet['cidr'])]
53 ints.sort()
Tony Mack6042aaf2014-04-07 19:45:46 -040054 if ints:
55 last_ip = IPAddress(ints[-1])
56 else:
57 last_ip = IPAddress('10.0.0.1')
Tony Macke4be32f2014-03-11 20:45:25 -040058 last_ip = IPAddress(ints[-1])
59 last_network = IPNetwork(str(last_ip) + "/24")
60 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
61 return next_network
62
63 def sync_record(self, slice_deployment):
64 logger.info("sync'ing slice deployment %s" % slice_deployment)
65 if not slice_deployment.tenant_id:
66 nova_fields = {'tenant_name': slice_deployment.slice.name,
67 'description': slice_deployment.slice.description,
68 'enabled': slice_deployment.slice.enabled}
69 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
70 tenant = driver.create_tenant(**nova_fields)
71 slice_deployment.tenant_id = tenant.id
72
73 # XXX give caller an admin role at the tenant they've created
Tony Mack69f1bc32014-03-12 13:20:34 -040074 deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator,
75 deployment=slice_deployment.deployment)
Tony Macke5968eb2014-03-17 19:19:13 -040076 if not deployment_users:
77 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 -040078 else:
Tony Mackd1a17e12014-03-19 15:18:15 -040079 deployment_user = deployment_users[0]
Tony Macke5968eb2014-03-17 19:19:13 -040080 # lookup user id at this deployment
81 kuser= driver.shell.keystone.users.find(email=slice_deployment.slice.creator.email)
Tony Macke4be32f2014-03-11 20:45:25 -040082
Tony Mackd1a17e12014-03-19 15:18:15 -040083 # add required roles at the slice's tenant
84 driver.add_user_role(kuser.id, tenant.id, 'admin')
85
Tony Mack69f1bc32014-03-12 13:20:34 -040086 # refresh credentials using this tenant
Tony Mackd1a17e12014-03-19 15:18:15 -040087 client_driver = self.driver.client_driver(caller=deployment_user.user,
88 tenant=tenant.name,
Tony Mack69f1bc32014-03-12 13:20:34 -040089 deployment=slice_deployment.deployment.name)
Tony Macke4be32f2014-03-11 20:45:25 -040090
Tony Mackd1a17e12014-03-19 15:18:15 -040091 # create a public key for the slice creator
92 if deployment_user.user.public_key:
93 keyname = deployment_user.user.email.lower().replace('@', 'AT').replace('.', '') +\
94 slice_deployment.slice.name
95 slice_deployment.keyname = keyname
96 key_fields = {'name': keyname,
97 'public_key': deployment_user.user.public_key}
98 client_driver.create_keypair(**key_fields)
99
Tony Mack69f1bc32014-03-12 13:20:34 -0400100 # create network
Tony Macke5968eb2014-03-17 19:19:13 -0400101 network = client_driver.create_network(slice_deployment.slice.name)
Tony Mack69f1bc32014-03-12 13:20:34 -0400102 slice_deployment.network_id = network['id']
Tony Macke4be32f2014-03-11 20:45:25 -0400103
Tony Mack69f1bc32014-03-12 13:20:34 -0400104 # create router
Tony Macke5968eb2014-03-17 19:19:13 -0400105 router = client_driver.create_router(slice_deployment.slice.name)
Tony Mack69f1bc32014-03-12 13:20:34 -0400106 slice_deployment.router_id = router['id']
Tony Macke4be32f2014-03-11 20:45:25 -0400107
Tony Mack69f1bc32014-03-12 13:20:34 -0400108 # create subnet for slice's private network
109 next_subnet = self.get_next_subnet(deployment=slice_deployment.deployment.name)
110 cidr = str(next_subnet.cidr)
111 ip_version = next_subnet.version
112 start = str(next_subnet[2])
113 end = str(next_subnet[-2])
Tony Macke5968eb2014-03-17 19:19:13 -0400114 subnet = client_driver.create_subnet(name=slice_deployment.slice.name,
Tony Mack69f1bc32014-03-12 13:20:34 -0400115 network_id = network['id'],
116 cidr_ip = cidr,
117 ip_version = ip_version,
118 start = start,
119 end = end)
120 slice_deployment.subnet_id = subnet['id']
121 # add subnet as interface to slice's router
122 client_driver.add_router_interface(router['id'], subnet['id'])
123 # add external route
124 client_driver.add_external_route(subnet)
Tony Macke4be32f2014-03-11 20:45:25 -0400125
126
127 if slice_deployment.id and slice_deployment.tenant_id:
128 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
129 driver.update_tenant(slice_deployment.tenant_id,
130 description=slice_deployment.slice.description,
131 enabled=slice_deployment.slice.enabled)
132
133 slice_deployment.save()