blob: c6021ba9ec9de62cc8d432457b245d795a9994fe [file] [log] [blame]
Tony Mack336e0f92014-11-30 15:53:08 -05001import 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 Mack336e0f92014-11-30 15:53:08 -05008from core.models.slice import Slice, ControllerSlices
Sapan Bhatia5e8bad02014-12-19 13:22:16 -05009from core.models.controllerusers import ControllerUsers
Tony Mack336e0f92014-11-30 15:53:08 -050010from util.logger import Logger, logging
11from observer.ansible import *
Sapan Bhatia6271d8c2014-12-22 01:44:38 -050012import pdb
Tony Mack336e0f92014-11-30 15:53:08 -050013
14logger = Logger(level=logging.INFO)
15
16class SyncControllerSlices(OpenStackSyncStep):
17 provides=[ControllerSlices]
18 requested_interval=0
19
20 def fetch_pending(self, deleted):
21 if (deleted):
22 return ControllerSlices.deleted_objects.all()
23 else:
24 return ControllerSlices.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
25
Tony Mack336e0f92014-11-30 15:53:08 -050026 def sync_record(self, controller_slice):
27 logger.info("sync'ing slice controller %s" % controller_slice)
28
29 if not controller_slice.controller.admin_user:
30 logger.info("controller %r has no admin_user, skipping" % controller_slice.controller)
31 return
32
Sapan Bhatiaf6db4db2014-12-22 11:25:31 -050033 controller_users = ControllerUsers.objects.filter(user=controller_slice.slice.creator,
34 controller=controller_slice.controller)
35 if not controller_users:
36 raise Exception("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
37 else:
38 controller_user = controller_users[0]
39 roles = ['admin']
Tony Mack336e0f92014-11-30 15:53:08 -050040
Sapan Bhatiaf6db4db2014-12-22 11:25:31 -050041 pdb.set_trace()
42 max_instances=int(controller_slice.slice.max_slivers)
43 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
44 'admin_user': controller_slice.controller.admin_user,
45 'admin_password': controller_slice.controller.admin_password,
46 'admin_tenant': 'admin',
47 'tenant': controller_slice.slice.name,
48 'tenant_description': controller_slice.slice.description,
49 'roles':roles,
50 'name':controller_user.user.email,
51 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
52 'max_instances':max_instances}
53
54 res = run_template('sync_controller_slices.yaml', tenant_fields, path='controller_slices')
55 expected_num = len(roles)+1
56 if (len(res)!=expected_num):
57 raise Exception('Could not sync tenants for slice %s'%controller_slice.slice.name)
58 else:
59 tenant_id = res[0]['id']
60 if (not controller_slice.tenant_id):
61 try:
62 driver = OpenStackDriver().client_driver(caller=controller_slice.controller.admin_user,
63 tenant=controller_slice.controller.admin_tenant,
64 controller=controller_network.controller)
65 driver.shell.nova.quotas.update(tenant_id=controller_slice.slice.name, instances=int(controller_slice.slice.max_slivers))
66 except:
67 logging.info('Could not update quota for %s'%controller_slice.slice.name)
68 raise Exception('Could not update quota for %s'%controller_slice.slice.name)
69
70 controller_slice.tenant_id = tenant_id
71 controller_slice.save()
72
Tony Mack336e0f92014-11-30 15:53:08 -050073
74
75 def delete_record(self, controller_slice):
76 user = User.objects.get(id=controller_slice.slice.creator.id)
77 driver = OpenStackDriver().admin_driver(controller=controller_slice.controller.name)
78 client_driver = driver.client_driver(caller=user,
79 tenant=controller_slice.slice.name,
80 controller=controller_slice.controller.name)
81
82 if controller_slice.router_id and controller_slice.subnet_id:
83 client_driver.delete_router_interface(controller_slice.router_id, controller_slice.subnet_id)
84 if controller_slice.subnet_id:
85 client_driver.delete_subnet(controller_slice.subnet_id)
Sapan Bhatiaf6db4db2014-12-22 11:25:31 -050086 if controller_slice.router_id:
Tony Mack336e0f92014-11-30 15:53:08 -050087 client_driver.delete_router(controller_slice.router_id)
88 if controller_slice.network_id:
89 client_driver.delete_network(controller_slice.network_id)
90 if controller_slice.tenant_id:
91 driver.delete_tenant(controller_slice.tenant_id)