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 | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 7 | from core.models.slice import SlicePrivilege |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 8 | |
| 9 | class SyncSlivers(OpenStackSyncStep): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 10 | provides=[Sliver] |
| 11 | requested_interval=0 |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 12 | |
| 13 | def fetch_pending(self): |
| 14 | return Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 15 | |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 16 | def get_requested_networks(self, slice): |
| 17 | network_ids = [x.network_id for x in slice.networks.all()] |
| 18 | |
| 19 | if slice.network_id is not None: |
| 20 | network_ids.append(slice.network_id) |
| 21 | |
| 22 | networks = [] |
| 23 | for network_id in network_ids: |
| 24 | networks.append({"net-id": network_id}) |
| 25 | |
| 26 | return networks |
| 27 | |
Tony Mack | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 28 | def sync_record(self, sliver): |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 29 | metadata_update = {} |
| 30 | if ("numberCores" in sliver.changed_fields): |
| 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 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 37 | if not sliver.instance_id: |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 38 | nics = self.get_requested_networks(sliver.slice) |
| 39 | file("/tmp/scott-manager","a").write("slice: %s\nreq: %s\n" % (str(sliver.slice.name), str(nics))) |
Tony Mack | 27ec0f5 | 2013-10-16 13:13:32 -0400 | [diff] [blame] | 40 | slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice) |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 41 | pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key] |
| 42 | pubkeys.append(sliver.creator.public_key) |
Tony Mack | a6cbd36 | 2013-10-21 10:31:59 -0400 | [diff] [blame] | 43 | driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name) |
| 44 | instance = driver.spawn_instance(name=sliver.name, |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 45 | key_name = sliver.creator.keyname, |
| 46 | image_id = sliver.image.image_id, |
| 47 | hostname = sliver.node.name, |
| 48 | pubkeys = pubkeys, |
| 49 | nics = nics ) |
| 50 | sliver.instance_id = instance.id |
| 51 | sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name') |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 52 | |
Tony Mack | 6fd3ef8 | 2013-10-08 22:17:54 -0400 | [diff] [blame] | 53 | if sliver.instance_id and metadata_update: |
| 54 | self.driver.update_instance_metadata(sliver.instance_id, metadata_update) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 55 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 56 | sliver.save() |