Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 1 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Allen Li | 64692a3 | 2016-11-04 13:35:19 -0700 | [diff] [blame] | 5 | import math |
| 6 | import os |
| 7 | import random |
| 8 | import re |
| 9 | import sys |
| 10 | import time |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 11 | |
| 12 | import common |
| 13 | from autotest_lib.client.common_lib import global_config |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 14 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 15 | from autotest_lib.frontend import database_settings_helper |
Dan Shi | 70647ca | 2015-07-16 22:52:35 -0700 | [diff] [blame] | 16 | from autotest_lib.server import site_utils |
jamesren | a12b8a0 | 2010-06-16 23:28:23 +0000 | [diff] [blame] | 17 | from autotest_lib.tko import utils |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 18 | |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 19 | |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 20 | def _log_error(msg): |
| 21 | """Log an error message. |
| 22 | |
| 23 | @param msg: Message string |
| 24 | """ |
| 25 | print >> sys.stderr, msg |
| 26 | sys.stderr.flush() # we want these msgs to show up immediately |
| 27 | |
| 28 | |
| 29 | def _format_operational_error(e): |
| 30 | """Format OperationalError. |
| 31 | |
| 32 | @param e: OperationalError instance. |
| 33 | """ |
| 34 | return ("%s: An operational error occurred during a database " |
| 35 | "operation: %s" % (time.strftime("%X %x"), str(e))) |
| 36 | |
| 37 | |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 38 | class MySQLTooManyRows(Exception): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 39 | """Too many records.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 40 | pass |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 41 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 42 | |
mbligh | 7636b3a | 2008-06-11 15:44:01 +0000 | [diff] [blame] | 43 | class db_sql(object): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 44 | """Data access.""" |
| 45 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 46 | def __init__(self, debug=False, autocommit=True, host=None, |
| 47 | database=None, user=None, password=None): |
| 48 | self.debug = debug |
| 49 | self.autocommit = autocommit |
| 50 | self._load_config(host, database, user, password) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 51 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 52 | self.con = None |
| 53 | self._init_db() |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 54 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 55 | # if not present, insert statuses |
| 56 | self.status_idx = {} |
| 57 | self.status_word = {} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 58 | status_rows = self.select('status_idx, word', 'tko_status', None) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 59 | for s in status_rows: |
| 60 | self.status_idx[s[1]] = s[0] |
| 61 | self.status_word[s[0]] = s[1] |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 62 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 63 | machine_map = os.path.join(os.path.dirname(__file__), |
| 64 | 'machines') |
| 65 | if os.path.exists(machine_map): |
| 66 | self.machine_map = machine_map |
| 67 | else: |
| 68 | self.machine_map = None |
| 69 | self.machine_group = {} |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 70 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 71 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 72 | def _load_config(self, host, database, user, password): |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 73 | """Loads configuration settings required to connect to the database. |
| 74 | |
| 75 | This will try to connect to use the settings prefixed with global_db_. |
| 76 | If they do not exist, they un-prefixed settings will be used. |
| 77 | |
| 78 | If parameters are supplied, these will be taken instead of the values |
| 79 | in global_config. |
| 80 | |
| 81 | @param host: If set, this host will be used, if not, the host will be |
| 82 | retrieved from global_config. |
| 83 | @param database: If set, this database will be used, if not, the |
| 84 | database will be retrieved from global_config. |
| 85 | @param user: If set, this user will be used, if not, the |
| 86 | user will be retrieved from global_config. |
| 87 | @param password: If set, this password will be used, if not, the |
| 88 | password will be retrieved from global_config. |
| 89 | """ |
| 90 | database_settings = database_settings_helper.get_global_db_config() |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 91 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 92 | # grab the host, database |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 93 | self.host = host or database_settings['HOST'] |
| 94 | self.database = database or database_settings['NAME'] |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 95 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 96 | # grab the user and password |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 97 | self.user = user or database_settings['USER'] |
| 98 | self.password = password or database_settings['PASSWORD'] |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 99 | |
Michael Spang | 7a27347 | 2014-10-08 12:08:13 -0400 | [diff] [blame] | 100 | # grab the timeout configuration |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 101 | self.query_timeout =( |
| 102 | database_settings.get('OPTIONS', {}).get('timeout', 3600)) |
| 103 | |
| 104 | # Using fallback to non-global in order to work without configuration |
| 105 | # overhead on non-shard instances. |
| 106 | get_value = global_config.global_config.get_config_value_with_fallback |
Jakob Juelich | 475b82b | 2014-09-30 11:17:07 -0700 | [diff] [blame] | 107 | self.min_delay = get_value("AUTOTEST_WEB", "global_db_min_retry_delay", |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 108 | "min_retry_delay", type=int, default=20) |
Jakob Juelich | 475b82b | 2014-09-30 11:17:07 -0700 | [diff] [blame] | 109 | self.max_delay = get_value("AUTOTEST_WEB", "global_db_max_retry_delay", |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 110 | "max_retry_delay", type=int, default=60) |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 111 | |
Richard Barnette | 2468fbd | 2014-11-07 01:12:46 +0000 | [diff] [blame] | 112 | # TODO(beeps): Move this to django settings once we have routers. |
| 113 | # On test instances mysql connects through a different port. No point |
| 114 | # piping this through our entire infrastructure when it is only really |
| 115 | # used for testing; Ideally we would specify this through django |
| 116 | # settings and default it to the empty string so django will figure out |
| 117 | # the default based on the database backend (eg: mysql, 3306), but until |
| 118 | # we have database routers in place any django settings will apply to |
| 119 | # both tko and afe. |
| 120 | # The intended use of this port is to allow a testing shard vm to |
| 121 | # update the master vm's database with test results. Specifying |
| 122 | # and empty string will fallback to not even specifying the port |
| 123 | # to the backend in tko/db.py. Unfortunately this means retries |
| 124 | # won't work on the test cluster till we've migrated to routers. |
| 125 | self.port = global_config.global_config.get_config_value( |
| 126 | "AUTOTEST_WEB", "global_db_port", type=str, default='') |
| 127 | |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 128 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 129 | def _init_db(self): |
| 130 | # make sure we clean up any existing connection |
| 131 | if self.con: |
| 132 | self.con.close() |
| 133 | self.con = None |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 134 | |
Dan Shi | 8684b92 | 2015-10-06 13:29:18 -0700 | [diff] [blame] | 135 | try: |
| 136 | # create the db connection and cursor |
| 137 | self.con = self.connect(self.host, self.database, |
| 138 | self.user, self.password, self.port) |
| 139 | except: |
| 140 | autotest_stats.Counter('tko_db_con_error').increment() |
| 141 | raise |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 142 | self.cur = self.con.cursor() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 143 | |
| 144 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 145 | def _random_delay(self): |
| 146 | delay = random.randint(self.min_delay, self.max_delay) |
| 147 | time.sleep(delay) |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 148 | |
| 149 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 150 | def run_with_retry(self, function, *args, **dargs): |
| 151 | """Call function(*args, **dargs) until either it passes |
| 152 | without an operational error, or a timeout is reached. |
| 153 | This will re-connect to the database, so it is NOT safe |
| 154 | to use this inside of a database transaction. |
jadmanski | e7a6909 | 2008-05-29 21:03:13 +0000 | [diff] [blame] | 155 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 156 | It can be safely used with transactions, but the |
| 157 | transaction start & end must be completely contained |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 158 | within the call to 'function'. |
| 159 | |
| 160 | @param function: The function to run with retry. |
| 161 | @param args: The arguments |
| 162 | @param dargs: The named arguments. |
| 163 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 164 | OperationalError = _get_error_class("OperationalError") |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 165 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 166 | success = False |
| 167 | start_time = time.time() |
| 168 | while not success: |
| 169 | try: |
| 170 | result = function(*args, **dargs) |
| 171 | except OperationalError, e: |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 172 | _log_error("%s; retrying, don't panic yet" |
| 173 | % _format_operational_error(e)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 174 | stop_time = time.time() |
| 175 | elapsed_time = stop_time - start_time |
| 176 | if elapsed_time > self.query_timeout: |
| 177 | raise |
| 178 | else: |
| 179 | try: |
| 180 | self._random_delay() |
| 181 | self._init_db() |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 182 | autotest_stats.Counter('tko_db_error').increment() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 183 | except OperationalError, e: |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 184 | _log_error('%s; panic now' |
| 185 | % _format_operational_error(e)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 186 | else: |
| 187 | success = True |
| 188 | return result |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 189 | |
| 190 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 191 | def dprint(self, value): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 192 | """Print out debug value. |
| 193 | |
| 194 | @param value: The value to print out. |
| 195 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 196 | if self.debug: |
| 197 | sys.stdout.write('SQL: ' + str(value) + '\n') |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 198 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 199 | |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 200 | def _commit(self): |
| 201 | """Private method for function commit to call for retry. |
| 202 | """ |
| 203 | return self.con.commit() |
| 204 | |
| 205 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 206 | def commit(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 207 | """Commit the sql transaction.""" |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 208 | if self.autocommit: |
| 209 | return self.run_with_retry(self._commit) |
| 210 | else: |
| 211 | return self._commit() |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 212 | |
| 213 | |
Simran Basi | e129a96 | 2012-08-31 13:03:53 -0700 | [diff] [blame] | 214 | def rollback(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 215 | """Rollback the sql transaction.""" |
Simran Basi | e129a96 | 2012-08-31 13:03:53 -0700 | [diff] [blame] | 216 | self.con.rollback() |
| 217 | |
| 218 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 219 | def get_last_autonumber_value(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 220 | """Gets the last auto number. |
| 221 | |
| 222 | @return: The last auto number. |
| 223 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 224 | self.cur.execute('SELECT LAST_INSERT_ID()', []) |
| 225 | return self.cur.fetchall()[0][0] |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 226 | |
| 227 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 228 | def _quote(self, field): |
| 229 | return '`%s`' % field |
| 230 | |
| 231 | |
| 232 | def _where_clause(self, where): |
| 233 | if not where: |
| 234 | return '', [] |
| 235 | |
| 236 | if isinstance(where, dict): |
| 237 | # key/value pairs (which should be equal, or None for null) |
| 238 | keys, values = [], [] |
| 239 | for field, value in where.iteritems(): |
| 240 | quoted_field = self._quote(field) |
| 241 | if value is None: |
| 242 | keys.append(quoted_field + ' is null') |
| 243 | else: |
| 244 | keys.append(quoted_field + '=%s') |
| 245 | values.append(value) |
| 246 | where_clause = ' and '.join(keys) |
| 247 | elif isinstance(where, basestring): |
| 248 | # the exact string |
| 249 | where_clause = where |
| 250 | values = [] |
| 251 | elif isinstance(where, tuple): |
| 252 | # preformatted where clause + values |
| 253 | where_clause, values = where |
| 254 | assert where_clause |
| 255 | else: |
| 256 | raise ValueError('Invalid "where" value: %r' % where) |
| 257 | |
| 258 | return ' WHERE ' + where_clause, values |
| 259 | |
| 260 | |
| 261 | |
| 262 | def select(self, fields, table, where, distinct=False, group_by=None, |
| 263 | max_rows=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 264 | """\ |
| 265 | This selects all the fields requested from a |
| 266 | specific table with a particular where clause. |
| 267 | The where clause can either be a dictionary of |
| 268 | field=value pairs, a string, or a tuple of (string, |
| 269 | a list of values). The last option is what you |
| 270 | should use when accepting user input as it'll |
| 271 | protect you against sql injection attacks (if |
| 272 | all user data is placed in the array rather than |
| 273 | the raw SQL). |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 274 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 275 | For example: |
| 276 | where = ("a = %s AND b = %s", ['val', 'val']) |
| 277 | is better than |
| 278 | where = "a = 'val' AND b = 'val'" |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 279 | |
| 280 | @param fields: The list of selected fields string. |
| 281 | @param table: The name of the database table. |
| 282 | @param where: The where clause string. |
| 283 | @param distinct: If select distinct values. |
| 284 | @param group_by: Group by clause. |
| 285 | @param max_rows: unused. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 286 | """ |
| 287 | cmd = ['select'] |
| 288 | if distinct: |
| 289 | cmd.append('distinct') |
| 290 | cmd += [fields, 'from', table] |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 291 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 292 | where_clause, values = self._where_clause(where) |
| 293 | cmd.append(where_clause) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 294 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 295 | if group_by: |
| 296 | cmd.append(' GROUP BY ' + group_by) |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 297 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 298 | self.dprint('%s %s' % (' '.join(cmd), values)) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 299 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 300 | # create a re-runable function for executing the query |
| 301 | def exec_sql(): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 302 | """Exeuctes an the sql command.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 303 | sql = ' '.join(cmd) |
| 304 | numRec = self.cur.execute(sql, values) |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 305 | if max_rows is not None and numRec > max_rows: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 306 | msg = 'Exceeded allowed number of records' |
| 307 | raise MySQLTooManyRows(msg) |
| 308 | return self.cur.fetchall() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 309 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 310 | # run the query, re-trying after operational errors |
| 311 | if self.autocommit: |
| 312 | return self.run_with_retry(exec_sql) |
| 313 | else: |
| 314 | return exec_sql() |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 315 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 316 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 317 | def select_sql(self, fields, table, sql, values): |
| 318 | """\ |
| 319 | select fields from table "sql" |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 320 | |
| 321 | @param fields: The list of selected fields string. |
| 322 | @param table: The name of the database table. |
| 323 | @param sql: The sql string. |
| 324 | @param values: The sql string parameter values. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 325 | """ |
| 326 | cmd = 'select %s from %s %s' % (fields, table, sql) |
| 327 | self.dprint(cmd) |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 328 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 329 | # create a -re-runable function for executing the query |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 330 | def _exec_sql(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 331 | self.cur.execute(cmd, values) |
| 332 | return self.cur.fetchall() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 333 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 334 | # run the query, re-trying after operational errors |
| 335 | if self.autocommit: |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 336 | return self.run_with_retry(_exec_sql) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 337 | else: |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 338 | return _exec_sql() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 339 | |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 340 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 341 | def _exec_sql_with_commit(self, sql, values, commit): |
| 342 | if self.autocommit: |
| 343 | # re-run the query until it succeeds |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 344 | def _exec_sql(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 345 | self.cur.execute(sql, values) |
| 346 | self.con.commit() |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 347 | self.run_with_retry(_exec_sql) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 348 | else: |
| 349 | # take one shot at running the query |
| 350 | self.cur.execute(sql, values) |
| 351 | if commit: |
| 352 | self.con.commit() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 353 | |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 354 | |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 355 | def insert(self, table, data, commit=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 356 | """\ |
| 357 | 'insert into table (keys) values (%s ... %s)', values |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 358 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 359 | data: |
| 360 | dictionary of fields and data |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 361 | |
| 362 | @param table: The name of the table. |
| 363 | @param data: The insert data. |
| 364 | @param commit: If commit the transaction . |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 365 | """ |
| 366 | fields = data.keys() |
| 367 | refs = ['%s' for field in fields] |
| 368 | values = [data[field] for field in fields] |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 369 | cmd = ('insert into %s (%s) values (%s)' % |
| 370 | (table, ','.join(self._quote(field) for field in fields), |
| 371 | ','.join(refs))) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 372 | self.dprint('%s %s' % (cmd, values)) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 373 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 374 | self._exec_sql_with_commit(cmd, values, commit) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 375 | |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 376 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 377 | def delete(self, table, where, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 378 | """Delete entries. |
| 379 | |
| 380 | @param table: The name of the table. |
| 381 | @param where: The where clause. |
| 382 | @param commit: If commit the transaction . |
| 383 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 384 | cmd = ['delete from', table] |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 385 | if commit is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 386 | commit = self.autocommit |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 387 | where_clause, values = self._where_clause(where) |
| 388 | cmd.append(where_clause) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 389 | sql = ' '.join(cmd) |
| 390 | self.dprint('%s %s' % (sql, values)) |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 391 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 392 | self._exec_sql_with_commit(sql, values, commit) |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 393 | |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 394 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 395 | def update(self, table, data, where, commit = None): |
| 396 | """\ |
| 397 | 'update table set data values (%s ... %s) where ...' |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 398 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 399 | data: |
| 400 | dictionary of fields and data |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 401 | |
| 402 | @param table: The name of the table. |
| 403 | @param data: The sql parameter values. |
| 404 | @param where: The where clause. |
| 405 | @param commit: If commit the transaction . |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 406 | """ |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 407 | if commit is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 408 | commit = self.autocommit |
| 409 | cmd = 'update %s ' % table |
| 410 | fields = data.keys() |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 411 | data_refs = [self._quote(field) + '=%s' for field in fields] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 412 | data_values = [data[field] for field in fields] |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 413 | cmd += ' set ' + ', '.join(data_refs) |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 414 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 415 | where_clause, where_values = self._where_clause(where) |
| 416 | cmd += where_clause |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 417 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 418 | values = data_values + where_values |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 419 | self.dprint('%s %s' % (cmd, values)) |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 420 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 421 | self._exec_sql_with_commit(cmd, values, commit) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 422 | |
| 423 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 424 | def delete_job(self, tag, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 425 | """Delete a tko job. |
| 426 | |
| 427 | @param tag: The job tag. |
| 428 | @param commit: If commit the transaction . |
| 429 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 430 | job_idx = self.find_job(tag) |
| 431 | for test_idx in self.find_tests(job_idx): |
| 432 | where = {'test_idx' : test_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 433 | self.delete('tko_iteration_result', where) |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 434 | self.delete('tko_iteration_perf_value', where) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 435 | self.delete('tko_iteration_attributes', where) |
| 436 | self.delete('tko_test_attributes', where) |
| 437 | self.delete('tko_test_labels_tests', {'test_id': test_idx}) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 438 | where = {'job_idx' : job_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 439 | self.delete('tko_tests', where) |
| 440 | self.delete('tko_jobs', where) |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 441 | |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 442 | |
Dan Shi | 70647ca | 2015-07-16 22:52:35 -0700 | [diff] [blame] | 443 | def insert_job(self, tag, job, parent_job_id=None, commit=None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 444 | """Insert a tko job. |
| 445 | |
| 446 | @param tag: The job tag. |
| 447 | @param job: The job object. |
| 448 | @param parent_job_id: The parent job id. |
| 449 | @param commit: If commit the transaction . |
Shuqian Zhao | 31425d5 | 2016-12-07 09:35:03 -0800 | [diff] [blame] | 450 | |
| 451 | @return The dict of data inserted into the tko_jobs table. |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 452 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 453 | job.machine_idx = self.lookup_machine(job.machine) |
| 454 | if not job.machine_idx: |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 455 | job.machine_idx = self.insert_machine(job, commit=commit) |
Dan Shi | d77fc1b | 2015-10-13 17:34:50 -0700 | [diff] [blame] | 456 | elif job.machine: |
| 457 | # Only try to update tko_machines record if machine is set. This |
| 458 | # prevents unnecessary db writes for suite jobs. |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 459 | self.update_machine_information(job, commit=commit) |
| 460 | |
jamesren | a12b8a0 | 2010-06-16 23:28:23 +0000 | [diff] [blame] | 461 | afe_job_id = utils.get_afe_job_id(tag) |
showard | c1c1caf | 2009-09-08 16:26:50 +0000 | [diff] [blame] | 462 | |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 463 | data = {'tag':tag, |
| 464 | 'label': job.label, |
| 465 | 'username': job.user, |
| 466 | 'machine_idx': job.machine_idx, |
| 467 | 'queued_time': job.queued_time, |
| 468 | 'started_time': job.started_time, |
| 469 | 'finished_time': job.finished_time, |
Dan Shi | 70647ca | 2015-07-16 22:52:35 -0700 | [diff] [blame] | 470 | 'afe_job_id': afe_job_id, |
| 471 | 'afe_parent_job_id': parent_job_id} |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 472 | job.afe_job_id = afe_job_id |
| 473 | if parent_job_id: |
| 474 | job.afe_parent_job_id = str(parent_job_id) |
Dan Shi | 70647ca | 2015-07-16 22:52:35 -0700 | [diff] [blame] | 475 | if job.label: |
| 476 | label_info = site_utils.parse_job_name(job.label) |
| 477 | if label_info: |
| 478 | data['build'] = label_info.get('build', None) |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 479 | job.build_version = data['build_version'] = label_info.get( |
| 480 | 'build_version', None) |
| 481 | job.board = data['board'] = label_info.get('board', None) |
| 482 | job.suite = data['suite'] = label_info.get('suite', None) |
| 483 | |
| 484 | # TODO(ntang): check job.index directly. |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 485 | is_update = hasattr(job, 'index') |
| 486 | if is_update: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 487 | self.update('tko_jobs', data, {'job_idx': job.index}, commit=commit) |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 488 | else: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 489 | self.insert('tko_jobs', data, commit=commit) |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 490 | job.index = self.get_last_autonumber_value() |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 491 | self.update_job_keyvals(job, commit=commit) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 492 | for test in job.tests: |
| 493 | self.insert_test(job, test, commit=commit) |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 494 | |
Shuqian Zhao | 31425d5 | 2016-12-07 09:35:03 -0800 | [diff] [blame] | 495 | return data |
| 496 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 497 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 498 | def update_job_keyvals(self, job, commit=None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 499 | """Updates the job key values. |
| 500 | |
| 501 | @param job: The job object. |
| 502 | @param commit: If commit the transaction . |
| 503 | """ |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 504 | for key, value in job.keyval_dict.iteritems(): |
| 505 | where = {'job_id': job.index, 'key': key} |
| 506 | data = dict(where, value=value) |
| 507 | exists = self.select('id', 'tko_job_keyvals', where=where) |
| 508 | |
| 509 | if exists: |
| 510 | self.update('tko_job_keyvals', data, where=where, commit=commit) |
| 511 | else: |
| 512 | self.insert('tko_job_keyvals', data, commit=commit) |
| 513 | |
| 514 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 515 | def insert_test(self, job, test, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 516 | """Inserts a job test. |
| 517 | |
| 518 | @param job: The job object. |
| 519 | @param test: The test object. |
| 520 | @param commit: If commit the transaction . |
| 521 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 522 | kver = self.insert_kernel(test.kernel, commit=commit) |
| 523 | data = {'job_idx':job.index, 'test':test.testname, |
| 524 | 'subdir':test.subdir, 'kernel_idx':kver, |
| 525 | 'status':self.status_idx[test.status], |
| 526 | 'reason':test.reason, 'machine_idx':job.machine_idx, |
| 527 | 'started_time': test.started_time, |
| 528 | 'finished_time':test.finished_time} |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 529 | is_update = hasattr(test, "test_idx") |
| 530 | if is_update: |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 531 | test_idx = test.test_idx |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 532 | self.update('tko_tests', data, |
| 533 | {'test_idx': test_idx}, commit=commit) |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 534 | where = {'test_idx': test_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 535 | self.delete('tko_iteration_result', where) |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 536 | self.delete('tko_iteration_perf_value', where) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 537 | self.delete('tko_iteration_attributes', where) |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 538 | where['user_created'] = 0 |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 539 | self.delete('tko_test_attributes', where) |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 540 | else: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 541 | self.insert('tko_tests', data, commit=commit) |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 542 | test_idx = test.test_idx = self.get_last_autonumber_value() |
| 543 | data = {'test_idx': test_idx} |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 544 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 545 | for i in test.iterations: |
| 546 | data['iteration'] = i.index |
| 547 | for key, value in i.attr_keyval.iteritems(): |
| 548 | data['attribute'] = key |
| 549 | data['value'] = value |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 550 | self.insert('tko_iteration_attributes', data, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 551 | commit=commit) |
| 552 | for key, value in i.perf_keyval.iteritems(): |
| 553 | data['attribute'] = key |
Allen Li | 64692a3 | 2016-11-04 13:35:19 -0700 | [diff] [blame] | 554 | if math.isnan(value) or math.isinf(value): |
| 555 | data['value'] = None |
| 556 | else: |
| 557 | data['value'] = value |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 558 | self.insert('tko_iteration_result', data, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 559 | commit=commit) |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 560 | |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 561 | data = {'test_idx': test_idx} |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 562 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 563 | for key, value in test.attributes.iteritems(): |
| 564 | data = {'test_idx': test_idx, 'attribute': key, |
| 565 | 'value': value} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 566 | self.insert('tko_test_attributes', data, commit=commit) |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 567 | |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 568 | if not is_update: |
| 569 | for label_index in test.labels: |
| 570 | data = {'test_id': test_idx, 'testlabel_id': label_index} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 571 | self.insert('tko_test_labels_tests', data, commit=commit) |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 572 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 573 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 574 | def read_machine_map(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 575 | """Reads the machine map.""" |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 576 | if self.machine_group or not self.machine_map: |
| 577 | return |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 578 | for line in open(self.machine_map, 'r').readlines(): |
| 579 | (machine, group) = line.split() |
| 580 | self.machine_group[machine] = group |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 581 | |
| 582 | |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 583 | def machine_info_dict(self, job): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 584 | """Reads the machine information of a job. |
| 585 | |
| 586 | @param job: The job object. |
| 587 | |
| 588 | @return: The machine info dictionary. |
| 589 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 590 | hostname = job.machine |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 591 | group = job.machine_group |
| 592 | owner = job.machine_owner |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 593 | |
| 594 | if not group: |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 595 | self.read_machine_map() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 596 | group = self.machine_group.get(hostname, hostname) |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 597 | if group == hostname and owner: |
| 598 | group = owner + '/' + hostname |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 599 | |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 600 | return {'hostname': hostname, 'machine_group': group, 'owner': owner} |
| 601 | |
| 602 | |
| 603 | def insert_machine(self, job, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 604 | """Inserts the job machine. |
| 605 | |
| 606 | @param job: The job object. |
| 607 | @param commit: If commit the transaction . |
| 608 | """ |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 609 | machine_info = self.machine_info_dict(job) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 610 | self.insert('tko_machines', machine_info, commit=commit) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 611 | return self.get_last_autonumber_value() |
| 612 | |
| 613 | |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 614 | def update_machine_information(self, job, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 615 | """Updates the job machine information. |
| 616 | |
| 617 | @param job: The job object. |
| 618 | @param commit: If commit the transaction . |
| 619 | """ |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 620 | machine_info = self.machine_info_dict(job) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 621 | self.update('tko_machines', machine_info, |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 622 | where={'hostname': machine_info['hostname']}, |
| 623 | commit=commit) |
| 624 | |
| 625 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 626 | def lookup_machine(self, hostname): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 627 | """Look up the machine information. |
| 628 | |
| 629 | @param hostname: The hostname as string. |
| 630 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 631 | where = { 'hostname' : hostname } |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 632 | rows = self.select('machine_idx', 'tko_machines', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 633 | if rows: |
| 634 | return rows[0][0] |
| 635 | else: |
| 636 | return None |
| 637 | |
| 638 | |
| 639 | def lookup_kernel(self, kernel): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 640 | """Look up the kernel. |
| 641 | |
| 642 | @param kernel: The kernel object. |
| 643 | """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 644 | rows = self.select('kernel_idx', 'tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 645 | {'kernel_hash':kernel.kernel_hash}) |
| 646 | if rows: |
| 647 | return rows[0][0] |
| 648 | else: |
| 649 | return None |
| 650 | |
| 651 | |
| 652 | def insert_kernel(self, kernel, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 653 | """Insert a kernel. |
| 654 | |
| 655 | @param kernel: The kernel object. |
| 656 | @param commit: If commit the transaction . |
| 657 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 658 | kver = self.lookup_kernel(kernel) |
| 659 | if kver: |
| 660 | return kver |
| 661 | |
| 662 | # If this kernel has any significant patches, append their hash |
| 663 | # as diferentiator. |
| 664 | printable = kernel.base |
| 665 | patch_count = 0 |
| 666 | for patch in kernel.patches: |
| 667 | match = re.match(r'.*(-mm[0-9]+|-git[0-9]+)\.(bz2|gz)$', |
| 668 | patch.reference) |
| 669 | if not match: |
| 670 | patch_count += 1 |
| 671 | |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 672 | self.insert('tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 673 | {'base':kernel.base, |
| 674 | 'kernel_hash':kernel.kernel_hash, |
| 675 | 'printable':printable}, |
| 676 | commit=commit) |
| 677 | kver = self.get_last_autonumber_value() |
| 678 | |
| 679 | if patch_count > 0: |
| 680 | printable += ' p%d' % (kver) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 681 | self.update('tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 682 | {'printable':printable}, |
| 683 | {'kernel_idx':kver}) |
| 684 | |
| 685 | for patch in kernel.patches: |
| 686 | self.insert_patch(kver, patch, commit=commit) |
| 687 | return kver |
| 688 | |
| 689 | |
| 690 | def insert_patch(self, kver, patch, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 691 | """Insert a kernel patch. |
| 692 | |
| 693 | @param kver: The kernel version. |
| 694 | @param patch: The kernel patch object. |
| 695 | @param commit: If commit the transaction . |
| 696 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 697 | print patch.reference |
| 698 | name = os.path.basename(patch.reference)[:80] |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 699 | self.insert('tko_patches', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 700 | {'kernel_idx': kver, |
| 701 | 'name':name, |
| 702 | 'url':patch.reference, |
| 703 | 'hash':patch.hash}, |
| 704 | commit=commit) |
| 705 | |
| 706 | |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 707 | def find_test(self, job_idx, testname, subdir): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 708 | """Find a test by name. |
| 709 | |
| 710 | @param job_idx: The job index. |
| 711 | @param testname: The test name. |
| 712 | @param subdir: The test sub directory under the job directory. |
| 713 | """ |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 714 | where = {'job_idx': job_idx , 'test': testname, 'subdir': subdir} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 715 | rows = self.select('test_idx', 'tko_tests', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 716 | if rows: |
| 717 | return rows[0][0] |
| 718 | else: |
| 719 | return None |
| 720 | |
| 721 | |
| 722 | def find_tests(self, job_idx): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 723 | """Find all tests by job index. |
| 724 | |
| 725 | @param job_idx: The job index. |
| 726 | @return: A list of tests. |
| 727 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 728 | where = { 'job_idx':job_idx } |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 729 | rows = self.select('test_idx', 'tko_tests', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 730 | if rows: |
| 731 | return [row[0] for row in rows] |
| 732 | else: |
| 733 | return [] |
| 734 | |
| 735 | |
| 736 | def find_job(self, tag): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 737 | """Find a job by tag. |
| 738 | |
| 739 | @param tag: The job tag name. |
| 740 | @return: The job object or None. |
| 741 | """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 742 | rows = self.select('job_idx', 'tko_jobs', {'tag': tag}) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 743 | if rows: |
| 744 | return rows[0][0] |
| 745 | else: |
| 746 | return None |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 747 | |
| 748 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 749 | def _get_db_type(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 750 | """Get the database type name to use from the global config.""" |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 751 | get_value = global_config.global_config.get_config_value_with_fallback |
| 752 | return "db_" + get_value("AUTOTEST_WEB", "global_db_type", "db_type", |
| 753 | default="mysql") |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 754 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 755 | |
| 756 | def _get_error_class(class_name): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 757 | """Retrieves the appropriate error class by name from the database |
| 758 | module.""" |
| 759 | db_module = __import__("autotest_lib.tko." + _get_db_type(), |
| 760 | globals(), locals(), ["driver"]) |
| 761 | return getattr(db_module.driver, class_name) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 762 | |
| 763 | |
| 764 | def db(*args, **dargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 765 | """Creates an instance of the database class with the arguments |
| 766 | provided in args and dargs, using the database type specified by |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 767 | the global configuration (defaulting to mysql). |
| 768 | |
| 769 | @param args: The db_type arguments. |
| 770 | @param dargs: The db_type named arguments. |
| 771 | |
| 772 | @return: An db object. |
| 773 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 774 | db_type = _get_db_type() |
| 775 | db_module = __import__("autotest_lib.tko." + db_type, globals(), |
| 776 | locals(), [db_type]) |
| 777 | db = getattr(db_module, db_type)(*args, **dargs) |
| 778 | return db |