15
15
# under the License.
20
22
from oslo.config import cfg
23
from oslo.serialization import jsonutils
21
24
from oslo.utils import timeutils
23
26
import six.moves.urllib.parse as urlparse
25
28
from ironic.common import exception
26
from ironic.openstack.common import jsonutils
30
32
LOG = logging.getLogger(__name__)
34
_GLANCE_API_SERVER = None
35
""" iterator that cycles (indefinitely) over glance API servers. """
33
38
def generate_glance_url():
34
39
"""Generate the URL to glance."""
113
def _get_api_server_iterator():
114
"""Return iterator over shuffled API servers.
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
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.
123
:returns: iterator that cycles (indefinitely) over shuffled glance API
124
servers. The iterator returns tuples of (host, port, use_ssl).
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)
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
144
"""Return a Glance API server.
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'.
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
151
if not _GLANCE_API_SERVER:
152
_GLANCE_API_SERVER = _get_api_server_iterator()
153
return _GLANCE_API_SERVER.next()
124
156
def parse_image_ref(image_href):
125
157
"""Parse an image href into composite parts.
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)
130
162
:raises ValueError