Scott Baker | 76a840e | 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 |
S.Çağlar Onur | 746c3a5 | 2015-02-12 12:36:14 -0500 | [diff] [blame] | 20 | images_path = Config().observer_images_directory |
| 21 | |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 22 | available_images = {} |
S.Çağlar Onur | 746c3a5 | 2015-02-12 12:36:14 -0500 | [diff] [blame] | 23 | 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 Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 28 | |
| 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 Onur | 746c3a5 | 2015-02-12 12:36:14 -0500 | [diff] [blame] | 41 | |
Scott Baker | 76a840e | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 42 | return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 43 | |
| 44 | def sync_record(self, image): |
| 45 | image.save() |