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 | |
Sapan Bhatia | b83643c | 2014-07-23 09:59:32 -0400 | [diff] [blame] | 12 | def fetch_pending(self, deleted): |
| 13 | # Images come from the back end |
| 14 | # You can't delete them |
| 15 | if (deleted): |
| 16 | return [] |
| 17 | |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 18 | # get list of images on disk |
| 19 | images_path = Config().observer_images_directory |
| 20 | available_images = {} |
| 21 | for f in os.listdir(images_path): |
| 22 | if os.path.isfile(os.path.join(images_path ,f)): |
| 23 | available_images[f] = os.path.join(images_path ,f) |
| 24 | |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 25 | images = Image.objects.all() |
| 26 | image_names = [image.name for image in images] |
Scott Baker | 6721c0a | 2014-06-02 23:19:38 -0700 | [diff] [blame] | 27 | |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 28 | for image_name in available_images: |
Scott Baker | 6721c0a | 2014-06-02 23:19:38 -0700 | [diff] [blame] | 29 | #remove file extension |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 30 | clean_name = ".".join(image_name.split('.')[:-1]) |
Scott Baker | 6721c0a | 2014-06-02 23:19:38 -0700 | [diff] [blame] | 31 | if clean_name not in image_names: |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 32 | image = Image(name=clean_name, |
| 33 | disk_format='raw', |
| 34 | container_format='bare', |
| 35 | path = available_images[image_name]) |
| 36 | image.save() |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 37 | |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 38 | |
| 39 | return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | 46c2d50 | 2013-10-09 13:04:28 -0400 | [diff] [blame] | 40 | |
| 41 | def sync_record(self, image): |
| 42 | image.save() |