Skip to content

Commit f32c1d5

Browse files
committed
Rewrite openqa-advanced-retrigger-jobs in python
1 parent 4093b20 commit f32c1d5

1 file changed

Lines changed: 85 additions & 51 deletions

File tree

openqa-advanced-retrigger-jobs

Lines changed: 85 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,88 @@
1-
#!/bin/bash -e
2-
#worker="${worker:-"openqaworker4"}"
3-
host="${host:-"openqa.opensuse.org"}"
4-
failed_since="${failed_since:-"$(date -I)"}"
5-
instance_string="${INSTANCE+" and instance='$INSTANCE'"}"
6-
worker_string="${WORKER+"assigned_worker_id in (select id from workers where (host='$WORKER'$instance_string)) and "}"
7-
result="${result:-"result='incomplete'"}"
8-
additional_filters="${additional_filters+" and $additional_filters"}"
9-
comment="${comment:-""}"
10-
dry_run="${dry_run:-"0"}"
11-
12-
usage() {
13-
cat << EOF
14-
Usage: $0 [OPTIONS]
1+
#!/usr/bin/env python3
152

3+
"""
164
Retrigger openQA jobs based on database queries.
175
18-
By default retriggers openQA jobs with result '$result' since '$failed_since'
19-
on '$host'.
20-
21-
Can be restricted to jobs that ran on worker by setting the variable 'WORKER'
22-
and optionally 'INSTANCE'.
23-
24-
Needs SSH access to the target openQA host '$host'.
25-
26-
Options:
27-
-h, --help display this help
28-
EOF
29-
exit "$1"
30-
}
31-
32-
main() {
33-
opts=$(getopt -o h -l help -n "$0" -- "$@") || usage 1
34-
eval set -- "$opts"
35-
while true; do
36-
case "$1" in
37-
-h | --help) usage 0 ;;
38-
--)
39-
shift
40-
break
41-
;;
42-
*) break ;;
43-
esac
44-
done
45-
46-
[ "$dry_run" = "1" ] && client_prefix="echo"
47-
# shellcheck disable=SC2029
48-
for i in $(ssh "$host" "sudo -u geekotest psql --no-align --tuples-only --command=\"select id from jobs where (${worker_string}${result} and clone_id is null and t_finished >= '$failed_since'$additional_filters);\" openqa"); do
49-
$client_prefix openqa-cli api --host "$host" -X POST jobs/"$i"/restart
50-
[ -n "$comment" ] && $client_prefix openqa-cli api --host "$host" -X POST jobs/"$i"/comments text="$comment"
51-
done
52-
}
53-
54-
caller 0 > /dev/null || main "$@"
6+
Needs SSH access to the specified target openQA host.
7+
"""
8+
9+
import argparse
10+
import logging
11+
import subprocess
12+
import sys
13+
from datetime import datetime
14+
15+
logging.basicConfig()
16+
log = logging.getLogger(sys.argv[0] if __name__ == "__main__" else __name__)
17+
18+
19+
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
20+
"""Preserve multi-line __doc__ and provide default arguments in help strings."""
21+
22+
pass
23+
24+
25+
def main():
26+
parser = argparse.ArgumentParser(description=__doc__, formatter_class=CustomFormatter)
27+
parser.add_argument(
28+
"-v",
29+
"--verbose",
30+
help="Increase verbosity level, specify multiple times to increase verbosity",
31+
action="count",
32+
default=1,
33+
)
34+
parser.add_argument("-H", "--host", default="openqa.opensuse.org", help="Target openQA host")
35+
parser.add_argument(
36+
"-s",
37+
"--failed-since",
38+
default=datetime.today().isoformat(),
39+
help="Filter jobs failed since this date",
40+
)
41+
parser.add_argument("-w", "--worker", default=None, help="Filter jobs assigned to this worker")
42+
parser.add_argument("-i", "--instance", default=None, help="Instance of the worker")
43+
parser.add_argument("-r", "--result", default="incomplete", help="Filter jobs with this result")
44+
parser.add_argument(
45+
"-a",
46+
"--additional-filters",
47+
default=None,
48+
help="Additional filters for the SQL query",
49+
)
50+
parser.add_argument("-c", "--comment", default=None, help="Comment to add to the retriggered jobs")
51+
parser.add_argument(
52+
"-d",
53+
"--dry-run",
54+
action="store_true",
55+
help="If set, only print the actions without executing",
56+
)
57+
args = parser.parse_args()
58+
verbose_to_log = {
59+
0: logging.CRITICAL,
60+
1: logging.ERROR,
61+
2: logging.WARN,
62+
3: logging.INFO,
63+
4: logging.DEBUG,
64+
}
65+
logging_level = logging.DEBUG if args.verbose > 4 else verbose_to_log[args.verbose]
66+
log.setLevel(logging_level)
67+
instance_string = f" and instance='{args.instance}'" if args.instance else ""
68+
worker_string = f"assigned_worker_id in (select id from workers where (host='{args.worker}'{instance_string})) and " if args.worker else ""
69+
additional_filters = f" and {args.additional_filters}" if args.additional_filters else ""
70+
client_prefix = "echo" if args.dry_run else ""
71+
72+
query = (
73+
f"select id from jobs where ({worker_string}result='{args.result}' "
74+
f"and clone_id is null and t_finished >= '{args.failed_since}'{additional_filters});"
75+
)
76+
77+
log.debug(f"Using SQL query: '{query}' on {args.host}")
78+
ssh_command = f'ssh {args.host} "sudo -u geekotest psql --no-align --tuples-only --command=\\"{query}\\" openqa"'
79+
job_ids = subprocess.check_output(ssh_command).decode().splitlines()
80+
81+
for job_id in job_ids:
82+
subprocess.run(f"{client_prefix} openqa-cli api --host {args.host} -X POST jobs/{job_id}/restart")
83+
if args.comment:
84+
subprocess.run(f'{client_prefix} openqa-cli api --host {args.host} -X POST jobs/{job_id}/comments text="{args.comment}"')
85+
86+
87+
if __name__ == "__main__":
88+
main()

0 commit comments

Comments
 (0)