Пример #1
0
def create_transfer(connection, backup_uuid, disk):
    progress("Creating image transfer for disk %s" % disk.id)
    transfers_service = connection.system_service().image_transfers_service()

    transfer = transfers_service.add(
        types.ImageTransfer(
            disk=disk,
            # required for when downloading backup disks
            backup=types.Backup(id=backup_uuid),
            direction=types.ImageTransferDirection.DOWNLOAD,
            # required for when downloading backup disks
            format=types.DiskFormat.RAW,
        ))

    progress("Waiting until transfer %s will be ready" % transfer.id)

    transfer_service = transfers_service.image_transfer_service(transfer.id)

    while transfer.phase == types.ImageTransferPhase.INITIALIZING:
        time.sleep(1)
        transfer = transfer_service.get()

    progress("Image transfer %s is ready" % transfer.id)
    progress("Transfer url: %s" % transfer.transfer_url)

    return transfer
def start_backup(connection, args):
    system_service = connection.system_service()
    vm_service = system_service.vms_service().vm_service(id=args.vm_uuid)
    backups_service = vm_service.backups_service()

    if args.disk_uuid:
        disks = [types.Disk(id=disk_id) for disk_id in args.disk_uuid]
    else:
        disks = get_vm_disks(connection, args.vm_uuid)

    backup = backups_service.add(
        types.Backup(disks=disks,
                     from_checkpoint_id=args.from_checkpoint_uuid))

    progress("Waiting until backup %s is ready" % backup.id)

    backup_service = backups_service.backup_service(backup.id)

    while backup.phase != types.BackupPhase.READY:
        time.sleep(1)
        try:
            backup = backup_service.get()
        except sdk.NotFoundError:
            raise RuntimeError("Backup {} failed".format(backup.id))

    if backup.to_checkpoint_id is not None:
        progress("Created checkpoint %r (to use in --from-checkpoint-uuid "
                 "for the next incremental backup)" % backup.to_checkpoint_id)

    return backup
Пример #3
0
def test_incremental_backup_vm2(engine_api):
    engine = engine_api.system_service()
    disks_service = engine.disks_service()
    disk2 = disks_service.list(search='name={}'.format(DISK2_NAME))[0]
    vm2_backups_service = test_utils.get_vm_service(
        engine, VM2_NAME).backups_service()
    created_checkpoint_id = None

    # The first iteration will be a full VM backup (from_checkpoint_id=None)
    # and the second iteration will be an incremental VM backup.
    for _ in range(2):
        correlation_id = 'test_incremental_backup'
        backup = vm2_backups_service.add(
            types.Backup(disks=[types.Disk(id=disk2.id)],
                         from_checkpoint_id=created_checkpoint_id),
            query={'correlation_id': correlation_id})

        backup_service = vm2_backups_service.backup_service(backup.id)
        assertions.assert_true_within_long(
            lambda: backup_service.get().phase == types.BackupPhase.READY,
            allowed_exceptions=[sdk4.NotFoundError])

        backup = backup_service.get()
        created_checkpoint_id = backup.to_checkpoint_id

        backup_service.finalize()

        assertions.assert_true_within_long(
            lambda: len(vm2_backups_service.list()) == 0)
        assertions.assert_true_within_long(lambda: disks_service.disk_service(
            disk2.id).get().status == types.DiskStatus.OK)
Пример #4
0
def perform_vm_backup(
    vm_backup_service,
    disks_service,
    disk,
    from_checkpoint_id=None,
    correlation_id=None,
):
    backup = vm_backup_service.add(
        types.Backup(
            disks=[types.Disk(id=disk.id)],
            from_checkpoint_id=from_checkpoint_id,
        ),
        query={'correlation_id': correlation_id},
    )

    backup_service = vm_backup_service.backup_service(backup.id)
    assert assert_utils.equals_within_long(
        lambda: backup_service.get().phase,
        types.BackupPhase.READY,
        allowed_exceptions=[sdk4.NotFoundError],
    )

    backup = backup_service.get()
    created_checkpoint_id = backup.to_checkpoint_id

    backup_service.finalize()

    assert assert_utils.equals_within_long(lambda: backup_service.get().phase,
                                           types.BackupPhase.SUCCEEDED)
    assert assert_utils.equals_within_long(
        lambda: disks_service.disk_service(disk.id).get().status,
        types.DiskStatus.OK,
    )

    return created_checkpoint_id
Пример #5
0
def perform_vm_backup(vm_backup_service,
                      disks_service,
                      disk,
                      from_checkpoint_id=None,
                      correlation_id=None):
    backup = vm_backup_service.add(types.Backup(
        disks=[types.Disk(id=disk.id)], from_checkpoint_id=from_checkpoint_id),
                                   query={'correlation_id': correlation_id})

    backup_service = vm_backup_service.backup_service(backup.id)
    assertions.assert_true_within_long(
        lambda: backup_service.get().phase == types.BackupPhase.READY,
        allowed_exceptions=[sdk4.NotFoundError])

    backup = backup_service.get()
    created_checkpoint_id = backup.to_checkpoint_id

    backup_service.finalize()

    assertions.assert_true_within_long(
        lambda: len(vm_backup_service.list()) == 0)
    assertions.assert_true_within_long(lambda: disks_service.disk_service(
        disk.id).get().status == types.DiskStatus.OK)

    return created_checkpoint_id
def create_transfer(connection, backup_uuid, disk):
    progress("Creating image transfer for disk %s" % disk.id)
    transfers_service = connection.system_service().image_transfers_service()

    transfer = transfers_service.add(
        types.ImageTransfer(
            disk=disk,
            # required for when downloading backup disks
            backup=types.Backup(id=backup_uuid),
            direction=types.ImageTransferDirection.DOWNLOAD,
            # required for when downloading backup disks
            format=types.DiskFormat.RAW,
        ))

    progress("Waiting until transfer %s will be ready" % transfer.id)

    transfer_service = transfers_service.image_transfer_service(transfer.id)

    while transfer.phase == types.ImageTransferPhase.INITIALIZING:
        time.sleep(1)
        transfer = transfer_service.get()

    try:
        transfer = transfer_service.get()
    except sdk.NotFoundError:
        # The system has removed the transfer.
        raise RuntimeError("transfer %s was removed" % transfer.id)

    if transfer.phase != types.ImageTransferPhase.TRANSFERRING:
        progress("Canceling transfer %s" % transfer.id)
        transfer_service.cancel()
        raise RuntimeError(
            "transfer {} initialization failed: {} != {}".format(
                transfer.id, transfer.phase,
                types.ImageTransferPhase.TRANSFERRING))

    if transfer.transfer_url is None:
        progress("Canceling transfer %s" % transfer.id)
        transfer_service.cancel()
        raise RuntimeError("Transfer {} missing transfer_url: {}".format(
            transfer.id, transfer.transfer_url))

    progress("Image transfer %s is ready" % transfer.id)
    progress("Transfer url: %s" % transfer.transfer_url)
    return transfer
Пример #7
0
def download_disk(connection,
                  backup_uuid,
                  disk,
                  disk_path,
                  args,
                  incremental=False):
    progress("Creating image transfer for disk %s" % disk.id)
    transfer = imagetransfer.create_transfer(
        connection,
        disk,
        types.ImageTransferDirection.DOWNLOAD,
        backup=types.Backup(id=backup_uuid))
    try:
        progress("Image transfer %s is ready" % transfer.id)
        download_url = transfer.transfer_url

        extra_args = {}

        parameters = inspect.signature(client.download).parameters

        # Use multiple workers to speed up the download.
        if "max_workers" in parameters:
            extra_args["max_workers"] = args.max_workers

        # Use proxy_url if available. Download will use proxy_url if
        # transfer_url is not available.
        if "proxy_url" in parameters:
            extra_args["proxy_url"] = transfer.proxy_url

        with client.ProgressBar() as pb:
            client.download(download_url,
                            disk_path,
                            args.cafile,
                            incremental=incremental,
                            secure=args.secure,
                            buffer_size=args.buffer_size,
                            progress=pb,
                            **extra_args)
    finally:
        progress("Finalizing image transfer")
        imagetransfer.finalize_transfer(connection, transfer, disk)
Пример #8
0
def start_backup(connection, args):
    system_service = connection.system_service()
    vm_service = system_service.vms_service().vm_service(id=args.vm_uuid)
    backups_service = vm_service.backups_service()

    if args.disk_uuid:
        disks = args.disk_uuid
    else:
        disks = get_vm_disks(connection, args.vm_uuid)

    backup = backups_service.add(types.Backup(disks=disks))

    progress("Waiting until backup %s is ready" % backup.id)

    backup_service = backups_service.backup_service(backup.id)

    while backup.phase != types.BackupPhase.READY:
        time.sleep(1)
        try:
            backup = backup_service.get()
        except sdk.NotFoundError:
            raise RuntimeError("Backup {} failed".format(backup.id))

    return backup