-
Notifications
You must be signed in to change notification settings - Fork 34
/
image-trigger
executable file
·127 lines (105 loc) · 3.59 KB
/
image-trigger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
# This file is part of Cockpit.
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
import argparse
import os
import random
import subprocess
import sys
import time
import task
from lib.constants import BASE_DIR
sys.dont_write_bytecode = True
# default for refresh-days
DAYS = 7
REFRESH_30 = {"refresh-days": 30}
# stable/old OSes don't need to be refreshed as often
REFRESH = {
"arch": {},
"centos-9-bootc": {},
"centos-9-stream": {},
"centos-10": {},
"debian-testing": {},
"debian-stable": {},
"fedora-39": {},
"fedora-40": {},
"fedora-41": {},
"fedora-coreos": {},
"fedora-rawhide": {},
"fedora-rawhide-boot": {},
"fedora-rawhide-anaconda-payload": REFRESH_30,
"fedora-rawhide-live-boot": REFRESH_30,
"opensuse-tumbleweed": {},
"ubuntu-2204": {},
"ubuntu-2404": {},
"ubuntu-stable": {},
"rhel-8-8": REFRESH_30,
"rhel-8-10": {},
"rhel-9-2": REFRESH_30,
"rhel-9-4": {},
"rhel-9-5": {},
"rhel-9-6": {},
"rhel-10-0": {},
"services": REFRESH_30,
}
def main():
parser = argparse.ArgumentParser(description='Ensure necessary issue exists for image refresh')
parser.add_argument('-v', '--verbose', action="store_true", default=False,
help="Print verbose information")
parser.add_argument("image", nargs="?")
opts = parser.parse_args()
api = task.github.GitHub()
try:
scan(api, opts.image, opts.verbose)
except RuntimeError as ex:
sys.stderr.write("image-trigger: " + str(ex) + "\n")
return 1
return 0
# Check if the given files that match @pathspec are stale
# and haven't been updated in @days.
def stale(days, pathspec, ref="HEAD", verbose=False):
def execute(*args):
if verbose:
sys.stderr.write("+ " + " ".join(args) + "\n")
output = subprocess.check_output(args, cwd=BASE_DIR, text=True)
if verbose:
sys.stderr.write("> " + output + "\n")
return output
timestamp = execute("git", "log", "--max-count=1", "--pretty=format:%ct", ref, "--", pathspec)
try:
timestamp = int(timestamp)
except ValueError:
timestamp = 0
# We randomize when we think this should happen over a day
offset = days * 86400
due = time.time() - random.randint(offset - 43200, offset + 43200)
return timestamp < due
def scan(api, force, verbose):
subprocess.check_call(["git", "fetch", "origin", "main"])
for (image, options) in REFRESH.items():
perform = False
if force:
perform = image == force
else:
days = options.get("refresh-days", DAYS)
perform = stale(days, os.path.join("images", image), "origin/main", verbose)
if perform:
text = f"Image refresh for {image}"
issue = task.issue(text, text, "image-refresh", image)
sys.stderr.write(f'#{issue["number"]}: image-refresh {image}\n')
if __name__ == '__main__':
sys.exit(main())