blob: d4ef430ce0ca98985e3af7527628ed9ef68bed4a [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import os
2import base64
3from django.db.models import F, Q
4from xos.config import Config
Scott Baker8b75e852016-08-16 15:04:59 -07005from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
Scott Bakerb63ea792016-08-11 10:24:48 -07006from core.models.image import Image
7from xos.logger import observer_logger as logger
8
9class SyncImages(OpenStackSyncStep):
10 provides=[Image]
11 requested_interval=0
12 observes=Image
13
14 def fetch_pending(self, deleted):
15 # Images come from the back end
16 # You can't delete them
17 if (deleted):
18 logger.info("SyncImages: returning because deleted=True")
19 return []
20
21 # get list of images on disk
22 images_path = Config().observer_images_directory
23
24 logger.info("SyncImages: deleted=False, images_path=%s" % images_path)
25
26 available_images = {}
27 if os.path.exists(images_path):
28 for f in os.listdir(images_path):
29 filename = os.path.join(images_path, f)
30 if os.path.isfile(filename) and filename.endswith(".img"):
31 available_images[f] = filename
32
33 logger.info("SyncImages: available_images = %s" % str(available_images))
34
35 images = Image.objects.all()
36 image_names = [image.name for image in images]
37
38 for image_name in available_images:
39 #remove file extension
40 clean_name = ".".join(image_name.split('.')[:-1])
41 if clean_name not in image_names:
42 logger.info("SyncImages: adding %s" % clean_name)
43 image = Image(name=clean_name,
44 disk_format='raw',
45 container_format='bare',
46 path = available_images[image_name])
47 image.save()
48
49 return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
50
51 def sync_record(self, image):
52 image.save()