blob: 0a0910e581c112dcf45a3d6113b7abf63577c817 [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):
15 provides=[ControllerImages]
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 Bhatia77f9f302014-12-22 01:41:55 -050027 import pdb
28 pdb.set_trace()
Tony Mack51c4a7d2014-11-30 15:33:35 -050029
Sapan Bhatiad9a4e1f2014-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 Bhatia77f9f302014-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 Bhatiad9a4e1f2014-12-19 13:21:06 -050036 }
Tony Mack51c4a7d2014-11-30 15:33:35 -050037
Sapan Bhatia77f9f302014-12-22 01:41:55 -050038 res = run_template('sync_controller_images.yaml', image_fields, path='controller_images')
Tony Mack51c4a7d2014-11-30 15:33:35 -050039
Sapan Bhatiad9a4e1f2014-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()