diff --git a/mirror_from_pagure.py b/mirror_from_pagure.py new file mode 100644 index 0000000..51bc14b --- /dev/null +++ b/mirror_from_pagure.py @@ -0,0 +1,100 @@ +""" +This script runs in a loop and clone or update the clone of the ansible repo +hosted in pagure.io +""" +from __future__ import print_function + +import datetime +import logging +import os +import sched +import subprocess +import sys +import time + + +_log = logging.getLogger(__name__) +s = sched.scheduler(time.time, time.sleep) + +# Time between each run in seconds +delay = 3 * 60 + +# Folder where the mirror should be +mirror_folder = "mirrors" + +# URLs to mirror +urls = ["https://pagure.io/Fedora-Infra/ansible.git"] + + +def run_command(command, cwd=None): + """ Run the specified command in a specific working directory if one + is specified. + + :arg command: the command to run + :type command: list + :kwarg cwd: the working directory in which to run this command + :type cwd: str or None + """ + output = None + try: + output = subprocess.check_output(command, cwd=cwd, stderr=subprocess.PIPE) + except subprocess.CalledProcessError as e: + _log.error("Command `%s` return code: `%s`", " ".join(command), e.returncode) + _log.error("stdout:\n-------\n%s", e.stdout) + _log.error("stderr:\n-------\n%s", e.stderr) + raise + + return output + + +def mirror_from_pagure(): + """ Sync packagers and schedules the next one. """ + for url in urls: + name = url.rsplit("/", 1)[-1] + + dest_folder = os.path.join(mirror_folder, name) + if not os.path.exists(dest_folder): + cmd = ["git", "clone", "--mirror", url] + run_command(cmd, cwd=mirror_folder) + + cmd = ["git", "fetch"] + run_command(cmd, cwd=dest_folder) + + cmd = ["git", "fsck"] + run_command(cmd, cwd=dest_folder) + + print( + "%s Run finished, next run in %s seconds" + % (datetime.datetime.utcnow().isoformat(), delay) + ) + s.enter(delay, 1, mirror_from_pagure, argument=()) + + +def main(): + """ Run the program and log error and exit if needed. """ + try: + run() + except Exception as err: + print("Error: %s" % err) + sys.exit(1) + + +def run(): + """ Schedule the first test and run the scheduler. """ + if not os.path.exists(mirror_folder): + raise OSError("No folder %s found on disk" % mirror_folder) + + s.enter(0, 1, mirror_from_pagure, argument=()) + s.run() + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + from code import InteractiveConsole + + InteractiveConsole(locals={"s": s}).interact( + "ENTERING THE DEBUG CONSOLE:\n s is the scheduler\n ^d to quit", + "LEAVING THE DEBUG CONSOLE", + ) diff --git a/mirror_from_pagure.service b/mirror_from_pagure.service new file mode 100644 index 0000000..ee3785d --- /dev/null +++ b/mirror_from_pagure.service @@ -0,0 +1,9 @@ +[Unit] +Description=Mirror the ansible repo from pagure locally +Documentation=https://pagure.io/fedora-infrastructure + +[Service] +ExecStart=/usr/bin/python /usr/libexec/mirror_from_pagure.py +Type=simple +User=packagerbot +Group=packagerbot diff --git a/mirror_from_pagure.spec b/mirror_from_pagure.spec new file mode 100644 index 0000000..0236ce1 --- /dev/null +++ b/mirror_from_pagure.spec @@ -0,0 +1,54 @@ +Name: mirror_from_pagure +Version: 0.1 +Release: 1%{?dist} +Summary: Small script and service to mirror a git repo from pagure locally + +License: MIT +URL: https://pagure.io/Fedora-Infra/mirror_from_pagure +Source0: https://releases.pagure.org/Fedora-Infra/mirror_from_pagure/mirror_from_pagure-%{version}.tar.gz + +BuildArch: noarch +BuildRequires: python-devel + +Requires: python +Requires: git +%{?systemd_requires} + + +%description +This program is a small script and service to mirror the ansible git repo +from pagure locally. + +%prep + +%build + +%install +mkdir -p %{buildroot}%{_libexecdir} +install -m 0644 mirror_from_pagure.py %{buildroot}%{_libexecdir} + +# Install the systemd file +mkdir -p %{buildroot}%{_unitdir} +install -p -m 644 files/mirror_from_pagure.service \ + %{buildroot}%{_unitdir}/mirror_from_pagure.service + + +%post +%systemd_post mirror_from_pagure.service + +%preun +%systemd_preun mirror_from_pagure.service + +%postun +%systemd_postun_with_restart mirror_from_pagure.service + + +%files +%license LICENSE +%{_libexecdir}/mirror_from_pagure.py +%{_unitdir}/mirror_from_pagure.service + + +%changelog +* Wed Apr 22 2020 Pierre-Yves Chibon - 0.1-1 +- initial package for Fedora