blob: e8d760da5331951a5aea398ad5c3e8e77e3db7bc [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
Sapan Bhatia39e85582014-11-19 15:07:36 -05008from core.models.site import Deployment, SiteDeployments
Sapan Bhatiabf7856b2014-11-19 15:09:38 -05009from core.models.slice import Slice, SliceDeployments
Sapan Bhatia3f879692014-11-19 15:10:51 -050010from core.models.userdeployments import UserDeployments
Tony Macke4be32f2014-03-11 20:45:25 -040011from util.logger import Logger, logging
Sapan Bhatia475c5972014-11-05 10:32:41 -050012from observer.ansible import *
Tony Macke4be32f2014-03-11 20:45:25 -040013
14logger = Logger(level=logging.INFO)
15
Sapan Bhatiabf7856b2014-11-19 15:09:38 -050016class SyncSliceDeployments(OpenStackSyncStep):
17 provides=[SliceDeployments]
Tony Macke4be32f2014-03-11 20:45:25 -040018 requested_interval=0
19
Sapan Bhatiad97caf32014-07-23 09:53:53 -040020 def fetch_pending(self, deleted):
21 if (deleted):
Sapan Bhatiabf7856b2014-11-19 15:09:38 -050022 return SliceDeployments.deleted_objects.all()
Sapan Bhatiad97caf32014-07-23 09:53:53 -040023 else:
Sapan Bhatiabf7856b2014-11-19 15:09:38 -050024 return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Macke4be32f2014-03-11 20:45:25 -040025
26 def get_next_subnet(self, deployment=None):
27 # limit ourself to 10.0.x.x for now
28 valid_subnet = lambda net: net.startswith('10.0')
29 driver = self.driver.admin_driver(deployment=deployment)
30 subnets = driver.shell.quantum.list_subnets()['subnets']
31 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
32 if valid_subnet(subnet['cidr'])]
33 ints.sort()
Tony Mack6042aaf2014-04-07 19:45:46 -040034 if ints:
35 last_ip = IPAddress(ints[-1])
36 else:
37 last_ip = IPAddress('10.0.0.1')
Tony Macke4be32f2014-03-11 20:45:25 -040038 last_ip = IPAddress(ints[-1])
39 last_network = IPNetwork(str(last_ip) + "/24")
40 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
41 return next_network
42
Tony Mack29bf5e82014-04-29 21:40:24 -040043
Tony Macke4be32f2014-03-11 20:45:25 -040044 def sync_record(self, slice_deployment):
45 logger.info("sync'ing slice deployment %s" % slice_deployment)
Scott Baker1e10f062014-09-04 15:13:40 -070046
47 if not slice_deployment.deployment.admin_user:
48 logger.info("deployment %r has no admin_user, skipping" % slice_deployment.deployment)
49 return
50
Sapan8f425eb2014-12-03 00:02:55 -050051 deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator,
52 deployment=slice_deployment.deployment)
53 if not deployment_users:
54 logger.info("slice createor %s has not accout at deployment %s" % (slice_deployment.slice.creator, slice_deployment.deployment.name))
55 roles = ['admin']
56 deployment_user = 'Unknown user'
57 else:
58 deployment_user = deployment_users[0].user.email
59 roles = ['admin']
60
61 max_instances=int(slice_deployment.slice.max_slivers)
62 tenant_fields = {'endpoint':slice_deployment.deployment.auth_url,
63 'admin_user': slice_deployment.deployment.admin_user,
64 'admin_password': slice_deployment.deployment.admin_password,
65 'admin_tenant': 'admin',
66 'tenant': slice_deployment.slice.name,
67 'tenant_description': slice_deployment.slice.description,
68 'roles':roles,
69 'name':deployment_user,
70 'ansible_tag':'%s@%s'%(slice_deployment.slice.name,slice_deployment.deployment.name),
71 'max_instances':max_instances}
Tony Macke4be32f2014-03-11 20:45:25 -040072
Sapan8f425eb2014-12-03 00:02:55 -050073 res = run_template('sync_slice_deployments.yaml', tenant_fields, path='slice_deployments')
74 expected_num = len(roles)+1
75 if (len(res)!=expected_num):
76 raise Exception('Could not sync tenants for slice %s'%slice_deployment.slice.name)
77 else:
78 tenant_id = res[0]['id']
79 if (not slice_deployment.tenant_id):
80 handle = os.popen('nova quota-update --instances %d %s'%(max_instances,tenant_id))
81 output = handle.read()
82 result = handle.close()
83 if (result):
84 logging.info('Could not update quota for %s'%slice_deployment.slice.name)
85 slice_deployment.tenant_id = tenant_id
86 slice_deployment.save()
87
Sapan Bhatiad97caf32014-07-23 09:53:53 -040088
89
90 def delete_record(self, slice_deployment):
91 user = User.objects.get(id=slice_deployment.slice.creator.id)
92 driver = OpenStackDriver().admin_driver(deployment=slice_deployment.deployment.name)
93 client_driver = driver.client_driver(caller=user,
94 tenant=slice_deployment.slice.name,
95 deployment=slice_deployment.deployment.name)
96
97 if slice_deployment.router_id and slice_deployment.subnet_id:
98 client_driver.delete_router_interface(slice_deployment.router_id, slice_deployment.subnet_id)
99 if slice_deployment.subnet_id:
100 client_driver.delete_subnet(slice_deployment.subnet_id)
Sapan8f425eb2014-12-03 00:02:55 -0500101 if slice_deployment.router_id:
Sapan Bhatiad97caf32014-07-23 09:53:53 -0400102 client_driver.delete_router(slice_deployment.router_id)
103 if slice_deployment.network_id:
104 client_driver.delete_network(slice_deployment.network_id)
105 if slice_deployment.tenant_id:
106 driver.delete_tenant(slice_deployment.tenant_id)
Sapan8f425eb2014-12-03 00:02:55 -0500107