mirror_from_pagure/mirror_from_pagure.py
Pierre-Yves Chibon d390ef18f4 Rework how logging it done
This will help knowing what the service is doing

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
2020-04-22 13:00:13 +02:00

96 lines
2.5 KiB
Python

"""
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:
_log.info("Syncing %s", url)
name = url.rsplit("/", 1)[-1]
dest_folder = os.path.join(mirror_folder, name)
if not os.path.exists(dest_folder):
_log.info(" Cloning as new %s", url)
cmd = ["git", "clone", "--mirror", url]
run_command(cmd, cwd=mirror_folder)
_log.info(" Running git fetch")
cmd = ["git", "fetch"]
run_command(cmd, cwd=dest_folder)
_log.info(" Running git fsck")
cmd = ["git", "fsck"]
run_command(cmd, cwd=dest_folder)
_log.info("Run finished, next run in %s seconds", 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__":
formater = '%(asctime)-15s %(message)s'
logging.basicConfig(format=formater)
_log.setLevel(logging.INFO)
main()