blob: 299d079a6af274dd1a0499c853ac6ad3bd989234 [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 Macked9925a2014-04-14 22:15:02 -04008from core.models.network import Network, NetworkDeployments
Tony Mackfe9d9bd2014-04-07 19:46:15 -04009from util.logger import Logger, logging
10
11logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040012
13class SyncSlivers(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -040014 provides=[Sliver]
15 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040016
17 def fetch_pending(self):
18 return Sliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
19
Tony Mack27ec0f52013-10-16 13:13:32 -040020 def sync_record(self, sliver):
Tony Mack8047cea2014-04-09 01:44:02 -040021 logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment))
Tony Mack6fd3ef82013-10-08 22:17:54 -040022 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 Mackdacfb982013-09-24 21:57:16 -040030 if not sliver.instance_id:
Tony Macked9925a2014-04-14 22:15:02 -040031 driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.deploymentNetwork.name)
32 # public keys
Tony Mack27ec0f52013-10-16 13:13:32 -040033 slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
Tony Mack8047cea2014-04-09 01:44:02 -040034 pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key]
Tony Mack976d7742014-03-18 22:00:52 -040035 if sliver.creator.public_key:
36 pubkeys.append(sliver.creator.public_key)
Tony Macked9925a2014-04-14 22:15:02 -040037
38 # netowrks
39 #nics = self.get_requested_networks(sliver.slice, sliver.node.deployment)
40 nics = []
41 networks = Network.objects.filter(owner=sliver.slice)
42 network_deployments = NetworkDeployments.objects.filter(network__in=networks,
43 deployment=sliver.node.deployment)
44 network_templates = [network.template.sharedNetworkName for network in networks \
45 if network.template.sharedNetworkName]
46 nics = [{'net-id': nd.net_id} for nd in network_deployments]
47 # include network template
48 for net in driver.shell.quantum.list_networks()['networks']:
49 if net['name'] in network_templates:
50 nics.append({'net-id': net['id']})
51
52 file("/tmp/scott-manager","a").write("slice: %s\nreq: %s\n" % (str(sliver.slice.name), str(nics)))
53
Tony Mack976d7742014-03-18 22:00:52 -040054 # look up image id
55 deployment_driver = self.driver.admin_driver(deployment=sliver.deploymentNetwork.name)
56 image_id = None
57 images = deployment_driver.shell.glance.get_images()
58 for image in images:
59 if image['name'] == sliver.image.name:
60 image_id = image['id']
Tony Mackd1a17e12014-03-19 15:18:15 -040061
62 # look up key name at the deployment
63 keyname = None
64 slice_deployments = SliceDeployments.objects.filter(slice = sliver.slice,
65 deployment = sliver.deploymentNetwork)
66 for slice_deployment in slice_deployments:
67 if slice_deployment.keyname:
68 keyname = slice_deployment.keyname
69 break
70
Tony Macka6cbd362013-10-21 10:31:59 -040071 instance = driver.spawn_instance(name=sliver.name,
Tony Mackd1a17e12014-03-19 15:18:15 -040072 key_name = keyname,
Tony Mack976d7742014-03-18 22:00:52 -040073 image_id = image_id,
Tony Mack66646d52013-09-24 21:47:12 -040074 hostname = sliver.node.name,
75 pubkeys = pubkeys,
76 nics = nics )
77 sliver.instance_id = instance.id
78 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
Sapan Bhatia24836f12013-08-27 10:16:05 -040079
Tony Mack6fd3ef82013-10-08 22:17:54 -040080 if sliver.instance_id and metadata_update:
Tony Mack976d7742014-03-18 22:00:52 -040081 driver.update_instance_metadata(sliver.instance_id, metadata_update)
Sapan Bhatia24836f12013-08-27 10:16:05 -040082
Tony Mackdacfb982013-09-24 21:57:16 -040083 sliver.save()