blob: 900840df7259e5cae8377660c5b83be0d69822a7 [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
Sapan Bhatia24836f12013-08-27 10:16:05 -04007
8class SyncSlivers(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -04009 provides=[Sliver]
10 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040011
12 def fetch_pending(self):
13 return Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
14
Tony Mackdacfb982013-09-24 21:57:16 -040015 def sync_record(self, slice):
16 if not sliver.instance_id:
Tony Mack66646d52013-09-24 21:47:12 -040017 nics = self.get_requested_networks(sliver.slice)
18 file("/tmp/scott-manager","a").write("slice: %s\nreq: %s\n" % (str(sliver.slice.name), str(nics)))
19 slice_memberships = SliceMembership.objects.filter(slice=sliver.slice)
20 pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key]
21 pubkeys.append(sliver.creator.public_key)
22 instance = self.driver.spawn_instance(name=sliver.name,
23 key_name = sliver.creator.keyname,
24 image_id = sliver.image.image_id,
25 hostname = sliver.node.name,
26 pubkeys = pubkeys,
27 nics = nics )
28 sliver.instance_id = instance.id
29 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
Sapan Bhatia24836f12013-08-27 10:16:05 -040030
Tony Mackdacfb982013-09-24 21:57:16 -040031 if sliver.instance_id and ("numberCores" in sliver.changed_fields):
32 self.driver.update_instance_metadata(sliver.instance_id, {"cpu_cores": str(sliver.numberCores)})
Sapan Bhatia24836f12013-08-27 10:16:05 -040033
Tony Mackdacfb982013-09-24 21:57:16 -040034 sliver.save()