blob: 0a0910e581c112dcf45a3d6113b7abf63577c817 [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):
15 provides=[ControllerImages]
Tony Mackf3bbe472014-11-30 15:33:35 -050016 requested_interval=0
17
18 def fetch_pending(self, deleted):
19 if (deleted):
20 return []
Tony Mackf3bbe472014-11-30 15:33:35 -050021
22 # now we return all images that need to be enacted
Tony Mack336e0f92014-11-30 15:53:08 -050023 return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mackf3bbe472014-11-30 15:33:35 -050024
Tony Mack336e0f92014-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 Bhatiae6f4f142014-12-22 01:41:55 -050027 import pdb
28 pdb.set_trace()
Tony Mackf3bbe472014-11-30 15:33:35 -050029
Sapan Bhatia828a56c2014-12-19 13:21:06 -050030 image_fields = {'endpoint':controller_image.controller.auth_url,
31 'admin_user':controller_image.controller.admin_user,
32 'admin_password':controller_image.controller.admin_password,
33 'name':controller_image.image.name,
Sapan Bhatiae6f4f142014-12-22 01:41:55 -050034 'filepath':controller_image.image.path,
35 'ansible_tag': '%s@%s'%(controller_image.image.name,controller_image.controller.name), # name of ansible playbook
Sapan Bhatia828a56c2014-12-19 13:21:06 -050036 }
Tony Mackf3bbe472014-11-30 15:33:35 -050037
Sapan Bhatiae6f4f142014-12-22 01:41:55 -050038 res = run_template('sync_controller_images.yaml', image_fields, path='controller_images')
Tony Mackf3bbe472014-11-30 15:33:35 -050039
Sapan Bhatia828a56c2014-12-19 13:21:06 -050040 if (len(res)!=1):
41 raise Exception('Could not sync image %s'%controller_image.image.name)
42 else:
43 image_id = res[0]['id']
44 controller_image.glance_image_id = image_id
45 controller_image.save()