blob: 01a10ddaf4baadbb3c47d56ff668ba9218f6f247 [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
Tony Mackb84fccc2014-04-12 10:28:52 -04008from core.models.deployment import Deployment
Tony Macke4be32f2014-03-11 20:45:25 -04009from core.models.site import SiteDeployments
10from core.models.slice import Slice, SliceDeployments
Tony Mack69f1bc32014-03-12 13:20:34 -040011from core.models.user import UserDeployments
Tony Macke4be32f2014-03-11 20:45:25 -040012from util.logger import Logger, logging
13
14logger = Logger(level=logging.INFO)
15
16class SyncSliceDeployments(OpenStackSyncStep):
Tony Mackb84fccc2014-04-12 10:28:52 -040017 provides=[SliceDeployments]
Tony Macke4be32f2014-03-11 20:45:25 -040018 requested_interval=0
19
20 def fetch_pending(self):
21 # slice deployments are not visible to users. We must ensure
22 # slices are deployed at all deploymets available to their site.
Tony Macke5968eb2014-03-17 19:19:13 -040023 site_deployments = SiteDeployments.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -040024 site_deploy_lookup = defaultdict(list)
25 for site_deployment in site_deployments:
26 site_deploy_lookup[site_deployment.site].append(site_deployment.deployment)
27
Tony Macke5968eb2014-03-17 19:19:13 -040028 slice_deployments = SliceDeployments.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -040029 slice_deploy_lookup = defaultdict(list)
30 for slice_deployment in slice_deployments:
31 slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment)
32
Tony Mackb84fccc2014-04-12 10:28:52 -040033 all_deployments = Deployment.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -040034 for slice in Slice.objects.all():
Tony Mack6042aaf2014-04-07 19:45:46 -040035 # slices are added to all deployments for now
36 expected_deployments = all_deployments
37 #expected_deployments = site_deploy_lookup[slice.site]
Tony Macke4be32f2014-03-11 20:45:25 -040038 for expected_deployment in expected_deployments:
39 if slice not in slice_deploy_lookup or \
40 expected_deployment not in slice_deploy_lookup[slice]:
41 sd = SliceDeployments(slice=slice, deployment=expected_deployment)
42 sd.save()
43
44 # now we can return all slice deployments that need to be enacted
45 return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
46
47 def get_next_subnet(self, deployment=None):
48 # limit ourself to 10.0.x.x for now
49 valid_subnet = lambda net: net.startswith('10.0')
50 driver = self.driver.admin_driver(deployment=deployment)
51 subnets = driver.shell.quantum.list_subnets()['subnets']
52 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
53 if valid_subnet(subnet['cidr'])]
54 ints.sort()
Tony Mack6042aaf2014-04-07 19:45:46 -040055 if ints:
56 last_ip = IPAddress(ints[-1])
57 else:
58 last_ip = IPAddress('10.0.0.1')
Tony Macke4be32f2014-03-11 20:45:25 -040059 last_ip = IPAddress(ints[-1])
60 last_network = IPNetwork(str(last_ip) + "/24")
61 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
62 return next_network
63
64 def sync_record(self, slice_deployment):
65 logger.info("sync'ing slice deployment %s" % slice_deployment)
66 if not slice_deployment.tenant_id:
67 nova_fields = {'tenant_name': slice_deployment.slice.name,
68 'description': slice_deployment.slice.description,
69 'enabled': slice_deployment.slice.enabled}
70 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
71 tenant = driver.create_tenant(**nova_fields)
72 slice_deployment.tenant_id = tenant.id
73
74 # XXX give caller an admin role at the tenant they've created
Tony Mack69f1bc32014-03-12 13:20:34 -040075 deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator,
76 deployment=slice_deployment.deployment)
Tony Macke5968eb2014-03-17 19:19:13 -040077 if not deployment_users:
78 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 -040079 else:
Tony Mackd1a17e12014-03-19 15:18:15 -040080 deployment_user = deployment_users[0]
Tony Macke5968eb2014-03-17 19:19:13 -040081 # lookup user id at this deployment
82 kuser= driver.shell.keystone.users.find(email=slice_deployment.slice.creator.email)
Tony Macke4be32f2014-03-11 20:45:25 -040083
Tony Mackd1a17e12014-03-19 15:18:15 -040084 # add required roles at the slice's tenant
85 driver.add_user_role(kuser.id, tenant.id, 'admin')
86
Tony Mack69f1bc32014-03-12 13:20:34 -040087 # refresh credentials using this tenant
Tony Mackd1a17e12014-03-19 15:18:15 -040088 client_driver = self.driver.client_driver(caller=deployment_user.user,
89 tenant=tenant.name,
Tony Mack69f1bc32014-03-12 13:20:34 -040090 deployment=slice_deployment.deployment.name)
Tony Macke4be32f2014-03-11 20:45:25 -040091
Tony Mackd1a17e12014-03-19 15:18:15 -040092 # create a public key for the slice creator
93 if deployment_user.user.public_key:
94 keyname = deployment_user.user.email.lower().replace('@', 'AT').replace('.', '') +\
95 slice_deployment.slice.name
96 slice_deployment.keyname = keyname
97 key_fields = {'name': keyname,
98 'public_key': deployment_user.user.public_key}
99 client_driver.create_keypair(**key_fields)
100
Tony Mack69f1bc32014-03-12 13:20:34 -0400101 # create network
Tony Mackb84fccc2014-04-12 10:28:52 -0400102 #network = client_driver.create_network(slice_deployment.slice.name)
103 #slice_deployment.network_id = network['id']
Tony Macke4be32f2014-03-11 20:45:25 -0400104
Tony Mack69f1bc32014-03-12 13:20:34 -0400105 # create router
Tony Mackb84fccc2014-04-12 10:28:52 -0400106 #router = client_driver.create_router(slice_deployment.slice.name)
107 #slice_deployment.router_id = router['id']
Tony Macke4be32f2014-03-11 20:45:25 -0400108
Tony Mack69f1bc32014-03-12 13:20:34 -0400109 # create subnet for slice's private network
Tony Mackb84fccc2014-04-12 10:28:52 -0400110 #next_subnet = self.get_next_subnet(deployment=slice_deployment.deployment.name)
111 #cidr = str(next_subnet.cidr)
112 #ip_version = next_subnet.version
113 #start = str(next_subnet[2])
114 #end = str(next_subnet[-2])
115 #subnet = client_driver.create_subnet(name=slice_deployment.slice.name,
116 # network_id = network['id'],
117 # cidr_ip = cidr,
118 # ip_version = ip_version,
119 # start = start,
120 # end = end)
121 #slice_deployment.subnet_id = subnet['id']
Tony Mack69f1bc32014-03-12 13:20:34 -0400122 # add subnet as interface to slice's router
Tony Mackb84fccc2014-04-12 10:28:52 -0400123 #client_driver.add_router_interface(router['id'], subnet['id'])
Tony Mack69f1bc32014-03-12 13:20:34 -0400124 # add external route
Tony Mackb84fccc2014-04-12 10:28:52 -0400125 #client_driver.add_external_route(subnet)
Tony Macke4be32f2014-03-11 20:45:25 -0400126
127
128 if slice_deployment.id and slice_deployment.tenant_id:
129 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
130 driver.update_tenant(slice_deployment.tenant_id,
131 description=slice_deployment.slice.description,
132 enabled=slice_deployment.slice.enabled)
133
134 slice_deployment.save()