blob: 3989c162049a2b502a359bbe9581b7627e539b9c [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
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 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
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 Bhatia6bc43282014-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 Bhatia14356b72014-11-05 10:32:41 -050029
Tony Mack6fd3ef82013-10-08 22:17:54 -040030 metadata_update = {}
Sapan6a975622014-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 Bhatia14356b72014-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])
Sapan6a975622014-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 Bhatia14356b72014-11-05 10:32:41 -050044 if sliver.slice.creator.public_key:
Sapan6a975622014-12-03 00:01:23 -050045 pubkeys.add(sliver.slice.creator.public_key)
Scott Baker9c7a1e12014-05-30 14:42:42 -070046
Sapan Bhatiaeeaf5fd2014-12-22 11:50:47 -050047 nics = []
48 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]
Tony Mack3066a952015-01-05 22:48:11 -050049 controller_networks = ControllerNetwork.objects.filter(network__in=networks,
Sapan Bhatia6bc43282014-12-22 13:01:18 -050050 controller=sliver.node.site_deployment.controller)
Scott Bakerdb0a1102014-08-08 09:57:28 -070051
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050056
Sapan6a975622014-12-03 00:01:23 -050057 # now include network template
Scott Baker369f9b92015-01-03 12:03:38 -080058 network_templates = [network.template.shared_network_name for network in networks \
59 if network.template.shared_network_name]
Sapan Bhatia14356b72014-11-05 10:32:41 -050060
Tony Mack06c8e472014-11-30 15:53:08 -050061 #driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, controller=sliver.controllerNetwork)
Tony Mack80c42542015-01-07 12:48:37 -050062 driver = self.driver.admin_driver(tenant='admin', controller=sliver.node.site_deployment.controller)
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050067
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050072
Sapan Bhatiaeeaf5fd2014-12-22 11:50:47 -050073 # look up image id
Tony Mack80c42542015-01-07 12:48:37 -050074 controller_driver = self.driver.admin_driver(controller=sliver.node.site_deployment.controller)
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050080
Sapan Bhatiaeeaf5fd2014-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 Bhatia14356b72014-11-05 10:32:41 -050088
Sapan Bhatiaeeaf5fd2014-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:
Tony Mack7da8d8f2015-01-15 13:32:54 -050097 host_filter = sliver.node.name.strip()
Sapan Bhatiaeeaf5fd2014-12-22 11:50:47 -050098
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
Tony Mack80c42542015-01-07 12:48:37 -0500106 controller = sliver.node.site_deployment.controller
Sapan Bhatia6bc43282014-12-22 13:01:18 -0500107 tenant_fields = {'endpoint':controller.auth_url,
Tony Mackd1275ea2015-01-10 22:31:26 -0500108 'admin_user': sliver.creator.email,
Sapan Bhatiad7750cf2015-01-16 22:16:10 +0000109 'admin_password': sliver.creator.remote_password,
Sapan Bhatiaeeaf5fd2014-12-22 11:50:47 -0500110 '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,
Tony Mack3f126ae2015-01-19 15:19:24 -0500118 'flavor_id':sliver.flavor.id,
Sapan Bhatiaeeaf5fd2014-12-22 11:50:47 -0500119 'nics':nics,
120 'meta':metadata_update,
121 'key':key_fields,
122 'user_data':r'%s'%escape(userData)}
123
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +0000124 res = run_template('sync_slivers.yaml', tenant_fields,path='slivers', expected_num=2)
125 sliver_id = res[1]['info']['OS-EXT-SRV-ATTR:instance_name'] # 0 is for the key
126 sliver_uuid = res[1]['id'] # 0 is for the key
Sapan Bhatia68b9c1c2015-01-20 20:59:24 +0000127
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +0000128 try:
129 hostname = res[1]['info']['OS-EXT-SRV-ATTR:hypervisor_hostname']
130 ip = socket.gethostbyname(hostname)
131 sliver.ip = ip
132 except:
133 pass
Sapan Bhatia14356b72014-11-05 10:32:41 -0500134
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +0000135 sliver.instance_id = sliver_id
136 sliver.instance_uuid = sliver_uuid
137 sliver.instance_name = sliver_name
138 sliver.save()
Sapan Bhatia24836f12013-08-27 10:16:05 -0400139
Sapan Bhatia96500042014-07-23 09:37:19 -0400140 def delete_record(self, sliver):
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +0000141 sliver_name = '%s-%d'%(sliver.slice.name,sliver.id)
142 controller = sliver.node.site_deployment.controller
143 tenant_fields = {'endpoint':controller.auth_url,
144 'admin_user': sliver.creator.email,
145 'admin_password': sliver.creator.remote_password,
146 'admin_tenant': sliver.slice.name,
147 'tenant': sliver.slice.name,
148 'tenant_description': sliver.slice.description,
149 'name':sliver_name,
150 'ansible_tag':sliver_name,
151 'delete': True}
152
153 res = run_template('sync_slivers.yaml', tenant_fields,path='slivers')
154 if (len(res)!=1):
155 raise Exception('Could not delete sliver %s'%sliver.slice.name)