blob: 9a25c19589fd83f7d3fe06177ebd35746b060c75 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Sapan Bhatia1e5d3442015-01-23 16:26:46 +00003import socket
Tony Mackae7f30c2013-09-25 12:46:50 -04004from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04005from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04006from observer.openstacksyncstep import OpenStackSyncStep
7from core.models.sliver import Sliver
Tony Macka7dbd422015-01-05 22:48:11 -05008from core.models.slice import Slice, SlicePrivilege, ControllerSlice
9from core.models.network import Network, NetworkSlice, ControllerNetwork
Tony Mackfe9d9bd2014-04-07 19:46:15 -040010from util.logger import Logger, logging
Sapan Bhatia475c5972014-11-05 10:32:41 -050011from observer.ansible import *
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
Sapan Bhatia99f49682015-01-29 20:58:25 +000022 observes=Sliver
Tony Mack66646d52013-09-24 21:47:12 -040023
Scott Bakerdb0a1102014-08-08 09:57:28 -070024 def get_userdata(self, sliver):
25 userdata = 'opencloud:\n slicename: "%s"\n hostname: "%s"\n' % (sliver.slice.name, sliver.node.name)
26 return userdata
27
Tony Mack27ec0f52013-10-16 13:13:32 -040028 def sync_record(self, sliver):
Sapan Bhatia2471d252014-12-22 13:01:18 -050029 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 -050030
Tony Mack6fd3ef82013-10-08 22:17:54 -040031 metadata_update = {}
Sapand1a582d2014-12-03 00:01:23 -050032 if (sliver.numberCores):
Tony Mack6fd3ef82013-10-08 22:17:54 -040033 metadata_update["cpu_cores"] = str(sliver.numberCores)
34
35 for tag in sliver.slice.tags.all():
36 if tag.name.startswith("sysctl-"):
37 metadata_update[tag.name] = tag.value
38
Sapan Bhatia475c5972014-11-05 10:32:41 -050039 # public keys
40 slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
41 pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key])
Sapand1a582d2014-12-03 00:01:23 -050042 if sliver.creator.public_key:
43 pubkeys.add(sliver.creator.public_key)
Tony Macked9925a2014-04-14 22:15:02 -040044
Sapan Bhatia475c5972014-11-05 10:32:41 -050045 if sliver.slice.creator.public_key:
Sapand1a582d2014-12-03 00:01:23 -050046 pubkeys.add(sliver.slice.creator.public_key)
Scott Baker9c7a1e12014-05-30 14:42:42 -070047
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050048 nics = []
49 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]
Tony Macka7dbd422015-01-05 22:48:11 -050050 controller_networks = ControllerNetwork.objects.filter(network__in=networks,
Sapan Bhatia2471d252014-12-22 13:01:18 -050051 controller=sliver.node.site_deployment.controller)
Scott Bakerdb0a1102014-08-08 09:57:28 -070052
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050053 for controller_network in controller_networks:
54 if controller_network.network.template.visibility == 'private' and \
55 controller_network.network.template.translation == 'none' and controller_network.net_id:
56 nics.append(controller_network.net_id)
Sapan Bhatia475c5972014-11-05 10:32:41 -050057
Sapand1a582d2014-12-03 00:01:23 -050058 # now include network template
Scott Baker81fa17f2015-01-03 12:03:38 -080059 network_templates = [network.template.shared_network_name for network in networks \
60 if network.template.shared_network_name]
Sapan Bhatia475c5972014-11-05 10:32:41 -050061
Tony Mack336e0f92014-11-30 15:53:08 -050062 #driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, controller=sliver.controllerNetwork)
Tony Mackdd8746b2015-01-07 12:48:37 -050063 driver = self.driver.admin_driver(tenant='admin', controller=sliver.node.site_deployment.controller)
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050064 nets = driver.shell.quantum.list_networks()['networks']
65 for net in nets:
66 if net['name'] in network_templates:
67 nics.append(net['id'])
Sapan Bhatia475c5972014-11-05 10:32:41 -050068
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050069 if (not nics):
70 for net in nets:
71 if net['name']=='public':
72 nics.append(net['id'])
Sapan Bhatia475c5972014-11-05 10:32:41 -050073
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050074 # look up image id
Tony Mackdd8746b2015-01-07 12:48:37 -050075 controller_driver = self.driver.admin_driver(controller=sliver.node.site_deployment.controller)
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050076 image_id = None
77 images = controller_driver.shell.glanceclient.images.list()
78 for image in images:
79 if image.name == sliver.image.name or not image_id:
80 image_id = image.id
Sapan Bhatia475c5972014-11-05 10:32:41 -050081
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050082 # look up key name at the controller
83 # create/fetch keypair
84 keyname = None
85 keyname = sliver.creator.email.lower().replace('@', 'AT').replace('.', '') +\
86 sliver.slice.name
87 key_fields = {'name': keyname,
88 'public_key': sliver.creator.public_key}
Sapan Bhatia475c5972014-11-05 10:32:41 -050089
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050090 try:
91 legacy = Config().observer_legacy
92 except:
93 legacy = False
94
95 if (legacy):
96 host_filter = sliver.node.name.split('.',1)[0]
97 else:
Tony Mack95f8e672015-01-15 13:32:54 -050098 host_filter = sliver.node.name.strip()
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050099
100 availability_zone_filter = 'nova:%s'%host_filter
101 sliver_name = '%s-%d'%(sliver.slice.name,sliver.id)
102
103 userData = self.get_userdata(sliver)
104 if sliver.userData:
105 userData = sliver.userData
106
Tony Mackdd8746b2015-01-07 12:48:37 -0500107 controller = sliver.node.site_deployment.controller
Sapan Bhatia2471d252014-12-22 13:01:18 -0500108 tenant_fields = {'endpoint':controller.auth_url,
Tony Mackbf0a90e2015-01-10 22:31:26 -0500109 'admin_user': sliver.creator.email,
Sapan Bhatia740f7ef2015-01-16 22:16:10 +0000110 'admin_password': sliver.creator.remote_password,
Sapan Bhatiaf3d21322014-12-22 11:50:47 -0500111 'admin_tenant': sliver.slice.name,
112 'tenant': sliver.slice.name,
113 'tenant_description': sliver.slice.description,
114 'name':sliver_name,
115 'ansible_tag':sliver_name,
116 'availability_zone': availability_zone_filter,
117 'image_id':image_id,
118 'key_name':keyname,
Tony Mackf210e3d2015-01-19 15:19:24 -0500119 'flavor_id':sliver.flavor.id,
Sapan Bhatiaf3d21322014-12-22 11:50:47 -0500120 'nics':nics,
121 'meta':metadata_update,
122 'key':key_fields,
123 'user_data':r'%s'%escape(userData)}
124
Sapan Bhatiab0464ba2015-01-23 16:21:57 +0000125 res = run_template('sync_slivers.yaml', tenant_fields,path='slivers', expected_num=2)
126 sliver_id = res[1]['info']['OS-EXT-SRV-ATTR:instance_name'] # 0 is for the key
127 sliver_uuid = res[1]['id'] # 0 is for the key
Sapan Bhatiacb5c24a2015-01-20 20:59:24 +0000128
Sapan Bhatiab0464ba2015-01-23 16:21:57 +0000129 try:
130 hostname = res[1]['info']['OS-EXT-SRV-ATTR:hypervisor_hostname']
131 ip = socket.gethostbyname(hostname)
132 sliver.ip = ip
133 except:
134 pass
Sapan Bhatia475c5972014-11-05 10:32:41 -0500135
Sapan Bhatiab0464ba2015-01-23 16:21:57 +0000136 sliver.instance_id = sliver_id
137 sliver.instance_uuid = sliver_uuid
138 sliver.instance_name = sliver_name
139 sliver.save()
Sapan Bhatia24836f12013-08-27 10:16:05 -0400140
Sapan Bhatia96500042014-07-23 09:37:19 -0400141 def delete_record(self, sliver):
Sapan Bhatiab0464ba2015-01-23 16:21:57 +0000142 sliver_name = '%s-%d'%(sliver.slice.name,sliver.id)
143 controller = sliver.node.site_deployment.controller
144 tenant_fields = {'endpoint':controller.auth_url,
145 'admin_user': sliver.creator.email,
146 'admin_password': sliver.creator.remote_password,
147 'admin_tenant': sliver.slice.name,
148 'tenant': sliver.slice.name,
149 'tenant_description': sliver.slice.description,
150 'name':sliver_name,
151 'ansible_tag':sliver_name,
152 'delete': True}
153
154 res = run_template('sync_slivers.yaml', tenant_fields,path='slivers')
155 if (len(res)!=1):
156 raise Exception('Could not delete sliver %s'%sliver.slice.name)