blob: 00e1691b04e4e910801c2924120d968ee1b1b21e [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Sapan Bhatia3346abe2015-01-23 16:26:46 +00003import socket
Tony Mackae7f30c2013-09-25 12:46:50 -04004from django.db.models import F, Q
Scott Baker86e132c2015-02-11 21:38:09 -08005from xos.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04006from observer.openstacksyncstep import OpenStackSyncStep
7from core.models.sliver import Sliver
Tony Mack3066a952015-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 Bhatia14356b72014-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 Bhatia14356b72014-11-05 10:32:41 -050015def escape(s):
16 s = s.replace('\n',r'\n').replace('"',r'\"')
17 return s
Sapan6a975622014-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 Bhatia39a775f2015-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 Bhatia6bc43282014-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 Bhatia14356b72014-11-05 10:32:41 -050030
Tony Mack6fd3ef82013-10-08 22:17:54 -040031 metadata_update = {}
Sapan6a975622014-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 Bhatia14356b72014-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])
Sapan6a975622014-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 Bhatia14356b72014-11-05 10:32:41 -050045 if sliver.slice.creator.public_key:
Sapan6a975622014-12-03 00:01:23 -050046 pubkeys.add(sliver.slice.creator.public_key)
Scott Baker9c7a1e12014-05-30 14:42:42 -070047
Sapan Bhatiaeeaf5fd2014-12-22 11:50:47 -050048 nics = []
49 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]
Tony Mack3066a952015-01-05 22:48:11 -050050 controller_networks = ControllerNetwork.objects.filter(network__in=networks,
Sapan Bhatia6bc43282014-12-22 13:01:18 -050051 controller=sliver.node.site_deployment.controller)
Scott Bakerdb0a1102014-08-08 09:57:28 -070052
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050057
Sapan6a975622014-12-03 00:01:23 -050058 # now include network template
Scott Baker369f9b92015-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 Bhatia14356b72014-11-05 10:32:41 -050061
Tony Mack06c8e472014-11-30 15:53:08 -050062 #driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, controller=sliver.controllerNetwork)
Tony Mack80c42542015-01-07 12:48:37 -050063 driver = self.driver.admin_driver(tenant='admin', controller=sliver.node.site_deployment.controller)
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050068
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050073
Sapan Bhatiaeeaf5fd2014-12-22 11:50:47 -050074 # look up image id
Tony Mack80c42542015-01-07 12:48:37 -050075 controller_driver = self.driver.admin_driver(controller=sliver.node.site_deployment.controller)
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050081
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050089
Sapan Bhatiaeeaf5fd2014-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 Mack7da8d8f2015-01-15 13:32:54 -050098 host_filter = sliver.node.name.strip()
Sapan Bhatiaeeaf5fd2014-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 Mack80c42542015-01-07 12:48:37 -0500107 controller = sliver.node.site_deployment.controller
Sapan Bhatia6bc43282014-12-22 13:01:18 -0500108 tenant_fields = {'endpoint':controller.auth_url,
Tony Mackd1275ea2015-01-10 22:31:26 -0500109 'admin_user': sliver.creator.email,
Sapan Bhatiad7750cf2015-01-16 22:16:10 +0000110 'admin_password': sliver.creator.remote_password,
Sapan Bhatiaeeaf5fd2014-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 Mack3f126ae2015-01-19 15:19:24 -0500119 'flavor_id':sliver.flavor.id,
Sapan Bhatiaeeaf5fd2014-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 Bhatia3b3e1e12015-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 Bhatia68b9c1c2015-01-20 20:59:24 +0000128
Sapan Bhatia3b3e1e12015-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 Bhatia14356b72014-11-05 10:32:41 -0500135
Sapan Bhatia3b3e1e12015-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 Bhatia3b3e1e12015-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)