blob: 3522eca354469f12ce88fa58ed93cdea656c6c1f [file] [log] [blame]
Tony Mackbf39d9f2014-05-06 21:42:36 -04001import os
2import base64
3from collections import defaultdict
4from django.db.models import F, Q
5from planetstack.config import Config
6from observer.openstacksyncstep import OpenStackSyncStep
7from core.models.deployment import Deployment
8from core.models.image import Image, ImageDeployments
9
10class SyncImageDeployments(OpenStackSyncStep):
11 provides=[ImageDeployments]
12 requested_interval=0
13
14 def fetch_pending(self):
15 # ensure images are available across all deployments
16 image_deployments = ImageDeployments.objects.all()
17 image_deploy_lookup = defaultdict(list)
18 for image_deployment in image_deployments:
19 image_deploy_lookup[image_deployment.image].append(image_deployment.deployment)
20
21 all_deployments = Deployment.objects.all()
22 for image in Image.objects.all():
23 expected_deployments = all_deployments
24 for expected_deployment in expected_deployments:
25 if image not in image_deploy_lookup or \
26 expected_deployment not in image_deploy_lookup[image]:
27 id = ImageDeployments(image=image, deployment=expected_deployment)
28 id.save()
29
30 # now we return all images that need to be enacted
31 return ImageDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
32
33 def sync_record(self, image_deployment):
34 driver = self.driver.admin_driver(deployment=image_deployment.deployment.name)
Tony Mack8b85d9a2014-05-06 23:43:14 -040035 images = driver.shell.glance.get_images()
36 glance_image = None
37 for image in images:
38 if image['name'] == image_deployment.image.name:
39 glance_image = image
40 break
41 if glance_image:
42 image_deployment.glance_image_id = glance_image['id']
43 elif image_deployment.image.path:
Scott Baker3949bc62014-05-12 10:42:16 -070044 image = {
45 'name': image_deployment.image.name,
46 'is_public': True,
47 'disk_format': 'raw',
48 'container_format': 'bare',
49 'file': image_deployment.image.path,
50 }
Tony Mack8b85d9a2014-05-06 23:43:14 -040051 glance_image = driver.shell.glanceclient.images.create(name=image_deployment.image.name,
52 is_public=True,
53 disk_format='raw',
54 container_format='bare')
55 glance_image.update(data=open(image_deployment.image.path, 'rb'))
Scott Baker3949bc62014-05-12 10:42:16 -070056
57 if not glance_image or not glance_image.get('id'):
58 raise Exception, "Add image failed at deployment %s" % image_deployment.deployment.name
59 image_deployment.glance_image_id = glance_image['id']
Tony Mackbf39d9f2014-05-06 21:42:36 -040060 image_deployment.save()