2022-06-06 18:25:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# Copyright © 2020 - 2022 Collabora Ltd.
|
|
|
|
|
# Authors:
|
|
|
|
|
# Tomeu Vizoso <tomeu.vizoso@collabora.com>
|
|
|
|
|
# David Heidelberg <david.heidelberg@collabora.com>
|
|
|
|
|
#
|
2023-03-22 19:23:47 +01:00
|
|
|
# For the dependencies, see the requirements.txt
|
2022-06-06 18:25:48 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Helper script to restrict running only required CI jobs
|
|
|
|
|
and show the job(s) logs.
|
|
|
|
|
"""
|
|
|
|
|
|
2022-07-15 18:15:52 -03:00
|
|
|
import argparse
|
2022-06-06 18:25:48 +02:00
|
|
|
import re
|
|
|
|
|
import sys
|
2022-07-15 18:15:52 -03:00
|
|
|
import time
|
2025-04-17 14:57:50 +02:00
|
|
|
from collections import defaultdict, Counter
|
2022-07-15 18:15:52 -03:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
from functools import partial
|
2022-07-15 18:17:02 -03:00
|
|
|
from itertools import chain
|
2024-02-12 14:43:27 +00:00
|
|
|
from subprocess import check_output, CalledProcessError
|
2025-02-27 14:05:54 -03:00
|
|
|
from typing import Callable, Dict, TYPE_CHECKING, Iterable, Literal, Optional, Tuple, cast
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2022-07-15 18:15:52 -03:00
|
|
|
import gitlab
|
2024-02-05 22:59:51 +00:00
|
|
|
import gitlab.v4.objects
|
2023-10-03 11:28:26 -03:00
|
|
|
from gitlab_common import (
|
2024-01-22 20:16:43 -03:00
|
|
|
GITLAB_URL,
|
|
|
|
|
TOKEN_DIR,
|
2023-10-18 17:09:48 -03:00
|
|
|
get_gitlab_pipeline_from_url,
|
2024-01-22 20:16:43 -03:00
|
|
|
get_gitlab_project,
|
|
|
|
|
get_token_from_default_dir,
|
|
|
|
|
pretty_duration,
|
2023-10-03 11:28:26 -03:00
|
|
|
read_token,
|
|
|
|
|
wait_for_pipeline,
|
|
|
|
|
)
|
2025-05-06 11:23:02 +02:00
|
|
|
from gitlab_gql import GitlabGQL, create_job_needs_dag, filter_dag, print_dag, print_formatted_list
|
2025-09-18 12:31:49 +02:00
|
|
|
from rich.console import Console
|
2022-06-06 18:25:48 +02:00
|
|
|
|
ci/bin: Print a summary list of dependency and target jobs
We already print all the detected target jobs from regex and its
dependencies. But for more complex regexes the list can be cumbersome,
and an aggregate list of dependencies and targets can be more value, so
add these prints as well.
This is what looks like:
```
Running 10 dependency jobs:
alpine/x86_64_lava_ssh_client, clang-format, debian-arm64,
debian-testing, debian/arm64_build, debian/x86_64_build,
debian/x86_64_build-base, kernel+rootfs_arm64, kernel+rootfs_x86_64,
rustfmt
Running 15 target jobs:
a618_gl 1/4, a660_gl 1/2, intel-tgl-skqp, iris-amly-egl, iris-apl-deqp
1/3, iris-cml-deqp 1/4, iris-glk-deqp 1/2, iris-kbl-deqp 1/3,
lima-mali450-deqp:arm64, lima-mali450-piglit:arm64 1/2,
panfrost-g52-gl:arm64 1/3, panfrost-g72-gl:arm64 1/3,
panfrost-t720-gles2:arm64, panfrost-t860-egl:arm64, zink-anv-tgl
```
Signed-off-by: Guilherme Gallo <guilherme.gallo@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25940>
2023-11-01 01:36:07 -03:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from gitlab_gql import Dag
|
|
|
|
|
|
2022-06-06 18:25:48 +02:00
|
|
|
REFRESH_WAIT_LOG = 10
|
|
|
|
|
REFRESH_WAIT_JOBS = 6
|
2025-02-27 15:57:28 -03:00
|
|
|
MAX_ENABLE_JOB_ATTEMPTS = 3
|
2022-06-06 18:25:48 +02:00
|
|
|
|
|
|
|
|
STATUS_COLORS = {
|
|
|
|
|
"created": "",
|
2025-09-18 12:31:49 +02:00
|
|
|
"running": "[blue]",
|
|
|
|
|
"success": "[green]",
|
|
|
|
|
"failed": "[red]",
|
|
|
|
|
"canceled": "[magenta]",
|
|
|
|
|
"canceling": "[magenta]",
|
2022-06-06 18:25:48 +02:00
|
|
|
"manual": "",
|
|
|
|
|
"pending": "",
|
|
|
|
|
"skipped": "",
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-05 19:04:10 -03:00
|
|
|
COMPLETED_STATUSES = frozenset({"success", "failed"})
|
|
|
|
|
RUNNING_STATUSES = frozenset({"created", "pending", "running"})
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2025-09-18 12:31:49 +02:00
|
|
|
console = Console(highlight=False)
|
|
|
|
|
print = console.print
|
|
|
|
|
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2024-05-24 09:10:22 +02:00
|
|
|
def print_job_status(
|
|
|
|
|
job: gitlab.v4.objects.ProjectPipelineJob,
|
|
|
|
|
new_status: bool = False,
|
|
|
|
|
) -> None:
|
2022-06-06 18:25:48 +02:00
|
|
|
"""It prints a nice, colored job status with a link to the job."""
|
2024-06-26 17:21:22 +02:00
|
|
|
if job.status in {"canceled", "canceling"}:
|
2022-06-06 18:25:48 +02:00
|
|
|
return
|
|
|
|
|
|
2024-02-01 18:45:59 +00:00
|
|
|
if new_status and job.status == "created":
|
|
|
|
|
return
|
|
|
|
|
|
2025-05-06 13:31:45 +02:00
|
|
|
global type_field_pad
|
|
|
|
|
global name_field_pad
|
|
|
|
|
jtype = "🞋 job"
|
|
|
|
|
job_name = job.name
|
|
|
|
|
type_field_pad = len(jtype) if len(jtype) > type_field_pad else type_field_pad
|
|
|
|
|
name_field_pad = len(job_name) if len(job_name) > name_field_pad else name_field_pad
|
2024-05-24 09:17:14 +02:00
|
|
|
|
2024-05-23 14:03:50 +02:00
|
|
|
duration = job_duration(job)
|
2023-10-03 11:28:26 -03:00
|
|
|
|
2025-09-18 12:31:49 +02:00
|
|
|
print(
|
|
|
|
|
f"{STATUS_COLORS[job.status]}"
|
|
|
|
|
f"{jtype:{type_field_pad}} " # U+1F78B Round target
|
2025-10-08 15:59:19 +01:00
|
|
|
f"{link2print(job.web_url, job.name, name_field_pad)} " +
|
|
|
|
|
(f" has new status: {job.status}" if new_status else f" {job.status}") +
|
|
|
|
|
(f" ({pretty_duration(duration)})" if job.started_at else "")
|
2022-06-06 18:25:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-05-23 14:03:50 +02:00
|
|
|
def job_duration(job: gitlab.v4.objects.ProjectPipelineJob) -> float:
|
|
|
|
|
"""
|
|
|
|
|
Given a job, report the time lapsed in execution.
|
|
|
|
|
:param job: Pipeline job
|
|
|
|
|
:return: Current time in execution
|
|
|
|
|
"""
|
|
|
|
|
if job.duration:
|
|
|
|
|
return job.duration
|
|
|
|
|
elif job.started_at:
|
2025-02-25 23:04:42 -03:00
|
|
|
# Convert both times to UTC timestamps for consistent comparison
|
|
|
|
|
current_time = time.time()
|
|
|
|
|
start_time = job.started_at.timestamp()
|
|
|
|
|
return current_time - start_time
|
2024-05-23 14:03:50 +02:00
|
|
|
return 0.0
|
|
|
|
|
|
|
|
|
|
|
2022-06-06 18:25:48 +02:00
|
|
|
def pretty_wait(sec: int) -> None:
|
|
|
|
|
"""shows progressbar in dots"""
|
|
|
|
|
for val in range(sec, 0, -1):
|
2025-02-27 16:09:29 -03:00
|
|
|
print(f"⏲ {val:2d} seconds", end="\r") # U+23F2 Timer clock
|
2022-06-06 18:25:48 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
2025-02-27 14:05:54 -03:00
|
|
|
def run_target_job(
|
|
|
|
|
job: gitlab.v4.objects.ProjectPipelineJob,
|
|
|
|
|
enable_job_fn: Callable,
|
|
|
|
|
stress: int,
|
|
|
|
|
execution_times: dict,
|
|
|
|
|
target_statuses: dict,
|
|
|
|
|
) -> None:
|
2025-04-17 23:50:01 +02:00
|
|
|
execution_times[job.name][job.id] = (job_duration(job), job.status, job.web_url)
|
2025-02-27 14:05:54 -03:00
|
|
|
if stress and job.status in COMPLETED_STATUSES:
|
|
|
|
|
if (
|
|
|
|
|
stress < 0
|
2025-04-17 14:57:50 +02:00
|
|
|
or len(execution_times[job.name]) < stress
|
2025-02-27 14:05:54 -03:00
|
|
|
):
|
2025-02-27 15:57:28 -03:00
|
|
|
enable_job_fn(job=job, action_type="retry")
|
|
|
|
|
# Wait for the next loop to get the updated job object
|
|
|
|
|
return
|
2025-02-27 14:05:54 -03:00
|
|
|
else:
|
2025-02-27 15:57:28 -03:00
|
|
|
enable_job_fn(job=job, action_type="target")
|
2025-02-27 14:05:54 -03:00
|
|
|
|
2025-05-06 13:31:45 +02:00
|
|
|
print_job_status(job, job.status not in target_statuses[job.name])
|
2025-02-27 14:05:54 -03:00
|
|
|
target_statuses[job.name] = job.status
|
|
|
|
|
|
|
|
|
|
|
2022-06-06 18:25:48 +02:00
|
|
|
def monitor_pipeline(
|
2024-05-24 09:10:22 +02:00
|
|
|
project: gitlab.v4.objects.Project,
|
|
|
|
|
pipeline: gitlab.v4.objects.ProjectPipeline,
|
2025-08-30 17:07:35 +02:00
|
|
|
job_filter: callable,
|
2024-05-24 09:10:22 +02:00
|
|
|
dependencies: set[str],
|
2023-09-29 23:31:30 -03:00
|
|
|
stress: int,
|
2024-05-23 14:03:50 +02:00
|
|
|
) -> tuple[Optional[int], Optional[int], Dict[str, Dict[int, Tuple[float, str, str]]]]:
|
2022-06-06 18:25:48 +02:00
|
|
|
"""Monitors pipeline and delegate canceling jobs"""
|
2023-09-29 20:47:00 -03:00
|
|
|
statuses: dict[str, str] = defaultdict(str)
|
|
|
|
|
target_statuses: dict[str, str] = defaultdict(str)
|
2025-04-17 14:57:50 +02:00
|
|
|
execution_times: dict[str, dict[str, tuple[float, str, str]]] = defaultdict(lambda: defaultdict(tuple))
|
2024-05-24 09:10:22 +02:00
|
|
|
target_id: int = -1
|
2025-05-06 13:31:45 +02:00
|
|
|
global type_field_pad
|
|
|
|
|
type_field_pad = 0
|
|
|
|
|
global name_field_pad
|
|
|
|
|
name_field_pad = len(max(dependencies, key=len))+2
|
2024-07-26 12:17:29 -03:00
|
|
|
# In a running pipeline, we can skip following job traces that are in these statuses.
|
2024-08-22 12:50:02 +01:00
|
|
|
skip_follow_statuses: frozenset[str] = (COMPLETED_STATUSES)
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2024-05-28 14:48:05 +02:00
|
|
|
# Pre-populate the stress status counter for already completed target jobs.
|
|
|
|
|
if stress:
|
|
|
|
|
# When stress test, it is necessary to collect this information before start.
|
|
|
|
|
for job in pipeline.jobs.list(all=True, include_retried=True):
|
2025-08-30 17:07:35 +02:00
|
|
|
if job_filter(
|
|
|
|
|
job_name=job.name,
|
|
|
|
|
job_stage=job.stage,
|
2025-08-30 17:26:15 +02:00
|
|
|
job_tags=job.tag_list,
|
2025-08-30 17:07:35 +02:00
|
|
|
) and job.status in COMPLETED_STATUSES:
|
2024-05-23 14:03:50 +02:00
|
|
|
execution_times[job.name][job.id] = (job_duration(job), job.status, job.web_url)
|
2024-05-28 14:48:05 +02:00
|
|
|
|
2024-07-25 00:34:31 -03:00
|
|
|
# jobs_waiting is a list of job names that are waiting for status update.
|
|
|
|
|
# It occurs when a job that we want to run depends on another job that is not yet finished.
|
|
|
|
|
jobs_waiting = []
|
2025-02-27 15:57:28 -03:00
|
|
|
# Dictionary to track the number of attempts made for each job
|
|
|
|
|
enable_attempts: dict[int, int] = {}
|
2024-07-25 00:34:31 -03:00
|
|
|
# FIXME: This function has too many parameters, consider refactoring.
|
|
|
|
|
enable_job_fn = partial(
|
|
|
|
|
enable_job,
|
|
|
|
|
project=project,
|
2025-02-27 15:57:28 -03:00
|
|
|
enable_attempts=enable_attempts,
|
2024-07-25 00:34:31 -03:00
|
|
|
jobs_waiting=jobs_waiting,
|
|
|
|
|
)
|
2022-06-06 18:25:48 +02:00
|
|
|
while True:
|
2023-11-10 18:41:42 -03:00
|
|
|
deps_failed = []
|
2022-06-06 18:25:48 +02:00
|
|
|
to_cancel = []
|
2024-07-25 00:34:31 -03:00
|
|
|
jobs_waiting.clear()
|
2024-06-13 14:32:23 +02:00
|
|
|
for job in sorted(pipeline.jobs.list(all=True), key=lambda j: j.name):
|
2025-02-27 14:05:54 -03:00
|
|
|
job = cast(gitlab.v4.objects.ProjectPipelineJob, job)
|
2025-08-30 17:07:35 +02:00
|
|
|
if job_filter(
|
|
|
|
|
job_name=job.name,
|
|
|
|
|
job_stage=job.stage,
|
2025-08-30 17:26:15 +02:00
|
|
|
job_tags=job.tag_list,
|
2025-08-30 17:07:35 +02:00
|
|
|
):
|
2025-02-27 14:05:54 -03:00
|
|
|
run_target_job(
|
|
|
|
|
job,
|
|
|
|
|
enable_job_fn,
|
|
|
|
|
stress,
|
|
|
|
|
execution_times,
|
2025-05-06 13:31:45 +02:00
|
|
|
target_statuses
|
2025-02-27 14:05:54 -03:00
|
|
|
)
|
2023-09-29 20:47:00 -03:00
|
|
|
target_id = job.id
|
2022-06-06 18:25:48 +02:00
|
|
|
continue
|
2024-05-27 13:47:52 +02:00
|
|
|
# all other non-target jobs
|
2023-09-29 20:47:00 -03:00
|
|
|
if job.status != statuses[job.name]:
|
2025-05-06 13:31:45 +02:00
|
|
|
print_job_status(job, True)
|
2023-09-29 20:47:00 -03:00
|
|
|
statuses[job.name] = job.status
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2023-09-29 20:47:00 -03:00
|
|
|
# run dependencies and cancel the rest
|
2022-06-06 18:25:48 +02:00
|
|
|
if job.name in dependencies:
|
2025-02-27 15:57:28 -03:00
|
|
|
if not enable_job_fn(job=job, action_type="dep"):
|
|
|
|
|
# Wait for the next loop to get the updated job object
|
|
|
|
|
continue
|
2023-11-10 18:41:42 -03:00
|
|
|
if job.status == "failed":
|
|
|
|
|
deps_failed.append(job.name)
|
2023-09-29 20:47:00 -03:00
|
|
|
else:
|
2022-06-06 18:25:48 +02:00
|
|
|
to_cancel.append(job)
|
|
|
|
|
|
2023-09-29 18:53:19 -03:00
|
|
|
cancel_jobs(project, to_cancel)
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2022-08-11 15:59:05 +02:00
|
|
|
if stress:
|
2023-09-29 23:31:30 -03:00
|
|
|
enough = True
|
2025-04-17 14:57:50 +02:00
|
|
|
status_counters = {
|
|
|
|
|
name: Counter(info[1] for info in runs.values())
|
|
|
|
|
for name, runs in execution_times.items()
|
|
|
|
|
}
|
|
|
|
|
for job_name, counter in sorted(status_counters.items()):
|
|
|
|
|
n_succeed = counter.get("success", 0)
|
|
|
|
|
n_failed = counter.get("failed", 0)
|
|
|
|
|
n_total_completed = n_succeed + n_failed
|
|
|
|
|
n_total_seen = len(execution_times[job_name])
|
2023-09-29 10:17:29 -03:00
|
|
|
print(
|
2025-05-06 13:31:45 +02:00
|
|
|
f"* {job_name:{name_field_pad}} succ: {n_succeed}; "
|
2025-04-17 14:57:50 +02:00
|
|
|
f"fail: {n_failed}; "
|
|
|
|
|
f"total: {n_total_seen} of {stress}",
|
2023-09-29 10:17:29 -03:00
|
|
|
)
|
2025-04-17 14:57:50 +02:00
|
|
|
if stress < 0 or n_total_completed < stress:
|
2023-09-29 23:31:30 -03:00
|
|
|
enough = False
|
|
|
|
|
|
|
|
|
|
if not enough:
|
|
|
|
|
pretty_wait(REFRESH_WAIT_JOBS)
|
|
|
|
|
continue
|
2022-08-11 15:59:05 +02:00
|
|
|
|
2024-07-25 00:34:31 -03:00
|
|
|
if jobs_waiting:
|
2025-09-18 12:31:49 +02:00
|
|
|
print(f"[yellow]Waiting for jobs to update status:")
|
|
|
|
|
print_formatted_list(jobs_waiting, indentation=8, color="[yellow]")
|
2024-07-25 00:34:31 -03:00
|
|
|
pretty_wait(REFRESH_WAIT_JOBS)
|
|
|
|
|
continue
|
|
|
|
|
|
2025-04-17 14:57:50 +02:00
|
|
|
if (
|
|
|
|
|
stress in [0, 1]
|
|
|
|
|
and len(target_statuses) == 1
|
|
|
|
|
and RUNNING_STATUSES.intersection(target_statuses.values())
|
2022-06-06 18:25:48 +02:00
|
|
|
):
|
2024-05-23 14:03:50 +02:00
|
|
|
return target_id, None, execution_times
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2023-10-20 08:52:12 -03:00
|
|
|
if (
|
|
|
|
|
{"failed"}.intersection(target_statuses.values())
|
2024-06-26 18:00:43 +02:00
|
|
|
and not RUNNING_STATUSES.intersection(target_statuses.values())
|
2023-10-20 08:52:12 -03:00
|
|
|
):
|
2024-05-23 14:03:50 +02:00
|
|
|
return None, 1, execution_times
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2023-11-10 18:41:42 -03:00
|
|
|
if (
|
|
|
|
|
{"skipped"}.intersection(target_statuses.values())
|
2024-06-26 18:00:43 +02:00
|
|
|
and not RUNNING_STATUSES.intersection(target_statuses.values())
|
2023-11-10 18:41:42 -03:00
|
|
|
):
|
|
|
|
|
print(
|
2025-09-18 12:31:49 +02:00
|
|
|
f"[red]Target in skipped state, aborting. Failed dependencies:{deps_failed}"
|
2023-11-10 18:41:42 -03:00
|
|
|
)
|
2024-05-23 14:03:50 +02:00
|
|
|
return None, 1, execution_times
|
2023-11-10 18:41:42 -03:00
|
|
|
|
2024-07-26 12:17:29 -03:00
|
|
|
if skip_follow_statuses.issuperset(target_statuses.values()):
|
2024-05-23 14:03:50 +02:00
|
|
|
return None, 0, execution_times
|
2022-06-06 18:25:48 +02:00
|
|
|
|
|
|
|
|
pretty_wait(REFRESH_WAIT_JOBS)
|
|
|
|
|
|
|
|
|
|
|
2023-09-29 20:47:00 -03:00
|
|
|
def enable_job(
|
2024-02-05 22:59:51 +00:00
|
|
|
project: gitlab.v4.objects.Project,
|
|
|
|
|
job: gitlab.v4.objects.ProjectPipelineJob,
|
2025-02-27 15:57:28 -03:00
|
|
|
enable_attempts: dict[int, int],
|
2024-02-05 22:59:51 +00:00
|
|
|
action_type: Literal["target", "dep", "retry"],
|
2025-05-06 13:31:45 +02:00
|
|
|
jobs_waiting: list[str] = list,
|
2025-02-27 15:57:28 -03:00
|
|
|
) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
Enable a job to run.
|
|
|
|
|
:param project: The GitLab project.
|
|
|
|
|
:param job: The job to enable.
|
|
|
|
|
:param enable_attempts: A dictionary to track the number of attempts made for each job.
|
|
|
|
|
:param action_type: The type of action to perform.
|
2025-05-06 13:31:45 +02:00
|
|
|
:param jobs_waiting:
|
2025-02-27 15:57:28 -03:00
|
|
|
:return: True if the job was enabled, False otherwise.
|
|
|
|
|
"""
|
2024-07-25 00:34:31 -03:00
|
|
|
# We want to run this job, but it is not ready to run yet, so let's try again in the next
|
|
|
|
|
# iteration.
|
|
|
|
|
if job.status == "created":
|
|
|
|
|
jobs_waiting.append(job.name)
|
2025-02-27 15:57:28 -03:00
|
|
|
return False
|
2024-07-25 00:34:31 -03:00
|
|
|
|
2023-09-29 20:47:00 -03:00
|
|
|
if (
|
2024-06-26 17:46:09 +02:00
|
|
|
(job.status in COMPLETED_STATUSES and action_type != "retry")
|
2024-06-26 18:00:43 +02:00
|
|
|
or job.status in {"skipped"} | RUNNING_STATUSES
|
2023-09-29 20:47:00 -03:00
|
|
|
):
|
2025-02-27 15:57:28 -03:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# Get current attempt number
|
|
|
|
|
attempt_count = enable_attempts.get(job.id, 0)
|
|
|
|
|
# Check if we've exceeded max attempts to avoid infinite loop
|
|
|
|
|
if attempt_count >= MAX_ENABLE_JOB_ATTEMPTS:
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"Maximum enabling attempts ({MAX_ENABLE_JOB_ATTEMPTS}) reached for job {job.name} "
|
|
|
|
|
f"({link2print(job.web_url, job.id)}). Giving up."
|
|
|
|
|
)
|
|
|
|
|
enable_attempts[job.id] = attempt_count + 1
|
2023-09-29 20:47:00 -03:00
|
|
|
|
2022-06-06 18:25:48 +02:00
|
|
|
pjob = project.jobs.get(job.id, lazy=True)
|
2023-09-29 20:32:48 -03:00
|
|
|
|
2024-06-26 17:37:02 +02:00
|
|
|
if job.status in {"success", "failed", "canceled", "canceling"}:
|
2025-02-27 15:57:28 -03:00
|
|
|
try:
|
|
|
|
|
pjob.retry()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error retrying job {job.name}: {e}")
|
|
|
|
|
return False
|
2023-09-29 20:32:48 -03:00
|
|
|
else:
|
2025-02-27 15:57:28 -03:00
|
|
|
try:
|
|
|
|
|
pjob.play()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error playing job {job.name}: {e}")
|
|
|
|
|
return False
|
2023-09-29 20:32:48 -03:00
|
|
|
|
|
|
|
|
if action_type == "target":
|
2024-07-17 13:34:29 +02:00
|
|
|
jtype = "🞋 target" # U+1F78B Round target
|
2023-09-29 20:32:48 -03:00
|
|
|
elif action_type == "retry":
|
2024-07-17 13:34:29 +02:00
|
|
|
jtype = "↻ retrying" # U+21BB Clockwise open circle arrow
|
2022-06-06 18:25:48 +02:00
|
|
|
else:
|
2024-07-17 13:34:29 +02:00
|
|
|
jtype = "↪ dependency" # U+21AA Left Arrow Curving Right
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2025-05-06 13:31:45 +02:00
|
|
|
global type_field_pad
|
|
|
|
|
global name_field_pad
|
|
|
|
|
job_name = job.name
|
|
|
|
|
type_field_pad = len(jtype) if len(jtype) > type_field_pad else type_field_pad
|
|
|
|
|
name_field_pad = len(job_name) if len(job_name) > name_field_pad else name_field_pad
|
|
|
|
|
print(
|
2025-09-18 12:31:49 +02:00
|
|
|
f"[magenta]{jtype:{type_field_pad}} {job.name:{name_field_pad}} manually enabled"
|
2025-05-06 13:31:45 +02:00
|
|
|
)
|
2022-08-11 15:59:05 +02:00
|
|
|
|
2025-02-27 15:57:28 -03:00
|
|
|
return True
|
2024-02-05 22:59:51 +00:00
|
|
|
|
2022-08-11 15:59:05 +02:00
|
|
|
|
2024-05-24 09:10:22 +02:00
|
|
|
def cancel_job(
|
|
|
|
|
project: gitlab.v4.objects.Project,
|
2025-05-06 12:19:44 +02:00
|
|
|
pipeline_job: gitlab.v4.objects.ProjectPipelineJob
|
|
|
|
|
) -> Optional[gitlab.v4.objects.ProjectPipelineJob]:
|
|
|
|
|
"""
|
|
|
|
|
Cancel GitLab job
|
|
|
|
|
:param project: project from the pipeline job comes from
|
|
|
|
|
:param pipeline_job: job made from the pipeline list
|
|
|
|
|
:return the job object when cancel was called
|
|
|
|
|
"""
|
|
|
|
|
if pipeline_job.status not in RUNNING_STATUSES:
|
2023-09-29 20:47:00 -03:00
|
|
|
return
|
2025-05-06 12:19:44 +02:00
|
|
|
try:
|
|
|
|
|
project_job = project.jobs.get(pipeline_job.id, lazy=True)
|
|
|
|
|
project_job.cancel()
|
|
|
|
|
except (gitlab.GitlabCancelError, gitlab.GitlabGetError):
|
|
|
|
|
# If the job failed to cancel, it will be retried in the monitor_pipeline() next iteration
|
|
|
|
|
return
|
|
|
|
|
return pipeline_job
|
2022-06-06 18:25:48 +02:00
|
|
|
|
|
|
|
|
|
2024-05-24 09:10:22 +02:00
|
|
|
def cancel_jobs(
|
|
|
|
|
project: gitlab.v4.objects.Project,
|
2025-05-06 12:19:44 +02:00
|
|
|
to_cancel: list[gitlab.v4.objects.ProjectPipelineJob]
|
2024-05-24 09:10:22 +02:00
|
|
|
) -> None:
|
2025-05-06 12:19:44 +02:00
|
|
|
"""
|
|
|
|
|
Cancel unwanted GitLab jobs
|
|
|
|
|
:param project: project from where the pipeline comes
|
|
|
|
|
:param to_cancel: list of jobs to be cancelled
|
|
|
|
|
"""
|
2022-06-06 18:25:48 +02:00
|
|
|
if not to_cancel:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
with ThreadPoolExecutor(max_workers=6) as exe:
|
|
|
|
|
part = partial(cancel_job, project)
|
2025-05-06 12:19:44 +02:00
|
|
|
maybe_cancelled_job = exe.map(part, to_cancel)
|
|
|
|
|
cancelled_jobs = [f"🗙 {job.name}" for job in maybe_cancelled_job if job] # U+1F5D9 Cancellation X
|
2024-07-26 12:48:32 -03:00
|
|
|
|
|
|
|
|
# The cancelled jobs are printed without a newline
|
2025-05-06 12:19:44 +02:00
|
|
|
if len(cancelled_jobs):
|
|
|
|
|
print(f"Cancelled {len(cancelled_jobs)} jobs:")
|
|
|
|
|
print_formatted_list(cancelled_jobs, indentation=8)
|
2022-06-06 18:25:48 +02:00
|
|
|
|
|
|
|
|
|
2024-05-24 09:10:22 +02:00
|
|
|
def print_log(
|
|
|
|
|
project: gitlab.v4.objects.Project,
|
|
|
|
|
job_id: int
|
|
|
|
|
) -> None:
|
2022-06-06 18:25:48 +02:00
|
|
|
"""Print job log into output"""
|
|
|
|
|
printed_lines = 0
|
|
|
|
|
while True:
|
|
|
|
|
job = project.jobs.get(job_id)
|
|
|
|
|
|
|
|
|
|
# GitLab's REST API doesn't offer pagination for logs, so we have to refetch it all
|
2024-02-16 12:00:56 +00:00
|
|
|
lines = job.trace().decode().splitlines()
|
2022-06-06 18:25:48 +02:00
|
|
|
for line in lines[printed_lines:]:
|
|
|
|
|
print(line)
|
|
|
|
|
printed_lines = len(lines)
|
|
|
|
|
|
|
|
|
|
if job.status in COMPLETED_STATUSES:
|
2025-09-18 12:31:49 +02:00
|
|
|
print(f"[green]Job finished: {job.web_url}")
|
2022-06-06 18:25:48 +02:00
|
|
|
return
|
|
|
|
|
pretty_wait(REFRESH_WAIT_LOG)
|
|
|
|
|
|
|
|
|
|
|
2024-05-24 09:10:22 +02:00
|
|
|
def parse_args() -> argparse.Namespace:
|
2022-06-06 18:25:48 +02:00
|
|
|
"""Parse args"""
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
description="Tool to trigger a subset of container jobs "
|
|
|
|
|
+ "and monitor the progress of a test job",
|
|
|
|
|
epilog="Example: mesa-monitor.py --rev $(git rev-parse HEAD) "
|
|
|
|
|
+ '--target ".*traces" ',
|
|
|
|
|
)
|
2025-06-30 11:55:10 +02:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--server",
|
|
|
|
|
metavar="gitlab-server",
|
|
|
|
|
type=str,
|
|
|
|
|
default=GITLAB_URL,
|
|
|
|
|
help=f"Specify the GitLab server work with (Default: {GITLAB_URL})",
|
|
|
|
|
)
|
2023-09-29 09:40:32 -03:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--target",
|
|
|
|
|
metavar="target-job",
|
2024-03-29 13:02:13 +00:00
|
|
|
help="Target job regex. For multiple targets, pass multiple values, "
|
2024-08-22 12:37:58 +01:00
|
|
|
"eg. `--target foo bar`. Only jobs in the target stage(s) "
|
|
|
|
|
"supplied, and their dependencies, will be considered.",
|
2023-09-29 18:53:19 -03:00
|
|
|
required=True,
|
2024-01-24 23:21:29 +00:00
|
|
|
nargs=argparse.ONE_OR_MORE,
|
2023-09-29 09:40:32 -03:00
|
|
|
)
|
2024-08-22 12:37:58 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--include-stage",
|
|
|
|
|
metavar="include-stage",
|
|
|
|
|
help="Job stages to include when searching for target jobs. "
|
|
|
|
|
"For multiple targets, pass multiple values, eg. "
|
|
|
|
|
"`--include-stage foo bar`.",
|
|
|
|
|
default=[".*"],
|
|
|
|
|
nargs=argparse.ONE_OR_MORE,
|
|
|
|
|
)
|
2024-08-22 12:45:39 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--exclude-stage",
|
|
|
|
|
metavar="exclude-stage",
|
|
|
|
|
help="Job stages to exclude when searching for target jobs. "
|
|
|
|
|
"For multiple targets, pass multiple values, eg. "
|
|
|
|
|
"`--exclude-stage foo bar`. By default, performance and "
|
2025-04-17 21:03:29 +02:00
|
|
|
"nightly jobs are excluded; pass --exclude-stage '' to "
|
2024-08-22 12:45:39 +01:00
|
|
|
"include them for consideration.",
|
2025-04-17 21:03:29 +02:00
|
|
|
default=["performance", ".*-postmerge", ".*-nightly"],
|
2024-08-22 12:45:39 +01:00
|
|
|
nargs=argparse.ONE_OR_MORE,
|
|
|
|
|
)
|
2025-08-30 17:26:15 +02:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--job-tags",
|
|
|
|
|
metavar="job-tags",
|
|
|
|
|
help="Job tags to require when searching for target jobs. If multiple "
|
|
|
|
|
"values are passed, eg. `--job-tags 'foo.*' 'bar'`, the job will "
|
|
|
|
|
"need to have a tag matching `foo.*` *and* a tag matching `bar` "
|
|
|
|
|
"to qualify. Passing `--job-tags '.*'` makes sure the job has "
|
|
|
|
|
"a tag defined, while not passing `--job-tags` also allows "
|
|
|
|
|
"untagged jobs.",
|
|
|
|
|
default=[],
|
|
|
|
|
nargs=argparse.ONE_OR_MORE,
|
|
|
|
|
)
|
2022-06-06 18:25:48 +02:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--token",
|
|
|
|
|
metavar="token",
|
2024-01-22 20:16:43 -03:00
|
|
|
type=str,
|
|
|
|
|
default=get_token_from_default_dir(),
|
2025-08-29 14:57:15 +02:00
|
|
|
help="Use the provided GitLab token (with `api` scope) or token file, "
|
2024-01-22 20:16:43 -03:00
|
|
|
f"otherwise it's read from {TOKEN_DIR / 'gitlab-token'}",
|
2022-06-06 18:25:48 +02:00
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
2024-08-22 12:50:02 +01:00
|
|
|
"--force-manual", action="store_true",
|
|
|
|
|
help="Deprecated argument; manual jobs are always force-enabled"
|
2022-06-06 18:25:48 +02:00
|
|
|
)
|
2023-09-29 23:31:30 -03:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--stress",
|
2025-07-02 14:47:24 +02:00
|
|
|
metavar="n",
|
2023-09-29 23:31:30 -03:00
|
|
|
type=int,
|
2025-07-02 14:47:24 +02:00
|
|
|
default=0,
|
2024-05-28 14:48:05 +02:00
|
|
|
help="Stresstest job(s). Specify the number of times to rerun the selected jobs, "
|
|
|
|
|
"or use -1 for indefinite. Defaults to 0. If jobs have already been executed, "
|
|
|
|
|
"this will ensure the total run count respects the specified number.",
|
2023-09-29 23:31:30 -03:00
|
|
|
)
|
2023-09-29 22:41:58 -03:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--project",
|
2025-07-02 14:47:24 +02:00
|
|
|
metavar="name",
|
|
|
|
|
type=str,
|
2023-09-29 22:41:58 -03:00
|
|
|
default="mesa",
|
|
|
|
|
help="GitLab project in the format <user>/<project> or just <project>",
|
|
|
|
|
)
|
2024-08-22 12:53:53 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--dry-run",
|
|
|
|
|
action="store_true",
|
|
|
|
|
help="Exit after printing target jobs and dependencies",
|
|
|
|
|
)
|
2023-07-28 13:09:24 +01:00
|
|
|
|
|
|
|
|
mutex_group1 = parser.add_mutually_exclusive_group()
|
|
|
|
|
mutex_group1.add_argument(
|
2025-06-30 12:13:22 +02:00
|
|
|
"--rev",
|
2025-07-02 14:47:24 +02:00
|
|
|
metavar="id",
|
|
|
|
|
type=str,
|
2025-06-30 12:13:22 +02:00
|
|
|
default="HEAD",
|
|
|
|
|
help="Repository git commit-ish, tag or branch name (default: HEAD)",
|
2023-07-28 13:09:24 +01:00
|
|
|
)
|
|
|
|
|
mutex_group1.add_argument(
|
|
|
|
|
"--pipeline-url",
|
2025-07-02 14:47:24 +02:00
|
|
|
metavar="url",
|
|
|
|
|
type=str,
|
2023-07-28 13:09:24 +01:00
|
|
|
help="URL of the pipeline to use, instead of auto-detecting it.",
|
|
|
|
|
)
|
2023-11-27 17:47:58 +00:00
|
|
|
mutex_group1.add_argument(
|
|
|
|
|
"--mr",
|
2025-07-02 14:47:24 +02:00
|
|
|
metavar="id",
|
2023-11-27 17:47:58 +00:00
|
|
|
type=int,
|
|
|
|
|
help="ID of a merge request; the latest pipeline in that MR will be used.",
|
|
|
|
|
)
|
2023-07-28 13:09:24 +01:00
|
|
|
|
2023-08-29 18:15:47 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
# argparse doesn't support groups inside add_mutually_exclusive_group(),
|
|
|
|
|
# which means we can't just put `--project` and `--rev` in a group together,
|
|
|
|
|
# we have to do this by heand instead.
|
|
|
|
|
if args.pipeline_url and args.project != parser.get_default("project"):
|
|
|
|
|
# weird phrasing but it's the error add_mutually_exclusive_group() gives
|
|
|
|
|
parser.error("argument --project: not allowed with argument --pipeline-url")
|
|
|
|
|
|
|
|
|
|
return args
|
2022-06-06 18:25:48 +02:00
|
|
|
|
|
|
|
|
|
ci/bin: Print a summary list of dependency and target jobs
We already print all the detected target jobs from regex and its
dependencies. But for more complex regexes the list can be cumbersome,
and an aggregate list of dependencies and targets can be more value, so
add these prints as well.
This is what looks like:
```
Running 10 dependency jobs:
alpine/x86_64_lava_ssh_client, clang-format, debian-arm64,
debian-testing, debian/arm64_build, debian/x86_64_build,
debian/x86_64_build-base, kernel+rootfs_arm64, kernel+rootfs_x86_64,
rustfmt
Running 15 target jobs:
a618_gl 1/4, a660_gl 1/2, intel-tgl-skqp, iris-amly-egl, iris-apl-deqp
1/3, iris-cml-deqp 1/4, iris-glk-deqp 1/2, iris-kbl-deqp 1/3,
lima-mali450-deqp:arm64, lima-mali450-piglit:arm64 1/2,
panfrost-g52-gl:arm64 1/3, panfrost-g72-gl:arm64 1/3,
panfrost-t720-gles2:arm64, panfrost-t860-egl:arm64, zink-anv-tgl
```
Signed-off-by: Guilherme Gallo <guilherme.gallo@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25940>
2023-11-01 01:36:07 -03:00
|
|
|
def print_detected_jobs(
|
2024-05-24 09:10:22 +02:00
|
|
|
target_dep_dag: "Dag",
|
|
|
|
|
dependency_jobs: Iterable[str],
|
|
|
|
|
target_jobs: Iterable[str],
|
ci/bin: Print a summary list of dependency and target jobs
We already print all the detected target jobs from regex and its
dependencies. But for more complex regexes the list can be cumbersome,
and an aggregate list of dependencies and targets can be more value, so
add these prints as well.
This is what looks like:
```
Running 10 dependency jobs:
alpine/x86_64_lava_ssh_client, clang-format, debian-arm64,
debian-testing, debian/arm64_build, debian/x86_64_build,
debian/x86_64_build-base, kernel+rootfs_arm64, kernel+rootfs_x86_64,
rustfmt
Running 15 target jobs:
a618_gl 1/4, a660_gl 1/2, intel-tgl-skqp, iris-amly-egl, iris-apl-deqp
1/3, iris-cml-deqp 1/4, iris-glk-deqp 1/2, iris-kbl-deqp 1/3,
lima-mali450-deqp:arm64, lima-mali450-piglit:arm64 1/2,
panfrost-g52-gl:arm64 1/3, panfrost-g72-gl:arm64 1/3,
panfrost-t720-gles2:arm64, panfrost-t860-egl:arm64, zink-anv-tgl
```
Signed-off-by: Guilherme Gallo <guilherme.gallo@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25940>
2023-11-01 01:36:07 -03:00
|
|
|
) -> None:
|
|
|
|
|
def print_job_set(color: str, kind: str, job_set: Iterable[str]):
|
2025-05-06 11:23:02 +02:00
|
|
|
job_list = list(job_set)
|
2025-09-18 12:31:49 +02:00
|
|
|
print(f"{color}Running {len(job_list)} {kind} jobs:")
|
|
|
|
|
print_formatted_list(job_list, indentation=8, color=color)
|
2025-05-06 11:23:02 +02:00
|
|
|
|
2025-09-18 12:31:49 +02:00
|
|
|
print("[yellow]Detected target job and its dependencies:")
|
|
|
|
|
print_dag(target_dep_dag, indentation=8, color="[yellow]")
|
|
|
|
|
print_job_set("[magenta]", "dependency", dependency_jobs)
|
|
|
|
|
print_job_set("[blue]", "target", target_jobs)
|
ci/bin: Print a summary list of dependency and target jobs
We already print all the detected target jobs from regex and its
dependencies. But for more complex regexes the list can be cumbersome,
and an aggregate list of dependencies and targets can be more value, so
add these prints as well.
This is what looks like:
```
Running 10 dependency jobs:
alpine/x86_64_lava_ssh_client, clang-format, debian-arm64,
debian-testing, debian/arm64_build, debian/x86_64_build,
debian/x86_64_build-base, kernel+rootfs_arm64, kernel+rootfs_x86_64,
rustfmt
Running 15 target jobs:
a618_gl 1/4, a660_gl 1/2, intel-tgl-skqp, iris-amly-egl, iris-apl-deqp
1/3, iris-cml-deqp 1/4, iris-glk-deqp 1/2, iris-kbl-deqp 1/3,
lima-mali450-deqp:arm64, lima-mali450-piglit:arm64 1/2,
panfrost-g52-gl:arm64 1/3, panfrost-g72-gl:arm64 1/3,
panfrost-t720-gles2:arm64, panfrost-t860-egl:arm64, zink-anv-tgl
```
Signed-off-by: Guilherme Gallo <guilherme.gallo@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25940>
2023-11-01 01:36:07 -03:00
|
|
|
|
|
|
|
|
|
2024-05-24 09:10:22 +02:00
|
|
|
def find_dependencies(
|
2025-06-30 11:55:10 +02:00
|
|
|
server: str,
|
2024-05-24 09:10:22 +02:00
|
|
|
token: str | None,
|
2025-08-30 17:07:35 +02:00
|
|
|
job_filter: callable,
|
2024-05-24 09:10:22 +02:00
|
|
|
project_path: str,
|
|
|
|
|
iid: int
|
|
|
|
|
) -> set[str]:
|
2024-01-22 20:13:54 -03:00
|
|
|
"""
|
|
|
|
|
Find the dependencies of the target jobs in a GitLab pipeline.
|
|
|
|
|
|
|
|
|
|
This function uses the GitLab GraphQL API to fetch the job dependency graph
|
|
|
|
|
of a pipeline, filters the graph to only include the target jobs and their
|
|
|
|
|
dependencies, and returns the names of these jobs.
|
|
|
|
|
|
|
|
|
|
Args:
|
2025-06-30 11:55:10 +02:00
|
|
|
server (str): The url to the GitLab server.
|
2024-01-22 20:13:54 -03:00
|
|
|
token (str | None): The GitLab API token. If None, the API is accessed without
|
|
|
|
|
authentication.
|
|
|
|
|
target_jobs_regex (re.Pattern): A regex pattern to match the names of the target jobs.
|
|
|
|
|
project_path (str): The path of the GitLab project.
|
|
|
|
|
iid (int): The internal ID of the pipeline.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
set[str]: A set of the names of the target jobs and their dependencies.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
SystemExit: If no target jobs are found in the pipeline.
|
|
|
|
|
"""
|
2025-06-30 11:55:10 +02:00
|
|
|
gql_instance = GitlabGQL(
|
|
|
|
|
url=f"{server}/api/graphql",
|
|
|
|
|
token=token
|
|
|
|
|
)
|
2023-10-28 00:45:03 -03:00
|
|
|
dag = create_job_needs_dag(
|
2023-10-24 00:08:37 -03:00
|
|
|
gql_instance, {"projectPath": project_path.path_with_namespace, "iid": iid}
|
2022-07-15 18:17:02 -03:00
|
|
|
)
|
2022-08-02 19:01:32 -03:00
|
|
|
|
2025-08-30 17:07:35 +02:00
|
|
|
target_dep_dag = filter_dag(dag, job_filter)
|
2022-11-19 21:30:39 +01:00
|
|
|
if not target_dep_dag:
|
2025-09-18 12:31:49 +02:00
|
|
|
print("[red]The job(s) were not found in the pipeline.")
|
2022-11-19 21:30:39 +01:00
|
|
|
sys.exit(1)
|
2023-10-28 00:45:03 -03:00
|
|
|
|
|
|
|
|
dependency_jobs = set(chain.from_iterable(d["needs"] for d in target_dep_dag.values()))
|
|
|
|
|
target_jobs = set(target_dep_dag.keys())
|
ci/bin: Print a summary list of dependency and target jobs
We already print all the detected target jobs from regex and its
dependencies. But for more complex regexes the list can be cumbersome,
and an aggregate list of dependencies and targets can be more value, so
add these prints as well.
This is what looks like:
```
Running 10 dependency jobs:
alpine/x86_64_lava_ssh_client, clang-format, debian-arm64,
debian-testing, debian/arm64_build, debian/x86_64_build,
debian/x86_64_build-base, kernel+rootfs_arm64, kernel+rootfs_x86_64,
rustfmt
Running 15 target jobs:
a618_gl 1/4, a660_gl 1/2, intel-tgl-skqp, iris-amly-egl, iris-apl-deqp
1/3, iris-cml-deqp 1/4, iris-glk-deqp 1/2, iris-kbl-deqp 1/3,
lima-mali450-deqp:arm64, lima-mali450-piglit:arm64 1/2,
panfrost-g52-gl:arm64 1/3, panfrost-g72-gl:arm64 1/3,
panfrost-t720-gles2:arm64, panfrost-t860-egl:arm64, zink-anv-tgl
```
Signed-off-by: Guilherme Gallo <guilherme.gallo@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25940>
2023-11-01 01:36:07 -03:00
|
|
|
print_detected_jobs(target_dep_dag, dependency_jobs, target_jobs)
|
2023-10-28 00:45:03 -03:00
|
|
|
return target_jobs.union(dependency_jobs)
|
2022-07-15 18:17:02 -03:00
|
|
|
|
|
|
|
|
|
2024-05-23 14:03:50 +02:00
|
|
|
def print_monitor_summary(
|
|
|
|
|
execution_collection: Dict[str, Dict[int, Tuple[float, str, str]]],
|
|
|
|
|
t_start: float,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Summary of the test execution"""
|
|
|
|
|
t_end = time.perf_counter()
|
|
|
|
|
spend_minutes = (t_end - t_start) / 60
|
2024-05-24 09:10:22 +02:00
|
|
|
print(f"⏲ Duration of script execution: {spend_minutes:0.1f} minutes") # U+23F2 Timer clock
|
2024-05-23 14:03:50 +02:00
|
|
|
if len(execution_collection) == 0:
|
|
|
|
|
return
|
2024-05-24 09:10:22 +02:00
|
|
|
print(f"⏲ Jobs execution times:") # U+23F2 Timer clock
|
2024-05-23 14:03:50 +02:00
|
|
|
job_names = list(execution_collection.keys())
|
|
|
|
|
job_names.sort()
|
|
|
|
|
name_field_pad = len(max(job_names, key=len)) + 2
|
|
|
|
|
for name in job_names:
|
|
|
|
|
job_executions = execution_collection[name]
|
|
|
|
|
job_times = ', '.join([__job_duration_record(job_execution)
|
|
|
|
|
for job_execution in sorted(job_executions.items())])
|
|
|
|
|
print(f"* {name:{name_field_pad}}: ({len(job_executions)}) {job_times}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __job_duration_record(dict_item: tuple) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Format each pair of job and its duration.
|
|
|
|
|
:param job_execution: item of execution_collection[name][idn]: Dict[int, Tuple[float, str, str]]
|
|
|
|
|
"""
|
2024-05-24 09:17:14 +02:00
|
|
|
job_id = f"{dict_item[0]}" # dictionary key
|
2024-05-23 14:03:50 +02:00
|
|
|
job_duration, job_status, job_url = dict_item[1] # dictionary value, the tuple
|
2025-09-18 12:31:49 +02:00
|
|
|
return (
|
|
|
|
|
f"{STATUS_COLORS[job_status]}"
|
|
|
|
|
f"{link2print(job_url, job_id)}: {pretty_duration(job_duration):>8}"
|
|
|
|
|
)
|
2024-05-23 14:03:50 +02:00
|
|
|
|
|
|
|
|
|
2024-05-24 09:17:14 +02:00
|
|
|
def link2print(url: str, text: str, text_pad: int = 0) -> str:
|
2025-06-17 15:46:16 +02:00
|
|
|
text = str(text)
|
|
|
|
|
text_pad = len(text) if text_pad < 1 else text_pad
|
2025-09-18 12:31:49 +02:00
|
|
|
return f"[link={url}]{text:{text_pad}}[/link]"
|
2024-05-23 14:03:50 +02:00
|
|
|
|
|
|
|
|
|
2024-05-24 09:10:22 +02:00
|
|
|
def main() -> None:
|
2022-06-06 18:25:48 +02:00
|
|
|
try:
|
|
|
|
|
t_start = time.perf_counter()
|
|
|
|
|
|
|
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
|
|
token = read_token(args.token)
|
|
|
|
|
|
2025-06-30 11:55:10 +02:00
|
|
|
gl = gitlab.Gitlab(url=args.server,
|
2022-12-01 10:49:56 +00:00
|
|
|
private_token=token,
|
|
|
|
|
retry_transient_errors=True)
|
2022-06-06 18:25:48 +02:00
|
|
|
|
2022-12-16 00:36:13 +01:00
|
|
|
REV: str = args.rev
|
2023-05-24 22:58:28 +01:00
|
|
|
|
|
|
|
|
if args.pipeline_url:
|
2023-10-18 17:09:48 -03:00
|
|
|
pipe, cur_project = get_gitlab_pipeline_from_url(gl, args.pipeline_url)
|
2023-07-28 13:09:24 +01:00
|
|
|
REV = pipe.sha
|
2023-05-24 22:58:28 +01:00
|
|
|
else:
|
2023-10-19 12:03:34 +02:00
|
|
|
mesa_project = gl.projects.get("mesa/mesa")
|
2023-11-27 17:47:58 +00:00
|
|
|
projects = [mesa_project]
|
|
|
|
|
if args.mr:
|
|
|
|
|
REV = mesa_project.mergerequests.get(args.mr).sha
|
|
|
|
|
else:
|
|
|
|
|
REV = check_output(['git', 'rev-parse', REV]).decode('ascii').strip()
|
2024-01-10 11:49:19 +00:00
|
|
|
|
|
|
|
|
if args.rev == 'HEAD':
|
2024-02-12 14:43:27 +00:00
|
|
|
try:
|
|
|
|
|
branch_name = check_output([
|
|
|
|
|
'git', 'symbolic-ref', '-q', 'HEAD',
|
|
|
|
|
]).decode('ascii').strip()
|
|
|
|
|
except CalledProcessError:
|
|
|
|
|
branch_name = ""
|
|
|
|
|
|
|
|
|
|
# Ignore detached heads
|
|
|
|
|
if branch_name:
|
|
|
|
|
tracked_remote = check_output([
|
|
|
|
|
'git', 'for-each-ref', '--format=%(upstream)',
|
|
|
|
|
branch_name,
|
2024-02-12 10:51:47 +01:00
|
|
|
]).decode('ascii').strip()
|
|
|
|
|
|
2024-02-12 14:43:27 +00:00
|
|
|
# Ignore local branches that do not track any remote
|
|
|
|
|
if tracked_remote:
|
|
|
|
|
remote_rev = check_output([
|
|
|
|
|
'git', 'rev-parse', tracked_remote,
|
|
|
|
|
]).decode('ascii').strip()
|
|
|
|
|
|
|
|
|
|
if REV != remote_rev:
|
|
|
|
|
print(
|
|
|
|
|
f"Local HEAD commit {REV[:10]} is different than "
|
|
|
|
|
f"tracked remote HEAD commit {remote_rev[:10]}"
|
|
|
|
|
)
|
|
|
|
|
print("Did you forget to `git push` ?")
|
2024-01-10 11:49:19 +00:00
|
|
|
|
2023-11-27 17:47:58 +00:00
|
|
|
projects.append(get_gitlab_project(gl, args.project))
|
|
|
|
|
(pipe, cur_project) = wait_for_pipeline(projects, REV)
|
2023-07-28 13:09:24 +01:00
|
|
|
|
|
|
|
|
print(f"Revision: {REV}")
|
2022-06-06 18:25:48 +02:00
|
|
|
print(f"Pipeline: {pipe.web_url}")
|
2023-05-24 22:58:28 +01:00
|
|
|
|
2024-01-24 23:21:29 +00:00
|
|
|
target = '|'.join(args.target)
|
2024-02-08 17:56:20 +00:00
|
|
|
target = target.strip()
|
|
|
|
|
|
2025-09-18 12:31:49 +02:00
|
|
|
print(f"🞋 target job: [blue]{target}") # U+1F78B Round target
|
2024-02-08 18:02:31 +00:00
|
|
|
|
2024-02-08 17:56:20 +00:00
|
|
|
# Implicitly include `parallel:` jobs
|
|
|
|
|
target = f'({target})' + r'( \d+/\d+)?'
|
|
|
|
|
|
|
|
|
|
target_jobs_regex = re.compile(target)
|
2023-11-04 14:52:41 +00:00
|
|
|
|
2024-08-22 12:37:58 +01:00
|
|
|
include_stage = '|'.join(args.include_stage)
|
|
|
|
|
include_stage = include_stage.strip()
|
|
|
|
|
|
2025-09-18 12:31:49 +02:00
|
|
|
print(f"🞋 target from stages: [blue]{include_stage}") # U+1F78B Round target
|
2024-08-22 12:37:58 +01:00
|
|
|
|
|
|
|
|
include_stage_regex = re.compile(include_stage)
|
|
|
|
|
|
2024-08-22 12:45:39 +01:00
|
|
|
exclude_stage = '|'.join(args.exclude_stage)
|
|
|
|
|
exclude_stage = exclude_stage.strip()
|
|
|
|
|
|
2025-09-18 12:31:49 +02:00
|
|
|
print(f"🞋 target excluding stages: [blue]{exclude_stage}") # U+1F78B Round target
|
2024-08-22 12:45:39 +01:00
|
|
|
|
|
|
|
|
exclude_stage_regex = re.compile(exclude_stage)
|
|
|
|
|
|
2025-09-18 12:31:49 +02:00
|
|
|
print(f"🞋 target jobs with tags: [blue]{str(args.job_tags)}") # U+1F78B Round target
|
2025-08-30 17:26:15 +02:00
|
|
|
job_tags_regexes = [re.compile(job_tag) for job_tag in args.job_tags]
|
|
|
|
|
|
2025-08-30 17:07:35 +02:00
|
|
|
def job_filter(
|
|
|
|
|
job_name: str,
|
|
|
|
|
job_stage: str,
|
2025-08-30 17:26:15 +02:00
|
|
|
job_tags: set[str],
|
2025-08-30 17:07:35 +02:00
|
|
|
) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
Apply user-specified filters to a job, and return whether the
|
|
|
|
|
filters allow that job (True) or not (False).
|
|
|
|
|
"""
|
|
|
|
|
if not target_jobs_regex.fullmatch(job_name):
|
|
|
|
|
return False
|
|
|
|
|
if not include_stage_regex.fullmatch(job_stage):
|
|
|
|
|
return False
|
|
|
|
|
if exclude_stage_regex.fullmatch(job_stage):
|
|
|
|
|
return False
|
2025-08-30 17:26:15 +02:00
|
|
|
if not all(
|
|
|
|
|
any(job_tags_regex.fullmatch(tag) for tag in job_tags)
|
|
|
|
|
for job_tags_regex in job_tags_regexes
|
|
|
|
|
):
|
|
|
|
|
return False
|
2025-08-30 17:07:35 +02:00
|
|
|
return True
|
|
|
|
|
|
2024-01-24 23:19:33 +00:00
|
|
|
deps = find_dependencies(
|
2025-06-30 11:55:10 +02:00
|
|
|
server=args.server,
|
2024-01-22 20:13:54 -03:00
|
|
|
token=token,
|
2025-08-30 17:07:35 +02:00
|
|
|
job_filter=job_filter,
|
2024-01-22 20:13:54 -03:00
|
|
|
iid=pipe.iid,
|
|
|
|
|
project_path=cur_project
|
2024-01-24 23:19:33 +00:00
|
|
|
)
|
2024-08-22 12:53:53 +01:00
|
|
|
|
|
|
|
|
if args.dry_run:
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2024-05-23 14:03:50 +02:00
|
|
|
target_job_id, ret, exec_t = monitor_pipeline(
|
2024-08-22 12:45:39 +01:00
|
|
|
cur_project,
|
|
|
|
|
pipe,
|
2025-08-30 17:07:35 +02:00
|
|
|
job_filter,
|
2024-08-22 12:45:39 +01:00
|
|
|
deps,
|
|
|
|
|
args.stress
|
2022-06-06 18:25:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if target_job_id:
|
|
|
|
|
print_log(cur_project, target_job_id)
|
|
|
|
|
|
2024-05-23 14:03:50 +02:00
|
|
|
print_monitor_summary(exec_t, t_start)
|
2022-06-06 18:25:48 +02:00
|
|
|
|
|
|
|
|
sys.exit(ret)
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
sys.exit(1)
|
2024-05-24 09:10:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|