• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2# Copyright 2017 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Utility to cleanup TKO database by removing old records.
7"""
8
9import argparse
10import logging
11import os
12import time
13
14import common
15from autotest_lib.client.bin import utils
16from autotest_lib.client.common_lib import global_config
17from autotest_lib.client.common_lib import logging_config
18
19from chromite.lib import metrics
20from chromite.lib import ts_mon_config
21
22
23CONFIG = global_config.global_config
24
25# SQL command to remove old test results in TKO database.
26CLEANUP_TKO_CMD = 'call remove_old_tests_sp()'
27CLEANUP_METRIC = 'chromeos/autotest/tko/cleanup_duration'
28
29
30def parse_options():
31    """Parse command line inputs.
32
33    @return: Options to run the script.
34    """
35    parser = argparse.ArgumentParser()
36    parser.add_argument('-l', '--logfile', type=str,
37                        default=None,
38                        help='Path to the log file to save logs.')
39    return parser.parse_args()
40
41
42def main():
43    """Main script."""
44    options = parse_options()
45    log_config = logging_config.LoggingConfig()
46    if options.logfile:
47        log_config.add_file_handler(
48                file_path=os.path.abspath(options.logfile), level=logging.DEBUG)
49
50    with ts_mon_config.SetupTsMonGlobalState(service_name='cleanup_tko_db',
51                                             indirect=True):
52        server = CONFIG.get_config_value(
53                    'AUTOTEST_WEB', 'global_db_host',
54                    default=CONFIG.get_config_value('AUTOTEST_WEB', 'host'))
55        user = CONFIG.get_config_value(
56                    'AUTOTEST_WEB', 'global_db_user',
57                    default=CONFIG.get_config_value('AUTOTEST_WEB', 'user'))
58        password = CONFIG.get_config_value(
59                    'AUTOTEST_WEB', 'global_db_password',
60                    default=CONFIG.get_config_value('AUTOTEST_WEB', 'password'))
61        database = CONFIG.get_config_value(
62                    'AUTOTEST_WEB', 'global_db_database',
63                    default=CONFIG.get_config_value('AUTOTEST_WEB', 'database'))
64
65        logging.info('Starting cleaning up old records in TKO database %s on '
66                     'server %s.', database, server)
67
68        start_time = time.time()
69        try:
70            with metrics.SecondsTimer(CLEANUP_METRIC,
71                                      fields={'success': False}) as fields:
72                utils.run_sql_cmd(server, user, password, CLEANUP_TKO_CMD,
73                                  database)
74                fields['success'] = True
75        except:
76            logging.exception('Cleanup failed with exception.')
77        finally:
78            duration = time.time() - start_time
79            logging.info('Cleanup attempt finished in %s seconds.', duration)
80
81
82if __name__ == '__main__':
83    main()
84