blob: 20c22a2c1ac570afd0c4072e6a7577c2ed6cc953 [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 Bhatia1b6bba22014-11-19 15:10:16 -05008from core.models import Image, ImageDeployments
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 Bhatia1b6bba22014-11-19 15:10:16 -050013class SyncImageDeployments(OpenStackSyncStep):
14 provides=[ImageDeployments]
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 Bhatia1b6bba22014-11-19 15:10:16 -050020 # smbaker: commented out automatic creation of ImageDeployments 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 Bhatia1b6bba22014-11-19 15:10:16 -050025# image_deployments = ImageDeployments.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 Bhatia1b6bba22014-11-19 15:10:16 -050036# id = ImageDeployments(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 Bhatia1b6bba22014-11-19 15:10:16 -050040 return ImageDeployments.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()