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] |
Scott Baker | 6721c0a | 2014-06-02 23:19:38 -0700 | [diff] [blame] | 22 | |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 23 | for image_name in available_images: |
Scott Baker | 6721c0a | 2014-06-02 23:19:38 -0700 | [diff] [blame] | 24 | #remove file extension |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 25 | clean_name = ".".join(image_name.split('.')[:-1]) |
Scott Baker | 6721c0a | 2014-06-02 23:19:38 -0700 | [diff] [blame] | 26 | if clean_name not in image_names: |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 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() |