blob: 5e3acfd7a05f0a62435c88ac82b95a333e6dfaf8 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04004from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
6from core.models.sliver import Sliver
Tony Mack27ec0f52013-10-16 13:13:32 -04007from core.models.slice import SlicePrivilege
Sapan Bhatia24836f12013-08-27 10:16:05 -04008
9class SyncSlivers(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -040010 provides=[Sliver]
11 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040012
13 def fetch_pending(self):
14 return Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
15
Tony Mack6fd3ef82013-10-08 22:17:54 -040016 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 Mack27ec0f52013-10-16 13:13:32 -040028 def sync_record(self, sliver):
Tony Mack6fd3ef82013-10-08 22:17:54 -040029 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 Mackdacfb982013-09-24 21:57:16 -040037 if not sliver.instance_id:
Tony Mack66646d52013-09-24 21:47:12 -040038 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 Mack27ec0f52013-10-16 13:13:32 -040040 slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
Tony Mack66646d52013-09-24 21:47:12 -040041 pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key]
42 pubkeys.append(sliver.creator.public_key)
43 instance = self.driver.spawn_instance(name=sliver.name,
44 key_name = sliver.creator.keyname,
45 image_id = sliver.image.image_id,
46 hostname = sliver.node.name,
47 pubkeys = pubkeys,
48 nics = nics )
49 sliver.instance_id = instance.id
50 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
Sapan Bhatia24836f12013-08-27 10:16:05 -040051
Tony Mack6fd3ef82013-10-08 22:17:54 -040052 if sliver.instance_id and metadata_update:
53 self.driver.update_instance_metadata(sliver.instance_id, metadata_update)
Sapan Bhatia24836f12013-08-27 10:16:05 -040054
Tony Mackdacfb982013-09-24 21:57:16 -040055 sliver.save()