Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Sapan Bhatia | 3346abe | 2015-01-23 16:26:46 +0000 | [diff] [blame] | 3 | import socket |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 4 | from django.db.models import F, Q |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 5 | from xos.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 6 | from observer.openstacksyncstep import OpenStackSyncStep |
| 7 | from core.models.sliver import Sliver |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 8 | from core.models.slice import Slice, SlicePrivilege, ControllerSlice |
| 9 | from core.models.network import Network, NetworkSlice, ControllerNetwork |
Tony Mack | fe9d9bd | 2014-04-07 19:46:15 -0400 | [diff] [blame] | 10 | from util.logger import Logger, logging |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 11 | from observer.ansible import * |
Tony Mack | fe9d9bd | 2014-04-07 19:46:15 -0400 | [diff] [blame] | 12 | |
| 13 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 14 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 15 | def escape(s): |
| 16 | s = s.replace('\n',r'\n').replace('"',r'\"') |
| 17 | return s |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 18 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 19 | class SyncSlivers(OpenStackSyncStep): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 20 | provides=[Sliver] |
| 21 | requested_interval=0 |
Sapan Bhatia | 39a775f | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 22 | observes=Sliver |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 23 | |
Scott Baker | db0a110 | 2014-08-08 09:57:28 -0700 | [diff] [blame] | 24 | 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 Mack | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 28 | def sync_record(self, sliver): |
Sapan Bhatia | 6bc4328 | 2014-12-22 13:01:18 -0500 | [diff] [blame] | 29 | logger.info("sync'ing sliver:%s slice:%s controller:%s " % (sliver, sliver.slice.name, sliver.node.site_deployment.controller)) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 30 | |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 31 | metadata_update = {} |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 32 | if (sliver.numberCores): |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 33 | 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 39 | # 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]) |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 42 | if sliver.creator.public_key: |
| 43 | pubkeys.add(sliver.creator.public_key) |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 44 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 45 | if sliver.slice.creator.public_key: |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 46 | pubkeys.add(sliver.slice.creator.public_key) |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 47 | |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 48 | nics = [] |
| 49 | networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)] |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 50 | controller_networks = ControllerNetwork.objects.filter(network__in=networks, |
Sapan Bhatia | 6bc4328 | 2014-12-22 13:01:18 -0500 | [diff] [blame] | 51 | controller=sliver.node.site_deployment.controller) |
Scott Baker | db0a110 | 2014-08-08 09:57:28 -0700 | [diff] [blame] | 52 | |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 53 | 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 57 | |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 58 | # now include network template |
Scott Baker | 369f9b9 | 2015-01-03 12:03:38 -0800 | [diff] [blame] | 59 | network_templates = [network.template.shared_network_name for network in networks \ |
| 60 | if network.template.shared_network_name] |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 61 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 62 | #driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, controller=sliver.controllerNetwork) |
Tony Mack | 80c4254 | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 63 | driver = self.driver.admin_driver(tenant='admin', controller=sliver.node.site_deployment.controller) |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 64 | 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 68 | |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 69 | if (not nics): |
| 70 | for net in nets: |
| 71 | if net['name']=='public': |
| 72 | nics.append(net['id']) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 73 | |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 74 | # look up image id |
Tony Mack | 80c4254 | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 75 | controller_driver = self.driver.admin_driver(controller=sliver.node.site_deployment.controller) |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 76 | 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 81 | |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 82 | # 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 89 | |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 90 | 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 Mack | 7da8d8f | 2015-01-15 13:32:54 -0500 | [diff] [blame] | 98 | host_filter = sliver.node.name.strip() |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 99 | |
| 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 Mack | 80c4254 | 2015-01-07 12:48:37 -0500 | [diff] [blame] | 107 | controller = sliver.node.site_deployment.controller |
Sapan Bhatia | 6bc4328 | 2014-12-22 13:01:18 -0500 | [diff] [blame] | 108 | tenant_fields = {'endpoint':controller.auth_url, |
Tony Mack | d1275ea | 2015-01-10 22:31:26 -0500 | [diff] [blame] | 109 | 'admin_user': sliver.creator.email, |
Sapan Bhatia | d7750cf | 2015-01-16 22:16:10 +0000 | [diff] [blame] | 110 | 'admin_password': sliver.creator.remote_password, |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 111 | '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 Mack | 3f126ae | 2015-01-19 15:19:24 -0500 | [diff] [blame] | 119 | 'flavor_id':sliver.flavor.id, |
Sapan Bhatia | eeaf5fd | 2014-12-22 11:50:47 -0500 | [diff] [blame] | 120 | 'nics':nics, |
| 121 | 'meta':metadata_update, |
| 122 | 'key':key_fields, |
| 123 | 'user_data':r'%s'%escape(userData)} |
| 124 | |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 125 | 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 Bhatia | 68b9c1c | 2015-01-20 20:59:24 +0000 | [diff] [blame] | 128 | |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 129 | 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 135 | |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 136 | sliver.instance_id = sliver_id |
| 137 | sliver.instance_uuid = sliver_uuid |
| 138 | sliver.instance_name = sliver_name |
| 139 | sliver.save() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 140 | |
Sapan Bhatia | 9650004 | 2014-07-23 09:37:19 -0400 | [diff] [blame] | 141 | def delete_record(self, sliver): |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 142 | 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) |