blob: 8049ac1fd8b2927eaba9ef283ebf97d3e0853179 [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
Sapan Bhatia16be1432016-01-14 11:41:38 -05005from synchronizers.base.openstacksyncstep import OpenStackSyncStep
Scott Baker86e132c2015-02-11 21:38:09 -08006from core.models.image import Image
Scott Baker3586c552016-01-14 15:30:20 -08007from xos.logger import observer_logger as logger
Scott Baker86e132c2015-02-11 21:38:09 -08008
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):
Scott Baker745cfa42015-03-09 16:25:38 -070018 logger.info("SyncImages: returning because deleted=True")
Scott Baker86e132c2015-02-11 21:38:09 -080019 return []
20
21 # get list of images on disk
S.Çağlar Onur347f4442015-02-12 12:36:14 -050022 images_path = Config().observer_images_directory
23
Scott Baker745cfa42015-03-09 16:25:38 -070024 logger.info("SyncImages: deleted=False, images_path=%s" % images_path)
25
Scott Baker86e132c2015-02-11 21:38:09 -080026 available_images = {}
S.Çağlar Onur347f4442015-02-12 12:36:14 -050027 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):
31 available_images[f] = filename
Scott Baker86e132c2015-02-11 21:38:09 -080032
Scott Baker745cfa42015-03-09 16:25:38 -070033 logger.info("SyncImages: available_images = %s" % str(available_images))
34
Scott Baker86e132c2015-02-11 21:38:09 -080035 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:
Scott Baker745cfa42015-03-09 16:25:38 -070042 logger.info("SyncImages: adding %s" % clean_name)
Scott Baker86e132c2015-02-11 21:38:09 -080043 image = Image(name=clean_name,
44 disk_format='raw',
45 container_format='bare',
46 path = available_images[image_name])
47 image.save()
S.Çağlar Onur347f4442015-02-12 12:36:14 -050048
Scott Baker86e132c2015-02-11 21:38:09 -080049 return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
50
51 def sync_record(self, image):
52 image.save()