blob: 636d007c3dc829725ecc16e25988eaf8cd8a424a [file] [log] [blame]
Tony Mack06c8e472014-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 Mack06c8e472014-11-30 15:53:08 -05008from core.models.slice import Slice, ControllerSlices
Sapan Bhatiac220f2a2014-12-19 13:22:16 -05009from core.models.controllerusers import ControllerUsers
Tony Mack06c8e472014-11-30 15:53:08 -050010from util.logger import Logger, logging
11from observer.ansible import *
12
13logger = Logger(level=logging.INFO)
14
15class SyncControllerSlices(OpenStackSyncStep):
16 provides=[ControllerSlices]
17 requested_interval=0
18
19 def fetch_pending(self, deleted):
20 if (deleted):
21 return ControllerSlices.deleted_objects.all()
22 else:
23 return ControllerSlices.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
24
Tony Mack06c8e472014-11-30 15:53:08 -050025 def sync_record(self, controller_slice):
26 logger.info("sync'ing slice controller %s" % controller_slice)
27
28 if not controller_slice.controller.admin_user:
29 logger.info("controller %r has no admin_user, skipping" % controller_slice.controller)
30 return
31
32 controller_users = ControllerUsers.objects.filter(user=controller_slice.slice.creator,
33 controller=controller_slice.controller)
34 if not controller_users:
35 logger.info("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
36 roles = []
37 else:
38 controller_user = controller_users[0]
39 roles = ['admin']
40
41 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 'max_instances':max_instances}
51
52 res = run_template('sync_controller_slices.yaml', tenant_fields)
53 expected_num = len(roles)+1
54 if (len(res)!=expected_num):
55 raise Exception('Could not sync tenants for slice %s'%controller_slice.slice.name)
56 else:
57 tenant_id = res[0]['id']
58 if (not controller_slice.tenant_id):
59 handle = os.popen('nova quota-update --instances %d %s'%(max_instances,tenant_id))
60 output = handle.read()
61 result = handle.close()
62 if (result):
63 logging.info('Could not update quota for %s'%controller_slice.slice.name)
64 controller_slice.tenant_id = tenant_id
65 controller_slice.save()
66
67
68
69 def delete_record(self, controller_slice):
70 user = User.objects.get(id=controller_slice.slice.creator.id)
71 driver = OpenStackDriver().admin_driver(controller=controller_slice.controller.name)
72 client_driver = driver.client_driver(caller=user,
73 tenant=controller_slice.slice.name,
74 controller=controller_slice.controller.name)
75
76 if controller_slice.router_id and controller_slice.subnet_id:
77 client_driver.delete_router_interface(controller_slice.router_id, controller_slice.subnet_id)
78 if controller_slice.subnet_id:
79 client_driver.delete_subnet(controller_slice.subnet_id)
80 if controller_slice.router_id:
81 client_driver.delete_router(controller_slice.router_id)
82 if controller_slice.network_id:
83 client_driver.delete_network(controller_slice.network_id)
84 if controller_slice.tenant_id:
85 driver.delete_tenant(controller_slice.tenant_id)
86