~ubuntu-branches/ubuntu/vivid/ironic/vivid-updates

« back to all changes in this revision

Viewing changes to ironic/common/glance_service/service_utils.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-01-05 12:21:37 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20150105122137-171bqrdpcxqipunk
Tags: 2015.1~b1-0ubuntu1
* New upstream beta release:
  - d/control: Align version requirements with upstream release.
* d/watch: Update uversionmangle to deal with kilo beta versioning
  changes.
* d/control: Bumped Standards-Version to 3.9.6, no changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#    under the License.
16
16
 
17
17
import copy
 
18
import itertools
18
19
import logging
 
20
import random
19
21
 
20
22
from oslo.config import cfg
 
23
from oslo.serialization import jsonutils
21
24
from oslo.utils import timeutils
22
25
import six
23
26
import six.moves.urllib.parse as urlparse
24
27
 
25
28
from ironic.common import exception
26
 
from ironic.openstack.common import jsonutils
27
29
 
28
30
 
29
31
CONF = cfg.CONF
30
32
LOG = logging.getLogger(__name__)
31
33
 
 
34
_GLANCE_API_SERVER = None
 
35
""" iterator that cycles (indefinitely) over glance API servers. """
 
36
 
32
37
 
33
38
def generate_glance_url():
34
39
    """Generate the URL to glance."""
105
110
    return output
106
111
 
107
112
 
 
113
def _get_api_server_iterator():
 
114
    """Return iterator over shuffled API servers.
 
115
 
 
116
    Shuffle a list of CONF.glance.glance_api_servers and return an iterator
 
117
    that will cycle through the list, looping around to the beginning if
 
118
    necessary.
 
119
 
 
120
    If CONF.glance.glance_api_servers isn't set, we fall back to using this
 
121
    as the server: CONF.glance.glance_host:CONF.glance.glance_port.
 
122
 
 
123
    :returns: iterator that cycles (indefinitely) over shuffled glance API
 
124
              servers. The iterator returns tuples of (host, port, use_ssl).
 
125
    """
 
126
    api_servers = []
 
127
 
 
128
    configured_servers = (CONF.glance.glance_api_servers or
 
129
                          ['%s:%s' % (CONF.glance.glance_host,
 
130
                                      CONF.glance.glance_port)])
 
131
    for api_server in configured_servers:
 
132
        if '//' not in api_server:
 
133
            api_server = '%s://%s' % (CONF.glance.glance_protocol, api_server)
 
134
        url = urlparse.urlparse(api_server)
 
135
        port = url.port or 80
 
136
        host = url.netloc.split(':', 1)[0]
 
137
        use_ssl = (url.scheme == 'https')
 
138
        api_servers.append((host, port, use_ssl))
 
139
    random.shuffle(api_servers)
 
140
    return itertools.cycle(api_servers)
 
141
 
 
142
 
108
143
def _get_api_server():
109
 
    """Shuffle a list of CONF.glance_api_servers and return an iterator
110
 
    that will cycle through the list, looping around to the beginning
111
 
    if necessary.
 
144
    """Return a Glance API server.
 
145
 
 
146
    :returns: for an API server, the tuple (host-or-IP, port, use_ssl), where
 
147
        use_ssl is True to use the 'https' scheme, and False to use 'http'.
112
148
    """
113
 
    api_server = CONF.glance.glance_api_servers or \
114
 
        CONF.glance.glance_host + ':' + str(CONF.glance.glance_port)
115
 
    if '//' not in api_server:
116
 
        api_server = CONF.glance.glance_protocol + '://' + api_server
117
 
    url = urlparse.urlparse(api_server)
118
 
    port = url.port or 80
119
 
    host = url.netloc.split(':', 1)[0]
120
 
    use_ssl = (url.scheme == 'https')
121
 
    return host, port, use_ssl
 
149
    global _GLANCE_API_SERVER
 
150
 
 
151
    if not _GLANCE_API_SERVER:
 
152
        _GLANCE_API_SERVER = _get_api_server_iterator()
 
153
    return _GLANCE_API_SERVER.next()
122
154
 
123
155
 
124
156
def parse_image_ref(image_href):
125
157
    """Parse an image href into composite parts.
126
158
 
127
159
    :param image_href: href of an image
128
 
    :returns: a tuple of the form (image_id, host, port)
 
160
    :returns: a tuple of the form (image_id, host, port, use_ssl)
129
161
 
130
162
    :raises ValueError
131
163
    """