diff options
| author | Brian Waldon <brian.waldon@rackspace.com> | 2011-06-13 12:54:43 -0400 |
|---|---|---|
| committer | Brian Waldon <brian.waldon@rackspace.com> | 2011-06-13 12:54:43 -0400 |
| commit | 7c68bb817224bb608e2d0c92e1b00384ca7284df (patch) | |
| tree | e0db8a2570ab9e9c84909f2cea1275627dce3279 /nova/image | |
| parent | 8cc17d57a9645c9bed6eebe5d6b3bbc3ffdea13e (diff) | |
| parent | 91e34d37d2907295e892e96ca2c3039c7fbe14bf (diff) | |
merging trunk
Diffstat (limited to 'nova/image')
| -rw-r--r-- | nova/image/__init__.py | 7 | ||||
| -rw-r--r-- | nova/image/glance.py | 37 |
2 files changed, 37 insertions, 7 deletions
diff --git a/nova/image/__init__.py b/nova/image/__init__.py index 93d83df24..a27d649d4 100644 --- a/nova/image/__init__.py +++ b/nova/image/__init__.py @@ -22,6 +22,7 @@ import nova from nova import exception from nova import utils from nova import flags +from nova.image import glance as glance_image_service FLAGS = flags.FLAGS @@ -48,6 +49,8 @@ def get_default_image_service(): return ImageService() +# FIXME(sirp): perhaps this should be moved to nova/images/glance so that we +# keep Glance specific code together for the most part def get_glance_client(image_href): """Get the correct glance client and id for the given image_href. @@ -62,7 +65,9 @@ def get_glance_client(image_href): """ image_href = image_href or 0 if str(image_href).isdigit(): - glance_client = GlanceClient(FLAGS.glance_host, FLAGS.glance_port) + glance_host, glance_port = \ + glance_image_service.pick_glance_api_server() + glance_client = GlanceClient(glance_host, glance_port) return (glance_client, int(image_href)) try: diff --git a/nova/image/glance.py b/nova/image/glance.py index 61308431d..6e058ab2f 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -20,6 +20,7 @@ from __future__ import absolute_import import datetime +import random from glance.common import exception as glance_exception @@ -39,6 +40,21 @@ FLAGS = flags.FLAGS GlanceClient = utils.import_class('glance.client.Client') +def pick_glance_api_server(): + """Return which Glance API server to use for the request + + This method provides a very primitive form of load-balancing suitable for + testing and sandbox environments. In production, it would be better to use + one IP and route that to a real load-balancer. + + Returns (host, port) + """ + host_port = random.choice(FLAGS.glance_api_servers) + host, port_str = host_port.split(':') + port = int(port_str) + return host, port + + class GlanceImageService(service.BaseImageService): """Provides storage and retrieval of disk image objects within Glance.""" @@ -51,12 +67,21 @@ class GlanceImageService(service.BaseImageService): GLANCE_ONLY_ATTRS def __init__(self, client=None): - # FIXME(sirp): can we avoid dependency-injection here by using - # stubbing out a fake? - if client is None: - self.client = GlanceClient(FLAGS.glance_host, FLAGS.glance_port) - else: - self.client = client + self._client = client + + def _get_client(self): + # NOTE(sirp): we want to load balance each request across glance + # servers. Since GlanceImageService is a long-lived object, `client` + # is made to choose a new server each time via this property. + if self._client is not None: + return self._client + glance_host, glance_port = pick_glance_api_server() + return GlanceClient(glance_host, glance_port) + + def _set_client(self, client): + self._client = client + + client = property(_get_client, _set_client) def index(self, context, filters=None, marker=None, limit=None): """Calls out to Glance for a list of images available.""" |
