blob: 01e724ee128026b9828b85bc62cdd5ed9afb7e60 [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 Mackd1a17e12014-03-19 15:18:15 -04007from core.models.slice import SlicePrivilege, SliceDeployments
Tony Mackfe9d9bd2014-04-07 19:46:15 -04008from util.logger import Logger, logging
9
10logger = Logger(level=logging.INFO)
11m util.logger import Logger, logging
12
13logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040014
15class SyncSlivers(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -040016 provides=[Sliver]
17 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040018
19 def fetch_pending(self):
20 return Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
21
Tony Mack6fd3ef82013-10-08 22:17:54 -040022 def get_requested_networks(self, slice):
23 network_ids = [x.network_id for x in slice.networks.all()]
24
25 if slice.network_id is not None:
26 network_ids.append(slice.network_id)
27
28 networks = []
29 for network_id in network_ids:
30 networks.append({"net-id": network_id})
31
32 return networks
33
Tony Mack27ec0f52013-10-16 13:13:32 -040034 def sync_record(self, sliver):
Tony Mackfe9d9bd2014-04-07 19:46:15 -040035 logger.info("sync'ing sliver %s" % sliver)
Tony Mack6fd3ef82013-10-08 22:17:54 -040036 metadata_update = {}
37 if ("numberCores" in sliver.changed_fields):
38 metadata_update["cpu_cores"] = str(sliver.numberCores)
39
40 for tag in sliver.slice.tags.all():
41 if tag.name.startswith("sysctl-"):
42 metadata_update[tag.name] = tag.value
43
Tony Mackdacfb982013-09-24 21:57:16 -040044 if not sliver.instance_id:
Tony Mack66646d52013-09-24 21:47:12 -040045 nics = self.get_requested_networks(sliver.slice)
46 file("/tmp/scott-manager","a").write("slice: %s\nreq: %s\n" % (str(sliver.slice.name), str(nics)))
Tony Mack27ec0f52013-10-16 13:13:32 -040047 slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
Tony Mack976d7742014-03-18 22:00:52 -040048 pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key is not None]
49 if sliver.creator.public_key:
50 pubkeys.append(sliver.creator.public_key)
51 driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.deploymentNetwork.name)
52 # look up image id
53 deployment_driver = self.driver.admin_driver(deployment=sliver.deploymentNetwork.name)
54 image_id = None
55 images = deployment_driver.shell.glance.get_images()
56 for image in images:
57 if image['name'] == sliver.image.name:
58 image_id = image['id']
Tony Mackd1a17e12014-03-19 15:18:15 -040059
60 # look up key name at the deployment
61 keyname = None
62 slice_deployments = SliceDeployments.objects.filter(slice = sliver.slice,
63 deployment = sliver.deploymentNetwork)
64 for slice_deployment in slice_deployments:
65 if slice_deployment.keyname:
66 keyname = slice_deployment.keyname
67 break
68
Tony Macka6cbd362013-10-21 10:31:59 -040069 instance = driver.spawn_instance(name=sliver.name,
Tony Mackd1a17e12014-03-19 15:18:15 -040070 key_name = keyname,
Tony Mack976d7742014-03-18 22:00:52 -040071 image_id = image_id,
Tony Mack66646d52013-09-24 21:47:12 -040072 hostname = sliver.node.name,
73 pubkeys = pubkeys,
74 nics = nics )
75 sliver.instance_id = instance.id
76 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
Sapan Bhatia24836f12013-08-27 10:16:05 -040077
Tony Mack6fd3ef82013-10-08 22:17:54 -040078 if sliver.instance_id and metadata_update:
Tony Mack976d7742014-03-18 22:00:52 -040079 driver.update_instance_metadata(sliver.instance_id, metadata_update)
Sapan Bhatia24836f12013-08-27 10:16:05 -040080
Tony Mackdacfb982013-09-24 21:57:16 -040081 sliver.save()