blob: 94cfd9999bdb73a454f41f57cf23190a5b372bcb [file] [log] [blame]
Scott Baker86e132c2015-02-11 21:38:09 -08001import os
2import base64
3from django.db.models import F, Q
4from xos.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 observes=Image
12
13 def fetch_pending(self, deleted):
14 # Images come from the back end
15 # You can't delete them
16 if (deleted):
17 return []
18
19 # get list of images on disk
S.Çağlar Onur347f4442015-02-12 12:36:14 -050020 images_path = Config().observer_images_directory
21
Scott Baker86e132c2015-02-11 21:38:09 -080022 available_images = {}
S.Çağlar Onur347f4442015-02-12 12:36:14 -050023 if os.path.exists(images_path):
24 for f in os.listdir(images_path):
25 filename = os.path.join(images_path, f)
26 if os.path.isfile(filename):
27 available_images[f] = filename
Scott Baker86e132c2015-02-11 21:38:09 -080028
29 images = Image.objects.all()
30 image_names = [image.name for image in images]
31
32 for image_name in available_images:
33 #remove file extension
34 clean_name = ".".join(image_name.split('.')[:-1])
35 if clean_name not in image_names:
36 image = Image(name=clean_name,
37 disk_format='raw',
38 container_format='bare',
39 path = available_images[image_name])
40 image.save()
S.Çağlar Onur347f4442015-02-12 12:36:14 -050041
Scott Baker86e132c2015-02-11 21:38:09 -080042 return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
43
44 def sync_record(self, image):
45 image.save()