blob: 9b915fbe2198a061dddbf3c049f25d661452fb7e [file] [log] [blame]
Tony Mack46c2d502013-10-09 13:04:28 -04001import os
2import base64
3from django.db.models import F, Q
4from planetstack.config import Config
5from observer.openstacksyncstep import OpenStackSyncStep
6from core.models.image import Image
7
8class SyncImages(OpenStackSyncStep):
9 provides=[Image]
10 requested_interval=0
11
Sapan Bhatiab83643c2014-07-23 09:59:32 -040012 def fetch_pending(self, deleted):
13 # Images come from the back end
14 # You can't delete them
15 if (deleted):
16 return []
17
Tony Mackbf39d9f2014-05-06 21:42:36 -040018 # get list of images on disk
19 images_path = Config().observer_images_directory
20 available_images = {}
21 for f in os.listdir(images_path):
22 if os.path.isfile(os.path.join(images_path ,f)):
23 available_images[f] = os.path.join(images_path ,f)
24
Tony Mack46c2d502013-10-09 13:04:28 -040025 images = Image.objects.all()
26 image_names = [image.name for image in images]
Scott Baker6721c0a2014-06-02 23:19:38 -070027
Tony Mackbf39d9f2014-05-06 21:42:36 -040028 for image_name in available_images:
Scott Baker6721c0a2014-06-02 23:19:38 -070029 #remove file extension
Tony Mackbf39d9f2014-05-06 21:42:36 -040030 clean_name = ".".join(image_name.split('.')[:-1])
Scott Baker6721c0a2014-06-02 23:19:38 -070031 if clean_name not in image_names:
Tony Mackbf39d9f2014-05-06 21:42:36 -040032 image = Image(name=clean_name,
33 disk_format='raw',
34 container_format='bare',
35 path = available_images[image_name])
36 image.save()
Tony Mack46c2d502013-10-09 13:04:28 -040037
Tony Mackbf39d9f2014-05-06 21:42:36 -040038
39 return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack46c2d502013-10-09 13:04:28 -040040
41 def sync_record(self, image):
42 image.save()