3 from collections import defaultdict
4 from django.db.models import F, Q
5 from planetstack.config import Config
6 from observer.openstacksyncstep import OpenStackSyncStep
7 from core.models import Deployment
8 from core.models import Image, ImageDeployments
9 from util.logger import Logger, logging
11 logger = Logger(level=logging.INFO)
13 class SyncImageDeployments(OpenStackSyncStep):
14 provides=[ImageDeployments]
17 def fetch_pending(self):
18 # smbaker: commented out automatic creation of ImageDeployments as
19 # as they will now be configured in GUI. Not sure if this is
22 # # 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)
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)
37 # now we return all images that need to be enacted
38 return ImageDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
40 def sync_record(self, image_deployment):
41 logger.info("Working on image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
42 driver = self.driver.admin_driver(deployment=image_deployment.deployment.name)
43 images = driver.shell.glance.get_images()
46 if image['name'] == image_deployment.image.name:
50 logger.info("Found image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
51 image_deployment.glance_image_id = glance_image['id']
52 elif image_deployment.image.path:
54 'name': image_deployment.image.name,
57 'container_format': 'bare',
58 'file': image_deployment.image.path,
61 logger.info("Creating image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name))
63 glance_image = driver.shell.glanceclient.images.create(name=image_deployment.image.name,
66 container_format='bare')
67 glance_image.update(data=open(image_deployment.image.path, 'rb'))
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):
73 raise Exception, "Add image failed at deployment %s" % image_deployment.deployment.name
74 image_deployment.glance_image_id = getattr(glance_image, "id")
75 image_deployment.save()