blob: 6f61f5337914dba283de5fce15c5b37e10005a32 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04004from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
6from core.models.sliver import Sliver
Tony Mack336e0f92014-11-30 15:53:08 -05007from core.models.slice import Slice, SlicePrivilege, ControllerSlices
8from core.models.network import Network, NetworkSlice, ControllerNetworks
Tony Mackfe9d9bd2014-04-07 19:46:15 -04009from util.logger import Logger, logging
Sapan Bhatia475c5972014-11-05 10:32:41 -050010from observer.ansible import *
Sapan Bhatia2471d252014-12-22 13:01:18 -050011import pdb
Tony Mackfe9d9bd2014-04-07 19:46:15 -040012
13logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040014
Sapan Bhatia475c5972014-11-05 10:32:41 -050015def escape(s):
16 s = s.replace('\n',r'\n').replace('"',r'\"')
17 return s
Sapand1a582d2014-12-03 00:01:23 -050018
Sapan Bhatia24836f12013-08-27 10:16:05 -040019class SyncSlivers(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -040020 provides=[Sliver]
21 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040022
Scott Bakerdb0a1102014-08-08 09:57:28 -070023 def get_userdata(self, sliver):
24 userdata = 'opencloud:\n slicename: "%s"\n hostname: "%s"\n' % (sliver.slice.name, sliver.node.name)
25 return userdata
26
Tony Mack27ec0f52013-10-16 13:13:32 -040027 def sync_record(self, sliver):
Sapan Bhatia2471d252014-12-22 13:01:18 -050028 logger.info("sync'ing sliver:%s slice:%s controller:%s " % (sliver, sliver.slice.name, sliver.node.site_deployment.controller))
Sapan Bhatia475c5972014-11-05 10:32:41 -050029
Tony Mack6fd3ef82013-10-08 22:17:54 -040030 metadata_update = {}
Sapand1a582d2014-12-03 00:01:23 -050031 if (sliver.numberCores):
Tony Mack6fd3ef82013-10-08 22:17:54 -040032 metadata_update["cpu_cores"] = str(sliver.numberCores)
33
34 for tag in sliver.slice.tags.all():
35 if tag.name.startswith("sysctl-"):
36 metadata_update[tag.name] = tag.value
37
Sapan Bhatia475c5972014-11-05 10:32:41 -050038 # public keys
39 slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
40 pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key])
Sapand1a582d2014-12-03 00:01:23 -050041 if sliver.creator.public_key:
42 pubkeys.add(sliver.creator.public_key)
Tony Macked9925a2014-04-14 22:15:02 -040043
Sapan Bhatia475c5972014-11-05 10:32:41 -050044 if sliver.slice.creator.public_key:
Sapand1a582d2014-12-03 00:01:23 -050045 pubkeys.add(sliver.slice.creator.public_key)
Scott Baker9c7a1e12014-05-30 14:42:42 -070046
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050047 nics = []
48 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]
49 controller_networks = ControllerNetworks.objects.filter(network__in=networks,
Sapan Bhatia2471d252014-12-22 13:01:18 -050050 controller=sliver.node.site_deployment.controller)
Scott Bakerdb0a1102014-08-08 09:57:28 -070051
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050052 for controller_network in controller_networks:
53 if controller_network.network.template.visibility == 'private' and \
54 controller_network.network.template.translation == 'none' and controller_network.net_id:
55 nics.append(controller_network.net_id)
Sapan Bhatia475c5972014-11-05 10:32:41 -050056
Sapand1a582d2014-12-03 00:01:23 -050057 # now include network template
58 network_templates = [network.template.sharedNetworkName for network in networks \
59 if network.template.sharedNetworkName]
Sapan Bhatia475c5972014-11-05 10:32:41 -050060
Tony Mack336e0f92014-11-30 15:53:08 -050061 #driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, controller=sliver.controllerNetwork)
62 driver = self.driver.admin_driver(tenant='admin', controller=sliver.controllerNetwork)
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050063 nets = driver.shell.quantum.list_networks()['networks']
64 for net in nets:
65 if net['name'] in network_templates:
66 nics.append(net['id'])
Sapan Bhatia475c5972014-11-05 10:32:41 -050067
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050068 if (not nics):
69 for net in nets:
70 if net['name']=='public':
71 nics.append(net['id'])
Sapan Bhatia475c5972014-11-05 10:32:41 -050072
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050073 # look up image id
Sapan Bhatia2471d252014-12-22 13:01:18 -050074 controller_driver = self.driver.admin_driver(controller=sliver.controllerNetwork)
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050075 image_id = None
76 images = controller_driver.shell.glanceclient.images.list()
77 for image in images:
78 if image.name == sliver.image.name or not image_id:
79 image_id = image.id
Sapan Bhatia475c5972014-11-05 10:32:41 -050080
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050081 # look up key name at the controller
82 # create/fetch keypair
83 keyname = None
84 keyname = sliver.creator.email.lower().replace('@', 'AT').replace('.', '') +\
85 sliver.slice.name
86 key_fields = {'name': keyname,
87 'public_key': sliver.creator.public_key}
Sapan Bhatia475c5972014-11-05 10:32:41 -050088
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050089 try:
90 legacy = Config().observer_legacy
91 except:
92 legacy = False
93
94 if (legacy):
95 host_filter = sliver.node.name.split('.',1)[0]
96 else:
97 host_filter = sliver.node.name
98
99 availability_zone_filter = 'nova:%s'%host_filter
100 sliver_name = '%s-%d'%(sliver.slice.name,sliver.id)
101
102 userData = self.get_userdata(sliver)
103 if sliver.userData:
104 userData = sliver.userData
105
Sapan Bhatia2471d252014-12-22 13:01:18 -0500106 controller = sliver.controllerNetwork
107 tenant_fields = {'endpoint':controller.auth_url,
Sapan Bhatiaf3d21322014-12-22 11:50:47 -0500108 'admin_user': sliver.creator.username,
109 'admin_password': sliver.creator.password,
110 'admin_tenant': sliver.slice.name,
111 'tenant': sliver.slice.name,
112 'tenant_description': sliver.slice.description,
113 'name':sliver_name,
114 'ansible_tag':sliver_name,
115 'availability_zone': availability_zone_filter,
116 'image_id':image_id,
117 'key_name':keyname,
118 'flavor_id':3,
119 'nics':nics,
120 'meta':metadata_update,
121 'key':key_fields,
122 'user_data':r'%s'%escape(userData)}
123
124 res = run_template('sync_slivers.yaml', tenant_fields,path='slivers')
125 if (len(res)!=2):
126 raise Exception('Could not sync sliver %s'%sliver.slice.name)
127 else:
128 sliver_id = res[1]['id'] # 0 is for the key
Sapan Bhatia475c5972014-11-05 10:32:41 -0500129
130 sliver.instance_id = sliver_id
131 sliver.instance_name = sliver_name
Sapand1a582d2014-12-03 00:01:23 -0500132 sliver.save()
Sapan Bhatia24836f12013-08-27 10:16:05 -0400133
Sapan Bhatia96500042014-07-23 09:37:19 -0400134 def delete_record(self, sliver):
Sapan9f0ae192014-12-03 01:27:07 -0500135 sliver_name = '@'.join([sliver.slice.name,sliver.node.name])
136 tenant_fields = {'name':sliver_name,
137 'ansible_tag':sliver_name
138 }
139 res = run_template('delete_slivers.yaml', tenant_fields, path='slivers')