blob: 17ffe57aef1dceea7eac8a26f50b543b66f0a0f0 [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 Bhatiab2d6a772015-01-16 22:12:20 +000015 provides=[ControllerImages, Image]
Tony Mack51c4a7d2014-11-30 15:33:35 -050016 requested_interval=0
17
18 def fetch_pending(self, deleted):
19 if (deleted):
20 return []
Tony Mack51c4a7d2014-11-30 15:33:35 -050021
22 # now we return all images that need to be enacted
Tony Mack06c8e472014-11-30 15:53:08 -050023 return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack51c4a7d2014-11-30 15:33:35 -050024
Tony Mack06c8e472014-11-30 15:53:08 -050025 def sync_record(self, controller_image):
26 logger.info("Working on image %s on controller %s" % (controller_image.image.name, controller_image.controller))
Sapan Bhatiab2d6a772015-01-16 22:12:20 +000027 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 Mack51c4a7d2014-11-30 15:33:35 -050034
Tony Mack51c4a7d2014-11-30 15:33:35 -050035
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000036 res = run_template('sync_controller_images.yaml', image_fields, path='controller_images', expected_num=1)
37
38 image_id = res[0]['id']
39 controller_image.glance_image_id = image_id
40 controller_image.save()