bundle db maintenance script to hub

Fixes: https://pagure.io/koji/issue/1478
This commit is contained in:
Tomas Kopecek 2019-06-11 15:02:30 +02:00
parent 66f8cd4cd7
commit f68eeb6cc0
6 changed files with 123 additions and 2 deletions

View file

@ -497,6 +497,28 @@ there is no password manipulation support exposed through the koji tools.
The sql commands you need to use vary by authentication mechanism.
Maintaining database
--------------------
For now, there is one table which needs periodical cleanup. As postgres doesn't
have any mechanism for this, we need to do it via some other mechanism. Default
handling is done by cron, but can be substituted by anything else (Ansible
tower, etc.)
Script is by default installed on hub as `/usr/sbin/koji-sweepd-db`. On systemd
systems it also has corresponding `koji-sweep-db` service and timer. Note, that
timer is not enabled by default, so you need to run usual `systemctl` commands.
Cron files are not distributed, but are pretty easy to set up, if you prefer
one.
If you don't want to use this script, be sure to run following SQL with
appropriate age setting. Default value of one day should be ok for normal
deployments.
::
DELETE FROM sessions WHERE update_time < now() - '1 day'::interval;
Set User/Password Authentication
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View file

@ -454,7 +454,7 @@ done
make DESTDIR=$RPM_BUILD_ROOT PYTHON=%{__python3} %{?install_opt} install
# alter python interpreter in koji CLI
scripts='%{_bindir}/koji %{_sbindir}/kojid %{_sbindir}/kojira %{_sbindir}/koji-shadow
%{_sbindir}/koji-gc %{_sbindir}/kojivmd'
%{_sbindir}/koji-gc %{_sbindir}/kojivmd %{_sbindir}/koji-sweep-db'
for fn in $scripts ; do
sed -i 's|#!/usr/bin/python2|#!/usr/bin/python3|' $RPM_BUILD_ROOT$fn
done
@ -538,6 +538,11 @@ rm -rf $RPM_BUILD_ROOT
%dir /etc/koji-hub
%config(noreplace) /etc/koji-hub/hub.conf
%dir /etc/koji-hub/hub.conf.d
%{_sbindir}/koji-sweep-db
%if %{use_systemd}
%{_unitdir}/koji-sweep-db.service
%{_unitdir}/koji-sweep-db.timer
%endif
%if 0%{py2_support} > 1
%files -n python2-%{name}-hub

View file

@ -1,4 +1,4 @@
BINFILES = kojira koji-gc koji-shadow
BINFILES = kojira koji-gc koji-shadow koji-sweep-db
SYSTEMDSYSTEMUNITDIR = $(shell pkg-config systemd --variable=systemdsystemunitdir)
TYPE = systemd
@ -30,6 +30,8 @@ _install:
install-systemd: _install
mkdir -p $(DESTDIR)$(SYSTEMDSYSTEMUNITDIR)
install -p -m 644 kojira.service $(DESTDIR)$(SYSTEMDSYSTEMUNITDIR)
install -p -m 644 koji-sweep-db.service $(DESTDIR)$(SYSTEMDSYSTEMUNITDIR)
install -p -m 644 koji-sweep-db.timer $(DESTDIR)$(SYSTEMDSYSTEMUNITDIR)
install-sysv: _install
mkdir -p $(DESTDIR)/etc/rc.d/init.d

70
util/koji-sweep-db Executable file
View file

@ -0,0 +1,70 @@
#!/usr/bin/python2
import os
from optparse import OptionParser
from six.moves.configparser import RawConfigParser
import koji.db
def clean_sessions(cursor):
q = " FROM sessions WHERE update_time < now() - '1 day'::interval"
if options.verbose:
cursor.execute("SELECT COUNT(*) " + q)
rows = cursor.fetchall()[0][0]
print("Deleting %d sessions" % rows)
cursor.execute("DELETE " + q)
if __name__ == "__main__":
global options
parser = OptionParser("%prog cleans koji database")
parser.add_option('-v', '--verbose', action="store_true", help="Be verbose")
parser.add_option('-c', '--conf', default='/etc/koji-hub/hub.conf',
action='store', help="Path to koji's hub.conf")
options, args = parser.parse_args()
if not os.path.exists(options.conf):
parser.error("Config file doesn't exist")
config = RawConfigParser()
config.read(options.conf)
cfgmap = [
#option, type, default
['DBName', 'string', None],
['DBUser', 'string', None],
['DBHost', 'string', None],
['DBhost', 'string', None], # alias for backwards compatibility
['DBPort', 'integer', None],
['DBPass', 'string', None],
]
opts = {}
for name, dtype, default in cfgmap:
key = ('hub', name)
if config and config.has_option(*key):
if dtype == 'integer':
opts[name] = config.getint(*key)
elif dtype == 'boolean':
opts[name] = config.getboolean(*key)
else:
opts[name] = config.get(*key)
continue
opts[name] = default
if opts['DBHost'] is None:
opts['DBHost'] = opts['DBhost']
koji.db.provideDBopts(database=opts["DBName"],
user=opts["DBUser"],
password=opts.get("DBPass", None),
host=opts.get("DBHost", None),
port=opts.get("DBPort", None))
conn = koji.db.connect()
cursor = conn.cursor()
clean_sessions(cursor)
conn.commit()

View file

@ -0,0 +1,10 @@
[Unit]
Description=Daily maintenance script for koji db
Documentation=https://pagure.io/docs/koji/
[Service]
Type=simple
ExecStart=/usr/sbin/koji-sweep-db
[Install]
WantedBy=multi-user.target

12
util/koji-sweep-db.timer Normal file
View file

@ -0,0 +1,12 @@
[Unit]
Description=Daily maintenance script for koji db
Documentation=https://pagure.io/docs/koji/
[Timer]
OnCalendar=daily
Accuracy=1h
Persistent=true
Unit
[Install]
WantedBy=timers.target