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 |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [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 |
| 10 | |
| 11 | logger = Logger(level=logging.INFO) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 12 | |
| 13 | class SyncSlivers(OpenStackSyncStep): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 14 | provides=[Sliver] |
| 15 | requested_interval=0 |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 16 | |
Scott Baker | db0a110 | 2014-08-08 09:57:28 -0700 | [diff] [blame] | 17 | def get_userdata(self, sliver): |
| 18 | userdata = 'opencloud:\n slicename: "%s"\n hostname: "%s"\n' % (sliver.slice.name, sliver.node.name) |
| 19 | return userdata |
| 20 | |
Tony Mack | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 21 | def sync_record(self, sliver): |
Tony Mack | 8047cea | 2014-04-09 01:44:02 -0400 | [diff] [blame] | 22 | logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment)) |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 23 | metadata_update = {} |
| 24 | if ("numberCores" in sliver.changed_fields): |
| 25 | metadata_update["cpu_cores"] = str(sliver.numberCores) |
| 26 | |
| 27 | for tag in sliver.slice.tags.all(): |
| 28 | if tag.name.startswith("sysctl-"): |
| 29 | metadata_update[tag.name] = tag.value |
| 30 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 31 | if not sliver.instance_id: |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 32 | driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.deploymentNetwork.name) |
| 33 | # public keys |
Tony Mack | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 34 | slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice) |
Tony Mack | 8047cea | 2014-04-09 01:44:02 -0400 | [diff] [blame] | 35 | pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key] |
Tony Mack | 976d774 | 2014-03-18 22:00:52 -0400 | [diff] [blame] | 36 | if sliver.creator.public_key: |
| 37 | pubkeys.append(sliver.creator.public_key) |
Tony Mack | 0c6459a | 2014-04-15 12:18:12 -0400 | [diff] [blame] | 38 | if sliver.slice.creator.public_key: |
| 39 | pubkeys.append(sliver.slice.creator.public_key) |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 40 | # netowrks |
Tony Mack | 02a4b4e | 2014-04-15 20:04:13 -0400 | [diff] [blame] | 41 | # include all networks available to the slice and/or associated network templates |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 42 | nics = [] |
Tony Mack | 02a4b4e | 2014-04-15 20:04:13 -0400 | [diff] [blame] | 43 | networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)] |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 44 | network_deployments = NetworkDeployments.objects.filter(network__in=networks, |
| 45 | deployment=sliver.node.deployment) |
Tony Mack | 02a4b4e | 2014-04-15 20:04:13 -0400 | [diff] [blame] | 46 | # Gather private networks first. This includes networks with a template that has |
| 47 | # visibility = private and translation = none |
| 48 | for network_deployment in network_deployments: |
| 49 | if network_deployment.network.template.visibility == 'private' and \ |
| 50 | network_deployment.network.template.translation == 'none': |
| 51 | nics.append({'net-id': network_deployment.net_id}) |
| 52 | |
| 53 | # now include network template |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 54 | network_templates = [network.template.sharedNetworkName for network in networks \ |
| 55 | if network.template.sharedNetworkName] |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [diff] [blame] | 56 | #logger.info("%s %s %s %s" % (driver.shell.quantum.username, driver.shell.quantum.password, driver.shell.quantum.tenant, driver.shell.quantum.url)) |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 57 | for net in driver.shell.quantum.list_networks()['networks']: |
| 58 | if net['name'] in network_templates: |
| 59 | nics.append({'net-id': net['id']}) |
| 60 | |
Tony Mack | 976d774 | 2014-03-18 22:00:52 -0400 | [diff] [blame] | 61 | # look up image id |
| 62 | deployment_driver = self.driver.admin_driver(deployment=sliver.deploymentNetwork.name) |
| 63 | image_id = None |
| 64 | images = deployment_driver.shell.glance.get_images() |
| 65 | for image in images: |
| 66 | if image['name'] == sliver.image.name: |
| 67 | image_id = image['id'] |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 68 | |
| 69 | # look up key name at the deployment |
Tony Mack | a7adc96 | 2014-04-17 21:57:48 -0400 | [diff] [blame] | 70 | # create/fetch keypair |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 71 | keyname = None |
Tony Mack | a7adc96 | 2014-04-17 21:57:48 -0400 | [diff] [blame] | 72 | if sliver.creator.public_key: |
| 73 | keyname = sliver.creator.email.lower().replace('@', 'AT').replace('.', '') +\ |
| 74 | sliver.slice.name |
| 75 | key_fields = {'name': keyname, |
| 76 | 'public_key': sliver.creator.public_key} |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 77 | driver.create_keypair(**key_fields) |
| 78 | |
Scott Baker | db0a110 | 2014-08-08 09:57:28 -0700 | [diff] [blame] | 79 | userData = self.get_userdata(sliver) |
| 80 | if sliver.userData: |
| 81 | userData = sliver.userData |
| 82 | |
Tony Mack | a6cbd36 | 2013-10-21 10:31:59 -0400 | [diff] [blame] | 83 | instance = driver.spawn_instance(name=sliver.name, |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 84 | key_name = keyname, |
Tony Mack | 976d774 | 2014-03-18 22:00:52 -0400 | [diff] [blame] | 85 | image_id = image_id, |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 86 | hostname = sliver.node.name, |
| 87 | pubkeys = pubkeys, |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 88 | nics = nics, |
Scott Baker | c132d09 | 2014-09-04 16:55:08 -0700 | [diff] [blame] | 89 | userdata = userData, |
| 90 | flavor_name = sliver.flavor.flavor ) |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 91 | sliver.instance_id = instance.id |
| 92 | sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name') |
Tony Mack | 0c6459a | 2014-04-15 12:18:12 -0400 | [diff] [blame] | 93 | sliver.save() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 94 | |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 95 | if sliver.instance_id and metadata_update: |
Tony Mack | 976d774 | 2014-03-18 22:00:52 -0400 | [diff] [blame] | 96 | driver.update_instance_metadata(sliver.instance_id, metadata_update) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 97 | |
Sapan Bhatia | 9650004 | 2014-07-23 09:37:19 -0400 | [diff] [blame] | 98 | def delete_record(self, sliver): |
| 99 | if sliver.instance_id: |
| 100 | driver = self.driver.client_driver(caller=sliver.creator, |
| 101 | tenant=sliver.slice.name, |
| 102 | deployment=sliver.deploymentNetwork.name) |
| 103 | driver.destroy_instance(sliver.instance_id) |