import os
import base64
from django.db.models import F, Q
from xos.config import Config
from observer.openstacksyncstep import OpenStackSyncStep
from core.models.image import Image

class SyncImages(OpenStackSyncStep):
    provides=[Image]
    requested_interval=0
    observes=Image

    def fetch_pending(self, deleted):
        # Images come from the back end
        # You can't delete them
        if (deleted):
            return []

        # get list of images on disk
        images_path = Config().observer_images_directory 
        available_images = {}
        for f in os.listdir(images_path):
            if os.path.isfile(os.path.join(images_path ,f)):
                available_images[f] = os.path.join(images_path ,f)

        images = Image.objects.all()
        image_names = [image.name for image in images]

        for image_name in available_images:
            #remove file extension
            clean_name = ".".join(image_name.split('.')[:-1])
            if clean_name not in image_names:
                image = Image(name=clean_name,
                              disk_format='raw',
                              container_format='bare', 
                              path = available_images[image_name])
                image.save()
       
        
        return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) 

    def sync_record(self, image):
        image.save()

import os
import base64
from django.db.models import F, Q
from xos.config import Config
from observer.openstacksyncstep import OpenStackSyncStep
from core.models.image import Image

class SyncImages(OpenStackSyncStep):
    provides=[Image]
    requested_interval=0
    observes=Image

    def fetch_pending(self, deleted):
        # Images come from the back end
        # You can't delete them
        if (deleted):
            return []

        # get list of images on disk
        images_path = Config().observer_images_directory

        available_images = {}
        if os.path.exists(images_path):
            for f in os.listdir(images_path):
                filename = os.path.join(images_path, f)
                if os.path.isfile(filename):
                    available_images[f] = filename

        images = Image.objects.all()
        image_names = [image.name for image in images]

        for image_name in available_images:
            #remove file extension
            clean_name = ".".join(image_name.split('.')[:-1])
            if clean_name not in image_names:
                image = Image(name=clean_name,
                              disk_format='raw',
                              container_format='bare', 
                              path = available_images[image_name])
                image.save()

        return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) 

    def sync_record(self, image):
        image.save()

Check the existence of the images_path
Check the existence of the images_path

ERROR:planetstack.log:[Errno 2] No such file or directory: '/opt/xos/images' BEG TRACEBACK
Traceback (most recent call last):
  File "/opt/xos/observer/event_loop.py", line 349, in sync
    failed_objects = sync_step(failed=list(self.failed_step_objects), deletion=deletion)
  File "/opt/xos/observer/openstacksyncstep.py", line 14, in __call__
    return self.call(**args)
  File "/opt/xos/observer/syncstep.py", line 97, in call
    pending = self.fetch_pending(deletion)
  File "/opt/xos/observer/steps/sync_images.py", line 22, in fetch_pending
    for f in os.listdir(images_path):
OSError: [Errno 2] No such file or directory: '/opt/xos/images'
ERROR:planetstack.log:[Errno 2] No such file or directory: '/opt/xos/images' END TRACEBACK

Signed-off-by: S.Çağlar Onur <acf5ae661bb0a9f738c88a741b1d35ac69ab5408@10ur.org>

  import os
  import base64
  from django.db.models import F, Q
  from xos.config import Config
  from observer.openstacksyncstep import OpenStackSyncStep
  from core.models.image import Image
  
  class SyncImages(OpenStackSyncStep):
      provides=[Image]
      requested_interval=0
      observes=Image
  
      def fetch_pending(self, deleted):
          # Images come from the back end
          # You can't delete them
          if (deleted):
              return []
  
          # get list of images on disk
-         images_path = Config().observer_images_directory 
+         images_path = Config().observer_images_directory
+ 
          available_images = {}
+         if os.path.exists(images_path):
-         for f in os.listdir(images_path):
+             for f in os.listdir(images_path):
-             if os.path.isfile(os.path.join(images_path ,f)):
-                 available_images[f] = os.path.join(images_path ,f)
+                 filename = os.path.join(images_path, f)
+                 if os.path.isfile(filename):
+                     available_images[f] = filename
  
          images = Image.objects.all()
          image_names = [image.name for image in images]
  
          for image_name in available_images:
              #remove file extension
              clean_name = ".".join(image_name.split('.')[:-1])
              if clean_name not in image_names:
                  image = Image(name=clean_name,
                                disk_format='raw',
                                container_format='bare', 
                                path = available_images[image_name])
                  image.save()
+ 
-        
-         
          return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) 
  
      def sync_record(self, image):
          image.save()
  
Check the existence of the images_path
## Code Before:
import os
import base64
from django.db.models import F, Q
from xos.config import Config
from observer.openstacksyncstep import OpenStackSyncStep
from core.models.image import Image

class SyncImages(OpenStackSyncStep):
    provides=[Image]
    requested_interval=0
    observes=Image

    def fetch_pending(self, deleted):
        # Images come from the back end
        # You can't delete them
        if (deleted):
            return []

        # get list of images on disk
        images_path = Config().observer_images_directory 
        available_images = {}
        for f in os.listdir(images_path):
            if os.path.isfile(os.path.join(images_path ,f)):
                available_images[f] = os.path.join(images_path ,f)

        images = Image.objects.all()
        image_names = [image.name for image in images]

        for image_name in available_images:
            #remove file extension
            clean_name = ".".join(image_name.split('.')[:-1])
            if clean_name not in image_names:
                image = Image(name=clean_name,
                              disk_format='raw',
                              container_format='bare', 
                              path = available_images[image_name])
                image.save()
       
        
        return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) 

    def sync_record(self, image):
        image.save()

## Instruction:
Check the existence of the images_path
## Code After:
import os
import base64
from django.db.models import F, Q
from xos.config import Config
from observer.openstacksyncstep import OpenStackSyncStep
from core.models.image import Image

class SyncImages(OpenStackSyncStep):
    provides=[Image]
    requested_interval=0
    observes=Image

    def fetch_pending(self, deleted):
        # Images come from the back end
        # You can't delete them
        if (deleted):
            return []

        # get list of images on disk
        images_path = Config().observer_images_directory

        available_images = {}
        if os.path.exists(images_path):
            for f in os.listdir(images_path):
                filename = os.path.join(images_path, f)
                if os.path.isfile(filename):
                    available_images[f] = filename

        images = Image.objects.all()
        image_names = [image.name for image in images]

        for image_name in available_images:
            #remove file extension
            clean_name = ".".join(image_name.split('.')[:-1])
            if clean_name not in image_names:
                image = Image(name=clean_name,
                              disk_format='raw',
                              container_format='bare', 
                              path = available_images[image_name])
                image.save()

        return Image.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) 

    def sync_record(self, image):
        image.save()
