blob: 0f623af39eb2c302232f2d54eca91c8542dbbaa6 [file] [log] [blame]
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Utility to cleanup TKO database by removing old records.
import argparse
import logging
import os
import time
import common
from autotest_lib.client.bin import utils
from autotest_lib.client.common_lib import global_config
from autotest_lib.client.common_lib import logging_config
from chromite.lib import metrics
from chromite.lib import ts_mon_config
CONFIG = global_config.global_config
# SQL command to remove old test results in TKO database.
CLEANUP_TKO_CMD = 'call remove_old_tests_sp()'
CLEANUP_METRIC = 'chromeos/autotest/tko/cleanup_duration'
def parse_options():
"""Parse command line inputs.
@return: Options to run the script.
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--logfile', type=str,
help='Path to the log file to save logs.')
return parser.parse_args()
def main():
"""Main script."""
options = parse_options()
log_config = logging_config.LoggingConfig()
if options.logfile:
file_path=os.path.abspath(options.logfile), level=logging.DEBUG)
with ts_mon_config.SetupTsMonGlobalState(service_name='cleanup_tko_db',
server = CONFIG.get_config_value(
'AUTOTEST_WEB', 'global_db_host',
default=CONFIG.get_config_value('AUTOTEST_WEB', 'host'))
user = CONFIG.get_config_value(
'AUTOTEST_WEB', 'global_db_user',
default=CONFIG.get_config_value('AUTOTEST_WEB', 'user'))
password = CONFIG.get_config_value(
'AUTOTEST_WEB', 'global_db_password',
default=CONFIG.get_config_value('AUTOTEST_WEB', 'password'))
database = CONFIG.get_config_value(
'AUTOTEST_WEB', 'global_db_database',
default=CONFIG.get_config_value('AUTOTEST_WEB', 'database'))'Starting cleaning up old records in TKO database %s on '
'server %s.', database, server)
start_time = time.time()
with metrics.SecondsTimer(CLEANUP_METRIC,
fields={'success': False}) as fields:
utils.run_sql_cmd(server, user, password, CLEANUP_TKO_CMD,
fields['success'] = True
logging.exception('Cleanup failed with exception.')
duration = time.time() - start_time'Cleanup attempt finished in %s seconds.', duration)
if __name__ == '__main__':