c6021ba9ec9de62cc8d432457b245d795a9994fe
[plstackapi.git] / planetstack / openstack_observer / steps / sync_controller_slices.py
1 import os
2 import base64
3 from collections import defaultdict
4 from netaddr import IPAddress, IPNetwork
5 from django.db.models import F, Q
6 from planetstack.config import Config
7 from observer.openstacksyncstep import OpenStackSyncStep
8 from core.models.slice import Slice, ControllerSlices
9 from core.models.controllerusers import ControllerUsers
10 from util.logger import Logger, logging
11 from observer.ansible import *
12 import pdb
13
14 logger = Logger(level=logging.INFO)
15
16 class 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
26     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
33         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']
40
41         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
73
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)
86         if controller_slice.router_id:
87             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)