blob: 07aca4c74846381428dc21b77986e534efb5f9e0 [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
9from core.models.usercontrollers import ControllerUsers
10from 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
25 def get_next_subnet(self, controller=None):
26 # limit ourself to 10.0.x.x for now
27 valid_subnet = lambda net: net.startswith('10.0')
28 driver = self.driver.admin_driver(controller=controller)
29 subnets = driver.shell.quantum.list_subnets()['subnets']
30 ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \
31 if valid_subnet(subnet['cidr'])]
32 ints.sort()
33 if ints:
34 last_ip = IPAddress(ints[-1])
35 else:
36 last_ip = IPAddress('10.0.0.1')
37 last_ip = IPAddress(ints[-1])
38 last_network = IPNetwork(str(last_ip) + "/24")
39 next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24")
40 return next_network
41
42
43 def sync_record(self, controller_slice):
44 logger.info("sync'ing slice controller %s" % controller_slice)
45
46 if not controller_slice.controller.admin_user:
47 logger.info("controller %r has no admin_user, skipping" % controller_slice.controller)
48 return
49
50 controller_users = ControllerUsers.objects.filter(user=controller_slice.slice.creator,
51 controller=controller_slice.controller)
52 if not controller_users:
53 logger.info("slice createor %s has not accout at controller %s" % (controller_slice.slice.creator, controller_slice.controller.name))
54 roles = []
55 else:
56 controller_user = controller_users[0]
57 roles = ['admin']
58
59 max_instances=int(controller_slice.slice.max_slivers)
60 tenant_fields = {'endpoint':controller_slice.controller.auth_url,
61 'admin_user': controller_slice.controller.admin_user,
62 'admin_password': controller_slice.controller.admin_password,
63 'admin_tenant': 'admin',
64 'tenant': controller_slice.slice.name,
65 'tenant_description': controller_slice.slice.description,
66 'roles':roles,
67 'name':controller_user.user.email,
68 'max_instances':max_instances}
69
70 res = run_template('sync_controller_slices.yaml', tenant_fields)
71 expected_num = len(roles)+1
72 if (len(res)!=expected_num):
73 raise Exception('Could not sync tenants for slice %s'%controller_slice.slice.name)
74 else:
75 tenant_id = res[0]['id']
76 if (not controller_slice.tenant_id):
77 handle = os.popen('nova quota-update --instances %d %s'%(max_instances,tenant_id))
78 output = handle.read()
79 result = handle.close()
80 if (result):
81 logging.info('Could not update quota for %s'%controller_slice.slice.name)
82 controller_slice.tenant_id = tenant_id
83 controller_slice.save()
84
85
86
87 def delete_record(self, controller_slice):
88 user = User.objects.get(id=controller_slice.slice.creator.id)
89 driver = OpenStackDriver().admin_driver(controller=controller_slice.controller.name)
90 client_driver = driver.client_driver(caller=user,
91 tenant=controller_slice.slice.name,
92 controller=controller_slice.controller.name)
93
94 if controller_slice.router_id and controller_slice.subnet_id:
95 client_driver.delete_router_interface(controller_slice.router_id, controller_slice.subnet_id)
96 if controller_slice.subnet_id:
97 client_driver.delete_subnet(controller_slice.subnet_id)
98 if controller_slice.router_id:
99 client_driver.delete_router(controller_slice.router_id)
100 if controller_slice.network_id:
101 client_driver.delete_network(controller_slice.network_id)
102 if controller_slice.tenant_id:
103 driver.delete_tenant(controller_slice.tenant_id)
104