blob: 0e37524f2abf3f32554792e1650aba2cba8a5649 [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 Macka7dbd422015-01-05 22:48:11 -05008from core.models.slice import Slice, ControllerSlice
Tony Mack26564362015-01-06 17:49:25 -05009from core.models.controlleruser import ControllerUser
Tony Mack33750882015-01-06 18:27:48 -050010from util.logger import Logger, logging, logger
Tony Mack336e0f92014-11-30 15:53:08 -050011from observer.ansible import *
Tony Mack33750882015-01-06 18:27:48 -050012from openstack.driver import OpenStackDriver
Tony Mack336e0f92014-11-30 15:53:08 -050013
14logger = Logger(level=logging.INFO)
15
Tony Mack26564362015-01-06 17:49:25 -050016class SyncControllerSlices(OpenStackSyncStep):
Sapan Bhatiae34efa02015-01-16 22:16:29 +000017 provides=[ControllerSlice, Slice]
Tony Mack336e0f92014-11-30 15:53:08 -050018 requested_interval=0
19
20 def fetch_pending(self, deleted):
21 if (deleted):
Tony Macka7dbd422015-01-05 22:48:11 -050022 return ControllerSlice.deleted_objects.all()
Tony Mack336e0f92014-11-30 15:53:08 -050023 else:
Tony Macka7dbd422015-01-05 22:48:11 -050024 return ControllerSlice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack336e0f92014-11-30 15:53:08 -050025
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
Tony Macka7dbd422015-01-05 22:48:11 -050033 controller_users = ControllerUser.objects.filter(user=controller_slice.slice.creator,
Sapan Bhatiaf6db4db2014-12-22 11:25:31 -050034 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]
Tony Mack33750882015-01-06 18:27:48 -050039 roles = ['Admin']
Tony Mack336e0f92014-11-30 15:53:08 -050040
Sapan Bhatiaf6db4db2014-12-22 11:25:31 -050041 max_instances=int(controller_slice.slice.max_slivers)
42 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
43 'admin_user': controller_slice.controller.admin_user,
44 'admin_password': controller_slice.controller.admin_password,
45 'admin_tenant': 'admin',
46 'tenant': controller_slice.slice.name,
47 'tenant_description': controller_slice.slice.description,
48 'roles':roles,
49 'name':controller_user.user.email,
50 'ansible_tag':'%s@%s'%(controller_slice.slice.name,controller_slice.controller.name),
51 'max_instances':max_instances}
52
Sapan Bhatiaf6db4db2014-12-22 11:25:31 -050053 expected_num = len(roles)+1
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000054 res = run_template('sync_controller_slices.yaml', tenant_fields, path='controller_slices', expected_num=expected_num)
55 tenant_id = res[0]['id']
56 if (not controller_slice.tenant_id):
57 try:
58 driver = OpenStackDriver().admin_driver(controller=controller_slice.controller)
59 driver.shell.nova.quotas.update(tenant_id=controller_slice.tenant_id, instances=int(controller_slice.slice.max_slivers))
60 except:
61 logger.log_exc('Could not update quota for %s'%controller_slice.slice.name)
62 raise Exception('Could not update quota for %s'%controller_slice.slice.name)
Tony Mack33750882015-01-06 18:27:48 -050063
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000064 controller_slice.tenant_id = tenant_id
65 controller_slice.save()
Sapan Bhatiaf6db4db2014-12-22 11:25:31 -050066
Tony Mack336e0f92014-11-30 15:53:08 -050067
Tony Mack336e0f92014-11-30 15:53:08 -050068 def delete_record(self, controller_slice):
69 user = User.objects.get(id=controller_slice.slice.creator.id)
70 driver = OpenStackDriver().admin_driver(controller=controller_slice.controller.name)
71 client_driver = driver.client_driver(caller=user,
72 tenant=controller_slice.slice.name,
73 controller=controller_slice.controller.name)
74
75 if controller_slice.router_id and controller_slice.subnet_id:
76 client_driver.delete_router_interface(controller_slice.router_id, controller_slice.subnet_id)
77 if controller_slice.subnet_id:
78 client_driver.delete_subnet(controller_slice.subnet_id)
Sapan Bhatiaf6db4db2014-12-22 11:25:31 -050079 if controller_slice.router_id:
Tony Mack336e0f92014-11-30 15:53:08 -050080 client_driver.delete_router(controller_slice.router_id)
81 if controller_slice.network_id:
82 client_driver.delete_network(controller_slice.network_id)
83 if controller_slice.tenant_id:
84 driver.delete_tenant(controller_slice.tenant_id)