blob: 97196d628423ad4d13132effd53876fdf5ba0fcc [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
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
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
Sapan Bhatiad97caf32014-07-23 09:53:53 -040020 def fetch_pending(self, deleted):
21 if (deleted):
22 return SliceDeployments.deleted_objects.all()
23 else:
Sapan Bhatiad97caf32014-07-23 09:53:53 -040024 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
Sapan Bhatia475c5972014-11-05 10:32:41 -050051 deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator,
Tony Mack69f1bc32014-03-12 13:20:34 -040052 deployment=slice_deployment.deployment)
Sapan Bhatia475c5972014-11-05 10:32:41 -050053 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 = []
56 else:
57 deployment_user = deployment_users[0]
58 roles = ['admin']
59
60 max_instances=int(slice_deployment.slice.max_slivers)
61 tenant_fields = {'endpoint':slice_deployment.deployment.auth_url,
62 'admin_user': slice_deployment.deployment.admin_user,
63 'admin_password': slice_deployment.deployment.admin_password,
64 'admin_tenant': 'admin',
65 'tenant': slice_deployment.slice.name,
66 'tenant_description': slice_deployment.slice.description,
67 'roles':roles,
68 'name':deployment_user.email,
69 'max_instances':max_instances}
Tony Macke4be32f2014-03-11 20:45:25 -040070
Sapan Bhatia475c5972014-11-05 10:32:41 -050071 res = run_template('sync_slice_deployments.yaml', tenant_fields)
72 expected_num = len(roles)+1
73 if (len(res)!=expected_num):
74 raise Exception('Could not sync tenants for slice %s'%slice_deployment.slice.name)
75 else:
76 tenant_id = res[0]['id']
77 if (not slice_deployment.tenant_id):
78 handle = os.popen('nova quota-update --instances %d %s'%(max_instances,tenant_id))
79 output = handle.read()
80 result = handle.close()
81 if (result):
82 logging.info('Could not update quota for %s'%slice_deployment.slice.name)
83 slice_deployment.tenant_id = tenant_id
84 slice_deployment.save()
85
Sapan Bhatiad97caf32014-07-23 09:53:53 -040086
87
88 def delete_record(self, slice_deployment):
89 user = User.objects.get(id=slice_deployment.slice.creator.id)
90 driver = OpenStackDriver().admin_driver(deployment=slice_deployment.deployment.name)
91 client_driver = driver.client_driver(caller=user,
92 tenant=slice_deployment.slice.name,
93 deployment=slice_deployment.deployment.name)
94
95 if slice_deployment.router_id and slice_deployment.subnet_id:
96 client_driver.delete_router_interface(slice_deployment.router_id, slice_deployment.subnet_id)
97 if slice_deployment.subnet_id:
98 client_driver.delete_subnet(slice_deployment.subnet_id)
99 if slice_deployment.router_id:
100 client_driver.delete_router(slice_deployment.router_id)
101 if slice_deployment.network_id:
102 client_driver.delete_network(slice_deployment.network_id)
103 if slice_deployment.tenant_id:
104 driver.delete_tenant(slice_deployment.tenant_id)
Sapan Bhatia475c5972014-11-05 10:32:41 -0500105