Exemplo n.º 1
0
async def schedule_build(build, session):
    """
    Sends the given build to
    the task queue.

    Args:
        build (molior.model.build.Build): Build to schedule.
    """
    if not chroot_ready(build, session):
        return False

    token = uuid.uuid4()
    buildtask = BuildTask(build=build, task_id=str(token))
    session.add(buildtask)
    session.commit()

    arch = build.buildconfiguration.buildvariant.architecture.name
    base_mirror_db = build.buildconfiguration.buildvariant.base_mirror
    distrelease_name = base_mirror_db.project.name
    distrelease_version = base_mirror_db.name

    # FIXME: why [0] ?
    project_version = build.buildconfiguration.projectversions[0]
    apt_urls = get_apt_repos(project_version, session, is_ci=build.is_ci)

    arch_any_only = False if arch == get_target_arch(build, session) else True

    config = Configuration()
    apt_url = config.aptly.get("apt_url")

    token = buildtask.task_id

    await build.set_scheduled()
    session.commit()  # pylint: disable=no-member

    await backend_queue.put({
        "schedule": [
            build.id,
            token,
            build.version,
            apt_url,
            arch,
            arch_any_only,
            distrelease_name,
            distrelease_version,
            "unstable" if build.is_ci else "stable",
            build.sourcename,
            project_version.project.name,
            project_version.name,
            apt_urls,
        ]
    })
    return True
Exemplo n.º 2
0
async def post_add_repository(request):
    """
    Adds given sourcerepositories to the given
    projectversion.

    ---
    description: Adds given sourcerepositories to given projectversion.
    tags:
        - ProjectVersions
    consumes:
        - application/json
    parameters:
        - name: projectversion_id
          in: path
          required: true
          type: integer
        - name: sourcerepository_id
          in: path
          required: true
          type: integer
        - name: body
          in: body
          required: true
          schema:
            type: object
            properties:
                buildvariants:
                    type: array
                    example: [1, 2]
    produces:
        - text/json
    responses:
        "200":
            description: successful
        "400":
            description: Invalid data received.
    """
    params = await request.json()

    projectversion_id = request.match_info["projectversion_id"]
    sourcerepository_id = request.match_info["sourcerepository_id"]
    buildvariants = params.get("buildvariants", [])

    if not buildvariants:
        return web.Response(status=400, text="No buildvariants recieved.")

    projectversion_id = parse_int(projectversion_id)

    project_v = (
        request.cirrina.db_session.query(ProjectVersion)  # pylint: disable=no-member
        .filter(ProjectVersion.id == projectversion_id).first())

    if not project_v:
        return web.Response(status=400, text="Invalid data received.")

    parsed_id = parse_int(sourcerepository_id)
    if not parsed_id:
        return web.Response(status=400, text="Invalid data received.")

    src_repo = (
        request.cirrina.db_session.query(SourceRepository)  # pylint: disable=no-member
        .filter(SourceRepository.id == parsed_id).first())
    if src_repo not in project_v.sourcerepositories:
        project_v.sourcerepositories.append(src_repo)
        request.cirrina.db_session.commit()  # pylint: disable=no-member

    sourepprover_id = ((
        request.cirrina.db_session.query(SouRepProVer)  # pylint: disable=no-member
        .filter(SouRepProVer.c.sourcerepository_id == src_repo.id).filter(
            SouRepProVer.c.projectversion_id == project_v.id)).first().id)

    for buildvariant in buildvariants:
        # if just the buildvariant id is given
        if buildvariant.get("id"):
            buildvar_id = parse_int(buildvariant.get("id"))
            buildvar = (
                request.cirrina.db_session.query(BuildVariant)  # pylint: disable=no-member
                .filter(BuildVariant.id == buildvar_id).first())
        # if basemirror and architecture is given
        elif buildvariant.get("architecture_id") and buildvariant.get(
                "base_mirror_id"):
            arch_id = parse_int(buildvariant.get("architecture_id"))
            base_mirror_id = parse_int(buildvariant.get("base_mirror_id"))
            buildvar = (
                request.cirrina.db_session.query(BuildVariant).filter(
                    BuildVariant.architecture_id == arch_id)  # pylint: disable=no-member
                .filter(BuildVariant.base_mirror_id == base_mirror_id).first())
        else:
            return web.Response(status=400,
                                text="Invalid buildvariants received.")

        buildconf = BuildConfiguration(
            buildvariant=buildvar,
            sourcerepositoryprojectversion_id=sourepprover_id)
        request.cirrina.db_session.add(buildconf)

    request.cirrina.db_session.commit()  # pylint: disable=no-member

    logger.info(
        "SourceRepository '%s' with id '%s' added to ProjectVersion '%s/%s'",
        src_repo.url,
        src_repo.id,
        project_v.project.name,
        project_v.name,
    )

    if src_repo.state == "new":
        build = Build(
            version=None,
            git_ref=None,
            ci_branch=None,
            is_ci=None,
            versiontimestamp=None,
            sourcename=src_repo.name,
            buildstate="new",
            buildtype="build",
            buildconfiguration=None,
            sourcerepository=src_repo,
            maintainer=None,
        )

        request.cirrina.db_session.add(build)
        request.cirrina.db_session.commit()
        await build_added(build)

        token = uuid.uuid4()
        buildtask = BuildTask(build=build, task_id=str(token))
        request.cirrina.db_session.add(buildtask)
        request.cirrina.db_session.commit()

        args = {"clone": [build.id, src_repo.id]}
        await request.cirrina.task_queue.put(args)

    return web.Response(status=200, text="SourceRepository added.")
Exemplo n.º 3
0
async def trigger_build(request):
    """
    Triggers a build.

    ---
    description: Triggers a build
    tags:
        - TriggerBuild
    consumes:
        - application/x-www-form-urlencoded
    parameters:
        - name: repository
          in: body
          required: true
          type: string
        - name: git_ref
          in: body
          required: false
          type: string
        - name: git_branch
          in: body
          required: false
          type: string
    produces:
        - text/json
    responses:
        "200":
            description: successful
        "500":
            description: internal server error
    """
    data = await request.json()

    repository = data.get("repository")
    git_ref = data.get("git_ref")
    git_branch = data.get("git_branch")

    maintenance_mode = False
    query = "SELECT value from metadata where name = :key"
    result = request.cirrina.db_session.execute(query,
                                                {"key": "maintenance_mode"})
    for value in result:
        if value[0] == "true":
            maintenance_mode = True
        break

    if maintenance_mode:
        return web.Response(status=503, text="Maintenance Mode")

    if not repository:
        return web.Response(text="Bad Request", status=400)

    logger.info("build triggered: %s %s %s", repository, git_ref, git_branch)

    repo = (request.cirrina.db_session.query(SourceRepository).filter(
        SourceRepository.url == repository).first())
    if not repo:
        return web.Response(text="Repo not found", status=400)

    build = Build(
        version=None,
        git_ref=git_ref,
        ci_branch=git_branch,
        is_ci=None,
        versiontimestamp=None,
        sourcename=repo.name,
        buildstate="new",
        buildtype="build",
        buildconfiguration=None,
        sourcerepository=repo,
        maintainer=None,
    )

    request.cirrina.db_session.add(build)
    request.cirrina.db_session.commit()
    await build_added(build)

    token = uuid.uuid4()
    buildtask = BuildTask(build=build, task_id=str(token))
    request.cirrina.db_session.add(buildtask)
    request.cirrina.db_session.commit()

    if git_ref == "":
        args = {"buildlatest": [repo.id, build.id]}
    else:
        args = {"build": [build.id, repo.id, git_ref, git_branch]}
    await request.cirrina.task_queue.put(args)

    return web.json_response({"build_token": str(token)})
Exemplo n.º 4
0
async def process_push(request, data):
    """
    Process incoming PUSH event from a GitLab instance.

    Args:
        request: The request instance.
        data (dict): The received data.
    """

    user_username = data.get("user_username")
    logger.info("GitLab-API: PUSH (user_username): %s", user_username)

    user_name = data.get("user_name")
    logger.info("GitLab-API: PUSH (user_name): %s", user_name)

    user_email = data.get("user_email")
    logger.debug("GitLab-API: PUSH (user_email): %s", user_email)

    git_ref = data.get("ref")
    logger.info("GitLab-API: PUSH (git_ref): %s", git_ref)

    checkout_sha = data.get("checkout_sha")
    logger.info("GitLab-API: PUSH (checkout_sha): %s", checkout_sha)

    repository_url = ""
    project = data.get("project")
    if project:

        project_ssh_url = project.get("ssh_url")
        logger.debug("GitLab-API: PUSH (project_ssh_url): %s", project_ssh_url)

        project_name = project.get("name")
        logger.info("GitLab-API: PUSH (project_name): %s", project_name)

        project_url = project.get("url")
        logger.debug("GitLab-API: PUSH (project_url): %s", project_url)

        project_git_ssh_url = project.get("git_ssh_url")
        logger.debug("GitLab-API: PUSH (project_git_ssh_url): %s",
                     project_git_ssh_url)

        if project_git_ssh_url:
            repository_url = project_git_ssh_url
        elif project_ssh_url:
            repository_url = project_ssh_url
        elif project_url:
            repository_url = project_url

    if not repository_url:
        repository = data.get("repository")
        if repository:
            repository_urlx = repository.get("url")
            logger.debug("GitLab-API: PUSH (repository_urlx): %s",
                         repository_urlx)

            repository_git_ssh_url = repository.get("git_ssh_url")
            logger.debug("GitLab-API: PUSH (repository_git_ssh_url): %s",
                         repository_git_ssh_url)

            if repository_git_ssh_url:
                repository_url = repository_git_ssh_url

            elif repository_urlx:
                repository_url = repository_urlx

    if not repository_url:
        return "Missing GIT repository URL (ssh)", 400
    else:
        logger.info("GitLab-API: PUSH (repository_url): %s", repository_url)

    # Prepare CI/CD branch from parsed data
    ci_branch = None
    if git_ref:
        tag_left = "refs/tags/"
        if git_ref.startswith(tag_left):
            # Skip processing of TAG_PUSH events
            logger.debug("GitLab-API: PUSH unhandled due to TAG_PUSH object")
            return "No TAG_PUSH objects allowed here", 400

        ref_left = "refs/heads/"
        if git_ref.startswith(ref_left):
            ci_branch = git_ref[len(ref_left):]

    # No further processing for API-tests with empty event_name
    # BEWARE: API-test from GitLab will have event_name set to 'push', so be careful
    event_name = data.get("event_name")
    if not event_name:
        logger.info("GitLab-API: TEST: Tickle, tickle ... Hihihi")
        return "OK", 200

    # CI-Branch
    logger.info("GitLab-API: PUSH (ci_branch): %s", ci_branch)

    repo = (request.cirrina.db_session.query(SourceRepository).filter(
        SourceRepository.url == repository_url).first())
    if not repo:
        return "Repo not found", 400

    build = Build(
        version=None,
        git_ref=
        checkout_sha,  # Use pure hash for CI-builds, instead of git_ref/branch
        ci_branch=ci_branch,
        is_ci=False,
        versiontimestamp=None,
        sourcename=repo.name,
        buildstate="new",
        buildtype="build",
        buildconfiguration=None,
        sourcerepository=repo,
        maintainer=None,
    )

    request.cirrina.db_session.add(build)
    request.cirrina.db_session.commit()
    await build_added(build)

    token = uuid.uuid4()
    build_task = BuildTask(build=build, task_id=str(token))
    request.cirrina.db_session.add(build_task)
    request.cirrina.db_session.commit()

    logger.debug("GitLab-API: CI-BUILD  (build_id): %s", build.id)
    if checkout_sha and repo.id:
        args = {"build": [build.id, repo.id, checkout_sha, ci_branch]}

        # Queue new build job
        if await request.cirrina.task_queue.put(args):
            logger.info("GitLab-API: CI-BUILD triggered (sourcename): %s",
                        build.sourcename)
            return "OK", 200

    return "Unprocessable Entity", 422
Exemplo n.º 5
0
async def process_tag_push(request, data):
    """
    Process incoming TAG_PUSH event from a GitLab instance.

     Args:
        request: The request instance.
        data (dict): The received data.
   """

    event_name = data.get("event_name")
    logger.info("GitLab-API: Incoming event (event_name): %s", event_name)

    user_username = data.get("user_username")
    logger.info("GitLab-API: TAG_PUSH (user_username): %s", user_username)

    user_name = data.get("user_name")
    logger.info("GitLab-API: TAG_PUSH (user_name): %s", user_name)

    git_ref = data.get("ref")
    logger.info("GitLab-API: TAG_PUSH (git_ref): %s", git_ref)

    user_email = data.get("user_email")
    logger.debug("GitLab-API: TAG_PUSH (user_email): %s", user_email)

    repository_url = ""
    project = data.get("project")
    if project:
        project_name = project.get("name")
        logger.info("GitLab-API: TAG_PUSH (project_name): %s", project_name)

        url = project.get("url")
        logger.debug("GitLab-API: TAG_PUSH (url): %s", url)

        ssl_url = project.get("ssl_url")
        logger.debug("GitLab-API: TAG_PUSH (ssl_url): %s", ssl_url)

        git_ssl_url = project.get("git_ssl_url")
        logger.debug("GitLab-API: TAG_PUSH (git_ssl_url): %s", git_ssl_url)

        if git_ssl_url:
            repository_url = git_ssl_url
        elif ssl_url:
            repository_url = ssl_url
        elif url:
            repository_url = url

    if not repository_url:
        repository = data.get("repository")
        if repository:
            url = repository.get("url")
            git_ssl_url = repository.get("git_ssl_url")

            if git_ssl_url:
                repository_url = git_ssl_url
            elif url:
                repository_url = url

    if not repository_url:
        return "Missing GIT repository URL (ssh)", 400
    else:
        logger.info("GitLab-API: TAG_PUSH (repository): %s", repository_url)

    # Prepare branch (used for UI only) from parsed data
    ui_branch = None
    if git_ref:
        tag_left = "refs/tags/"
        if git_ref.startswith(tag_left):
            ui_branch = git_ref[len(tag_left):]

        ref_left = "refs/heads/"
        if git_ref.startswith(ref_left):
            ui_branch = git_ref[len(ref_left):]

    repo = (request.cirrina.db_session.query(SourceRepository).filter(
        SourceRepository.url == repository_url).first())
    if not repo:
        return "Repo not found", 400

    build = Build(
        version=None,
        git_ref=git_ref,
        ci_branch=ui_branch,
        is_ci=False,
        versiontimestamp=None,
        sourcename=repo.name,
        buildstate="new",
        buildtype="build",
        buildconfiguration=None,
        sourcerepository=repo,
        maintainer=None,
    )

    request.cirrina.db_session.add(build)
    request.cirrina.db_session.commit()
    await build_added(build)

    token = uuid.uuid4()
    build_task = BuildTask(build=build, task_id=str(token))
    request.cirrina.db_session.add(build_task)
    request.cirrina.db_session.commit()

    logger.debug("GitLab-API: CI-BUILD  (build_id): %s", build.id)
    if git_ref and repo.id:
        args = {"build": [build.id, repo.id, git_ref, ui_branch]}

        # Queue new build job
        if await request.cirrina.task_queue.put(args):
            logger.info("GitLab-API: BUILD triggered (sourcename): %s",
                        build.sourcename)
            return "OK", 200

    return "Unprocessable Entity", 422
Exemplo n.º 6
0
async def trigger_build(request):
    """
    Triggers a build latest job on a sourcerepository.

    ---
    description: Triggers a build latest job on a sourcerepository.
    tags:
        - Builds
    consumes:
        - application/x-www-form-urlencoded
    parameters:
        - name: repository_id
          in: path
          required: true
          type: integer
    produces:
        - text/json
    responses:
        "200":
            description: successful
        "400":
            description: Incorrect value for repository_id
        "500":
            description: internal server error
    """
    repository_id = request.match_info.get("repository_id")

    try:
        repository_id = int(repository_id)
    except (ValueError, TypeError):
        logger.error(
            "trigger_build_latest error: invalid repository_id received")
        return web.Response(text="Incorrect value for repository_id",
                            status=400)

    repository = (request.cirrina.db_session.query(SourceRepository).filter(
        SourceRepository.id == repository_id).first())
    if not repository:
        logger.error("trigger_build_latest error: repo %d not found" %
                     repository_id)
        return web.Response(text="Repository not found", status=400)

    logger.info("trigger_build_latest for repo %d" % repository_id)

    build = Build(
        version=None,
        git_ref=None,
        ci_branch=None,
        is_ci=None,
        versiontimestamp=None,
        sourcename=repository.name,
        buildstate="new",
        buildtype="build",
        buildconfiguration=None,
        sourcerepository=repository,
        maintainer=None,
    )

    request.cirrina.db_session.add(build)
    request.cirrina.db_session.commit()
    await build_added(build)

    await build.set_building()

    token = uuid.uuid4()
    buildtask = BuildTask(build=build, task_id=str(token))
    request.cirrina.db_session.add(buildtask)
    request.cirrina.db_session.commit()

    args = {"buildlatest": [repository_id, build.id]}
    await request.cirrina.task_queue.put(args)

    return web.json_response({"build_token": str(token)})