blob: fff2e0497e41b84fcf0c126f097197ba41d6442e [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
Sapan Bhatiad97caf32014-07-23 09:53:53 -040019 def fetch_pending(self, deleted):
20 if (deleted):
21 return SliceDeployments.deleted_objects.all()
22 else:
Sapan Bhatiad97caf32014-07-23 09:53:53 -040023 return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Macke4be32f2014-03-11 20:45:25 -040024
25 def get_next_subnet(self, deployment=None):
26 # limit ourself to 10.0.x.x for now
27 valid_subnet = lambda net: net.startswith('10.0')
28 driver = self.driver.admin_driver(deployment=deployment)
29 subnets = driver.shell.quantum.list_subnets()['subnets']
30 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
31 if valid_subnet(subnet['cidr'])]
32 ints.sort()
Tony Mack6042aaf2014-04-07 19:45:46 -040033 if ints:
34 last_ip = IPAddress(ints[-1])
35 else:
36 last_ip = IPAddress('10.0.0.1')
Tony Macke4be32f2014-03-11 20:45:25 -040037 last_ip = IPAddress(ints[-1])
38 last_network = IPNetwork(str(last_ip) + "/24")
39 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
40 return next_network
41
Tony Mack29bf5e82014-04-29 21:40:24 -040042
Tony Macke4be32f2014-03-11 20:45:25 -040043 def sync_record(self, slice_deployment):
44 logger.info("sync'ing slice deployment %s" % slice_deployment)
45 if not slice_deployment.tenant_id:
46 nova_fields = {'tenant_name': slice_deployment.slice.name,
47 'description': slice_deployment.slice.description,
48 'enabled': slice_deployment.slice.enabled}
49 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
50 tenant = driver.create_tenant(**nova_fields)
51 slice_deployment.tenant_id = tenant.id
52
53 # XXX give caller an admin role at the tenant they've created
Tony Mack69f1bc32014-03-12 13:20:34 -040054 deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator,
55 deployment=slice_deployment.deployment)
Tony Macke5968eb2014-03-17 19:19:13 -040056 if not deployment_users:
57 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 -040058 else:
Tony Mackd1a17e12014-03-19 15:18:15 -040059 deployment_user = deployment_users[0]
Tony Macke5968eb2014-03-17 19:19:13 -040060 # lookup user id at this deployment
61 kuser= driver.shell.keystone.users.find(email=slice_deployment.slice.creator.email)
Tony Macke4be32f2014-03-11 20:45:25 -040062
Tony Mackd1a17e12014-03-19 15:18:15 -040063 # add required roles at the slice's tenant
64 driver.add_user_role(kuser.id, tenant.id, 'admin')
65
Tony Mack69f1bc32014-03-12 13:20:34 -040066 # refresh credentials using this tenant
Tony Mackd1a17e12014-03-19 15:18:15 -040067 client_driver = self.driver.client_driver(caller=deployment_user.user,
68 tenant=tenant.name,
Tony Mack69f1bc32014-03-12 13:20:34 -040069 deployment=slice_deployment.deployment.name)
Tony Macke4be32f2014-03-11 20:45:25 -040070
Tony Macke4be32f2014-03-11 20:45:25 -040071
72 if slice_deployment.id and slice_deployment.tenant_id:
Tony Mack29bf5e82014-04-29 21:40:24 -040073 # update existing tenant
Tony Macke4be32f2014-03-11 20:45:25 -040074 driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name)
75 driver.update_tenant(slice_deployment.tenant_id,
76 description=slice_deployment.slice.description,
Tony Mack29bf5e82014-04-29 21:40:24 -040077 enabled=slice_deployment.slice.enabled)
78
79 if slice_deployment.tenant_id:
80 # update slice/tenant quota
Scott Baker3949bc62014-05-12 10:42:16 -070081 driver = self.driver.client_driver(deployment=slice_deployment.deployment.name, tenant=slice_deployment.slice.name)
Tony Mack29bf5e82014-04-29 21:40:24 -040082 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 -040083
84 slice_deployment.save()
Sapan Bhatiad97caf32014-07-23 09:53:53 -040085
86
87 def delete_record(self, slice_deployment):
88 user = User.objects.get(id=slice_deployment.slice.creator.id)
89 driver = OpenStackDriver().admin_driver(deployment=slice_deployment.deployment.name)
90 client_driver = driver.client_driver(caller=user,
91 tenant=slice_deployment.slice.name,
92 deployment=slice_deployment.deployment.name)
93
94 if slice_deployment.router_id and slice_deployment.subnet_id:
95 client_driver.delete_router_interface(slice_deployment.router_id, slice_deployment.subnet_id)
96 if slice_deployment.subnet_id:
97 client_driver.delete_subnet(slice_deployment.subnet_id)
98 if slice_deployment.router_id:
99 client_driver.delete_router(slice_deployment.router_id)
100 if slice_deployment.network_id:
101 client_driver.delete_network(slice_deployment.network_id)
102 if slice_deployment.tenant_id:
103 driver.delete_tenant(slice_deployment.tenant_id)
104 # delete external route
105 #subnet = None
106 #subnets = client_driver.shell.quantum.list_subnets()['subnets']
107 #for snet in subnets:
108 # if snet['id'] == slice_deployment.subnet_id:
109 # subnet = snet
110 #if subnet:
111 # driver.delete_external_route(subnet)