mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
recator uploading disk and downloading (#43635)
This commit is contained in:
parent
1b54af068d
commit
c2fa0d2c4b
1 changed files with 32 additions and 42 deletions
|
@ -357,34 +357,28 @@ def transfer(connection, module, direction, transfer_func):
|
||||||
|
|
||||||
def download_disk_image(connection, module):
|
def download_disk_image(connection, module):
|
||||||
def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket):
|
def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket):
|
||||||
disks_service = connection.system_service().disks_service()
|
BUF_SIZE = 128 * 1024
|
||||||
disk = disks_service.disk_service(module.params['id']).get()
|
|
||||||
size = disk.actual_size
|
|
||||||
transfer_headers = {
|
transfer_headers = {
|
||||||
'Authorization': transfer_ticket,
|
'Authorization': transfer_ticket,
|
||||||
}
|
}
|
||||||
with open(module.params['download_image_path'], "wb") as mydisk:
|
proxy_connection.request(
|
||||||
|
'GET',
|
||||||
|
proxy_url.path,
|
||||||
|
headers=transfer_headers,
|
||||||
|
)
|
||||||
|
r = proxy_connection.getresponse()
|
||||||
|
path = module.params["download_image_path"]
|
||||||
|
image_size = int(r.getheader('Content-Length'))
|
||||||
|
with open(path, "wb") as mydisk:
|
||||||
pos = 0
|
pos = 0
|
||||||
MiB_per_request = 8
|
while pos < image_size:
|
||||||
chunk_size = 1024 * 1024 * MiB_per_request
|
to_read = min(image_size - pos, BUF_SIZE)
|
||||||
while pos < size:
|
chunk = r.read(to_read)
|
||||||
transfer_service.extend()
|
if not chunk:
|
||||||
transfer_headers['Range'] = 'bytes=%d-%d' % (pos, min(size, pos + chunk_size) - 1)
|
raise RuntimeError("Socket disconnected")
|
||||||
proxy_connection.request(
|
mydisk.write(chunk)
|
||||||
'GET',
|
pos += len(chunk)
|
||||||
proxy_url.path,
|
|
||||||
headers=transfer_headers,
|
|
||||||
)
|
|
||||||
r = proxy_connection.getresponse()
|
|
||||||
if r.status >= 300:
|
|
||||||
raise Exception("Error: %s" % r.read())
|
|
||||||
|
|
||||||
try:
|
|
||||||
mydisk.write(r.read())
|
|
||||||
except IncompleteRead as e:
|
|
||||||
mydisk.write(e.partial)
|
|
||||||
break
|
|
||||||
pos += chunk_size
|
|
||||||
return transfer(
|
return transfer(
|
||||||
connection,
|
connection,
|
||||||
module,
|
module,
|
||||||
|
@ -395,28 +389,24 @@ def download_disk_image(connection, module):
|
||||||
|
|
||||||
def upload_disk_image(connection, module):
|
def upload_disk_image(connection, module):
|
||||||
def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket):
|
def _transfer(transfer_service, proxy_connection, proxy_url, transfer_ticket):
|
||||||
|
BUF_SIZE = 128 * 1024
|
||||||
path = module.params['upload_image_path']
|
path = module.params['upload_image_path']
|
||||||
transfer_headers = {
|
|
||||||
'Authorization': transfer_ticket,
|
image_size = os.path.getsize(path)
|
||||||
}
|
proxy_connection.putrequest("PUT", proxy_url.path)
|
||||||
|
proxy_connection.putheader('Content-Length', "%d" % (image_size,))
|
||||||
|
proxy_connection.endheaders()
|
||||||
with open(path, "rb") as disk:
|
with open(path, "rb") as disk:
|
||||||
pos = 0
|
pos = 0
|
||||||
MiB_per_request = 8
|
while pos < image_size:
|
||||||
size = os.path.getsize(path)
|
to_read = min(image_size - pos, BUF_SIZE)
|
||||||
chunk_size = 1024 * 1024 * MiB_per_request
|
chunk = disk.read(to_read)
|
||||||
while pos < size:
|
if not chunk:
|
||||||
transfer_service.extend()
|
transfer_service.pause()
|
||||||
transfer_headers['Content-Range'] = "bytes %d-%d/%d" % (pos, min(pos + chunk_size, size) - 1, size)
|
raise RuntimeError("Unexpected end of file at pos=%d" % pos)
|
||||||
proxy_connection.request(
|
proxy_connection.send(chunk)
|
||||||
'PUT',
|
pos += len(chunk)
|
||||||
proxy_url.path,
|
|
||||||
disk.read(chunk_size),
|
|
||||||
headers=transfer_headers,
|
|
||||||
)
|
|
||||||
r = proxy_connection.getresponse()
|
|
||||||
if r.status >= 400:
|
|
||||||
raise Exception("Failed to upload disk image.")
|
|
||||||
pos += chunk_size
|
|
||||||
return transfer(
|
return transfer(
|
||||||
connection,
|
connection,
|
||||||
module,
|
module,
|
||||||
|
|
Loading…
Reference in a new issue