diff options
| author | Jenkins <jenkins@review.openstack.org> | 2011-11-08 21:22:54 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2011-11-08 21:22:54 +0000 |
| commit | 15076d36df0878205ec6b1a6de67d1660b054f09 (patch) | |
| tree | 96bedc4f7af8817a889db0c57487424cca451d14 /plugins | |
| parent | f60ad21da95395eeca2697a588ca312fc961af18 (diff) | |
| parent | 9a32891107dd8c8fb3ab0240a5f9d5f875953c62 (diff) | |
Merge "Switch glance XenAPI plugin to use urllib2."
Diffstat (limited to 'plugins')
| -rwxr-xr-x | plugins/xenserver/xenapi/etc/xapi.d/plugins/glance | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/glance b/plugins/xenserver/xenapi/etc/xapi.d/plugins/glance index 47052905d..1889b7e6f 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/glance +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/glance @@ -34,6 +34,7 @@ import shutil import subprocess import tempfile import time +import urllib2 import XenAPIPlugin @@ -77,29 +78,52 @@ def _download_tarball(sr_path, staging_path, image_id, glance_host, if auth_token: headers['x-auth-token'] = auth_token - conn = httplib.HTTPConnection(glance_host, glance_port) + url = "http://%(glance_host)s:%(glance_port)d/v1/images/"\ + "%(image_id)s" % locals() + + logging.debug("Downloading tarball from %(url)s" % locals()) + + request = urllib2.Request(url, headers=headers) + response = None - for count in xrange(1 + num_retries): - conn.request('GET', '/v1/images/%s' % image_id, headers=headers) - resp = conn.getresponse() - if resp.status == httplib.OK: + for try_num in xrange(1, num_retries + 2): + try: + response = urllib2.urlopen(request) break - elif resp.status == httplib.NOT_FOUND: - raise Exception("Image '%s' not found in Glance" % image_id) - elif count == num_retries: - raise Exception("Unexpected response from Glance %i" % resp.status) + + except urllib2.HTTPError, error: + if error.code == 404: + msg = "Image '%s' not found in Glance" % image_id + logging.error(msg) + raise Exception(msg) + + elif try_num == (num_retries + 1): + msg = "Unable to retrieve image after %d attempts." % try_num + logging.error(msg) + raise Exception(msg) + + except urllib2.URLError, error: + pass + + logging.error("Download attempt %d error: %s" % (try_num, error)) time.sleep(1) + if response is None: + msg = "Unable to retrieve image: %(error)s" % locals() + logging.error(msg) + raise Exception(msg) + tar_cmd = "tar -zx --directory=%(staging_path)s" % locals() tar_proc = _make_subprocess(tar_cmd, stderr=True, stdin=True) - chunk = resp.read(CHUNK_SIZE) + logging.info("Reading image data from %s" % url) + + chunk = response.read(CHUNK_SIZE) while chunk: tar_proc.stdin.write(chunk) - chunk = resp.read(CHUNK_SIZE) + chunk = response.read(CHUNK_SIZE) _finish_subprocess(tar_proc, tar_cmd) - conn.close() def _import_vhds(sr_path, staging_path, uuid_stack): |
