blob: 67cd7b5d99c6a6d9a2891c6407fe874ea24a1b93 [file] [log] [blame]
Tony Mackf3bbe472014-11-30 15:33:35 -05001import 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 Mack336e0f92014-11-30 15:53:08 -05007from core.models import Controller
8from core.models import Image, ControllerImages
Tony Mackf3bbe472014-11-30 15:33:35 -05009from util.logger import Logger, logging
10
11logger = Logger(level=logging.INFO)
12
Tony Mack336e0f92014-11-30 15:53:08 -050013class SyncControllerImages(OpenStackSyncStep):
14 provides=[ControllerImages]
Tony Mackf3bbe472014-11-30 15:33:35 -050015 requested_interval=0
16
17 def fetch_pending(self, deleted):
18 if (deleted):
19 return []
Tony Mack336e0f92014-11-30 15:53:08 -050020 # smbaker: commented out automatic creation of ControllerImages as
Tony Mackf3bbe472014-11-30 15:33:35 -050021 # as they will now be configured in GUI. Not sure if this is
22 # sufficient.
23
Tony Mack336e0f92014-11-30 15:53:08 -050024# # ensure images are available across all controllers
25# controller_images = ControllerImages.objects.all()
Tony Mackf3bbe472014-11-30 15:33:35 -050026# image_deploy_lookup = defaultdict(list)
Tony Mack336e0f92014-11-30 15:53:08 -050027# for controller_image in controller_images:
28# image_deploy_lookup[controller_image.image].append(controller_image.controller)
Tony Mackf3bbe472014-11-30 15:33:35 -050029#
Tony Mack336e0f92014-11-30 15:53:08 -050030# all_controllers = Controller.objects.all()
Tony Mackf3bbe472014-11-30 15:33:35 -050031# for image in Image.objects.all():
Tony Mack336e0f92014-11-30 15:53:08 -050032# expected_controllers = all_controllers
33# for expected_controller in expected_controllers:
Tony Mackf3bbe472014-11-30 15:33:35 -050034# if image not in image_deploy_lookup or \
Tony Mack336e0f92014-11-30 15:53:08 -050035# expected_controller not in image_deploy_lookup[image]:
36# id = ControllerImages(image=image, controller=expected_controller)
Tony Mackf3bbe472014-11-30 15:33:35 -050037# id.save()
38
39 # now we return all images that need to be enacted
Tony Mack336e0f92014-11-30 15:53:08 -050040 return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mackf3bbe472014-11-30 15:33:35 -050041
Tony Mack336e0f92014-11-30 15:53:08 -050042 def sync_record(self, controller_image):
43 logger.info("Working on image %s on controller %s" % (controller_image.image.name, controller_image.controller))
44 driver = self.driver.admin_driver(controller=controller_image.controller.name)
Tony Mackf3bbe472014-11-30 15:33:35 -050045 images = driver.shell.glance.get_images()
46 glance_image = None
47 for image in images:
Tony Mack336e0f92014-11-30 15:53:08 -050048 if image['name'] == controller_image.image.name:
Tony Mackf3bbe472014-11-30 15:33:35 -050049 glance_image = image
50 break
51 if glance_image:
Tony Mack336e0f92014-11-30 15:53:08 -050052 logger.info("Found image %s on controller %s" % (controller_image.image.name, controller_image.controller.name))
53 controller_image.glance_image_id = glance_image['id']
54 elif controller_image.image.path:
Tony Mackf3bbe472014-11-30 15:33:35 -050055 image = {
Tony Mack336e0f92014-11-30 15:53:08 -050056 'name': controller_image.image.name,
Tony Mackf3bbe472014-11-30 15:33:35 -050057 'is_public': True,
58 'disk_format': 'raw',
59 'container_format': 'bare',
Tony Mack336e0f92014-11-30 15:53:08 -050060 'file': controller_image.image.path,
Tony Mackf3bbe472014-11-30 15:33:35 -050061 }
62
Tony Mack336e0f92014-11-30 15:53:08 -050063 logger.info("Creating image %s on controller %s" % (controller_image.image.name, controller_image.controller.name))
Tony Mackf3bbe472014-11-30 15:33:35 -050064
Tony Mack336e0f92014-11-30 15:53:08 -050065 glance_image = driver.shell.glanceclient.images.create(name=controller_image.image.name,
Tony Mackf3bbe472014-11-30 15:33:35 -050066 is_public=True,
67 disk_format='raw',
68 container_format='bare')
Tony Mack336e0f92014-11-30 15:53:08 -050069 glance_image.update(data=open(controller_image.image.path, 'rb'))
Tony Mackf3bbe472014-11-30 15:33:35 -050070
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):
Tony Mack336e0f92014-11-30 15:53:08 -050075 raise Exception, "Add image failed at controller %s" % controller_image.controller.name
76 controller_image.glance_image_id = getattr(glance_image, "id")
77 controller_image.save()