def transfer(connection, module, direction, transfer_func): transfers_service = connection.system_service().image_transfers_service() transfer = transfers_service.add( otypes.ImageTransfer( image=otypes.Image( id=module.params['id'], ), direction=direction, ) ) transfer_service = transfers_service.image_transfer_service(transfer.id) try: # After adding a new transfer for the disk, the transfer's status will be INITIALIZING. # Wait until the init phase is over. The actual transfer can start when its status is "Transferring". while transfer.phase == otypes.ImageTransferPhase.INITIALIZING: time.sleep(module.params['poll_interval']) transfer = transfer_service.get() context = ssl.create_default_context() auth = module.params['auth'] if auth.get('insecure'): context.check_hostname = False context.verify_mode = ssl.CERT_NONE elif auth.get('ca_file'): context.load_verify_locations(cafile=auth.get('ca_file')) transfer_connection, transfer_url = create_transfer_connection(module, transfer, context) transfer_func( transfer_service, transfer_connection, transfer_url, ) return True finally: transfer_service.finalize() while transfer.phase in [ otypes.ImageTransferPhase.TRANSFERRING, otypes.ImageTransferPhase.FINALIZING_SUCCESS, ]: time.sleep(module.params['poll_interval']) transfer = transfer_service.get() if transfer.phase in [ otypes.ImageTransferPhase.UNKNOWN, otypes.ImageTransferPhase.FINISHED_FAILURE, otypes.ImageTransferPhase.FINALIZING_FAILURE, otypes.ImageTransferPhase.CANCELLED, ]: raise Exception( "Error occurred while uploading image. The transfer is in %s" % transfer.phase ) if not module.params.get('logical_unit'): disks_service = connection.system_service().disks_service() wait( service=disks_service.service(module.params['id']), condition=lambda d: d.status == otypes.DiskStatus.OK, wait=module.params['wait'], timeout=module.params['timeout'], )
) ] ) ) # Wait till the disk is up, as the transfer can't start if the # disk is locked: disk_service = disks_service.disk_service(disk.id) while disk_service.get().status != types.DiskStatus.OK: time.sleep(5) # Add a new image transfer: transfer = transfers_service.add( types.ImageTransfer( image=types.Image( id=disk.id ) ) ) # Get reference to the created transfer service: transfer_service = transfers_service.image_transfer_service(transfer.id) # After adding a new transfer for the disk, the transfer's status will be # INITIALIZING. Wait until the init phase is over. The actual transfer can # start when its status is "Transferring". while transfer_service.get().phase == types.ImageTransferPhase.INITIALIZING: time.sleep(1) # Set needed headers for uploading: upload_headers = {
format=disk_format, provisioned_size=int(props['capacity']) * 2**30, initial_size=int(props['populatedSize']), storage_domains=[types.StorageDomain( name=target_storage_domain_name)])) # Wait till the disk is up, as the transfer can't start if the # disk is locked: disk_service = disks_service.disk_service(disk.id) while disk_service.get().status != types.DiskStatus.OK: time.sleep(5) print("Creating transfer session...") # Add a new image transfer: transfer = transfers_service.add( types.ImageTransfer(image=types.Image(id=disk.id))) # Get reference to the created transfer service: transfer_service = transfers_service.image_transfer_service(transfer.id) # After adding a new transfer for the disk, the transfer's status will be # INITIALIZING. Wait until the init phase is over. The actual transfer can # start when its status is "Transferring". while transfer_service.get( ).phase == types.ImageTransferPhase.INITIALIZING: time.sleep(1) # At this stage, the SDK granted the permission to start transferring the # disk, and the user should choose its preferred tool for doing it - # regardless of the SDK. In this example, we will use Python's # httplib.HTTPSConnection for transferring the data.
def upload_disk_image(connection, module): size = os.path.getsize(module.params['image_path']) transfers_service = connection.system_service().image_transfers_service() transfer = transfers_service.add( otypes.ImageTransfer( image=otypes.Image( id=module.params['id'], ) ) ) transfer_service = transfers_service.image_transfer_service(transfer.id) try: # After adding a new transfer for the disk, the transfer's status will be INITIALIZING. # Wait until the init phase is over. The actual transfer can start when its status is "Transferring". while transfer.phase == otypes.ImageTransferPhase.INITIALIZING: time.sleep(module.params['poll_interval']) transfer = transfer_service.get() # Set needed headers for uploading: upload_headers = { 'Authorization': transfer.signed_ticket, } proxy_url = urlparse(transfer.proxy_url) context = ssl.create_default_context() auth = module.params['auth'] if auth.get('insecure'): context.check_hostname = False context.verify_mode = ssl.CERT_NONE 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, ) with open(module.params['image_path'], "rb") as disk: chunk_size = 1024 * 1024 * 8 pos = 0 while pos < size: transfer_service.extend() upload_headers['Content-Range'] = "bytes %d-%d/%d" % (pos, min(pos + chunk_size, size) - 1, size) proxy_connection.request( 'PUT', proxy_url.path, disk.read(chunk_size), headers=upload_headers, ) r = proxy_connection.getresponse() if r.status >= 400: raise Exception("Failed to upload disk image.") pos += chunk_size finally: transfer_service.finalize() while transfer.phase in [ otypes.ImageTransferPhase.TRANSFERRING, otypes.ImageTransferPhase.FINALIZING_SUCCESS, ]: time.sleep(module.params['poll_interval']) transfer = transfer_service.get() if transfer.phase in [ otypes.ImageTransferPhase.UNKNOWN, otypes.ImageTransferPhase.FINISHED_FAILURE, otypes.ImageTransferPhase.FINALIZING_FAILURE, otypes.ImageTransferPhase.CANCELLED, ]: raise Exception( "Error occured while uploading image. The transfer is in %s" % transfer.phase ) if module.params.get('logical_unit'): disks_service = connection.system_service().disks_service() wait( service=disks_service.service(module.params['id']), condition=lambda d: d.status == otypes.DiskStatus.OK, wait=module.params['wait'], timeout=module.params['timeout'], ) return True