-
Notifications
You must be signed in to change notification settings - Fork 91
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
Changes from all commits
f281406
af575d4
36446c7
7c90057
c6da921
0fdab53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -347,6 +347,25 @@ def _search_by_lun(disks_service, lun_id): | |
return res[0] if res else None | ||
|
||
|
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same as using {}. Please check module.warn() interface - does it work like Pyhon logging, accepting Then check how other code in this area generate messages and use the same style. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh that was your question it only accepts one string no other args. |
||
|
||
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 transfer(connection, module, direction, transfer_func): | ||
transfers_service = connection.system_service().image_transfers_service() | ||
transfer = transfers_service.add( | ||
|
@@ -366,7 +385,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'): | ||
|
@@ -375,17 +393,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: | ||
|
@@ -416,17 +428,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: | ||
|
@@ -448,14 +453,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, 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: | ||
|
@@ -464,7 +469,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( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be useful to expose the timeouts to the user so they can override the timeouts if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah thought about that but we have default timeout 3min and not sure if it would be best to wait that long
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would this to future work.