debian-koji/util/koji-sweep-db
2019-12-12 11:00:55 +00:00

90 lines
2.8 KiB
Python
Executable file

#!/usr/bin/python2
import os
from optparse import OptionParser
from six.moves.configparser import RawConfigParser
import koji.db
def clean_sessions(cursor, vacuum):
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 vacuum:
cursor.execute("VACUUM ANALYZE sessions")
def clean_reservations(cursor, vacuum):
q = " FROM build_reservations WHERE created < now() - '1 day'::interval"
if options.verbose:
cursor.execute("SELECT COUNT(*) " + q)
rows = cursor.fetchall()[0][0]
print("Deleting %d build reservations" % rows)
cursor.execute("DELETE " + q)
if vacuum:
cursor.execute("VACUUM ANALYZE build_reservations")
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")
parser.add_option('--no-vacuum', action="store_false", dest="vacuum",
default=True,
help="Don't run vacuum on affected tables")
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()
conn.set_session(autocommit=True)
cursor = conn.cursor()
clean_sessions(cursor, options.vacuum)
clean_reservations(cursor, options.vacuum)
conn.commit()