Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 3 | from django.db.models import F, Q |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 5 | from observer.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.sliver import Sliver |
Sapan Bhatia | 85e0908 | 2014-11-19 15:09:38 -0500 | [diff] [blame] | 7 | from core.models.slice import Slice, SlicePrivilege, SliceDeployments |
Tony Mack | 02a4b4e | 2014-04-15 20:04:13 -0400 | [diff] [blame] | 8 | from core.models.network import Network, NetworkSlice, NetworkDeployments |
Tony Mack | fe9d9bd | 2014-04-07 19:46:15 -0400 | [diff] [blame] | 9 | from util.logger import Logger, logging |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 10 | from observer.ansible import * |
Tony Mack | fe9d9bd | 2014-04-07 19:46:15 -0400 | [diff] [blame] | 11 | |
| 12 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 13 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 14 | def escape(s): |
| 15 | s = s.replace('\n',r'\n').replace('"',r'\"') |
| 16 | return s |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 17 | |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 18 | class SyncSlivers(OpenStackSyncStep): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 19 | provides=[Sliver] |
| 20 | requested_interval=0 |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 21 | |
Scott Baker | db0a110 | 2014-08-08 09:57:28 -0700 | [diff] [blame] | 22 | 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 Mack | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 26 | def sync_record(self, sliver): |
Scott Baker | 78c6b98 | 2014-11-05 09:05:14 -0800 | [diff] [blame] | 27 | logger.info("sync'ing sliver:%s slice:%s deployment:%s " % (sliver, sliver.slice.name, sliver.node.deployment)) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 28 | |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 29 | metadata_update = {} |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 30 | if (sliver.numberCores): |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 31 | 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 37 | # 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]) |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 40 | if sliver.creator.public_key: |
| 41 | pubkeys.add(sliver.creator.public_key) |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 42 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 43 | if sliver.slice.creator.public_key: |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 44 | pubkeys.add(sliver.slice.creator.public_key) |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 45 | |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 46 | nics = [] |
| 47 | networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)] |
| 48 | network_deployments = NetworkDeployments.objects.filter(network__in=networks, |
| 49 | deployment=sliver.node.deployment) |
Scott Baker | db0a110 | 2014-08-08 09:57:28 -0700 | [diff] [blame] | 50 | |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 51 | for network_deployment in network_deployments: |
| 52 | if network_deployment.network.template.visibility == 'private' and \ |
| 53 | network_deployment.network.template.translation == 'none' and network_deployment.net_id: |
| 54 | nics.append(network_deployment.net_id) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 55 | |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 56 | # now include network template |
| 57 | network_templates = [network.template.sharedNetworkName for network in networks \ |
| 58 | if network.template.sharedNetworkName] |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 59 | |
| 60 | #driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.deploymentNetwork) |
| 61 | driver = self.driver.admin_driver(tenant='admin', deployment=sliver.deploymentNetwork) |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 62 | 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 66 | |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 67 | if (not nics): |
| 68 | for net in nets: |
| 69 | if net['name']=='public': |
| 70 | nics.append(net['id']) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 71 | |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 72 | # look up image id |
| 73 | deployment_driver = self.driver.admin_driver(deployment=sliver.deploymentNetwork.name) |
| 74 | image_id = None |
| 75 | images = deployment_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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 79 | |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 80 | # look up key name at the deployment |
| 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 Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 87 | |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 88 | |
| 89 | userData = self.get_userdata(sliver) |
| 90 | if sliver.userData: |
| 91 | userData = sliver.userData |
| 92 | |
| 93 | try: |
| 94 | legacy = Config().observer_legacy |
| 95 | except: |
| 96 | legacy = False |
| 97 | |
| 98 | if (legacy): |
| 99 | host_filter = sliver.node.name.split('.',1)[0] |
| 100 | else: |
| 101 | host_filter = sliver.node.name |
| 102 | |
| 103 | availability_zone_filter = 'nova:%s'%host_filter |
| 104 | sliver_name = '@'.join([sliver.slice.name,sliver.node.name]) |
| 105 | tenant_fields = {'endpoint':sliver.node.deployment.auth_url, |
| 106 | 'admin_user': sliver.node.deployment.admin_user, |
| 107 | 'admin_password': sliver.node.deployment.admin_password, |
| 108 | 'admin_tenant': 'admin', |
| 109 | 'tenant': sliver.slice.name, |
| 110 | 'availability_zone': availability_zone_filter, |
| 111 | 'tenant_description': sliver.slice.description, |
| 112 | 'name':sliver_name, |
| 113 | 'ansible_tag':sliver_name, |
| 114 | 'image_id':image_id, |
| 115 | 'key_name':keyname, |
| 116 | 'flavor_id':3, |
| 117 | 'nics':nics, |
| 118 | 'meta':metadata_update, |
| 119 | 'key':key_fields, |
| 120 | 'user_data':r'%s'%escape(userData)} |
| 121 | |
| 122 | res = run_template('sync_slivers.yaml', tenant_fields, path='slivers') |
| 123 | if (len(res)!=2): |
| 124 | raise Exception('Could not sync sliver %s'%sliver.slice.name) |
| 125 | else: |
| 126 | sliver_id = res[1]['id'] # 0 is for the key |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 127 | |
| 128 | sliver.instance_id = sliver_id |
| 129 | sliver.instance_name = sliver_name |
Sapan | 6a97562 | 2014-12-03 00:01:23 -0500 | [diff] [blame] | 130 | sliver.save() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 131 | |
Sapan Bhatia | 9650004 | 2014-07-23 09:37:19 -0400 | [diff] [blame] | 132 | def delete_record(self, sliver): |
Sapan | 01d8917 | 2014-12-03 01:27:07 -0500 | [diff] [blame] | 133 | sliver_name = '@'.join([sliver.slice.name,sliver.node.name]) |
| 134 | tenant_fields = {'name':sliver_name, |
| 135 | 'ansible_tag':sliver_name |
| 136 | } |
| 137 | res = run_template('delete_slivers.yaml', tenant_fields, path='slivers') |
| 138 | |