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 |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 7 | from core.models.slice import SlicePrivilege, SliceDeployments |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 8 | from core.models.network import Network, 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 | |
| 17 | def fetch_pending(self): |
| 18 | return Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 19 | |
Tony Mack | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 20 | def sync_record(self, sliver): |
Tony Mack | 8047cea | 2014-04-09 01:44:02 -0400 | [diff] [blame] | 21 | logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment)) |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 22 | metadata_update = {} |
| 23 | if ("numberCores" in sliver.changed_fields): |
| 24 | metadata_update["cpu_cores"] = str(sliver.numberCores) |
| 25 | |
| 26 | for tag in sliver.slice.tags.all(): |
| 27 | if tag.name.startswith("sysctl-"): |
| 28 | metadata_update[tag.name] = tag.value |
| 29 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 30 | if not sliver.instance_id: |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 31 | driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.deploymentNetwork.name) |
| 32 | # public keys |
Tony Mack | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 33 | slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice) |
Tony Mack | 8047cea | 2014-04-09 01:44:02 -0400 | [diff] [blame] | 34 | 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] | 35 | if sliver.creator.public_key: |
| 36 | pubkeys.append(sliver.creator.public_key) |
Tony Mack | 0c6459a | 2014-04-15 12:18:12 -0400 | [diff] [blame^] | 37 | if sliver.slice.creator.public_key: |
| 38 | pubkeys.append(sliver.slice.creator.public_key) |
Tony Mack | ed9925a | 2014-04-14 22:15:02 -0400 | [diff] [blame] | 39 | # netowrks |
| 40 | #nics = self.get_requested_networks(sliver.slice, sliver.node.deployment) |
| 41 | nics = [] |
| 42 | networks = Network.objects.filter(owner=sliver.slice) |
| 43 | network_deployments = NetworkDeployments.objects.filter(network__in=networks, |
| 44 | deployment=sliver.node.deployment) |
| 45 | network_templates = [network.template.sharedNetworkName for network in networks \ |
| 46 | if network.template.sharedNetworkName] |
| 47 | nics = [{'net-id': nd.net_id} for nd in network_deployments] |
| 48 | # include network template |
| 49 | for net in driver.shell.quantum.list_networks()['networks']: |
| 50 | if net['name'] in network_templates: |
| 51 | nics.append({'net-id': net['id']}) |
| 52 | |
| 53 | file("/tmp/scott-manager","a").write("slice: %s\nreq: %s\n" % (str(sliver.slice.name), str(nics))) |
| 54 | |
Tony Mack | 976d774 | 2014-03-18 22:00:52 -0400 | [diff] [blame] | 55 | # look up image id |
| 56 | deployment_driver = self.driver.admin_driver(deployment=sliver.deploymentNetwork.name) |
| 57 | image_id = None |
| 58 | images = deployment_driver.shell.glance.get_images() |
| 59 | for image in images: |
| 60 | if image['name'] == sliver.image.name: |
| 61 | image_id = image['id'] |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 62 | |
| 63 | # look up key name at the deployment |
| 64 | keyname = None |
| 65 | slice_deployments = SliceDeployments.objects.filter(slice = sliver.slice, |
| 66 | deployment = sliver.deploymentNetwork) |
| 67 | for slice_deployment in slice_deployments: |
| 68 | if slice_deployment.keyname: |
| 69 | keyname = slice_deployment.keyname |
| 70 | break |
| 71 | |
Tony Mack | a6cbd36 | 2013-10-21 10:31:59 -0400 | [diff] [blame] | 72 | instance = driver.spawn_instance(name=sliver.name, |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 73 | key_name = keyname, |
Tony Mack | 976d774 | 2014-03-18 22:00:52 -0400 | [diff] [blame] | 74 | image_id = image_id, |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 75 | hostname = sliver.node.name, |
| 76 | pubkeys = pubkeys, |
| 77 | nics = nics ) |
| 78 | sliver.instance_id = instance.id |
| 79 | sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name') |
Tony Mack | 0c6459a | 2014-04-15 12:18:12 -0400 | [diff] [blame^] | 80 | sliver.save() |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 81 | |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 82 | if sliver.instance_id and metadata_update: |
Tony Mack | 976d774 | 2014-03-18 22:00:52 -0400 | [diff] [blame] | 83 | driver.update_instance_metadata(sliver.instance_id, metadata_update) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 84 | |