blob: 2014a257f94dd2aeb89eb7e914c5ca84332a270b [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
Sapan Bhatiab2d6a772015-01-16 22:12:20 +000035 res = run_template('sync_controller_images.yaml', image_fields, path='controller_images')
Tony Mack51c4a7d2014-11-30 15:33:35 -050036
Sapan Bhatiab2d6a772015-01-16 22:12:20 +000037 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 Bhatiad9a4e1f2014-12-19 13:21:06 -050041 controller_image.glance_image_id = image_id
42 controller_image.save()