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 |
Sapan Bhatia | 77f9f30 | 2014-12-22 01:41:55 -0500 | [diff] [blame] | 10 | from observer.ansible import * |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 11 |
|
| 12 | logger = Logger(level=logging.INFO) |
| 13 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 14 | class SyncControllerImages(OpenStackSyncStep): |
Sapan Bhatia | 5d662c7 | 2015-01-27 03:52:19 +0000 | [diff] [blame] | 15 | provides=[ControllerImages] |
Sapan Bhatia | 39a775f | 2015-01-29 20:58:25 +0000 | [diff] [blame] | 16 | observes = ControllerImages |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 17 | requested_interval=0 |
| 18 | |
| 19 | def fetch_pending(self, deleted): |
| 20 | if (deleted): |
| 21 | return [] |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 22 | |
| 23 | # now we return all images that need to be enacted |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 24 | return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 25 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 26 | def sync_record(self, controller_image): |
| 27 | logger.info("Working on image %s on controller %s" % (controller_image.image.name, controller_image.controller)) |
Sapan Bhatia | b2d6a77 | 2015-01-16 22:12:20 +0000 | [diff] [blame] | 28 | image_fields = {'endpoint':controller_image.controller.auth_url, |
| 29 | 'admin_user':controller_image.controller.admin_user, |
| 30 | 'admin_password':controller_image.controller.admin_password, |
| 31 | 'name':controller_image.image.name, |
| 32 | 'filepath':controller_image.image.path, |
| 33 | 'ansible_tag': '%s@%s'%(controller_image.image.name,controller_image.controller.name), # name of ansible playbook |
| 34 | } |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 35 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 36 | |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 37 | res = run_template('sync_controller_images.yaml', image_fields, path='controller_images', expected_num=1) |
| 38 | |
| 39 | image_id = res[0]['id'] |
| 40 | controller_image.glance_image_id = image_id |
Sapan Bhatia | c88c9a8 | 2015-01-27 03:52:43 +0000 | [diff] [blame] | 41 | controller_image.backend_status = '1 - OK' |
Sapan Bhatia | 3b3e1e1 | 2015-01-23 16:21:57 +0000 | [diff] [blame] | 42 | controller_image.save() |