blob: e8698dedb3a4e6694c22abbeca84d3bd037dc94d [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
Dan Shi95329e92017-02-02 11:12:15 -08002# 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
Aviv Keshetf57a0b22018-01-24 11:55:05 -080020from chromite.lib import ts_mon_config
Dan Shi95329e92017-02-02 11:12:15 -080021
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()'
Aviv Keshetf57a0b22018-01-24 11:55:05 -080027CLEANUP_METRIC = 'chromeos/autotest/tko/cleanup_duration'
Xixuan Wu76c62442018-04-25 16:01:53 -070028RECREATE_TEST_ATTRIBUTES_METRIC = (
29 'chromeos/autotest/tko/recreate_test_attributes')
30RECREATE_TABLE = 'tko_test_attributes'
Dan Shi95329e92017-02-02 11:12:15 -080031
32
33def parse_options():
34 """Parse command line inputs.
35
36 @return: Options to run the script.
37 """
38 parser = argparse.ArgumentParser()
Xixuan Wu76c62442018-04-25 16:01:53 -070039 parser.add_argument('--recreate_test_attributes',
40 action="store_true",
41 default=False,
42 help=('Delete and recreate table tko_test_attributes.'
43 'Please use it MANUALLY with CAREFULNESS & make'
44 'sure the table is properly created back.'))
Dan Shi95329e92017-02-02 11:12:15 -080045 parser.add_argument('-l', '--logfile', type=str,
46 default=None,
47 help='Path to the log file to save logs.')
48 return parser.parse_args()
49
50
Xixuan Wu76c62442018-04-25 16:01:53 -070051def _recreate_test_attributes(server, user, password, database):
52 """Drop & recreate the table tko_test_attributes."""
53 table_schema = utils.run_sql_cmd(
54 server, user, password,
55 'SHOW CREATE TABLE %s\G' % RECREATE_TABLE, database)
56 logging.info(table_schema)
57 # Format executable command for creating table.
58 create_table_cmd = table_schema.split('Create Table: ')[1]
59 create_table_cmd = create_table_cmd.replace('`', '').replace('\n', '')
60 utils.run_sql_cmd(server, user, password,
61 'DROP TABLE IF EXISTS %s' % RECREATE_TABLE, database)
62 utils.run_sql_cmd(server, user, password, create_table_cmd, database)
63
64
Dan Shi95329e92017-02-02 11:12:15 -080065def main():
66 """Main script."""
67 options = parse_options()
68 log_config = logging_config.LoggingConfig()
69 if options.logfile:
70 log_config.add_file_handler(
71 file_path=os.path.abspath(options.logfile), level=logging.DEBUG)
72
Aviv Keshetf57a0b22018-01-24 11:55:05 -080073 with ts_mon_config.SetupTsMonGlobalState(service_name='cleanup_tko_db',
74 indirect=True):
75 server = CONFIG.get_config_value(
76 'AUTOTEST_WEB', 'global_db_host',
77 default=CONFIG.get_config_value('AUTOTEST_WEB', 'host'))
78 user = CONFIG.get_config_value(
79 'AUTOTEST_WEB', 'global_db_user',
80 default=CONFIG.get_config_value('AUTOTEST_WEB', 'user'))
81 password = CONFIG.get_config_value(
82 'AUTOTEST_WEB', 'global_db_password',
83 default=CONFIG.get_config_value('AUTOTEST_WEB', 'password'))
84 database = CONFIG.get_config_value(
85 'AUTOTEST_WEB', 'global_db_database',
86 default=CONFIG.get_config_value('AUTOTEST_WEB', 'database'))
Dan Shi95329e92017-02-02 11:12:15 -080087
Aviv Keshetf57a0b22018-01-24 11:55:05 -080088 logging.info('Starting cleaning up old records in TKO database %s on '
89 'server %s.', database, server)
Dan Shi95329e92017-02-02 11:12:15 -080090
Aviv Keshetf57a0b22018-01-24 11:55:05 -080091 start_time = time.time()
92 try:
Xixuan Wu76c62442018-04-25 16:01:53 -070093 if options.recreate_test_attributes:
94 with metrics.SecondsTimer(RECREATE_TEST_ATTRIBUTES_METRIC,
95 fields={'success': False}) as fields:
96 _recreate_test_attributes(server, user, password, database)
97 fields['success'] = True
98 else:
99 with metrics.SecondsTimer(CLEANUP_METRIC,
100 fields={'success': False}) as fields:
101 utils.run_sql_cmd(server, user, password, CLEANUP_TKO_CMD,
102 database)
103 fields['success'] = True
Aviv Keshetf57a0b22018-01-24 11:55:05 -0800104 except:
105 logging.exception('Cleanup failed with exception.')
106 finally:
107 duration = time.time() - start_time
108 logging.info('Cleanup attempt finished in %s seconds.', duration)
Dan Shi95329e92017-02-02 11:12:15 -0800109
110
111if __name__ == '__main__':
112 main()