Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame^] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
| 4 | from xos.config import Config |
| 5 | from observer.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.image import Image |
| 7 | |
| 8 | class 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 |
| 20 | images_path = Config().observer_images_directory |
| 21 | available_images = {} |
| 22 | for f in os.listdir(images_path): |
| 23 | if os.path.isfile(os.path.join(images_path ,f)): |
| 24 | available_images[f] = os.path.join(images_path ,f) |
| 25 | |
| 26 | images = Image.objects.all() |
| 27 | image_names = [image.name for image in images] |
| 28 | |
| 29 | for image_name in available_images: |
| 30 | #remove file extension |
| 31 | clean_name = ".".join(image_name.split('.')[:-1]) |
| 32 | if clean_name not in image_names: |
| 33 | image = Image(name=clean_name, |
| 34 | disk_format='raw', |
| 35 | container_format='bare', |
| 36 | path = available_images[image_name]) |
| 37 | image.save() |
| 38 | |
| 39 | |
| 40 | return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 41 | |
| 42 | def sync_record(self, image): |
| 43 | image.save() |