sync images
diff --git a/planetstack/observer/steps/__init__.py b/planetstack/observer/steps/__init__.py
index 50405a8..926a7dc 100644
--- a/planetstack/observer/steps/__init__.py
+++ b/planetstack/observer/steps/__init__.py
@@ -10,4 +10,5 @@
from .sync_users import SyncUsers
from .sync_roles import SyncRoles
from .sync_nodes import SyncNodes
+from .sync_images import SyncImages
from .garbage_collector import GarbageCollector
diff --git a/planetstack/observer/steps/garbage_collector.py b/planetstack/observer/steps/garbage_collector.py
index a45db8c..705ed67 100644
--- a/planetstack/observer/steps/garbage_collector.py
+++ b/planetstack/observer/steps/garbage_collector.py
@@ -227,4 +227,18 @@
Node.objects.filter(name__in=old_node_names).delete()
def gc_images(self):
- pass
+ # collect local images
+ images = Image.objects.all()
+ images_dict = {}
+ for image in images:
+ images_dict[image.name] = image
+
+ # collect glance images
+ glance_images = self.driver.shell.glance.get_images()
+ glance_images_dict = {}
+ for glance_image in glance_images:
+ glance_images_dict[glance_image['name']] = glance_image
+
+ # remove old images
+ old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys())
+ Image.objects.filter(name__in=old_image_names).delete()
diff --git a/planetstack/observer/steps/sync_images.py b/planetstack/observer/steps/sync_images.py
new file mode 100644
index 0000000..2dbd74d
--- /dev/null
+++ b/planetstack/observer/steps/sync_images.py
@@ -0,0 +1,29 @@
+import os
+import base64
+from django.db.models import F, Q
+from planetstack.config import Config
+from observer.openstacksyncstep import OpenStackSyncStep
+from core.models.image import Image
+
+class SyncImages(OpenStackSyncStep):
+ provides=[Image]
+ requested_interval=0
+
+ def fetch_pending(self):
+ images = Image.objects.all()
+ image_names = [image.name for image in images]
+
+ new_images = []
+ glance_images = self.driver.shell.glance.get_images()
+ for glance_image in glance_images:
+ if glance_image['name'] not in image_names:
+ image = Image(image_id=glance_image['id'],
+ name=glance_image['name'],
+ disk_format=glance_image['disk_format'],
+ container_format=glance_image['container_format'])
+ new_images.append(image)
+
+ return new_images
+
+ def sync_record(self, image):
+ image.save()