def wrapper(*args, **kwargs):
        for mirror in RepoMirrorConfig.select():
            mirror.is_enabled = False
            mirror.save()

        func(*args, **kwargs)

        for mirror in RepoMirrorConfig.select():
            mirror.is_enabled = True
            mirror.save()
Beispiel #2
0
def get_eligible_mirrors():
    """
    Returns the RepoMirrorConfig that are ready to run now.

    This includes those that are:
    1. Not currently syncing but whose start time is in the past
    2. Status of "sync now"
    3. Currently marked as syncing but whose expiration time is in the past
    """
    now = datetime.utcnow()
    immediate_candidates_filter = (
        RepoMirrorConfig.sync_status == RepoMirrorStatus.SYNC_NOW) & (
            RepoMirrorConfig.sync_expiration_date >> None)

    ready_candidates_filter = (
        (RepoMirrorConfig.sync_start_date <= now)
        & (RepoMirrorConfig.sync_retries_remaining > 0)
        & (RepoMirrorConfig.sync_status != RepoMirrorStatus.SYNCING)
        & (RepoMirrorConfig.sync_expiration_date >> None)
        & (RepoMirrorConfig.is_enabled == True))

    expired_candidates_filter = (
        (RepoMirrorConfig.sync_start_date <= now)
        & (RepoMirrorConfig.sync_retries_remaining > 0)
        & (RepoMirrorConfig.sync_status == RepoMirrorStatus.SYNCING)
        & (RepoMirrorConfig.sync_expiration_date <= now)
        & (RepoMirrorConfig.is_enabled == True))

    return (RepoMirrorConfig.select().join(Repository).where(
        Repository.state == RepositoryState.MIRROR).where(
            immediate_candidates_filter | ready_candidates_filter
            | expired_candidates_filter).order_by(
                RepoMirrorConfig.sync_start_date.asc()))
Beispiel #3
0
def get_mirror(repository):
    """
    Return the RepoMirrorConfig associated with the given Repository, or None if it doesn't exist.
    """
    try:
        return (RepoMirrorConfig.select(
            RepoMirrorConfig, User,
            RepoMirrorRule).join(User, JOIN.LEFT_OUTER).switch(
                RepoMirrorConfig).join(RepoMirrorRule).where(
                    RepoMirrorConfig.repository == repository).get())
    except RepoMirrorConfig.DoesNotExist:
        return None
def disable_existing_mirrors():
    mirrors = RepoMirrorConfig.select().execute()
    for mirror in mirrors:
        mirror.is_enabled = False
        mirror.save()
Beispiel #5
0
def ask_disable_namespace(username, queue_name):
    user = model.user.get_namespace_user(username)
    if user is None:
        raise Exception("Unknown user or organization %s" % username)

    if not user.enabled:
        print("NOTE: Namespace %s is already disabled" % username)

    queue_prefix = "%s/%s/%%" % (queue_name, username)
    existing_queue_item_count = (QueueItem.select().where(
        QueueItem.queue_name**queue_prefix).where(
            QueueItem.available == 1,
            QueueItem.retries_remaining > 0,
            QueueItem.processing_expires > datetime.now(),
        ).count())

    repository_trigger_count = (
        RepositoryBuildTrigger.select().join(Repository).where(
            Repository.namespace_user == user).count())

    print("=============================================")
    print("For namespace %s" % username)
    print("=============================================")

    print("User %s has email address %s" % (username, user.email))
    print("User %s has %s queued builds in their namespace" %
          (username, existing_queue_item_count))
    print("User %s has %s build triggers in their namespace" %
          (username, repository_trigger_count))

    confirm_msg = (
        "Would you like to disable this user and delete their triggers and builds? [y/N]> "
    )
    letter = str(input(confirm_msg))
    if letter.lower() != "y":
        print("Action canceled")
        return

    print("=============================================")

    triggers = []
    count_removed = 0
    with db_transaction():
        user.enabled = False
        user.save()

        repositories_query = Repository.select().where(
            Repository.namespace_user == user)
        if len(repositories_query.clone()):
            builds = list(RepositoryBuild.select().where(
                RepositoryBuild.repository << list(repositories_query)))

            triggers = list(RepositoryBuildTrigger.select().where(
                RepositoryBuildTrigger.repository << list(repositories_query)))

            mirrors = list(RepoMirrorConfig.select().where(
                RepoMirrorConfig.repository << list(repositories_query)))

            # Delete all builds for the user's repositories.
            if builds:
                RepositoryBuild.delete().where(
                    RepositoryBuild.id << builds).execute()

            # Delete all build triggers for the user's repositories.
            if triggers:
                RepositoryBuildTrigger.delete().where(
                    RepositoryBuildTrigger.id << triggers).execute()

            # Delete all mirrors for the user's repositories.
            if mirrors:
                RepoMirrorConfig.delete().where(
                    RepoMirrorConfig.id << mirrors).execute()

            # Delete all queue items for the user's namespace.
            dockerfile_build_queue = WorkQueue(queue_name,
                                               tf,
                                               has_namespace=True)
            count_removed = dockerfile_build_queue.delete_namespaced_items(
                user.username)

    info = (user.username, len(triggers), count_removed, len(mirrors))
    print(
        "Namespace %s disabled, %s triggers deleted, %s queued builds removed, %s mirrors deleted"
        % info)
    return user
Beispiel #6
0
def get_min_id_for_repo_mirror_config():
    """
    Gets the minimum id for a repository mirroring.
    """
    return RepoMirrorConfig.select(fn.Min(RepoMirrorConfig.id)).scalar()
Beispiel #7
0
def get_max_id_for_repo_mirror_config():
    """ Gets the maximum id for repository mirroring """
    return RepoMirrorConfig.select(fn.Max(RepoMirrorConfig.id)).scalar()