Skip to content

Commit 0f3ed0d

Browse files
committed
Allow customizable HTTP block size when reading from remote OVA
1 parent 7900a80 commit 0f3ed0d

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎samples/deploy_ova.py‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import sys
1818
import tarfile
1919
import time
20+
import http.client
21+
from urllib.parse import urlparse
2022

2123
from threading import Timer
2224
from six.moves.urllib.request import Request, urlopen
@@ -31,7 +33,8 @@
3133
def main():
3234
parser = cli.Parser()
3335
parser.add_optional_arguments(cli.Argument.OVA_PATH, cli.Argument.DATACENTER_NAME,
34-
cli.Argument.RESOURCE_POOL, cli.Argument.DATASTORE_NAME)
36+
cli.Argument.RESOURCE_POOL, cli.Argument.DATASTORE_NAME,
37+
cli.Argument.HTTP_BLOCK_SIZE)
3538
args = parser.get_args()
3639
si = service_instance.connect(args)
3740

@@ -50,7 +53,11 @@ def main():
5053
else:
5154
datastore = get_largest_free_ds(datacenter)
5255

53-
ovf_handle = OvfHandler(args.ova_path)
56+
http_block_size = None
57+
if args.http_block_size:
58+
http_block_size = int(args.http_block_size)
59+
60+
ovf_handle = OvfHandler(args.ova_path, http_block_size)
5461

5562
ovf_manager = si.content.ovfManager
5663
# CreateImportSpecParams can specify many useful things such as
@@ -183,7 +190,7 @@ class OvfHandler(object):
183190
It processes the tarfile, matches disk keys to files and
184191
uploads the disks, while keeping the progress up to date for the lease.
185192
"""
186-
def __init__(self, ovafile):
193+
def __init__(self, ovafile, http_block_size=None):
187194
"""
188195
Performs necessary initialization, opening the OVA file,
189196
processing the files and reading the embedded ovf file.
@@ -194,6 +201,10 @@ def __init__(self, ovafile):
194201
self.tarfile.getnames()))[0]
195202
ovffile = self.tarfile.extractfile(ovffilename)
196203
self.descriptor = ovffile.read().decode()
204+
if http_block_size is None:
205+
self.http_block_size = 10485760 # 10MB by default
206+
else:
207+
self.http_block_size = http_block_size
197208

198209
def _create_file_handle(self, entry):
199210
"""
@@ -264,8 +275,23 @@ def upload_disk(self, file_item, lease, host):
264275
ssl_context = ssl._create_unverified_context()
265276
else:
266277
ssl_context = None
267-
req = Request(url, ovffile, headers)
268-
urlopen(req, context=ssl_context)
278+
279+
method = 'POST'
280+
if not device_url.disk:
281+
headers['Overwrite'] = 't'
282+
method = 'PUT'
283+
284+
p = urlparse(url)
285+
286+
if p.scheme == 'http':
287+
conn = http.client.HTTPConnection(p.netloc,
288+
blocksize=self.http_block_size)
289+
else:
290+
conn = http.client.HTTPSConnection(p.netloc, context=ssl_context,
291+
blocksize=self.http_block_size)
292+
conn.request(method, p.path, body=ovffile, headers=headers)
293+
conn.close()
294+
269295

270296
def start_timer(self):
271297
"""

‎samples/tools/cli.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ def __init__(self):
311311
'choices': ['physicalMode', 'virtualMode'],
312312
'help': 'See vim.vm.device.VirtualDiskOption.CompatibilityMode documentation.'}}
313313

314+
HTTP_BLOCK_SIZE = {
315+
'name_or_flags': ['--http-block-size'],
316+
'options': {'action': 'store', 'help': 'Block size to use when reading OVA files from http source, in bytes'}
317+
}
314318
ISO = {
315319
'name_or_flags': ['--iso'],
316320
'options': {'action': 'store',

0 commit comments

Comments
 (0)