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 | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 20 | |
| 21 | # now we return all images that need to be enacted |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 22 | return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 23 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 24 | def sync_record(self, controller_image): |
| 25 | logger.info("Working on image %s on controller %s" % (controller_image.image.name, controller_image.controller)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 26 | |
Sapan Bhatia | d9a4e1f | 2014-12-19 13:21:06 -0500 | [diff] [blame^] | 27 | image_fields = {'endpoint':controller_image.controller.auth_url, |
| 28 | 'admin_user':controller_image.controller.admin_user, |
| 29 | 'admin_password':controller_image.controller.admin_password, |
| 30 | 'name':controller_image.image.name, |
| 31 | 'filepath':controller_image.image.path |
| 32 | } |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 33 | |
Sapan Bhatia | d9a4e1f | 2014-12-19 13:21:06 -0500 | [diff] [blame^] | 34 | res = run_template('sync_controller_images.yaml', image_fields) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 35 | |
Sapan Bhatia | d9a4e1f | 2014-12-19 13:21:06 -0500 | [diff] [blame^] | 36 | if (len(res)!=1): |
| 37 | raise Exception('Could not sync image %s'%controller_image.image.name) |
| 38 | else: |
| 39 | image_id = res[0]['id'] |
| 40 | controller_image.glance_image_id = image_id |
| 41 | controller_image.save() |