Skip to content

Commit

Permalink
backend: manpage for copr-backend-resultdir-cleaner && logger fix
Browse files Browse the repository at this point in the history
Build the manual page from an updated --help output.  The logger needs
to be initialized in `_main`, otherwise the log-file can mistakenly be
created under the `root` user before the `script_requires_user()` check
is done.

Relates: fedora-copr#3495
  • Loading branch information
praiskup committed Nov 1, 2024
1 parent 4ec0f9a commit 675d740
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
11 changes: 8 additions & 3 deletions backend/copr-backend.spec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Source1: https://github.com/fedora-copr/%{tests_tar}/archive/v%{tests_version

BuildArch: noarch
BuildRequires: asciidoc
BuildRequires: argparse-manpage
BuildRequires: createrepo_c >= 0.16.1
BuildRequires: libappstream-glib-builder
BuildRequires: libxslt
Expand Down Expand Up @@ -131,7 +132,8 @@ only.
%build
make -C docs %{?_smp_mflags} html
%py3_build

PYTHONPATH=`pwd` argparse-manpage --pyfile run/copr-backend-resultdir-cleaner \
--function _get_arg_parser > copr-backend-resultdir-cleaner.1

%install
%py3_install
Expand Down Expand Up @@ -180,6 +182,9 @@ cp -a conf/logstash/copr_backend.conf %{buildroot}%{_pkgdocdir}/examples/%{_sysc

cp -a docs/build/html %{buildroot}%{_pkgdocdir}/

install -d %{buildroot}%{_mandir}/man1
install -p -m 644 copr-backend-resultdir-cleaner.1 %{buildroot}/%{_mandir}/man1/


%check
./run_tests.sh -vv --no-cov
Expand Down Expand Up @@ -229,12 +234,12 @@ useradd -r -g copr -G lighttpd -s /bin/bash -c "COPR user" copr
%config(noreplace) %{_sysconfdir}/cron.weekly/copr-backend
%{_datadir}/logstash/patterns/lighttpd.pattern


%config(noreplace) %attr(0600, root, root) %{_sysconfdir}/sudoers.d/copr

%{_mandir}/man1/copr-backend*.1*

%files doc
%license LICENSE
%doc
%{_pkgdocdir}/
%exclude %{_pkgdocdir}/lighttpd

Expand Down
30 changes: 18 additions & 12 deletions backend/run/copr-backend-resultdir-cleaner
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/python3

"""
Cleanup the old chroot_scan folders
Cleanup the files in resultdir that are no longer needed.
"""

import logging
Expand All @@ -17,19 +17,23 @@ from copr_common.helpers import script_requires_user
from copr_backend.helpers import BackendConfigReader


logging.basicConfig(level=logging.DEBUG)
LOG = logging.getLogger(__name__)
setup_script_logger(LOG, "/var/log/copr-backend/resultdir-cleaner.log")

OLDER_THAN = time.time() - 24*3600*14

parser = argparse.ArgumentParser(
description=("TBD")
)
parser.add_argument(
"--real-run",
action='store_true',
help=("Also perform the changes, not just checks"))

def _get_arg_parser():
parser = argparse.ArgumentParser(
description=(
"Traverse the Copr Backend result directory and remove things "
"that are no longer needed → outdated log files, not uncleaned "
"temporary directories, etc."))
parser.add_argument(
"--real-run",
action='store_true',
help=(
"Perform the real removals (by default the tool just prints "
"what would normally happen = \"dry run\")."))
return parser


def remove_old_dir(directory, dry_run):
Expand Down Expand Up @@ -162,7 +166,9 @@ def clean_in(resultdir, dry_run=True):


def _main():
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
setup_script_logger(LOG, "/var/log/copr-backend/resultdir-cleaner.log")
args = _get_arg_parser().parse_args()
dry_run = not args.real_run
if dry_run:
LOG.warning("Just doing dry run, run with --real-run")
Expand Down

0 comments on commit 675d740

Please sign in to comment.