def _vhd_stream_to_vdi(self, vhd_file_parser, vdi_ref, file_size):
headers = {'Content-Type': 'application/octet-stream',
'Content-Length': '%s' % file_size}
if self.host_url.scheme == 'http':
conn = httplib.HTTPConnection(self.host_url.netloc)
elif self.host_url.scheme == 'https':
conn = httplib.HTTPSConnection(self.host_url.netloc)
vdi_import_path = utils.get_vdi_import_path(
self.session, self.task_ref, vdi_ref)
try:
conn.connect()
except Exception:
LOG.error('Failed connecting to host: %s', self.host_url.netloc)
raise exception.HostConnectionFailure(
host_netloc=self.host_url.netloc)
try:
conn.request('PUT', vdi_import_path, headers=headers)
# Send the data already processed by vhd file parser firstly;
# then send the remaining data from the stream.
conn.send(vhd_file_parser.cached_buff)
remain_size = file_size - len(vhd_file_parser.cached_buff)
file_obj = vhd_file_parser.src_file
while remain_size >= CHUNK_SIZE:
chunk = file_obj.read(CHUNK_SIZE)
remain_size -= CHUNK_SIZE
conn.send(chunk)
if remain_size != 0:
chunk = file_obj.read(remain_size)
conn.send(chunk)
except Exception:
LOG.error('Failed importing VDI from VHD stream - vdi_ref:%s',
vdi_ref)
raise exception.VdiImportFailure(vdi_ref=vdi_ref)
finally:
resp = conn.getresponse()
LOG.debug("Connection response status/reason is "
"%(status)s:%(reason)s",
{'status': resp.status, 'reason': resp.reason})
conn.close()
评论列表
文章目录