import os
import base64
from collections import defaultdict
from django.db.models import F, Q
from planetstack.config import Config
from observer.openstacksyncstep import OpenStackSyncStep
from core.models.deployment import Deployment
from core.models.image import Image, ImageDeployments

class SyncImageDeployments(OpenStackSyncStep):
    provides=[ImageDeployments]
    requested_interval=0

    def fetch_pending(self):
        # ensure images are available across all deployments
        image_deployments = ImageDeployments.objects.all()
        image_deploy_lookup = defaultdict(list)
        for image_deployment in image_deployments:
            image_deploy_lookup[image_deployment.image].append(image_deployment.deployment)
        
        all_deployments = Deployment.objects.all() 
        for image in Image.objects.all():
            expected_deployments = all_deployments
            for expected_deployment in expected_deployments:
                if image not in image_deploy_lookup or \
                  expected_deployment not in image_deploy_lookup[image]:
                    id = ImageDeployments(image=image, deployment=expected_deployment)
                    id.save()
            
        # now we return all images that need to be enacted
        return ImageDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) 
                      
    def sync_record(self, image_deployment):
        driver = self.driver.admin_driver(deployment=image_deployment.deployment.name)
        image = {
            'name': image_deployment.image.name,
            'is_public': True,
            'disk_format': 'raw', 
            'container_format': 'bare',
            'file': image_deployment.image.path, 
        }  
        glance_image = driver.shell.glance.add_image(image)  
        image_deployment.glance_image_id = glance_image['id']
        image_deployment.save()

@@ -1529,75 +1529,523 @@
 mage
+s
  = 
-%7B%0A            'name': image_deployment.image.name,%0A            '
+driver.shell.glance.get_images()%0A        glance_image = None%0A        for image in images:%0A            if image%5B'name'%5D == image_deployment.image.name:%0A                glance_image = image%0A                break%0A        if glance_image:%0A            image_deployment.glance_image_id = glance_image%5B'id'%5D%0A        elif image_deployment.image.path:%0A            glance_image = driver.shell.glanceclient.images.create(name=image_deployment.image.name,%0A                                                                   
 is_p
@@ -2049,19 +2049,17 @@
 s_public
-': 
+=
 True,%0A  
@@ -2068,17 +2068,71 @@
         
-'
+                                                       
 disk_for
@@ -2138,18 +2138,15 @@
 rmat
-': 
+=
 'raw',
- 
 %0A   
@@ -2154,17 +2154,71 @@
         
-'
+                                                       
 containe
@@ -2229,18 +2229,16 @@
 rmat
-': 
+=
 'bare'
-,
+)
 %0A   
@@ -2250,16 +2250,38 @@
     
-'file': 
+glance_image.update(data=open(
 imag
@@ -2304,16 +2304,24 @@
 ge.path,
+ 'rb'))%0A
  %0A      
@@ -2325,75 +2325,173 @@
     
- %7D
   
-%0A
    
-     glance_image = driver.shell.glance.add_
+if not glance_image or not glance_image.get('id'): %0A                raise Exception, %22Add 
 image
-(image)
+ failed at deployment %25s%22 %25 image_deployment.deployment.name%0A  
   
-%0A
     

use python-glanceclient intead of glance.client to upload image