blob: 7cce152971fd6a5991b1c4196a12afde17bdabdf [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 Mack91463d92014-06-19 20:42:03 -04008from core.models.site import Deployment, SiteDeployments
Tony Macke4be32f2014-03-11 20:45:25 -04009from core.models.slice import Slice, SliceDeployments
Tony Mack91463d92014-06-19 20:42:03 -040010from core.models.userdeployments 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):
Tony Mackb84fccc2014-04-12 10:28:52 -040016 provides=[SliceDeployments]
Tony Macke4be32f2014-03-11 20:45:25 -040017 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 Mackb84fccc2014-04-12 10:28:52 -040032 all_deployments = Deployment.objects.all()
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
Tony Mack29bf5e82014-04-29 21:40:24 -040063
Tony Macke4be32f2014-03-11 20:45:25 -040064 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 Macke4be32f2014-03-11 20:45:25 -040092
93 if slice_deployment.id and slice_deployment.tenant_id:
Tony Mack29bf5e82014-04-29 21:40:24 -040094 # update existing tenant
Tony Macke4be32f2014-03-11 20:45:25 -040095 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
96 driver.update_tenant(slice_deployment.tenant_id,
97 description=slice_deployment.slice.description,
Tony Mack29bf5e82014-04-29 21:40:24 -040098 enabled=slice_deployment.slice.enabled)
99
100 if slice_deployment.tenant_id:
101 # update slice/tenant quota
Scott Baker3949bc62014-05-12 10:42:16 -0700102 driver = self.driver.client_driver(deployment=slice_deployment.deployment.name, tenant=slice_deployment.slice.name)
Tony Mack29bf5e82014-04-29 21:40:24 -0400103 driver.shell.nova.quotas.update(tenant_id=slice_deployment.tenant_id, instances=int(slice_deployment.slice.max_slivers))
Tony Macke4be32f2014-03-11 20:45:25 -0400104
105 slice_deployment.save()