Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame^] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
| 4 | from xos.config import Config |
| 5 | from synchronizers.base.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.image import Image |
| 7 | from xos.logger import observer_logger as logger |
| 8 | |
| 9 | class 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() |