blob: 4a69b1c02f84d2044d59d324cc9b5ce5ea423932 [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
8from 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
13class SyncImageDeployments(OpenStackSyncStep):
14 provides=[ImageDeployments]
15 requested_interval=0
16
17 def fetch_pending(self):
Scott Bakerb3a7bca2014-06-11 11:35:40 -070018 # smbaker: commented out automatic creation of ImageDeployments as
19 # as they will now be configured in GUI. Not sure if this is
20 # sufficient.
Scott Bakere3969b22014-06-02 22:59:08 -070021
Scott Bakerb3a7bca2014-06-11 11:35:40 -070022# # ensure images are available across all deployments
23# image_deployments = ImageDeployments.objects.all()
24# image_deploy_lookup = defaultdict(list)
25# for image_deployment in image_deployments:
26# image_deploy_lookup[image_deployment.image].append(image_deployment.deployment)
27#
28# all_deployments = Deployment.objects.all()
29# for image in Image.objects.all():
30# expected_deployments = all_deployments
31# for expected_deployment in expected_deployments:
32# if image not in image_deploy_lookup or \
33# expected_deployment not in image_deploy_lookup[image]:
34# id = ImageDeployments(image=image, deployment=expected_deployment)
35# id.save()
Scott Bakere3969b22014-06-02 22:59:08 -070036
Tony Mackbf39d9f2014-05-06 21:42:36 -040037 # now we return all images that need to be enacted
Scott Bakere3969b22014-06-02 22:59:08 -070038 return ImageDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
39
Tony Mackbf39d9f2014-05-06 21:42:36 -040040 def sync_record(self, image_deployment):
Scott Bakere3969b22014-06-02 22:59:08 -070041 logger.info("Working on image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
Tony Mackbf39d9f2014-05-06 21:42:36 -040042 driver = self.driver.admin_driver(deployment=image_deployment.deployment.name)
Tony Mack8b85d9a2014-05-06 23:43:14 -040043 images = driver.shell.glance.get_images()
44 glance_image = None
45 for image in images:
46 if image['name'] == image_deployment.image.name:
47 glance_image = image
48 break
49 if glance_image:
Scott Bakere3969b22014-06-02 22:59:08 -070050 logger.info("Found image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
Tony Mack8b85d9a2014-05-06 23:43:14 -040051 image_deployment.glance_image_id = glance_image['id']
52 elif image_deployment.image.path:
Scott Baker3949bc62014-05-12 10:42:16 -070053 image = {
54 'name': image_deployment.image.name,
55 'is_public': True,
Scott Bakere3969b22014-06-02 22:59:08 -070056 'disk_format': 'raw',
Scott Baker3949bc62014-05-12 10:42:16 -070057 'container_format': 'bare',
Scott Bakere3969b22014-06-02 22:59:08 -070058 'file': image_deployment.image.path,
59 }
60
61 logger.info("Creating image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
62
Tony Mack8b85d9a2014-05-06 23:43:14 -040063 glance_image = driver.shell.glanceclient.images.create(name=image_deployment.image.name,
64 is_public=True,
65 disk_format='raw',
66 container_format='bare')
67 glance_image.update(data=open(image_deployment.image.path, 'rb'))
Scott Bakere3969b22014-06-02 22:59:08 -070068
69 # While the images returned by driver.shell.glance.get_images()
70 # are dicts, the images returned by driver.shell.glanceclient.images.create
71 # are not dicts. We have to use getattr() instead of [] operator.
72 if not glance_image or not getattr(glance_image,"id",None):
Scott Baker3949bc62014-05-12 10:42:16 -070073 raise Exception, "Add image failed at deployment %s" % image_deployment.deployment.name
Scott Bakere3969b22014-06-02 22:59:08 -070074 image_deployment.glance_image_id = getattr(glance_image, "id")
Tony Mackbf39d9f2014-05-06 21:42:36 -040075 image_deployment.save()