Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from collections import defaultdict |
| 4 | from django.db.models import F, Q |
| 5 | from planetstack.config import Config |
| 6 | from observer.openstacksyncstep import OpenStackSyncStep |
Tony Mack | 91463d9 | 2014-06-19 20:42:03 -0400 | [diff] [blame] | 7 | from core.models import Deployment |
Sapan Bhatia | b19c22a | 2014-11-11 23:05:53 -0500 | [diff] [blame] | 8 | from core.models import Image, ImageDeployment |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 9 | from util.logger import Logger, logging |
| 10 |
|
| 11 | logger = Logger(level=logging.INFO) |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 12 | |
Sapan Bhatia | b19c22a | 2014-11-11 23:05:53 -0500 | [diff] [blame] | 13 | class SyncImageDeployment(OpenStackSyncStep): |
| 14 | provides=[ImageDeployment] |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 15 | requested_interval=0 |
| 16 | |
Sapan Bhatia | b83643c | 2014-07-23 09:59:32 -0400 | [diff] [blame] | 17 | def fetch_pending(self, deleted): |
| 18 | if (deleted): |
| 19 | return [] |
Sapan Bhatia | b19c22a | 2014-11-11 23:05:53 -0500 | [diff] [blame] | 20 | # smbaker: commented out automatic creation of ImageDeployment as |
Scott Baker | b3a7bca | 2014-06-11 11:35:40 -0700 | [diff] [blame] | 21 | # as they will now be configured in GUI. Not sure if this is |
| 22 | # sufficient. |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 23 | |
Scott Baker | b3a7bca | 2014-06-11 11:35:40 -0700 | [diff] [blame] | 24 | # # ensure images are available across all deployments |
Sapan Bhatia | b19c22a | 2014-11-11 23:05:53 -0500 | [diff] [blame] | 25 | # image_deployments = ImageDeployment.objects.all() |
Scott Baker | b3a7bca | 2014-06-11 11:35:40 -0700 | [diff] [blame] | 26 | # image_deploy_lookup = defaultdict(list) |
| 27 | # for image_deployment in image_deployments: |
| 28 | # image_deploy_lookup[image_deployment.image].append(image_deployment.deployment) |
| 29 | # |
| 30 | # all_deployments = Deployment.objects.all() |
| 31 | # for image in Image.objects.all(): |
| 32 | # expected_deployments = all_deployments |
| 33 | # for expected_deployment in expected_deployments: |
| 34 | # if image not in image_deploy_lookup or \ |
| 35 | # expected_deployment not in image_deploy_lookup[image]: |
Sapan Bhatia | b19c22a | 2014-11-11 23:05:53 -0500 | [diff] [blame] | 36 | # id = ImageDeployment(image=image, deployment=expected_deployment) |
Scott Baker | b3a7bca | 2014-06-11 11:35:40 -0700 | [diff] [blame] | 37 | # id.save() |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 38 | |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 39 | # now we return all images that need to be enacted |
Sapan Bhatia | b19c22a | 2014-11-11 23:05:53 -0500 | [diff] [blame] | 40 | return ImageDeployment.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 41 | |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 42 | def sync_record(self, image_deployment): |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 43 | logger.info("Working on image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name)) |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 44 | driver = self.driver.admin_driver(deployment=image_deployment.deployment.name) |
Tony Mack | 8b85d9a | 2014-05-06 23:43:14 -0400 | [diff] [blame] | 45 | images = driver.shell.glance.get_images() |
| 46 | glance_image = None |
| 47 | for image in images: |
| 48 | if image['name'] == image_deployment.image.name: |
| 49 | glance_image = image |
| 50 | break |
| 51 | if glance_image: |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 52 | logger.info("Found image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name)) |
Tony Mack | 8b85d9a | 2014-05-06 23:43:14 -0400 | [diff] [blame] | 53 | image_deployment.glance_image_id = glance_image['id'] |
| 54 | elif image_deployment.image.path: |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [diff] [blame] | 55 | image = { |
| 56 | 'name': image_deployment.image.name, |
| 57 | 'is_public': True, |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 58 | 'disk_format': 'raw', |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [diff] [blame] | 59 | 'container_format': 'bare', |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 60 | 'file': image_deployment.image.path, |
| 61 | } |
| 62 | |
| 63 | logger.info("Creating image %s on deployment %s" % (image_deployment.image.name, image_deployment.deployment.name)) |
| 64 | |
Tony Mack | 8b85d9a | 2014-05-06 23:43:14 -0400 | [diff] [blame] | 65 | glance_image = driver.shell.glanceclient.images.create(name=image_deployment.image.name, |
| 66 | is_public=True, |
| 67 | disk_format='raw', |
| 68 | container_format='bare') |
| 69 | glance_image.update(data=open(image_deployment.image.path, 'rb')) |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 70 | |
| 71 | # While the images returned by driver.shell.glance.get_images() |
| 72 | # are dicts, the images returned by driver.shell.glanceclient.images.create |
| 73 | # are not dicts. We have to use getattr() instead of [] operator. |
| 74 | if not glance_image or not getattr(glance_image,"id",None): |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [diff] [blame] | 75 | raise Exception, "Add image failed at deployment %s" % image_deployment.deployment.name |
Scott Baker | e3969b2 | 2014-06-02 22:59:08 -0700 | [diff] [blame] | 76 | image_deployment.glance_image_id = getattr(glance_image, "id") |
Tony Mack | bf39d9f | 2014-05-06 21:42:36 -0400 | [diff] [blame] | 77 | image_deployment.save() |