Exemplo n.º 1
0
def set_bundle_permissions(new_permissions):
    # Check if current user has permission to set bundle permissions
    check_bundles_have_all_permission(
        local.model, request.user, [p['object_uuid'] for p in new_permissions]
    )
    # Sequentially set bundle permissions
    for p in new_permissions:
        bundle = local.model.get_bundle(p['object_uuid'])
        bundle_util.check_bundle_not_frozen(bundle)
        local.model.set_group_bundle_permission(p['group_uuid'], p['object_uuid'], p['permission'])
Exemplo n.º 2
0
def _update_bundles():
    """
    Bulk update bundles.
    """
    bundle_updates = (BundleSchema(
        strict=True, many=True,
        dump_only=BUNDLE_UPDATE_RESTRICTED_FIELDS).load(request.json,
                                                        partial=True).data)

    # Check permissions
    bundle_uuids = [b.pop('uuid') for b in bundle_updates]
    check_bundles_have_all_permission(local.model, request.user, bundle_uuids)
    bundles = local.model.batch_get_bundles(uuid=bundle_uuids)
    for bundle, update in zip(bundles, bundle_updates):
        if "frozen" not in update:
            bundle_util.check_bundle_not_frozen(bundle)
        else:
            # If we're freezing or unfreezing the bundle, check that
            # the bundle is in a final state.
            # If we're freezing, additionally check that the bundle is not already frozen.
            bundle_util.check_bundle_freezable(bundle)
            if update["frozen"]:
                bundle_util.check_bundle_not_frozen(bundle)

    # Update bundles
    for bundle, update in zip(bundles, bundle_updates):
        local.model.update_bundle(bundle, update)

    # Get updated bundles
    bundles_dict = get_bundle_infos(bundle_uuids)
    # Create list of bundles in original order
    # Need to check if the UUID is in the dict, since there is a chance that a bundle is deleted
    # right after being updated.
    updated_bundles = [
        bundles_dict[uuid] for uuid in bundle_uuids if uuid in bundles_dict
    ]

    return BundleSchema(many=True).dump(updated_bundles).data
Exemplo n.º 3
0
def delete_bundles(uuids, force, recursive, data_only, dry_run):
    """
    Delete the bundles specified by |uuids|.
    If |force|, allow deletion of bundles that have descendants or that appear across multiple worksheets.
    If |recursive|, add all bundles downstream too.
    If |data_only|, only remove from the bundle store, not the bundle metadata.
    """
    relevant_uuids = local.model.get_self_and_descendants(uuids, depth=sys.maxsize)
    if not recursive:
        # If any descendants exist, then we only delete uuids if force = True.
        if (not force) and set(uuids) != set(relevant_uuids):
            relevant = local.model.batch_get_bundles(uuid=(set(relevant_uuids) - set(uuids)))
            raise UsageError(
                'Can\'t delete bundles %s because the following bundles depend on them:\n  %s'
                % (' '.join(uuids), '\n  '.join(bundle.simple_str() for bundle in relevant))
            )
        relevant_uuids = uuids
    check_bundles_have_all_permission(local.model, request.user, relevant_uuids)

    # Make sure we don't delete bundles which are active.
    bundles = local.model.batch_get_bundles(uuid=uuids)
    states = [bundle.state for bundle in bundles]
    logger.debug('delete states: %s', states)
    active_uuids = [uuid for (uuid, state) in zip(uuids, states) if state in State.ACTIVE_STATES]
    logger.debug('delete actives: %s', active_uuids)
    if len(active_uuids) > 0:
        raise UsageError(
            'Can\'t delete bundles: %s. ' % (' '.join(active_uuids))
            + 'For run bundles, kill them first. '
            + 'Bundles stuck not running will eventually '
            + 'automatically be moved to a state where they '
            + 'can be deleted.'
        )

    # Make sure we don't delete frozen bundles
    for bundle in bundles:
        bundle_util.check_bundle_not_frozen(bundle)

    # Make sure that bundles are not referenced in multiple places (otherwise, it's very dangerous)
    result = local.model.get_all_host_worksheet_uuids(relevant_uuids)
    for uuid, host_worksheet_uuids in result.items():
        worksheets = local.model.batch_get_worksheets(fetch_items=False, uuid=host_worksheet_uuids)
        frozen_worksheets = [worksheet for worksheet in worksheets if worksheet.frozen]
        if len(frozen_worksheets) > 0:
            raise UsageError(
                "Can't delete bundle %s because it appears in frozen worksheets "
                "(need to delete worksheet first):\n  %s"
                % (uuid, '\n  '.join(worksheet.simple_str() for worksheet in frozen_worksheets))
            )
        if not force and len(host_worksheet_uuids) > 1:
            raise UsageError(
                "Can't delete bundle %s because it appears in multiple worksheets "
                "(--force to override):\n  %s"
                % (uuid, '\n  '.join(worksheet.simple_str() for worksheet in worksheets))
            )

    # Delete the actual bundle
    if not dry_run:
        if data_only:
            # Just remove references to the data hashes
            local.model.remove_data_hash_references(relevant_uuids)
        else:
            # Actually delete the bundle
            local.model.delete_bundles(relevant_uuids)

        # Update user statistics
        local.model.update_user_disk_used(request.user.user_id)

    # Delete the data.
    bundle_link_urls = local.model.get_bundle_metadata(relevant_uuids, "link_url")
    for uuid in relevant_uuids:
        bundle_link_url = bundle_link_urls.get(uuid)
        if bundle_link_url:
            # Don't physically delete linked bundles.
            pass
        else:
            bundle_location = local.bundle_store.get_bundle_location(uuid)
            if os.path.lexists(bundle_location):
                local.bundle_store.cleanup(uuid, dry_run)

    return relevant_uuids