#!/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 . DAYS = 7 REFRESH = { "candlepin": { "refresh-days": 120 }, "centos-7": { }, "continuous-atomic": { }, "debian-testing": { }, "debian-stable": { }, "fedora-29": { }, "fedora-30": { }, "fedora-atomic": { }, "fedora-testing": { }, "fedora-i386": { }, "ipa": { "refresh-days": 120 }, "ubuntu-1804": { }, "ubuntu-stable": { }, "openshift": { "refresh-days": 30 }, 'rhel-7-7': { }, 'rhel-8-0': { }, 'rhel-8-1': { }, 'rhel-atomic': { }, "selenium": { "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} && bots/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("bots", "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())