blob: 94fd0d5fd809cc8092266ce7c5193667c4440ff9 [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 *
Tony Mackfe9d9bd2014-04-07 19:46:15 -040011
12logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040013
Sapan Bhatia475c5972014-11-05 10:32:41 -050014def escape(s):
15 s = s.replace('\n',r'\n').replace('"',r'\"')
16 return s
Sapand1a582d2014-12-03 00:01:23 -050017
Sapan Bhatia24836f12013-08-27 10:16:05 -040018class SyncSlivers(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -040019 provides=[Sliver]
20 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040021
Scott Bakerdb0a1102014-08-08 09:57:28 -070022 def get_userdata(self, sliver):
23 userdata = 'opencloud:\n slicename: "%s"\n hostname: "%s"\n' % (sliver.slice.name, sliver.node.name)
24 return userdata
25
Tony Mack27ec0f52013-10-16 13:13:32 -040026 def sync_record(self, sliver):
Sapan Bhatia2471d252014-12-22 13:01:18 -050027 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 -050028
Tony Mack6fd3ef82013-10-08 22:17:54 -040029 metadata_update = {}
Sapand1a582d2014-12-03 00:01:23 -050030 if (sliver.numberCores):
Tony Mack6fd3ef82013-10-08 22:17:54 -040031 metadata_update["cpu_cores"] = str(sliver.numberCores)
32
33 for tag in sliver.slice.tags.all():
34 if tag.name.startswith("sysctl-"):
35 metadata_update[tag.name] = tag.value
36
Sapan Bhatia475c5972014-11-05 10:32:41 -050037 # public keys
38 slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
39 pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key])
Sapand1a582d2014-12-03 00:01:23 -050040 if sliver.creator.public_key:
41 pubkeys.add(sliver.creator.public_key)
Tony Macked9925a2014-04-14 22:15:02 -040042
Sapan Bhatia475c5972014-11-05 10:32:41 -050043 if sliver.slice.creator.public_key:
Sapand1a582d2014-12-03 00:01:23 -050044 pubkeys.add(sliver.slice.creator.public_key)
Scott Baker9c7a1e12014-05-30 14:42:42 -070045
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050046 nics = []
47 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]
48 controller_networks = ControllerNetworks.objects.filter(network__in=networks,
Sapan Bhatia2471d252014-12-22 13:01:18 -050049 controller=sliver.node.site_deployment.controller)
Scott Bakerdb0a1102014-08-08 09:57:28 -070050
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050051 for controller_network in controller_networks:
52 if controller_network.network.template.visibility == 'private' and \
53 controller_network.network.template.translation == 'none' and controller_network.net_id:
54 nics.append(controller_network.net_id)
Sapan Bhatia475c5972014-11-05 10:32:41 -050055
Sapand1a582d2014-12-03 00:01:23 -050056 # now include network template
Scott Baker81fa17f2015-01-03 12:03:38 -080057 network_templates = [network.template.shared_network_name for network in networks \
58 if network.template.shared_network_name]
Sapan Bhatia475c5972014-11-05 10:32:41 -050059
Tony Mack336e0f92014-11-30 15:53:08 -050060 #driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, controller=sliver.controllerNetwork)
61 driver = self.driver.admin_driver(tenant='admin', controller=sliver.controllerNetwork)
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050062 nets = driver.shell.quantum.list_networks()['networks']
63 for net in nets:
64 if net['name'] in network_templates:
65 nics.append(net['id'])
Sapan Bhatia475c5972014-11-05 10:32:41 -050066
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050067 if (not nics):
68 for net in nets:
69 if net['name']=='public':
70 nics.append(net['id'])
Sapan Bhatia475c5972014-11-05 10:32:41 -050071
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050072 # look up image id
Sapan Bhatia2471d252014-12-22 13:01:18 -050073 controller_driver = self.driver.admin_driver(controller=sliver.controllerNetwork)
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050074 image_id = None
75 images = controller_driver.shell.glanceclient.images.list()
76 for image in images:
77 if image.name == sliver.image.name or not image_id:
78 image_id = image.id
Sapan Bhatia475c5972014-11-05 10:32:41 -050079
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050080 # look up key name at the controller
81 # create/fetch keypair
82 keyname = None
83 keyname = sliver.creator.email.lower().replace('@', 'AT').replace('.', '') +\
84 sliver.slice.name
85 key_fields = {'name': keyname,
86 'public_key': sliver.creator.public_key}
Sapan Bhatia475c5972014-11-05 10:32:41 -050087
Sapan Bhatiaf3d21322014-12-22 11:50:47 -050088 try:
89 legacy = Config().observer_legacy
90 except:
91 legacy = False
92
93 if (legacy):
94 host_filter = sliver.node.name.split('.',1)[0]
95 else:
96 host_filter = sliver.node.name
97
98 availability_zone_filter = 'nova:%s'%host_filter
99 sliver_name = '%s-%d'%(sliver.slice.name,sliver.id)
100
101 userData = self.get_userdata(sliver)
102 if sliver.userData:
103 userData = sliver.userData
104
Sapan Bhatia2471d252014-12-22 13:01:18 -0500105 controller = sliver.controllerNetwork
106 tenant_fields = {'endpoint':controller.auth_url,
Sapan Bhatiaf3d21322014-12-22 11:50:47 -0500107 'admin_user': sliver.creator.username,
108 'admin_password': sliver.creator.password,
109 'admin_tenant': sliver.slice.name,
110 'tenant': sliver.slice.name,
111 'tenant_description': sliver.slice.description,
112 'name':sliver_name,
113 'ansible_tag':sliver_name,
114 'availability_zone': availability_zone_filter,
115 'image_id':image_id,
116 'key_name':keyname,
117 'flavor_id':3,
118 'nics':nics,
119 'meta':metadata_update,
120 'key':key_fields,
121 'user_data':r'%s'%escape(userData)}
122
123 res = run_template('sync_slivers.yaml', tenant_fields,path='slivers')
124 if (len(res)!=2):
125 raise Exception('Could not sync sliver %s'%sliver.slice.name)
126 else:
127 sliver_id = res[1]['id'] # 0 is for the key
Sapan Bhatia475c5972014-11-05 10:32:41 -0500128
129 sliver.instance_id = sliver_id
130 sliver.instance_name = sliver_name
Sapand1a582d2014-12-03 00:01:23 -0500131 sliver.save()
Sapan Bhatia24836f12013-08-27 10:16:05 -0400132
Sapan Bhatia96500042014-07-23 09:37:19 -0400133 def delete_record(self, sliver):
Sapan9f0ae192014-12-03 01:27:07 -0500134 sliver_name = '@'.join([sliver.slice.name,sliver.node.name])
135 tenant_fields = {'name':sliver_name,
136 'ansible_tag':sliver_name
137 }
138 res = run_template('delete_slivers.yaml', tenant_fields, path='slivers')