blob: 4bdad87fa818c9cdc906c5744a92a0ba4f6278a5 [file] [log] [blame]
Tony Mackbf39d9f2014-05-06 21:42:36 -04001import os
2import base64
3from collections import defaultdict
4from django.db.models import F, Q
5from planetstack.config import Config
6from observer.openstacksyncstep import OpenStackSyncStep
Tony Mack91463d92014-06-19 20:42:03 -04007from core.models import Deployment
Sapan Bhatia18b779c2014-11-11 23:05:53 -05008from core.models import Image, ImageDeployment
Scott Bakere3969b22014-06-02 22:59:08 -07009from util.logger import Logger, logging
10
11logger = Logger(level=logging.INFO)
Tony Mackbf39d9f2014-05-06 21:42:36 -040012
Sapan Bhatia18b779c2014-11-11 23:05:53 -050013class SyncImageDeployment(OpenStackSyncStep):
14 provides=[ImageDeployment]
Tony Mackbf39d9f2014-05-06 21:42:36 -040015 requested_interval=0
16
Sapan Bhatiab83643c2014-07-23 09:59:32 -040017 def fetch_pending(self, deleted):
18 if (deleted):
19 return []
Sapan Bhatia18b779c2014-11-11 23:05:53 -050020 # smbaker: commented out automatic creation of ImageDeployment as
Scott Bakerb3a7bca2014-06-11 11:35:40 -070021 # as they will now be configured in GUI. Not sure if this is
22 # sufficient.
Scott Bakere3969b22014-06-02 22:59:08 -070023
Scott Bakerb3a7bca2014-06-11 11:35:40 -070024# # ensure images are available across all deployments
Sapan Bhatia18b779c2014-11-11 23:05:53 -050025# image_deployments = ImageDeployment.objects.all()
Scott Bakerb3a7bca2014-06-11 11:35:40 -070026# image_deploy_lookup = defaultdict(list)
27# for image_deployment in image_deployments:
28# image_deploy_lookup[image_deployment.image].append(image_deployment.deployment)
29#
30# all_deployments = Deployment.objects.all()
31# for image in Image.objects.all():
32# expected_deployments = all_deployments
33# for expected_deployment in expected_deployments:
34# if image not in image_deploy_lookup or \
35# expected_deployment not in image_deploy_lookup[image]:
Sapan Bhatia18b779c2014-11-11 23:05:53 -050036# id = ImageDeployment(image=image, deployment=expected_deployment)
Scott Bakerb3a7bca2014-06-11 11:35:40 -070037# id.save()
Scott Bakere3969b22014-06-02 22:59:08 -070038
Tony Mackbf39d9f2014-05-06 21:42:36 -040039 # now we return all images that need to be enacted
Sapan Bhatia18b779c2014-11-11 23:05:53 -050040 return ImageDeployment.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Scott Bakere3969b22014-06-02 22:59:08 -070041
Tony Mackbf39d9f2014-05-06 21:42:36 -040042 def sync_record(self, image_deployment):
Scott Bakere3969b22014-06-02 22:59:08 -070043 logger.info("Working on image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
Tony Mackbf39d9f2014-05-06 21:42:36 -040044 driver = self.driver.admin_driver(deployment=image_deployment.deployment.name)
Tony Mack8b85d9a2014-05-06 23:43:14 -040045 images = driver.shell.glance.get_images()
46 glance_image = None
47 for image in images:
48 if image['name'] == image_deployment.image.name:
49 glance_image = image
50 break
51 if glance_image:
Scott Bakere3969b22014-06-02 22:59:08 -070052 logger.info("Found image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
Tony Mack8b85d9a2014-05-06 23:43:14 -040053 image_deployment.glance_image_id = glance_image['id']
54 elif image_deployment.image.path:
Scott Baker3949bc62014-05-12 10:42:16 -070055 image = {
56 'name': image_deployment.image.name,
57 'is_public': True,
Scott Bakere3969b22014-06-02 22:59:08 -070058 'disk_format': 'raw',
Scott Baker3949bc62014-05-12 10:42:16 -070059 'container_format': 'bare',
Scott Bakere3969b22014-06-02 22:59:08 -070060 'file': image_deployment.image.path,
61 }
62
63 logger.info("Creating image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
64
Tony Mack8b85d9a2014-05-06 23:43:14 -040065 glance_image = driver.shell.glanceclient.images.create(name=image_deployment.image.name,
66 is_public=True,
67 disk_format='raw',
68 container_format='bare')
69 glance_image.update(data=open(image_deployment.image.path, 'rb'))
Scott Bakere3969b22014-06-02 22:59:08 -070070
71 # While the images returned by driver.shell.glance.get_images()
72 # are dicts, the images returned by driver.shell.glanceclient.images.create
73 # are not dicts. We have to use getattr() instead of [] operator.
74 if not glance_image or not getattr(glance_image,"id",None):
Scott Baker3949bc62014-05-12 10:42:16 -070075 raise Exception, "Add image failed at deployment %s" % image_deployment.deployment.name
Scott Bakere3969b22014-06-02 22:59:08 -070076 image_deployment.glance_image_id = getattr(glance_image, "id")
Tony Mackbf39d9f2014-05-06 21:42:36 -040077 image_deployment.save()