Initial import of the code

This includes the script doing the actual mirroring, the systemd service
file and a spec file to compile and install this all

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
This commit is contained in:
Pierre-Yves Chibon 2020-04-22 12:44:46 +02:00
parent 7c0cfe9cca
commit 75ee3327a1
3 changed files with 163 additions and 0 deletions

100
mirror_from_pagure.py Normal file
View file

@ -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",
)

View file

@ -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

54
mirror_from_pagure.spec Normal file
View file

@ -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 <pingou@pingoured.fr> - 0.1-1
- initial package for Fedora