blob: 4446d9a9f079d8887bd078c5cd43b347c79287c7 [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
10
11logger = Logger(level=logging.INFO)
12
Tony Mack336e0f92014-11-30 15:53:08 -050013class SyncControllerImages(OpenStackSyncStep):
14 provides=[ControllerImages]
Tony Mackf3bbe472014-11-30 15:33:35 -050015 requested_interval=0
16
17 def fetch_pending(self, deleted):
18 if (deleted):
19 return []
Tony Mackf3bbe472014-11-30 15:33:35 -050020
21 # now we return all images that need to be enacted
Tony Mack336e0f92014-11-30 15:53:08 -050022 return ControllerImages.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mackf3bbe472014-11-30 15:33:35 -050023
Tony Mack336e0f92014-11-30 15:53:08 -050024 def sync_record(self, controller_image):
25 logger.info("Working on image %s on controller %s" % (controller_image.image.name, controller_image.controller))
Tony Mackf3bbe472014-11-30 15:33:35 -050026
Sapan Bhatia828a56c2014-12-19 13:21:06 -050027 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 }
Tony Mackf3bbe472014-11-30 15:33:35 -050033
Sapan Bhatia828a56c2014-12-19 13:21:06 -050034 res = run_template('sync_controller_images.yaml', image_fields)
Tony Mackf3bbe472014-11-30 15:33:35 -050035
Sapan Bhatia828a56c2014-12-19 13:21:06 -050036 if (len(res)!=1):
37 raise Exception('Could not sync image %s'%controller_image.image.name)
38 else:
39 image_id = res[0]['id']
40 controller_image.glance_image_id = image_id
41 controller_image.save()