diff options
author | Martin Nečas <necas.marty@gmail.com> | 2020-06-17 02:42:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-16 17:42:05 -0700 |
commit | 28b99ead4499c16bcab36fb4381f32251721d0fe (patch) | |
tree | 5f47c3bccdc217953c0fe0e7440c44bc696e459c | |
parent | d5b648ee41dcc888f97cf4179268a9d0d11961db (diff) | |
download | ansible-28b99ead4499c16bcab36fb4381f32251721d0fe.tar.gz |
Backport: Support direct upload/download to Image I/O daemon on oVirt node (#69691)
* ovirt_disk: fix upload/download of image
* add changelog
-rw-r--r-- | changelogs/fragments/69691-ovirt_disk-fix-upload-download-of-image.yml | 2 | ||||
-rw-r--r-- | lib/ansible/modules/cloud/ovirt/ovirt_disk.py | 55 |
2 files changed, 32 insertions, 25 deletions
diff --git a/changelogs/fragments/69691-ovirt_disk-fix-upload-download-of-image.yml b/changelogs/fragments/69691-ovirt_disk-fix-upload-download-of-image.yml new file mode 100644 index 0000000000..f85d27266c --- /dev/null +++ b/changelogs/fragments/69691-ovirt_disk-fix-upload-download-of-image.yml @@ -0,0 +1,2 @@ +bugfixes: + - "ovirt_disk: fix upload/download of images for ovirt 4.4" diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_disk.py b/lib/ansible/modules/cloud/ovirt/ovirt_disk.py index 78375c5c5b..38322eb807 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_disk.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_disk.py @@ -345,6 +345,25 @@ from ansible.module_utils.ovirt import ( ) +def create_transfer_connection(module, transfer, context, connect_timeout=10, read_timeout=60): + url = urlparse(transfer.transfer_url) + connection = HTTPSConnection( + url.netloc, context=context, timeout=connect_timeout) + try: + connection.connect() + except OSError as e: + # Typically ConnectionRefusedError or socket.gaierror. + module.warn("Cannot connect to %s, trying %s: %s" % (transfer.transfer_url, transfer.proxy_url, e)) + + url = urlparse(transfer.proxy_url) + connection = HTTPSConnection( + url.netloc, context=context, timeout=connect_timeout) + connection.connect() + + connection.sock.settimeout(read_timeout) + return connection, url + + def _search_by_lun(disks_service, lun_id): """ Find disk by LUN ID. @@ -376,7 +395,6 @@ def transfer(connection, module, direction, transfer_func): time.sleep(module.params['poll_interval']) transfer = transfer_service.get() - proxy_url = urlparse(transfer.proxy_url) context = ssl.create_default_context() auth = module.params['auth'] if auth.get('insecure'): @@ -385,17 +403,11 @@ def transfer(connection, module, direction, transfer_func): elif auth.get('ca_file'): context.load_verify_locations(cafile=auth.get('ca_file')) - proxy_connection = HTTPSConnection( - proxy_url.hostname, - proxy_url.port, - context=context, - ) - + transfer_connection, transfer_url = create_transfer_connection(module, transfer, context) transfer_func( transfer_service, - proxy_connection, - proxy_url, - transfer.signed_ticket + transfer_connection, + transfer_url, ) return True finally: @@ -426,17 +438,10 @@ def transfer(connection, module, direction, transfer_func): def download_disk_image(connection, module): - def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket): + def _transfer(transfer_service, transfer_connection, transfer_url): BUF_SIZE = 128 * 1024 - transfer_headers = { - 'Authorization': transfer_ticket, - } - proxy_connection.request( - 'GET', - proxy_url.path, - headers=transfer_headers, - ) - r = proxy_connection.getresponse() + transfer_connection.request('GET', transfer_url.path) + r = transfer_connection.getresponse() path = module.params["download_image_path"] image_size = int(r.getheader('Content-Length')) with open(path, "wb") as mydisk: @@ -458,14 +463,14 @@ def download_disk_image(connection, module): def upload_disk_image(connection, module): - def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket): + def _transfer(transfer_service, transfer_connection, transfer_url): BUF_SIZE = 128 * 1024 path = module.params['upload_image_path'] image_size = os.path.getsize(path) - proxy_connection.putrequest("PUT", proxy_url.path) - proxy_connection.putheader('Content-Length', "%d" % (image_size,)) - proxy_connection.endheaders() + transfer_connection.putrequest("PUT", transfer_url.path) + transfer_connection.putheader('Content-Length', "%d" % (image_size,)) + transfer_connection.endheaders() with open(path, "rb") as disk: pos = 0 while pos < image_size: @@ -474,7 +479,7 @@ def upload_disk_image(connection, module): if not chunk: transfer_service.pause() raise RuntimeError("Unexpected end of file at pos=%d" % pos) - proxy_connection.send(chunk) + transfer_connection.send(chunk) pos += len(chunk) return transfer( |