blob: e076d81068f684a32fbd75f905195aee7d3f4381 [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
Sapan Bhatiae6f4f142014-12-22 01:41:55 -050010from observer.ansible import *
Tony Mackf3bbe472014-11-30 15:33:35 -050011
12logger = Logger(level=logging.INFO)
13
Tony Mack336e0f92014-11-30 15:53:08 -050014class SyncControllerImages(OpenStackSyncStep):
Sapan Bhatiab3048aa2015-01-27 03:52:19 +000015 provides=[ControllerImages]
Sapan Bhatia99f49682015-01-29 20:58:25 +000016 observes = ControllerImages
Tony Mackf3bbe472014-11-30 15:33:35 -050017 requested_interval=0
18
19 def fetch_pending(self, deleted):
20 if (deleted):
21 return []
Tony Mackf3bbe472014-11-30 15:33:35 -050022
23 # now we return all images that need to be enacted
Tony Mack336e0f92014-11-30 15:53:08 -050024 return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mackf3bbe472014-11-30 15:33:35 -050025
Tony Mack336e0f92014-11-30 15:53:08 -050026 def sync_record(self, controller_image):
27 logger.info("Working on image %s on controller %s" % (controller_image.image.name, controller_image.controller))
Sapan Bhatia90671a82015-01-16 22:12:20 +000028 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 Mackf3bbe472014-11-30 15:33:35 -050035
Tony Mackf3bbe472014-11-30 15:33:35 -050036
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000037 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 Bhatia5851db42015-01-27 03:52:43 +000041 controller_image.backend_status = '1 - OK'
Sapan Bhatiab0464ba2015-01-23 16:21:57 +000042 controller_image.save()