Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 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 |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 7 | from core.models import Controller |
| 8 | from core.models import Image, ControllerImages |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 9 | from util.logger import Logger, logging |
| 10 |
|
| 11 | logger = Logger(level=logging.INFO) |
| 12 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 13 | class SyncControllerImages(OpenStackSyncStep): |
| 14 | provides=[ControllerImages] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 15 | requested_interval=0 |
| 16 | |
| 17 | def fetch_pending(self, deleted): |
| 18 | if (deleted): |
| 19 | return [] |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 20 | # smbaker: commented out automatic creation of ControllerImages as |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 21 | # as they will now be configured in GUI. Not sure if this is |
| 22 | # sufficient. |
| 23 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 24 | # # ensure images are available across all controllers |
| 25 | # controller_images = ControllerImages.objects.all() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 26 | # image_deploy_lookup = defaultdict(list) |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 27 | # for controller_image in controller_images: |
| 28 | # image_deploy_lookup[controller_image.image].append(controller_image.controller) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 29 | # |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 30 | # all_controllers = Controller.objects.all() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 31 | # for image in Image.objects.all(): |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 32 | # expected_controllers = all_controllers |
| 33 | # for expected_controller in expected_controllers: |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 34 | # if image not in image_deploy_lookup or \ |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 35 | # expected_controller not in image_deploy_lookup[image]: |
| 36 | # id = ControllerImages(image=image, controller=expected_controller) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 37 | # id.save() |
| 38 | |
| 39 | # now we return all images that need to be enacted |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 40 | return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 41 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 42 | 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 Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 45 | images = driver.shell.glance.get_images() |
| 46 | glance_image = None |
| 47 | for image in images: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 48 | if image['name'] == controller_image.image.name: |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 49 | glance_image = image |
| 50 | break |
| 51 | if glance_image: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 52 | 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 Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 55 | image = { |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 56 | 'name': controller_image.image.name, |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 57 | 'is_public': True, |
| 58 | 'disk_format': 'raw', |
| 59 | 'container_format': 'bare', |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 60 | 'file': controller_image.image.path, |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 63 | logger.info("Creating image %s on controller %s" % (controller_image.image.name, controller_image.controller.name)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 64 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 65 | glance_image = driver.shell.glanceclient.images.create(name=controller_image.image.name, |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 66 | is_public=True, |
| 67 | disk_format='raw', |
| 68 | container_format='bare') |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 69 | glance_image.update(data=open(controller_image.image.path, 'rb')) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 70 | |
| 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 Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 75 | 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() |