Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from django.db.models import F, Q |
| 4 | from planetstack.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 | |
| 12 | def fetch_pending(self): |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 13 | # get list of images on disk |
| 14 | images_path = Config().observer_images_directory |
| 15 | available_images = {} |
| 16 | for f in os.listdir(images_path): |
| 17 | if os.path.isfile(os.path.join(images_path ,f)): |
| 18 | available_images[f] = os.path.join(images_path ,f) |
| 19 | |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 20 | images = Image.objects.all() |
| 21 | image_names = [image.name for image in images] |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 22 | |
| 23 | for image_name in available_images: |
| 24 | #remove file extension |
| 25 | clean_name = ".".join(image_name.split('.')[:-1]) |
| 26 | if image_name not in image_names: |
| 27 | image = Image(name=clean_name, |
| 28 | disk_format='raw', |
| 29 | container_format='bare', |
| 30 | path = available_images[image_name]) |
| 31 | image.save() |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 32 | |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 33 | |
| 34 | return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 35 | |
| 36 | def sync_record(self, image): |
| 37 | image.save() |