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

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