Tony Mack | f3bbe47 | 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 | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 7 | from core.models import Controller |
| 8 | from core.models import Image, ControllerImages |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 9 | from util.logger import Logger, logging |
Sapan Bhatia | e6f4f14 | 2014-12-22 01:41:55 -0500 | [diff] [blame] | 10 | from observer.ansible import * |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 11 |
|
| 12 | logger = Logger(level=logging.INFO) |
| 13 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 14 | class SyncControllerImages(OpenStackSyncStep): |
Sapan Bhatia | 90671a8 | 2015-01-16 22:12:20 +0000 | [diff] [blame] | 15 | provides=[ControllerImages, Image] |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 16 | requested_interval=0 |
| 17 | |
| 18 | def fetch_pending(self, deleted): |
| 19 | if (deleted): |
| 20 | return [] |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 21 | |
| 22 | # now we return all images that need to be enacted |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 23 | return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 24 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 25 | def sync_record(self, controller_image): |
| 26 | logger.info("Working on image %s on controller %s" % (controller_image.image.name, controller_image.controller)) |
Sapan Bhatia | 90671a8 | 2015-01-16 22:12:20 +0000 | [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 | 'ansible_tag': '%s@%s'%(controller_image.image.name,controller_image.controller.name), # name of ansible playbook |
| 33 | } |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 34 | |
Sapan Bhatia | 90671a8 | 2015-01-16 22:12:20 +0000 | [diff] [blame] | 35 | res = run_template('sync_controller_images.yaml', image_fields, path='controller_images') |
Tony Mack | f3bbe47 | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 36 | |
Sapan Bhatia | 90671a8 | 2015-01-16 22:12:20 +0000 | [diff] [blame] | 37 | if (len(res)!=1): |
| 38 | raise Exception('Could not sync image %s'%controller_image.image.name) |
| 39 | else: |
| 40 | image_id = res[0]['id'] |
Sapan Bhatia | 828a56c | 2014-12-19 13:21:06 -0500 | [diff] [blame] | 41 | controller_image.glance_image_id = image_id |
| 42 | controller_image.save() |