Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support direct upload/download to Image I/O daemon on oVirt node #35

Merged
merged 6 commits into from
May 25, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions plugins/modules/ovirt_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@
- I(True) if the disk should be activated.
- When creating disk of virtual machine it is set to I(True).
type: bool
use_proxy:
description:
- "Use Image I/O proxy when uploading or downloading disk image. Set
this to I(True) if you cannot directly connect to the oVirt node."
mnecas marked this conversation as resolved.
Show resolved Hide resolved
type: bool
extends_documentation_fragment: ovirt.ovirt.ovirt
'''

Expand Down Expand Up @@ -332,6 +337,7 @@
get_dict_of_struct,
search_by_name,
wait,
engine_supported,
)


Expand Down Expand Up @@ -366,7 +372,10 @@ def transfer(connection, module, direction, transfer_func):
time.sleep(module.params['poll_interval'])
transfer = transfer_service.get()

proxy_url = urlparse(transfer.proxy_url)
if module.params['use_proxy'] or not engine_supported(connection, '4.4'):
mnecas marked this conversation as resolved.
Show resolved Hide resolved
destination_url = urlparse(transfer.proxy_url)
else:
destination_url = urlparse(transfer.transfer_url)
context = ssl.create_default_context()
auth = module.params['auth']
if auth.get('insecure'):
Expand All @@ -375,16 +384,16 @@ 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,
transfer_connection = HTTPSConnection(
destination_url.hostname,
destination_url.port,
context=context,
)

transfer_func(
transfer_service,
proxy_connection,
proxy_url,
transfer_connection,
destination_url,
transfer.signed_ticket
mnecas marked this conversation as resolved.
Show resolved Hide resolved
)
return True
Expand Down Expand Up @@ -416,17 +425,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, destination_url, transfer_ticket):
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', destination_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:
Expand All @@ -448,14 +450,14 @@ def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket):


def upload_disk_image(connection, module):
def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket):
def _transfer(transfer_service, transfer_connection, destination_url, transfer_ticket):
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", destination_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:
Expand All @@ -464,7 +466,7 @@ def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket):
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(
Expand Down Expand Up @@ -671,6 +673,7 @@ def main():
host=dict(default=None),
wipe_after_delete=dict(type='bool', default=None),
activate=dict(default=None, type='bool'),
use_proxy=dict(default=None, type='bool'),
mnecas marked this conversation as resolved.
Show resolved Hide resolved
)
module = AnsibleModule(
argument_spec=argument_spec,
Expand Down