forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-trigger
executable file
·114 lines (91 loc) · 3.02 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
#!/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/>.
DAYS = 7
REFRESH = {
"centos-7": {},
"centos-8-stream": {},
"continuous-atomic": {},
"debian-testing": {},
"debian-stable": {},
"fedora-31": {},
"fedora-32": {},
"fedora-testing": {},
"fedora-coreos": {},
"ubuntu-2004": {},
"ubuntu-stable": {},
"openshift": {"refresh-days": 30},
"rhel-7-8": {},
"rhel-7-9": {},
"rhel-8-2": {},
"rhel-8-3": {},
"rhel-atomic": {},
"services": {"refresh-days": 30},
}
import argparse
import os
import sys
import tempfile
import time
import subprocess
sys.dont_write_bytecode = True
import task
from task import github
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 = github.GitHub()
try:
results = scan(api, opts.image, opts.verbose)
except RuntimeError as ex:
sys.stderr.write("image-trigger: " + str(ex) + "\n")
return 1
for result in results:
if result:
sys.stdout.write(result + "\n")
return 0
# Prepare an image prune command
def scan_for_prune():
tasks = []
stamp = os.path.join(tempfile.gettempdir(), "cockpit-image-prune.stamp")
# Don't prune more than once per hour
try:
mtime = os.stat(stamp).st_mtime
except OSError:
mtime = 0
if mtime < time.time() - 3600:
tasks.append("PRIORITY=0000 touch {0} && ./image-prune".format(stamp))
return tasks
def scan(api, force, verbose):
subprocess.check_call(["git", "fetch", "origin", "master"])
for (image, options) in REFRESH.items():
perform = False
if force:
perform = image == force
else:
days = options.get("refresh-days", DAYS)
perform = task.stale(days, os.path.join("images", image), "origin/master")
if perform:
text = "Image refresh for {0}".format(image)
issue = task.issue(text, text, "image-refresh", image)
sys.stderr.write("#{0}: image-refresh {1}\n".format(issue["number"], image))
return scan_for_prune()
if __name__ == '__main__':
sys.exit(main())