blob: 249b938933c2abbbba2e5a098cd36a2b3c23ba37 [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
Scott Baker3949bc62014-05-12 10:42:16 -07007from core.models.slice import Slice, SlicePrivilege, SliceDeployments
Tony Mack02a4b4e2014-04-15 20:04:13 -04008from core.models.network import Network, NetworkSlice, 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
Tony Mack27ec0f52013-10-16 13:13:32 -040017 def sync_record(self, sliver):
Tony Mack8047cea2014-04-09 01:44:02 -040018 logger.info("sync'ing sliver:%s deployment:%s " % (sliver, sliver.node.deployment))
Tony Mack6fd3ef82013-10-08 22:17:54 -040019 metadata_update = {}
20 if ("numberCores" in sliver.changed_fields):
21 metadata_update["cpu_cores"] = str(sliver.numberCores)
22
23 for tag in sliver.slice.tags.all():
24 if tag.name.startswith("sysctl-"):
25 metadata_update[tag.name] = tag.value
26
Tony Mackdacfb982013-09-24 21:57:16 -040027 if not sliver.instance_id:
Tony Macked9925a2014-04-14 22:15:02 -040028 driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.deploymentNetwork.name)
29 # public keys
Tony Mack27ec0f52013-10-16 13:13:32 -040030 slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
Tony Mack8047cea2014-04-09 01:44:02 -040031 pubkeys = [sm.user.public_key for sm in slice_memberships if sm.user.public_key]
Tony Mack976d7742014-03-18 22:00:52 -040032 if sliver.creator.public_key:
33 pubkeys.append(sliver.creator.public_key)
Tony Mack0c6459a2014-04-15 12:18:12 -040034 if sliver.slice.creator.public_key:
35 pubkeys.append(sliver.slice.creator.public_key)
Tony Macked9925a2014-04-14 22:15:02 -040036 # netowrks
Tony Mack02a4b4e2014-04-15 20:04:13 -040037 # include all networks available to the slice and/or associated network templates
Tony Macked9925a2014-04-14 22:15:02 -040038 nics = []
Tony Mack02a4b4e2014-04-15 20:04:13 -040039 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]
Tony Macked9925a2014-04-14 22:15:02 -040040 network_deployments = NetworkDeployments.objects.filter(network__in=networks,
41 deployment=sliver.node.deployment)
Tony Mack02a4b4e2014-04-15 20:04:13 -040042 # Gather private networks first. This includes networks with a template that has
43 # visibility = private and translation = none
44 for network_deployment in network_deployments:
45 if network_deployment.network.template.visibility == 'private' and \
46 network_deployment.network.template.translation == 'none':
47 nics.append({'net-id': network_deployment.net_id})
48
49 # now include network template
Tony Macked9925a2014-04-14 22:15:02 -040050 network_templates = [network.template.sharedNetworkName for network in networks \
51 if network.template.sharedNetworkName]
Scott Baker3949bc62014-05-12 10:42:16 -070052 #logger.info("%s %s %s %s" % (driver.shell.quantum.username, driver.shell.quantum.password, driver.shell.quantum.tenant, driver.shell.quantum.url))
Tony Macked9925a2014-04-14 22:15:02 -040053 for net in driver.shell.quantum.list_networks()['networks']:
54 if net['name'] in network_templates:
55 nics.append({'net-id': net['id']})
56
Tony Mack976d7742014-03-18 22:00:52 -040057 # look up image id
58 deployment_driver = self.driver.admin_driver(deployment=sliver.deploymentNetwork.name)
59 image_id = None
60 images = deployment_driver.shell.glance.get_images()
61 for image in images:
62 if image['name'] == sliver.image.name:
63 image_id = image['id']
Tony Mackd1a17e12014-03-19 15:18:15 -040064
65 # look up key name at the deployment
Tony Macka7adc962014-04-17 21:57:48 -040066 # create/fetch keypair
Tony Mackd1a17e12014-03-19 15:18:15 -040067 keyname = None
Tony Macka7adc962014-04-17 21:57:48 -040068 if sliver.creator.public_key:
69 keyname = sliver.creator.email.lower().replace('@', 'AT').replace('.', '') +\
70 sliver.slice.name
71 key_fields = {'name': keyname,
72 'public_key': sliver.creator.public_key}
Scott Baker9c7a1e12014-05-30 14:42:42 -070073 driver.create_keypair(**key_fields)
74
Tony Macka6cbd362013-10-21 10:31:59 -040075 instance = driver.spawn_instance(name=sliver.name,
Tony Mackd1a17e12014-03-19 15:18:15 -040076 key_name = keyname,
Tony Mack976d7742014-03-18 22:00:52 -040077 image_id = image_id,
Tony Mack66646d52013-09-24 21:47:12 -040078 hostname = sliver.node.name,
79 pubkeys = pubkeys,
Scott Baker9c7a1e12014-05-30 14:42:42 -070080 nics = nics,
81 userdata = sliver.userData )
Tony Mack66646d52013-09-24 21:47:12 -040082 sliver.instance_id = instance.id
83 sliver.instance_name = getattr(instance, 'OS-EXT-SRV-ATTR:instance_name')
Tony Mack0c6459a2014-04-15 12:18:12 -040084 sliver.save()
Sapan Bhatia24836f12013-08-27 10:16:05 -040085
Tony Mack6fd3ef82013-10-08 22:17:54 -040086 if sliver.instance_id and metadata_update:
Tony Mack976d7742014-03-18 22:00:52 -040087 driver.update_instance_metadata(sliver.instance_id, metadata_update)
Sapan Bhatia24836f12013-08-27 10:16:05 -040088
Sapan Bhatia96500042014-07-23 09:37:19 -040089 def delete_record(self, sliver):
90 if sliver.instance_id:
91 driver = self.driver.client_driver(caller=sliver.creator,
92 tenant=sliver.slice.name,
93 deployment=sliver.deploymentNetwork.name)
94 driver.destroy_instance(sliver.instance_id)