blob: e076d81068f684a32fbd75f905195aee7d3f4381 [file] [log] [blame]
Tony Mack51c4a7d2014-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 Mack06c8e472014-11-30 15:53:08 -05007from core.models import Controller
8from core.models import Image, ControllerImages
Tony Mack51c4a7d2014-11-30 15:33:35 -05009from util.logger import Logger, logging
Sapan Bhatia77f9f302014-12-22 01:41:55 -050010from observer.ansible import *
Tony Mack51c4a7d2014-11-30 15:33:35 -050011
12logger = Logger(level=logging.INFO)
13
Tony Mack06c8e472014-11-30 15:53:08 -050014class SyncControllerImages(OpenStackSyncStep):
Sapan Bhatia5d662c72015-01-27 03:52:19 +000015 provides=[ControllerImages]
Sapan Bhatia39a775f2015-01-29 20:58:25 +000016 observes = ControllerImages
Tony Mack51c4a7d2014-11-30 15:33:35 -050017 requested_interval=0
18
19 def fetch_pending(self, deleted):
20 if (deleted):
21 return []
Tony Mack51c4a7d2014-11-30 15:33:35 -050022
23 # now we return all images that need to be enacted
Tony Mack06c8e472014-11-30 15:53:08 -050024 return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack51c4a7d2014-11-30 15:33:35 -050025
Tony Mack06c8e472014-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 Bhatiab2d6a772015-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 Mack51c4a7d2014-11-30 15:33:35 -050035
Tony Mack51c4a7d2014-11-30 15:33:35 -050036
Sapan Bhatia3b3e1e12015-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 Bhatiac88c9a82015-01-27 03:52:43 +000041 controller_image.backend_status = '1 - OK'
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000042 controller_image.save()