Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 7 | """Script to archive old Autotest results to Google Storage. |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 8 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 9 | Uses gsutil to archive files to the configured Google Storage bucket. |
| 10 | Upon successful copy, the local results directory is deleted. |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 11 | """ |
| 12 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 13 | import abc |
Simran Basi | bd9ded0 | 2013-11-04 15:49:11 -0800 | [diff] [blame] | 14 | import datetime |
Dan Shi | faf50db | 2015-09-25 13:40:45 -0700 | [diff] [blame] | 15 | import errno |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 16 | import glob |
| 17 | import gzip |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 18 | import logging |
Simran Basi | a253228 | 2014-12-04 13:28:16 -0800 | [diff] [blame] | 19 | import logging.handlers |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 20 | import os |
Dan Shi | affb922 | 2015-04-15 17:05:47 -0700 | [diff] [blame] | 21 | import re |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 22 | import shutil |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 23 | import socket |
Laurence Goodby | ca7726d | 2017-02-14 17:09:07 -0800 | [diff] [blame] | 24 | import stat |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 25 | import subprocess |
| 26 | import sys |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 27 | import tarfile |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 28 | import tempfile |
| 29 | import time |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 30 | |
Simran Basi | 7d9a149 | 2012-10-25 13:51:54 -0700 | [diff] [blame] | 31 | from optparse import OptionParser |
| 32 | |
Simran Basi | 981a927 | 2012-11-14 10:46:03 -0800 | [diff] [blame] | 33 | import common |
Dan Shi | 96c3bdc | 2017-05-24 11:34:30 -0700 | [diff] [blame] | 34 | from autotest_lib.client.common_lib import file_utils |
Prathmesh Prabhu | beb9e01 | 2017-01-30 16:18:39 -0800 | [diff] [blame] | 35 | from autotest_lib.client.common_lib import global_config |
Simran Basi | dd12997 | 2014-09-11 14:34:49 -0700 | [diff] [blame] | 36 | from autotest_lib.client.common_lib import utils |
Prathmesh Prabhu | beb9e01 | 2017-01-30 16:18:39 -0800 | [diff] [blame] | 37 | from autotest_lib.site_utils import job_directories |
Michael Tang | e8bc959 | 2017-07-06 10:59:32 -0700 | [diff] [blame] | 38 | # For unittest, the cloud_console.proto is not compiled yet. |
| 39 | try: |
| 40 | from autotest_lib.site_utils import cloud_console_client |
| 41 | except ImportError: |
| 42 | cloud_console_client = None |
Prathmesh Prabhu | beb9e01 | 2017-01-30 16:18:39 -0800 | [diff] [blame] | 43 | from autotest_lib.tko import models |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 44 | from autotest_lib.utils import labellib |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 45 | from autotest_lib.utils import gslib |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 46 | from chromite.lib import timeout_util |
Prathmesh Prabhu | beb9e01 | 2017-01-30 16:18:39 -0800 | [diff] [blame] | 47 | |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame] | 48 | # Autotest requires the psutil module from site-packages, so it must be imported |
Ilja H. Friedel | 73cf6cd | 2017-03-01 12:23:00 -0800 | [diff] [blame] | 49 | # after "import common". |
Simran Basi | 627a75e | 2017-02-08 11:07:20 -0800 | [diff] [blame] | 50 | try: |
| 51 | # Does not exist, nor is needed, on moblab. |
| 52 | import psutil |
| 53 | except ImportError: |
| 54 | psutil = None |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 55 | |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame] | 56 | from chromite.lib import parallel |
| 57 | try: |
| 58 | from chromite.lib import metrics |
| 59 | from chromite.lib import ts_mon_config |
| 60 | except ImportError: |
Paul Hobbs | cd10e48 | 2017-08-28 12:00:06 -0700 | [diff] [blame] | 61 | metrics = utils.metrics_mock |
| 62 | ts_mon_config = utils.metrics_mock |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame] | 63 | |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 64 | |
Simran Basi | f3e305f | 2014-10-03 14:43:53 -0700 | [diff] [blame] | 65 | GS_OFFLOADING_ENABLED = global_config.global_config.get_config_value( |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 66 | 'CROS', 'gs_offloading_enabled', type=bool, default=True) |
Simran Basi | f3e305f | 2014-10-03 14:43:53 -0700 | [diff] [blame] | 67 | |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 68 | # Nice setting for process, the higher the number the lower the priority. |
| 69 | NICENESS = 10 |
| 70 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 71 | # Maximum number of seconds to allow for offloading a single |
| 72 | # directory. |
J. Richard Barnette | 7e0f859 | 2014-09-03 17:00:55 -0700 | [diff] [blame] | 73 | OFFLOAD_TIMEOUT_SECS = 60 * 60 |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 74 | |
Simran Basi | 392d4a5 | 2012-12-14 10:29:44 -0800 | [diff] [blame] | 75 | # Sleep time per loop. |
| 76 | SLEEP_TIME_SECS = 5 |
| 77 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 78 | # Minimum number of seconds between e-mail reports. |
| 79 | REPORT_INTERVAL_SECS = 60 * 60 |
| 80 | |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 81 | # Location of Autotest results on disk. |
| 82 | RESULTS_DIR = '/usr/local/autotest/results' |
Prathmesh Prabhu | 16f9e5c | 2017-01-30 17:54:40 -0800 | [diff] [blame] | 83 | FAILED_OFFLOADS_FILE = os.path.join(RESULTS_DIR, 'FAILED_OFFLOADS') |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 84 | |
Prathmesh Prabhu | 16f9e5c | 2017-01-30 17:54:40 -0800 | [diff] [blame] | 85 | FAILED_OFFLOADS_FILE_HEADER = ''' |
| 86 | This is the list of gs_offloader failed jobs. |
| 87 | Last offloader attempt at %s failed to offload %d files. |
| 88 | Check http://go/cros-triage-gsoffloader to triage the issue |
| 89 | |
| 90 | |
| 91 | First failure Count Directory name |
| 92 | =================== ====== ============================== |
| 93 | ''' |
| 94 | # --+----1----+---- ----+ ----+----1----+----2----+----3 |
| 95 | |
Prathmesh Prabhu | 80dfb1e | 2017-01-30 18:01:29 -0800 | [diff] [blame] | 96 | FAILED_OFFLOADS_LINE_FORMAT = '%19s %5d %-1s\n' |
| 97 | FAILED_OFFLOADS_TIME_FORMAT = '%Y-%m-%d %H:%M:%S' |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 98 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 99 | USE_RSYNC_ENABLED = global_config.global_config.get_config_value( |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 100 | 'CROS', 'gs_offloader_use_rsync', type=bool, default=False) |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 101 | |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 102 | LIMIT_FILE_COUNT = global_config.global_config.get_config_value( |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 103 | 'CROS', 'gs_offloader_limit_file_count', type=bool, default=False) |
| 104 | |
Michael Tang | 0df2eb4 | 2016-05-13 19:06:54 -0700 | [diff] [blame] | 105 | # Use multiprocessing for gsutil uploading. |
| 106 | GS_OFFLOADER_MULTIPROCESSING = global_config.global_config.get_config_value( |
| 107 | 'CROS', 'gs_offloader_multiprocessing', type=bool, default=False) |
| 108 | |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 109 | D = '[0-9][0-9]' |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 110 | TIMESTAMP_PATTERN = '%s%s.%s.%s_%s.%s.%s' % (D, D, D, D, D, D, D) |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 111 | CTS_RESULT_PATTERN = 'testResult.xml' |
Ilja H. Friedel | 73cf6cd | 2017-03-01 12:23:00 -0800 | [diff] [blame] | 112 | CTS_V2_RESULT_PATTERN = 'test_result.xml' |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 113 | # Google Storage bucket URI to store results in. |
| 114 | DEFAULT_CTS_RESULTS_GSURI = global_config.global_config.get_config_value( |
| 115 | 'CROS', 'cts_results_server', default='') |
Ningning Xia | 205a1d4 | 2016-06-21 16:46:28 -0700 | [diff] [blame] | 116 | DEFAULT_CTS_APFE_GSURI = global_config.global_config.get_config_value( |
| 117 | 'CROS', 'cts_apfe_server', default='') |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 118 | |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 119 | # metadata type |
| 120 | GS_OFFLOADER_SUCCESS_TYPE = 'gs_offloader_success' |
| 121 | GS_OFFLOADER_FAILURE_TYPE = 'gs_offloader_failure' |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 122 | |
Rohit Makasana | 6a7b14d | 2017-08-23 13:51:44 -0700 | [diff] [blame] | 123 | # Autotest test to collect list of CTS tests |
| 124 | TEST_LIST_COLLECTOR = 'tradefed-run-collect-tests-only' |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 125 | |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 126 | def _get_metrics_fields(dir_entry): |
| 127 | """Get metrics fields for the given test result directory, including board |
| 128 | and milestone. |
| 129 | |
| 130 | @param dir_entry: Directory entry to offload. |
| 131 | @return A dictionary for the metrics data to be uploaded. |
| 132 | """ |
| 133 | fields = {'board': 'unknown', |
| 134 | 'milestone': 'unknown'} |
| 135 | if dir_entry: |
| 136 | # There could be multiple hosts in the job directory, use the first one |
| 137 | # available. |
| 138 | for host in glob.glob(os.path.join(dir_entry, '*')): |
Dan Shi | 2310901 | 2017-05-28 20:23:48 -0700 | [diff] [blame] | 139 | try: |
| 140 | keyval = models.test.parse_job_keyval(host) |
| 141 | except ValueError: |
| 142 | continue |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 143 | build = keyval.get('build') |
| 144 | if build: |
Dan Shi | 02dd066 | 2017-05-23 11:24:32 -0700 | [diff] [blame] | 145 | try: |
| 146 | cros_version = labellib.parse_cros_version(build) |
| 147 | fields['board'] = cros_version.board |
| 148 | fields['milestone'] = cros_version.milestone |
| 149 | break |
| 150 | except ValueError: |
| 151 | # Ignore version parsing error so it won't crash |
| 152 | # gs_offloader. |
| 153 | pass |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 154 | |
| 155 | return fields; |
| 156 | |
| 157 | |
| 158 | def _get_es_metadata(dir_entry): |
| 159 | """Get ES metadata for the given test result directory. |
| 160 | |
| 161 | @param dir_entry: Directory entry to offload. |
| 162 | @return A dictionary for the metadata to be uploaded. |
| 163 | """ |
| 164 | fields = _get_metrics_fields(dir_entry) |
| 165 | fields['hostname'] = socket.gethostname() |
| 166 | # Include more data about the test job in metadata. |
| 167 | if dir_entry: |
| 168 | fields['dir_entry'] = dir_entry |
| 169 | fields['job_id'] = job_directories.get_job_id_or_task_id(dir_entry) |
| 170 | |
| 171 | return fields |
| 172 | |
| 173 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 174 | def _get_cmd_list(multiprocessing, dir_entry, gs_path): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 175 | """Return the command to offload a specified directory. |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 176 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 177 | @param multiprocessing: True to turn on -m option for gsutil. |
| 178 | @param dir_entry: Directory entry/path that which we need a cmd_list |
| 179 | to offload. |
| 180 | @param gs_path: Location in google storage where we will |
| 181 | offload the directory. |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 182 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 183 | @return A command list to be executed by Popen. |
| 184 | """ |
Dan Shi | 365049f | 2017-05-28 08:00:02 +0000 | [diff] [blame] | 185 | cmd = ['gsutil'] |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 186 | if multiprocessing: |
| 187 | cmd.append('-m') |
| 188 | if USE_RSYNC_ENABLED: |
| 189 | cmd.append('rsync') |
| 190 | target = os.path.join(gs_path, os.path.basename(dir_entry)) |
| 191 | else: |
| 192 | cmd.append('cp') |
| 193 | target = gs_path |
| 194 | cmd += ['-eR', dir_entry, target] |
| 195 | return cmd |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 196 | |
Jakob Juelich | 24f22c2 | 2014-09-26 11:46:11 -0700 | [diff] [blame] | 197 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 198 | def sanitize_dir(dirpath): |
| 199 | """Sanitize directory for gs upload. |
Dan Shi | affb922 | 2015-04-15 17:05:47 -0700 | [diff] [blame] | 200 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 201 | Symlinks and FIFOS are converted to regular files to fix bugs. |
Dan Shi | affb922 | 2015-04-15 17:05:47 -0700 | [diff] [blame] | 202 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 203 | @param dirpath: Directory entry to be sanitized. |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 204 | """ |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 205 | if not os.path.exists(dirpath): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 206 | return |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 207 | _escape_rename(dirpath) |
| 208 | _escape_rename_dir_contents(dirpath) |
| 209 | _sanitize_fifos(dirpath) |
| 210 | _sanitize_symlinks(dirpath) |
| 211 | |
| 212 | |
| 213 | def _escape_rename_dir_contents(dirpath): |
| 214 | """Recursively rename directory to escape filenames for gs upload. |
| 215 | |
| 216 | @param dirpath: Directory path string. |
| 217 | """ |
| 218 | for filename in os.listdir(dirpath): |
| 219 | path = os.path.join(dirpath, filename) |
| 220 | _escape_rename(path) |
| 221 | for filename in os.listdir(dirpath): |
| 222 | path = os.path.join(dirpath, filename) |
| 223 | if os.path.isdir(path): |
| 224 | _escape_rename_dir_contents(path) |
| 225 | |
| 226 | |
| 227 | def _escape_rename(path): |
| 228 | """Rename file to escape filenames for gs upload. |
| 229 | |
| 230 | @param path: File path string. |
| 231 | """ |
| 232 | dirpath, filename = os.path.split(path) |
| 233 | sanitized_filename = gslib.escape(filename) |
| 234 | sanitized_path = os.path.join(dirpath, sanitized_filename) |
| 235 | os.rename(path, sanitized_path) |
| 236 | |
| 237 | |
| 238 | def _sanitize_fifos(dirpath): |
| 239 | """Convert fifos to regular files (fixes crbug.com/684122). |
| 240 | |
| 241 | @param dirpath: Directory path string. |
| 242 | """ |
| 243 | for root, _, files in os.walk(dirpath): |
| 244 | for filename in files: |
| 245 | path = os.path.join(root, filename) |
| 246 | file_stat = os.lstat(path) |
Laurence Goodby | ca7726d | 2017-02-14 17:09:07 -0800 | [diff] [blame] | 247 | if stat.S_ISFIFO(file_stat.st_mode): |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 248 | _replace_fifo_with_file(path) |
| 249 | |
| 250 | |
| 251 | def _replace_fifo_with_file(path): |
| 252 | """Replace a fifo with a normal file. |
| 253 | |
| 254 | @param path: Fifo path string. |
| 255 | """ |
| 256 | logging.debug('Removing fifo %s', path) |
| 257 | os.remove(path) |
| 258 | logging.debug('Creating marker %s', path) |
| 259 | with open(path, 'w') as f: |
| 260 | f.write('<FIFO>') |
| 261 | |
| 262 | |
| 263 | def _sanitize_symlinks(dirpath): |
| 264 | """Convert Symlinks to regular files (fixes crbug.com/692788). |
| 265 | |
| 266 | @param dirpath: Directory path string. |
| 267 | """ |
| 268 | for root, _, files in os.walk(dirpath): |
| 269 | for filename in files: |
| 270 | path = os.path.join(root, filename) |
| 271 | file_stat = os.lstat(path) |
| 272 | if stat.S_ISLNK(file_stat.st_mode): |
| 273 | _replace_symlink_with_file(path) |
| 274 | |
| 275 | |
| 276 | def _replace_symlink_with_file(path): |
| 277 | """Replace a symlink with a normal file. |
| 278 | |
| 279 | @param path: Symlink path string. |
| 280 | """ |
| 281 | target = os.readlink(path) |
| 282 | logging.debug('Removing symlink %s', path) |
| 283 | os.remove(path) |
| 284 | logging.debug('Creating marker %s', path) |
| 285 | with open(path, 'w') as f: |
| 286 | f.write('<symlink to %s>' % target) |
| 287 | |
| 288 | |
| 289 | # Maximum number of files in the folder. |
| 290 | _MAX_FILE_COUNT = 500 |
| 291 | _FOLDERS_NEVER_ZIP = ['debug', 'ssp_logs', 'autoupdate_logs'] |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 292 | |
| 293 | |
| 294 | def _get_zippable_folders(dir_entry): |
| 295 | folders_list = [] |
| 296 | for folder in os.listdir(dir_entry): |
| 297 | folder_path = os.path.join(dir_entry, folder) |
| 298 | if (not os.path.isfile(folder_path) and |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 299 | not folder in _FOLDERS_NEVER_ZIP): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 300 | folders_list.append(folder_path) |
| 301 | return folders_list |
Dan Shi | affb922 | 2015-04-15 17:05:47 -0700 | [diff] [blame] | 302 | |
| 303 | |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 304 | def limit_file_count(dir_entry): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 305 | """Limit the number of files in given directory. |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 306 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 307 | The method checks the total number of files in the given directory. |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 308 | If the number is greater than _MAX_FILE_COUNT, the method will |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 309 | compress each folder in the given directory, except folders in |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 310 | _FOLDERS_NEVER_ZIP. |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 311 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 312 | @param dir_entry: Directory entry to be checked. |
| 313 | """ |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 314 | try: |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 315 | count = _count_files(dir_entry) |
| 316 | except ValueError: |
Prathmesh Prabhu | 8f85cd2 | 2017-02-01 13:04:58 -0800 | [diff] [blame] | 317 | logging.warning('Fail to get the file count in folder %s.', dir_entry) |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 318 | return |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 319 | if count < _MAX_FILE_COUNT: |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 320 | return |
| 321 | |
| 322 | # For test job, zip folders in a second level, e.g. 123-debug/host1. |
| 323 | # This is to allow autoserv debug folder still be accessible. |
| 324 | # For special task, it does not need to dig one level deeper. |
| 325 | is_special_task = re.match(job_directories.SPECIAL_TASK_PATTERN, |
| 326 | dir_entry) |
| 327 | |
| 328 | folders = _get_zippable_folders(dir_entry) |
| 329 | if not is_special_task: |
| 330 | subfolders = [] |
| 331 | for folder in folders: |
| 332 | subfolders.extend(_get_zippable_folders(folder)) |
| 333 | folders = subfolders |
| 334 | |
| 335 | for folder in folders: |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 336 | _make_into_tarball(folder) |
| 337 | |
| 338 | |
| 339 | def _count_files(dirpath): |
| 340 | """Count the number of files in a directory recursively. |
| 341 | |
| 342 | @param dirpath: Directory path string. |
| 343 | """ |
| 344 | return sum(len(files) for _path, _dirs, files in os.walk(dirpath)) |
| 345 | |
| 346 | |
| 347 | def _make_into_tarball(dirpath): |
| 348 | """Make directory into tarball. |
| 349 | |
| 350 | @param dirpath: Directory path string. |
| 351 | """ |
| 352 | tarpath = '%s.tgz' % dirpath |
| 353 | with tarfile.open(tarpath, 'w:gz') as tar: |
| 354 | tar.add(dirpath, arcname=os.path.basename(dirpath)) |
| 355 | shutil.rmtree(dirpath) |
Dan Shi | 1b4c7c3 | 2015-10-05 10:38:57 -0700 | [diff] [blame] | 356 | |
| 357 | |
Dan Shi | e4a4f9f | 2015-07-20 09:00:25 -0700 | [diff] [blame] | 358 | def correct_results_folder_permission(dir_entry): |
| 359 | """Make sure the results folder has the right permission settings. |
| 360 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 361 | For tests running with server-side packaging, the results folder has |
| 362 | the owner of root. This must be changed to the user running the |
| 363 | autoserv process, so parsing job can access the results folder. |
Dan Shi | e4a4f9f | 2015-07-20 09:00:25 -0700 | [diff] [blame] | 364 | |
| 365 | @param dir_entry: Path to the results folder. |
Dan Shi | e4a4f9f | 2015-07-20 09:00:25 -0700 | [diff] [blame] | 366 | """ |
| 367 | if not dir_entry: |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 368 | return |
Prathmesh Prabhu | 6c4ed33 | 2017-01-30 15:51:43 -0800 | [diff] [blame] | 369 | |
| 370 | logging.info('Trying to correct file permission of %s.', dir_entry) |
Dan Shi | e4a4f9f | 2015-07-20 09:00:25 -0700 | [diff] [blame] | 371 | try: |
Dan Shi | ebcd873 | 2017-10-09 14:54:52 -0700 | [diff] [blame] | 372 | owner = '%s:%s' % (os.getuid(), os.getgid()) |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 373 | subprocess.check_call( |
Dan Shi | ebcd873 | 2017-10-09 14:54:52 -0700 | [diff] [blame] | 374 | ['sudo', '-n', 'chown', '-R', owner, dir_entry]) |
Dan Shi | e4a4f9f | 2015-07-20 09:00:25 -0700 | [diff] [blame] | 375 | except subprocess.CalledProcessError as e: |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 376 | logging.error('Failed to modify permission for %s: %s', |
| 377 | dir_entry, e) |
Dan Shi | e4a4f9f | 2015-07-20 09:00:25 -0700 | [diff] [blame] | 378 | |
| 379 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 380 | def _upload_cts_testresult(dir_entry, multiprocessing): |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 381 | """Upload test results to separate gs buckets. |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 382 | |
Ilja H. Friedel | bfa6314 | 2017-01-26 00:56:29 -0800 | [diff] [blame] | 383 | Upload testResult.xml.gz/test_result.xml.gz file to cts_results_bucket. |
Ningning Xia | 205a1d4 | 2016-06-21 16:46:28 -0700 | [diff] [blame] | 384 | Upload timestamp.zip to cts_apfe_bucket. |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 385 | |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 386 | @param dir_entry: Path to the results folder. |
| 387 | @param multiprocessing: True to turn on -m option for gsutil. |
| 388 | """ |
Ningning Xia | 2d981ee | 2016-07-06 17:59:54 -0700 | [diff] [blame] | 389 | for host in glob.glob(os.path.join(dir_entry, '*')): |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 390 | cts_path = os.path.join(host, 'cheets_CTS.*', 'results', '*', |
| 391 | TIMESTAMP_PATTERN) |
Ilja H. Friedel | 73cf6cd | 2017-03-01 12:23:00 -0800 | [diff] [blame] | 392 | cts_v2_path = os.path.join(host, 'cheets_CTS_*', 'results', '*', |
| 393 | TIMESTAMP_PATTERN) |
| 394 | gts_v2_path = os.path.join(host, 'cheets_GTS.*', 'results', '*', |
| 395 | TIMESTAMP_PATTERN) |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 396 | for result_path, result_pattern in [(cts_path, CTS_RESULT_PATTERN), |
Ilja H. Friedel | 73cf6cd | 2017-03-01 12:23:00 -0800 | [diff] [blame] | 397 | (cts_v2_path, CTS_V2_RESULT_PATTERN), |
| 398 | (gts_v2_path, CTS_V2_RESULT_PATTERN)]: |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 399 | for path in glob.glob(result_path): |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 400 | try: |
| 401 | _upload_files(host, path, result_pattern, multiprocessing) |
| 402 | except Exception as e: |
| 403 | logging.error('ERROR uploading test results %s to GS: %s', |
| 404 | path, e) |
Ningning Xia | 205a1d4 | 2016-06-21 16:46:28 -0700 | [diff] [blame] | 405 | |
Ningning Xia | 205a1d4 | 2016-06-21 16:46:28 -0700 | [diff] [blame] | 406 | |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 407 | def _is_valid_result(build, result_pattern, suite): |
| 408 | """Check if the result should be uploaded to CTS/GTS buckets. |
| 409 | |
| 410 | @param build: Builder name. |
| 411 | @param result_pattern: XML result file pattern. |
| 412 | @param suite: Test suite name. |
| 413 | |
| 414 | @returns: Bool flag indicating whether a valid result. |
| 415 | """ |
| 416 | if build is None or suite is None: |
| 417 | return False |
| 418 | |
| 419 | # Not valid if it's not a release build. |
| 420 | if not re.match(r'(?!trybot-).*-release/.*', build): |
| 421 | return False |
| 422 | |
Ilja H. Friedel | ad6d879 | 2016-11-28 21:53:44 -0800 | [diff] [blame] | 423 | # Not valid if it's cts result but not 'arc-cts*' or 'test_that_wrapper' |
| 424 | # suite. |
Ilja H. Friedel | 73cf6cd | 2017-03-01 12:23:00 -0800 | [diff] [blame] | 425 | result_patterns = [CTS_RESULT_PATTERN, CTS_V2_RESULT_PATTERN] |
Ilja H. Friedel | 61a70d3 | 2017-05-20 01:43:02 -0700 | [diff] [blame] | 426 | if result_pattern in result_patterns and not ( |
| 427 | suite.startswith('arc-cts') or suite.startswith('arc-gts') or |
| 428 | suite.startswith('test_that_wrapper')): |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 429 | return False |
| 430 | |
| 431 | return True |
Ningning Xia | 21922c8 | 2016-07-29 11:03:15 -0700 | [diff] [blame] | 432 | |
| 433 | |
Rohit Makasana | 6a7b14d | 2017-08-23 13:51:44 -0700 | [diff] [blame] | 434 | def _is_test_collector(package): |
| 435 | """Returns true if the test run is just to collect list of CTS tests. |
| 436 | |
| 437 | @param package: Autotest package name. e.g. cheets_CTS_N.CtsGraphicsTestCase |
| 438 | |
| 439 | @return Bool flag indicating a test package is CTS list generator or not. |
| 440 | """ |
| 441 | return TEST_LIST_COLLECTOR in package |
| 442 | |
| 443 | |
Ningning Xia | 2d88eec | 2016-07-25 23:18:46 -0700 | [diff] [blame] | 444 | def _upload_files(host, path, result_pattern, multiprocessing): |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 445 | keyval = models.test.parse_job_keyval(host) |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 446 | build = keyval.get('build') |
| 447 | suite = keyval.get('suite') |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 448 | |
Ningning Xia | 8db632f | 2016-08-19 11:01:35 -0700 | [diff] [blame] | 449 | if not _is_valid_result(build, result_pattern, suite): |
| 450 | # No need to upload current folder, return. |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 451 | return |
Ningning Xia | 21922c8 | 2016-07-29 11:03:15 -0700 | [diff] [blame] | 452 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 453 | parent_job_id = str(keyval['parent_job_id']) |
Ningning Xia | 21922c8 | 2016-07-29 11:03:15 -0700 | [diff] [blame] | 454 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 455 | folders = path.split(os.sep) |
| 456 | job_id = folders[-6] |
| 457 | package = folders[-4] |
| 458 | timestamp = folders[-1] |
Ningning Xia | 2d981ee | 2016-07-06 17:59:54 -0700 | [diff] [blame] | 459 | |
Rohit Makasana | 6a7b14d | 2017-08-23 13:51:44 -0700 | [diff] [blame] | 460 | # Results produced by CTS test list collector are dummy results. |
| 461 | # They don't need to be copied to APFE bucket which is mainly being used for |
| 462 | # CTS APFE submission. |
| 463 | if not _is_test_collector(package): |
| 464 | # Path: bucket/build/parent_job_id/cheets_CTS.*/job_id_timestamp/ |
| 465 | # or bucket/build/parent_job_id/cheets_GTS.*/job_id_timestamp/ |
| 466 | cts_apfe_gs_path = os.path.join( |
| 467 | DEFAULT_CTS_APFE_GSURI, build, parent_job_id, |
| 468 | package, job_id + '_' + timestamp) + '/' |
| 469 | |
| 470 | for zip_file in glob.glob(os.path.join('%s.zip' % path)): |
| 471 | utils.run(' '.join(_get_cmd_list( |
| 472 | multiprocessing, zip_file, cts_apfe_gs_path))) |
| 473 | logging.debug('Upload %s to %s ', zip_file, cts_apfe_gs_path) |
| 474 | else: |
| 475 | logging.debug('%s is a CTS Test collector Autotest test run.', package) |
| 476 | logging.debug('Skipping CTS results upload to APFE gs:// bucket.') |
Ningning Xia | 2d981ee | 2016-07-06 17:59:54 -0700 | [diff] [blame] | 477 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 478 | # Path: bucket/cheets_CTS.*/job_id_timestamp/ |
| 479 | # or bucket/cheets_GTS.*/job_id_timestamp/ |
| 480 | test_result_gs_path = os.path.join( |
| 481 | DEFAULT_CTS_RESULTS_GSURI, package, |
| 482 | job_id + '_' + timestamp) + '/' |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 483 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 484 | for test_result_file in glob.glob(os.path.join(path, result_pattern)): |
Ilja H. Friedel | bfa6314 | 2017-01-26 00:56:29 -0800 | [diff] [blame] | 485 | # gzip test_result_file(testResult.xml/test_result.xml) |
| 486 | |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 487 | test_result_file_gz = '%s.gz' % test_result_file |
| 488 | with open(test_result_file, 'r') as f_in, ( |
| 489 | gzip.open(test_result_file_gz, 'w')) as f_out: |
| 490 | shutil.copyfileobj(f_in, f_out) |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 491 | utils.run(' '.join(_get_cmd_list( |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 492 | multiprocessing, test_result_file_gz, test_result_gs_path))) |
| 493 | logging.debug('Zip and upload %s to %s', |
| 494 | test_result_file_gz, test_result_gs_path) |
Ilja H. Friedel | bfa6314 | 2017-01-26 00:56:29 -0800 | [diff] [blame] | 495 | # Remove test_result_file_gz(testResult.xml.gz/test_result.xml.gz) |
Ningning Xia | 0c27d9b | 2016-08-04 14:02:39 -0700 | [diff] [blame] | 496 | os.remove(test_result_file_gz) |
| 497 | |
Ningning Xia | 4211124 | 2016-06-15 14:35:58 -0700 | [diff] [blame] | 498 | |
Aviv Keshet | 1d8df7d | 2017-04-20 12:35:31 -0700 | [diff] [blame] | 499 | def _emit_gs_returncode_metric(returncode): |
| 500 | """Increment the gs_returncode counter based on |returncode|.""" |
| 501 | m_gs_returncode = 'chromeos/autotest/gs_offloader/gs_returncode' |
| 502 | rcode = int(returncode) |
| 503 | if rcode < 0 or rcode > 255: |
| 504 | rcode = -1 |
| 505 | metrics.Counter(m_gs_returncode).increment(fields={'return_code': rcode}) |
| 506 | |
| 507 | |
Dan Shi | ebcd873 | 2017-10-09 14:54:52 -0700 | [diff] [blame] | 508 | def _handle_dir_os_error(dir_entry, fix_permission=False): |
| 509 | """Try to fix the result directory's permission issue if needed. |
| 510 | |
| 511 | @param dir_entry: Directory entry to offload. |
| 512 | @param fix_permission: True to change the directory's owner to the same one |
| 513 | running gs_offloader. |
| 514 | """ |
| 515 | if fix_permission: |
| 516 | correct_results_folder_permission(dir_entry) |
| 517 | m_permission_error = ('chromeos/autotest/errors/gs_offloader/' |
| 518 | 'wrong_permissions_count') |
| 519 | metrics_fields = _get_metrics_fields(dir_entry) |
| 520 | metrics.Counter(m_permission_error).increment(fields=metrics_fields) |
| 521 | |
| 522 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 523 | class BaseGSOffloader(object): |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 524 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 525 | """Google Storage offloader interface.""" |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 526 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 527 | __metaclass__ = abc.ABCMeta |
| 528 | |
| 529 | @abc.abstractmethod |
| 530 | def offload(self, dir_entry, dest_path, job_complete_time): |
| 531 | """Offload a directory entry to Google Storage. |
| 532 | |
| 533 | @param dir_entry: Directory entry to offload. |
| 534 | @param dest_path: Location in google storage where we will |
| 535 | offload the directory. |
| 536 | @param job_complete_time: The complete time of the job from the AFE |
| 537 | database. |
| 538 | """ |
| 539 | |
| 540 | |
| 541 | class GSOffloader(BaseGSOffloader): |
| 542 | """Google Storage Offloader.""" |
| 543 | |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 544 | def __init__(self, gs_uri, multiprocessing, delete_age, |
| 545 | console_client=None): |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 546 | """Returns the offload directory function for the given gs_uri |
| 547 | |
| 548 | @param gs_uri: Google storage bucket uri to offload to. |
| 549 | @param multiprocessing: True to turn on -m option for gsutil. |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 550 | @param console_client: The cloud console client. If None, |
| 551 | cloud console APIs are not called. |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 552 | """ |
| 553 | self._gs_uri = gs_uri |
| 554 | self._multiprocessing = multiprocessing |
| 555 | self._delete_age = delete_age |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 556 | self._console_client = console_client |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 557 | |
Prathmesh Prabhu | eeaa7ef | 2017-01-30 17:17:06 -0800 | [diff] [blame] | 558 | @metrics.SecondsTimerDecorator( |
| 559 | 'chromeos/autotest/gs_offloader/job_offload_duration') |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 560 | def offload(self, dir_entry, dest_path, job_complete_time): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 561 | """Offload the specified directory entry to Google storage. |
Jakob Juelich | c17f311 | 2014-09-11 14:32:30 -0700 | [diff] [blame] | 562 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 563 | @param dir_entry: Directory entry to offload. |
| 564 | @param dest_path: Location in google storage where we will |
| 565 | offload the directory. |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 566 | @param job_complete_time: The complete time of the job from the AFE |
| 567 | database. |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 568 | """ |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 569 | with tempfile.TemporaryFile('w+') as stdout_file, \ |
| 570 | tempfile.TemporaryFile('w+') as stderr_file: |
| 571 | try: |
Dan Shi | ebcd873 | 2017-10-09 14:54:52 -0700 | [diff] [blame] | 572 | try: |
| 573 | self._offload(dir_entry, dest_path, stdout_file, |
| 574 | stderr_file) |
| 575 | except OSError as e: |
| 576 | # Correct file permission error of the directory, then raise |
| 577 | # the exception so gs_offloader can retry later. |
| 578 | _handle_dir_os_error(dir_entry, e.errno==errno.EACCES) |
| 579 | # Try again after the permission issue is fixed. |
| 580 | self._offload(dir_entry, dest_path, stdout_file, |
| 581 | stderr_file) |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 582 | except _OffloadError as e: |
| 583 | metrics_fields = _get_metrics_fields(dir_entry) |
Aviv Keshet | 1d8df7d | 2017-04-20 12:35:31 -0700 | [diff] [blame] | 584 | m_any_error = 'chromeos/autotest/errors/gs_offloader/any_error' |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 585 | metrics.Counter(m_any_error).increment(fields=metrics_fields) |
| 586 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 587 | # Rewind the log files for stdout and stderr and log |
| 588 | # their contents. |
| 589 | stdout_file.seek(0) |
| 590 | stderr_file.seek(0) |
| 591 | stderr_content = stderr_file.read() |
Prathmesh Prabhu | 867cec5 | 2017-01-30 15:58:12 -0800 | [diff] [blame] | 592 | logging.warning('Error occurred when offloading %s:', dir_entry) |
| 593 | logging.warning('Stdout:\n%s \nStderr:\n%s', stdout_file.read(), |
| 594 | stderr_content) |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 595 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 596 | # Some result files may have wrong file permission. Try |
| 597 | # to correct such error so later try can success. |
| 598 | # TODO(dshi): The code is added to correct result files |
| 599 | # with wrong file permission caused by bug 511778. After |
| 600 | # this code is pushed to lab and run for a while to |
| 601 | # clean up these files, following code and function |
| 602 | # correct_results_folder_permission can be deleted. |
| 603 | if 'CommandException: Error opening file' in stderr_content: |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 604 | correct_results_folder_permission(dir_entry) |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 605 | else: |
| 606 | self._prune(dir_entry, job_complete_time) |
Dan Shi | b2751fc | 2017-05-16 11:05:15 -0700 | [diff] [blame] | 607 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 608 | def _offload(self, dir_entry, dest_path, |
| 609 | stdout_file, stderr_file): |
| 610 | """Offload the specified directory entry to Google storage. |
| 611 | |
| 612 | @param dir_entry: Directory entry to offload. |
| 613 | @param dest_path: Location in google storage where we will |
| 614 | offload the directory. |
| 615 | @param job_complete_time: The complete time of the job from the AFE |
| 616 | database. |
| 617 | @param stdout_file: Log file. |
| 618 | @param stderr_file: Log file. |
| 619 | """ |
| 620 | if _is_uploaded(dir_entry): |
| 621 | return |
| 622 | start_time = time.time() |
| 623 | metrics_fields = _get_metrics_fields(dir_entry) |
| 624 | es_metadata = _get_es_metadata(dir_entry) |
| 625 | error_obj = _OffloadError(start_time, es_metadata) |
| 626 | try: |
| 627 | sanitize_dir(dir_entry) |
| 628 | if DEFAULT_CTS_RESULTS_GSURI: |
| 629 | _upload_cts_testresult(dir_entry, self._multiprocessing) |
| 630 | |
| 631 | if LIMIT_FILE_COUNT: |
| 632 | limit_file_count(dir_entry) |
| 633 | es_metadata['size_kb'] = file_utils.get_directory_size_kibibytes(dir_entry) |
| 634 | |
| 635 | process = None |
| 636 | with timeout_util.Timeout(OFFLOAD_TIMEOUT_SECS): |
| 637 | gs_path = '%s%s' % (self._gs_uri, dest_path) |
| 638 | process = subprocess.Popen( |
| 639 | _get_cmd_list(self._multiprocessing, dir_entry, gs_path), |
| 640 | stdout=stdout_file, stderr=stderr_file) |
| 641 | process.wait() |
| 642 | |
| 643 | _emit_gs_returncode_metric(process.returncode) |
| 644 | if process.returncode != 0: |
| 645 | raise error_obj |
| 646 | _emit_offload_metrics(dir_entry) |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 647 | |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 648 | if self._console_client: |
| 649 | gcs_uri = os.path.join(gs_path, |
| 650 | os.path.basename(dir_entry)) |
| 651 | if not self._console_client.send_test_job_offloaded_message( |
| 652 | gcs_uri): |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 653 | raise error_obj |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 654 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 655 | _mark_uploaded(dir_entry) |
| 656 | except timeout_util.TimeoutError: |
| 657 | m_timeout = 'chromeos/autotest/errors/gs_offloader/timed_out_count' |
| 658 | metrics.Counter(m_timeout).increment(fields=metrics_fields) |
| 659 | # If we finished the call to Popen(), we may need to |
| 660 | # terminate the child process. We don't bother calling |
| 661 | # process.poll(); that inherently races because the child |
| 662 | # can die any time it wants. |
| 663 | if process: |
| 664 | try: |
| 665 | process.terminate() |
| 666 | except OSError: |
| 667 | # We don't expect any error other than "No such |
| 668 | # process". |
| 669 | pass |
| 670 | logging.error('Offloading %s timed out after waiting %d ' |
| 671 | 'seconds.', dir_entry, OFFLOAD_TIMEOUT_SECS) |
| 672 | raise error_obj |
| 673 | |
| 674 | def _prune(self, dir_entry, job_complete_time): |
| 675 | """Prune directory if it is uploaded and expired. |
| 676 | |
| 677 | @param dir_entry: Directory entry to offload. |
| 678 | @param job_complete_time: The complete time of the job from the AFE |
| 679 | database. |
| 680 | """ |
| 681 | if not (_is_uploaded(dir_entry) |
| 682 | and job_directories.is_job_expired(self._delete_age, |
| 683 | job_complete_time)): |
| 684 | return |
| 685 | try: |
| 686 | shutil.rmtree(dir_entry) |
| 687 | except OSError as e: |
Dan Shi | ebcd873 | 2017-10-09 14:54:52 -0700 | [diff] [blame] | 688 | # The wrong file permission can lead call `shutil.rmtree(dir_entry)` |
| 689 | # to raise OSError with message 'Permission denied'. Details can be |
| 690 | # found in crbug.com/536151 |
| 691 | _handle_dir_os_error(dir_entry, e.errno==errno.EACCES) |
| 692 | # Try again after the permission issue is fixed. |
| 693 | shutil.rmtree(dir_entry) |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 694 | |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 695 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 696 | class _OffloadError(Exception): |
| 697 | """Google Storage offload failed.""" |
Simran Basi | bd9ded0 | 2013-11-04 15:49:11 -0800 | [diff] [blame] | 698 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 699 | def __init__(self, start_time, es_metadata): |
| 700 | super(_OffloadError, self).__init__(start_time, es_metadata) |
| 701 | self.start_time = start_time |
| 702 | self.es_metadata = es_metadata |
Simran Basi | bd9ded0 | 2013-11-04 15:49:11 -0800 | [diff] [blame] | 703 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 704 | |
| 705 | |
| 706 | class FakeGSOffloader(BaseGSOffloader): |
| 707 | |
| 708 | """Fake Google Storage Offloader that only deletes directories.""" |
| 709 | |
| 710 | def offload(self, dir_entry, dest_path, job_complete_time): |
| 711 | """Pretend to offload a directory and delete it. |
| 712 | |
| 713 | @param dir_entry: Directory entry to offload. |
| 714 | @param dest_path: Location in google storage where we will |
| 715 | offload the directory. |
| 716 | @param job_complete_time: The complete time of the job from the AFE |
| 717 | database. |
| 718 | """ |
| 719 | shutil.rmtree(dir_entry) |
| 720 | |
| 721 | |
| 722 | def _is_expired(job, age_limit): |
| 723 | """Return whether job directory is expired for uploading |
| 724 | |
| 725 | @param job: _JobDirectory instance. |
| 726 | @param age_limit: Minimum age in days at which a job may be offloaded. |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 727 | """ |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 728 | job_timestamp = job.get_timestamp_if_finished() |
| 729 | if not job_timestamp: |
| 730 | return False |
| 731 | return job_directories.is_job_expired(age_limit, job_timestamp) |
| 732 | |
| 733 | |
| 734 | def _emit_offload_metrics(dirpath): |
| 735 | """Emit gs offload metrics. |
| 736 | |
| 737 | @param dirpath: Offloaded directory path. |
| 738 | """ |
| 739 | dir_size = file_utils.get_directory_size_kibibytes(dirpath) |
| 740 | metrics_fields = _get_metrics_fields(dirpath) |
| 741 | |
| 742 | m_offload_count = ( |
| 743 | 'chromeos/autotest/gs_offloader/jobs_offloaded') |
| 744 | metrics.Counter(m_offload_count).increment( |
| 745 | fields=metrics_fields) |
| 746 | m_offload_size = ('chromeos/autotest/gs_offloader/' |
| 747 | 'kilobytes_transferred') |
| 748 | metrics.Counter(m_offload_size).increment_by( |
| 749 | dir_size, fields=metrics_fields) |
Simran Basi | bd9ded0 | 2013-11-04 15:49:11 -0800 | [diff] [blame] | 750 | |
| 751 | |
Allen Li | 9579b38 | 2017-05-05 17:07:43 -0700 | [diff] [blame] | 752 | def _is_uploaded(dirpath): |
| 753 | """Return whether directory has been uploaded. |
| 754 | |
| 755 | @param dirpath: Directory path string. |
| 756 | """ |
| 757 | return os.path.isfile(_get_uploaded_marker_file(dirpath)) |
| 758 | |
| 759 | |
| 760 | def _mark_uploaded(dirpath): |
| 761 | """Mark directory as uploaded. |
| 762 | |
| 763 | @param dirpath: Directory path string. |
| 764 | """ |
| 765 | with open(_get_uploaded_marker_file(dirpath), 'a'): |
| 766 | pass |
| 767 | |
| 768 | |
| 769 | def _get_uploaded_marker_file(dirpath): |
| 770 | """Return path to upload marker file for directory. |
| 771 | |
| 772 | @param dirpath: Directory path string. |
| 773 | """ |
| 774 | return '%s/.GS_UPLOADED' % (dirpath,) |
| 775 | |
| 776 | |
Prathmesh Prabhu | fda271a | 2017-01-30 17:53:12 -0800 | [diff] [blame] | 777 | def _format_job_for_failure_reporting(job): |
| 778 | """Formats a _JobDirectory for reporting / logging. |
| 779 | |
| 780 | @param job: The _JobDirectory to format. |
| 781 | """ |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 782 | d = datetime.datetime.fromtimestamp(job.first_offload_start) |
Prathmesh Prabhu | 80dfb1e | 2017-01-30 18:01:29 -0800 | [diff] [blame] | 783 | data = (d.strftime(FAILED_OFFLOADS_TIME_FORMAT), |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 784 | job.offload_count, |
| 785 | job.dirname) |
Prathmesh Prabhu | 80dfb1e | 2017-01-30 18:01:29 -0800 | [diff] [blame] | 786 | return FAILED_OFFLOADS_LINE_FORMAT % data |
Prathmesh Prabhu | fda271a | 2017-01-30 17:53:12 -0800 | [diff] [blame] | 787 | |
| 788 | |
Simran Basi | ac0edb2 | 2015-04-23 16:15:51 -0700 | [diff] [blame] | 789 | def wait_for_gs_write_access(gs_uri): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 790 | """Verify and wait until we have write access to Google Storage. |
Simran Basi | ac0edb2 | 2015-04-23 16:15:51 -0700 | [diff] [blame] | 791 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 792 | @param gs_uri: The Google Storage URI we are trying to offload to. |
| 793 | """ |
| 794 | # TODO (sbasi) Try to use the gsutil command to check write access. |
| 795 | # Ensure we have write access to gs_uri. |
| 796 | dummy_file = tempfile.NamedTemporaryFile() |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 797 | test_cmd = _get_cmd_list(False, dummy_file.name, gs_uri) |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 798 | while True: |
| 799 | try: |
| 800 | subprocess.check_call(test_cmd) |
| 801 | subprocess.check_call( |
Dan Shi | 365049f | 2017-05-28 08:00:02 +0000 | [diff] [blame] | 802 | ['gsutil', 'rm', |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 803 | os.path.join(gs_uri, |
| 804 | os.path.basename(dummy_file.name))]) |
| 805 | break |
| 806 | except subprocess.CalledProcessError: |
| 807 | logging.debug('Unable to offload to %s, sleeping.', gs_uri) |
| 808 | time.sleep(120) |
Simran Basi | ac0edb2 | 2015-04-23 16:15:51 -0700 | [diff] [blame] | 809 | |
| 810 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 811 | class Offloader(object): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 812 | """State of the offload process. |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 813 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 814 | Contains the following member fields: |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 815 | * _gs_offloader: _BaseGSOffloader to use to offload a job directory. |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 816 | * _jobdir_classes: List of classes of job directory to be |
| 817 | offloaded. |
| 818 | * _processes: Maximum number of outstanding offload processes |
| 819 | to allow during an offload cycle. |
| 820 | * _age_limit: Minimum age in days at which a job may be |
| 821 | offloaded. |
| 822 | * _open_jobs: a dictionary mapping directory paths to Job |
| 823 | objects. |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 824 | """ |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 825 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 826 | def __init__(self, options): |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 827 | self._upload_age_limit = options.age_to_upload |
| 828 | self._delete_age_limit = options.age_to_delete |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 829 | if options.delete_only: |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 830 | self._gs_offloader = FakeGSOffloader() |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 831 | else: |
| 832 | self.gs_uri = utils.get_offload_gsuri() |
| 833 | logging.debug('Offloading to: %s', self.gs_uri) |
Michael Tang | 0df2eb4 | 2016-05-13 19:06:54 -0700 | [diff] [blame] | 834 | multiprocessing = False |
| 835 | if options.multiprocessing: |
| 836 | multiprocessing = True |
| 837 | elif options.multiprocessing is None: |
| 838 | multiprocessing = GS_OFFLOADER_MULTIPROCESSING |
| 839 | logging.info( |
| 840 | 'Offloader multiprocessing is set to:%r', multiprocessing) |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 841 | console_client = None |
Michael Tang | e8bc959 | 2017-07-06 10:59:32 -0700 | [diff] [blame] | 842 | if (cloud_console_client and |
| 843 | cloud_console_client.is_cloud_notification_enabled()): |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 844 | console_client = cloud_console_client.PubSubBasedClient() |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 845 | self._gs_offloader = GSOffloader( |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 846 | self.gs_uri, multiprocessing, self._delete_age_limit, |
Michael Tang | 0f553bd | 2017-06-16 17:38:45 -0700 | [diff] [blame] | 847 | console_client) |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 848 | classlist = [] |
| 849 | if options.process_hosts_only or options.process_all: |
| 850 | classlist.append(job_directories.SpecialJobDirectory) |
| 851 | if not options.process_hosts_only: |
| 852 | classlist.append(job_directories.RegularJobDirectory) |
| 853 | self._jobdir_classes = classlist |
| 854 | assert self._jobdir_classes |
| 855 | self._processes = options.parallelism |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 856 | self._open_jobs = {} |
Michael Tang | 97d188c | 2016-06-25 11:18:42 -0700 | [diff] [blame] | 857 | self._pusub_topic = None |
Allen Li | 0be2f2d | 2017-05-15 15:53:21 -0700 | [diff] [blame] | 858 | self._offload_count_limit = 3 |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 859 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 860 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 861 | def _add_new_jobs(self): |
| 862 | """Find new job directories that need offloading. |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 863 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 864 | Go through the file system looking for valid job directories |
| 865 | that are currently not in `self._open_jobs`, and add them in. |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 866 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 867 | """ |
| 868 | new_job_count = 0 |
| 869 | for cls in self._jobdir_classes: |
| 870 | for resultsdir in cls.get_job_directories(): |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 871 | if ( |
| 872 | resultsdir in self._open_jobs |
| 873 | or _is_uploaded(resultsdir)): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 874 | continue |
| 875 | self._open_jobs[resultsdir] = cls(resultsdir) |
| 876 | new_job_count += 1 |
| 877 | logging.debug('Start of offload cycle - found %d new jobs', |
| 878 | new_job_count) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 879 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 880 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 881 | def _remove_offloaded_jobs(self): |
| 882 | """Removed offloaded jobs from `self._open_jobs`.""" |
| 883 | removed_job_count = 0 |
| 884 | for jobkey, job in self._open_jobs.items(): |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 885 | if ( |
| 886 | not os.path.exists(job.dirname) |
| 887 | or _is_uploaded(job.dirname)): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 888 | del self._open_jobs[jobkey] |
| 889 | removed_job_count += 1 |
| 890 | logging.debug('End of offload cycle - cleared %d new jobs, ' |
| 891 | 'carrying %d open jobs', |
| 892 | removed_job_count, len(self._open_jobs)) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 893 | |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 894 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 895 | def _report_failed_jobs(self): |
| 896 | """Report status after attempting offload. |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 897 | |
| 898 | This function processes all jobs in `self._open_jobs`, assuming |
| 899 | an attempt has just been made to offload all of them. |
| 900 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 901 | If any jobs have reportable errors, and we haven't generated |
| 902 | an e-mail report in the last `REPORT_INTERVAL_SECS` seconds, |
| 903 | send new e-mail describing the failures. |
| 904 | |
| 905 | """ |
Prathmesh Prabhu | 343d171 | 2017-01-30 16:54:15 -0800 | [diff] [blame] | 906 | failed_jobs = [j for j in self._open_jobs.values() if |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 907 | j.first_offload_start] |
Prathmesh Prabhu | ea86973 | 2017-01-30 17:04:25 -0800 | [diff] [blame] | 908 | self._report_failed_jobs_count(failed_jobs) |
Prathmesh Prabhu | 16f9e5c | 2017-01-30 17:54:40 -0800 | [diff] [blame] | 909 | self._log_failed_jobs_locally(failed_jobs) |
Prathmesh Prabhu | 343d171 | 2017-01-30 16:54:15 -0800 | [diff] [blame] | 910 | |
| 911 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 912 | def offload_once(self): |
| 913 | """Perform one offload cycle. |
| 914 | |
| 915 | Find all job directories for new jobs that we haven't seen |
| 916 | before. Then, attempt to offload the directories for any |
| 917 | jobs that have finished running. Offload of multiple jobs |
| 918 | is done in parallel, up to `self._processes` at a time. |
| 919 | |
| 920 | After we've tried uploading all directories, go through the list |
| 921 | checking the status of all uploaded directories. If necessary, |
| 922 | report failures via e-mail. |
| 923 | |
| 924 | """ |
| 925 | self._add_new_jobs() |
Prathmesh Prabhu | c985685 | 2017-01-30 16:52:59 -0800 | [diff] [blame] | 926 | self._report_current_jobs_count() |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 927 | with parallel.BackgroundTaskRunner( |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 928 | self._gs_offloader.offload, processes=self._processes) as queue: |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 929 | for job in self._open_jobs.values(): |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 930 | _enqueue_offload(job, queue, self._upload_age_limit) |
Allen Li | 0be2f2d | 2017-05-15 15:53:21 -0700 | [diff] [blame] | 931 | self._give_up_on_jobs_over_limit() |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 932 | self._remove_offloaded_jobs() |
| 933 | self._report_failed_jobs() |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 934 | |
| 935 | |
Allen Li | 0be2f2d | 2017-05-15 15:53:21 -0700 | [diff] [blame] | 936 | def _give_up_on_jobs_over_limit(self): |
| 937 | """Give up on jobs that have gone over the offload limit. |
| 938 | |
| 939 | We mark them as uploaded as we won't try to offload them any more. |
| 940 | """ |
| 941 | for job in self._open_jobs.values(): |
Allen Li | 808828b | 2017-06-23 13:36:41 -0700 | [diff] [blame] | 942 | if job.offload_count >= self._offload_count_limit: |
Allen Li | 0be2f2d | 2017-05-15 15:53:21 -0700 | [diff] [blame] | 943 | _mark_uploaded(job.dirname) |
| 944 | |
| 945 | |
Prathmesh Prabhu | 16f9e5c | 2017-01-30 17:54:40 -0800 | [diff] [blame] | 946 | def _log_failed_jobs_locally(self, failed_jobs, |
| 947 | log_file=FAILED_OFFLOADS_FILE): |
| 948 | """Updates a local file listing all the failed jobs. |
| 949 | |
| 950 | The dropped file can be used by the developers to list jobs that we have |
| 951 | failed to upload. |
| 952 | |
| 953 | @param failed_jobs: A list of failed _JobDirectory objects. |
| 954 | @param log_file: The file to log the failed jobs to. |
| 955 | """ |
| 956 | now = datetime.datetime.now() |
Prathmesh Prabhu | 80dfb1e | 2017-01-30 18:01:29 -0800 | [diff] [blame] | 957 | now_str = now.strftime(FAILED_OFFLOADS_TIME_FORMAT) |
Prathmesh Prabhu | 16f9e5c | 2017-01-30 17:54:40 -0800 | [diff] [blame] | 958 | formatted_jobs = [_format_job_for_failure_reporting(job) |
| 959 | for job in failed_jobs] |
| 960 | formatted_jobs.sort() |
| 961 | |
| 962 | with open(log_file, 'w') as logfile: |
| 963 | logfile.write(FAILED_OFFLOADS_FILE_HEADER % |
| 964 | (now_str, len(failed_jobs))) |
| 965 | logfile.writelines(formatted_jobs) |
| 966 | |
| 967 | |
Prathmesh Prabhu | c985685 | 2017-01-30 16:52:59 -0800 | [diff] [blame] | 968 | def _report_current_jobs_count(self): |
| 969 | """Report the number of outstanding jobs to monarch.""" |
| 970 | metrics.Gauge('chromeos/autotest/gs_offloader/current_jobs_count').set( |
| 971 | len(self._open_jobs)) |
| 972 | |
| 973 | |
Prathmesh Prabhu | ea86973 | 2017-01-30 17:04:25 -0800 | [diff] [blame] | 974 | def _report_failed_jobs_count(self, failed_jobs): |
| 975 | """Report the number of outstanding failed offload jobs to monarch. |
| 976 | |
| 977 | @param: List of failed jobs. |
| 978 | """ |
| 979 | metrics.Gauge('chromeos/autotest/gs_offloader/failed_jobs_count').set( |
| 980 | len(failed_jobs)) |
| 981 | |
| 982 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 983 | def _enqueue_offload(job, queue, age_limit): |
| 984 | """Enqueue the job for offload, if it's eligible. |
| 985 | |
| 986 | The job is eligible for offloading if the database has marked |
| 987 | it finished, and the job is older than the `age_limit` |
| 988 | parameter. |
| 989 | |
| 990 | If the job is eligible, offload processing is requested by |
| 991 | passing the `queue` parameter's `put()` method a sequence with |
| 992 | the job's `dirname` attribute and its directory name. |
| 993 | |
| 994 | @param job _JobDirectory instance to offload. |
| 995 | @param queue If the job should be offloaded, put the offload |
| 996 | parameters into this queue for processing. |
| 997 | @param age_limit Minimum age for a job to be offloaded. A value |
| 998 | of 0 means that the job will be offloaded as |
| 999 | soon as it is finished. |
| 1000 | |
| 1001 | """ |
| 1002 | if not job.offload_count: |
| 1003 | if not _is_expired(job, age_limit): |
| 1004 | return |
| 1005 | job.first_offload_start = time.time() |
| 1006 | job.offload_count += 1 |
| 1007 | if job.process_gs_instructions(): |
| 1008 | timestamp = job.get_timestamp_if_finished() |
| 1009 | queue.put([job.dirname, os.path.dirname(job.dirname), timestamp]) |
| 1010 | |
| 1011 | |
Simran Basi | 7d9a149 | 2012-10-25 13:51:54 -0700 | [diff] [blame] | 1012 | def parse_options(): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1013 | """Parse the args passed into gs_offloader.""" |
| 1014 | defaults = 'Defaults:\n Destination: %s\n Results Path: %s' % ( |
| 1015 | utils.DEFAULT_OFFLOAD_GSURI, RESULTS_DIR) |
| 1016 | usage = 'usage: %prog [options]\n' + defaults |
| 1017 | parser = OptionParser(usage) |
| 1018 | parser.add_option('-a', '--all', dest='process_all', |
| 1019 | action='store_true', |
| 1020 | help='Offload all files in the results directory.') |
| 1021 | parser.add_option('-s', '--hosts', dest='process_hosts_only', |
| 1022 | action='store_true', |
| 1023 | help='Offload only the special tasks result files ' |
| 1024 | 'located in the results/hosts subdirectory') |
| 1025 | parser.add_option('-p', '--parallelism', dest='parallelism', |
| 1026 | type='int', default=1, |
| 1027 | help='Number of parallel workers to use.') |
| 1028 | parser.add_option('-o', '--delete_only', dest='delete_only', |
| 1029 | action='store_true', |
| 1030 | help='GS Offloader will only the delete the ' |
| 1031 | 'directories and will not offload them to google ' |
| 1032 | 'storage. NOTE: If global_config variable ' |
| 1033 | 'CROS.gs_offloading_enabled is False, --delete_only ' |
| 1034 | 'is automatically True.', |
| 1035 | default=not GS_OFFLOADING_ENABLED) |
| 1036 | parser.add_option('-d', '--days_old', dest='days_old', |
| 1037 | help='Minimum job age in days before a result can be ' |
| 1038 | 'offloaded.', type='int', default=0) |
| 1039 | parser.add_option('-l', '--log_size', dest='log_size', |
| 1040 | help='Limit the offloader logs to a specified ' |
| 1041 | 'number of Mega Bytes.', type='int', default=0) |
| 1042 | parser.add_option('-m', dest='multiprocessing', action='store_true', |
Michael Tang | 0df2eb4 | 2016-05-13 19:06:54 -0700 | [diff] [blame] | 1043 | help='Turn on -m option for gsutil. If not set, the ' |
| 1044 | 'global config setting gs_offloader_multiprocessing ' |
| 1045 | 'under CROS section is applied.') |
Keith Haddow | 44b5e4b | 2016-10-14 11:25:57 -0700 | [diff] [blame] | 1046 | parser.add_option('-i', '--offload_once', dest='offload_once', |
| 1047 | action='store_true', |
| 1048 | help='Upload all available results and then exit.') |
| 1049 | parser.add_option('-y', '--normal_priority', dest='normal_priority', |
| 1050 | action='store_true', |
| 1051 | help='Upload using normal process priority.') |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 1052 | parser.add_option('-u', '--age_to_upload', dest='age_to_upload', |
| 1053 | help='Minimum job age in days before a result can be ' |
| 1054 | 'offloaded, but not removed from local storage', |
| 1055 | type='int', default=None) |
| 1056 | parser.add_option('-n', '--age_to_delete', dest='age_to_delete', |
| 1057 | help='Minimum job age in days before a result can be ' |
| 1058 | 'removed from local storage', |
| 1059 | type='int', default=None) |
Prathmesh Prabhu | f6b3add | 2017-11-29 15:25:43 -0800 | [diff] [blame] | 1060 | parser.add_option( |
| 1061 | '--metrics-file', |
| 1062 | help='If provided, drop metrics to this local file instead of ' |
| 1063 | 'reporting to ts_mon', |
| 1064 | type=str, |
| 1065 | default=None, |
| 1066 | ) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 1067 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1068 | options = parser.parse_args()[0] |
| 1069 | if options.process_all and options.process_hosts_only: |
| 1070 | parser.print_help() |
| 1071 | print ('Cannot process all files and only the hosts ' |
| 1072 | 'subdirectory. Please remove an argument.') |
| 1073 | sys.exit(1) |
Keith Haddow | 5ba5fb8 | 2016-11-09 11:39:36 -0800 | [diff] [blame] | 1074 | |
| 1075 | if options.days_old and (options.age_to_upload or options.age_to_delete): |
| 1076 | parser.print_help() |
| 1077 | print('Use the days_old option or the age_to_* options but not both') |
| 1078 | sys.exit(1) |
| 1079 | |
| 1080 | if options.age_to_upload == None: |
| 1081 | options.age_to_upload = options.days_old |
| 1082 | if options.age_to_delete == None: |
| 1083 | options.age_to_delete = options.days_old |
| 1084 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1085 | return options |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 1086 | |
Simran Basi | 9523eaa | 2012-06-28 17:18:45 -0700 | [diff] [blame] | 1087 | |
| 1088 | def main(): |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1089 | """Main method of gs_offloader.""" |
| 1090 | options = parse_options() |
Alex Miller | 0c8db6d | 2013-02-15 15:41:00 -0800 | [diff] [blame] | 1091 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1092 | if options.process_all: |
| 1093 | offloader_type = 'all' |
| 1094 | elif options.process_hosts_only: |
| 1095 | offloader_type = 'hosts' |
| 1096 | else: |
| 1097 | offloader_type = 'jobs' |
Alex Miller | 0c8db6d | 2013-02-15 15:41:00 -0800 | [diff] [blame] | 1098 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 1099 | _setup_logging(options, offloader_type) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1100 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1101 | # Nice our process (carried to subprocesses) so we don't overload |
| 1102 | # the system. |
Keith Haddow | 44b5e4b | 2016-10-14 11:25:57 -0700 | [diff] [blame] | 1103 | if not options.normal_priority: |
| 1104 | logging.debug('Set process to nice value: %d', NICENESS) |
| 1105 | os.nice(NICENESS) |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1106 | if psutil: |
| 1107 | proc = psutil.Process() |
| 1108 | logging.debug('Set process to ionice IDLE') |
| 1109 | proc.ionice(psutil.IOPRIO_CLASS_IDLE) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1110 | |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1111 | # os.listdir returns relative paths, so change to where we need to |
| 1112 | # be to avoid an os.path.join on each loop. |
| 1113 | logging.debug('Offloading Autotest results in %s', RESULTS_DIR) |
| 1114 | os.chdir(RESULTS_DIR) |
J. Richard Barnette | ea78536 | 2014-03-17 16:00:53 -0700 | [diff] [blame] | 1115 | |
Aviv Keshet | 6fe79f0 | 2017-04-27 16:38:46 -0700 | [diff] [blame] | 1116 | service_name = 'gs_offloader(%s)' % offloader_type |
| 1117 | with ts_mon_config.SetupTsMonGlobalState(service_name, indirect=True, |
Prathmesh Prabhu | f6b3add | 2017-11-29 15:25:43 -0800 | [diff] [blame] | 1118 | short_lived=False, |
| 1119 | debug_file=options.metrics_file): |
Don Garrett | fb984d5 | 2017-10-27 13:08:57 -0700 | [diff] [blame] | 1120 | with metrics.SuccessCounter('chromeos/autotest/gs_offloader/exit'): |
| 1121 | offloader = Offloader(options) |
| 1122 | if not options.delete_only: |
| 1123 | wait_for_gs_write_access(offloader.gs_uri) |
| 1124 | while True: |
| 1125 | offloader.offload_once() |
| 1126 | if options.offload_once: |
| 1127 | break |
| 1128 | time.sleep(SLEEP_TIME_SECS) |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 1129 | |
| 1130 | |
Allen Li | b41527d | 2017-06-22 17:28:00 -0700 | [diff] [blame] | 1131 | _LOG_LOCATION = '/usr/local/autotest/logs/' |
| 1132 | _LOG_FILENAME_FORMAT = 'gs_offloader_%s_log_%s.txt' |
| 1133 | _LOG_TIMESTAMP_FORMAT = '%Y%m%d_%H%M%S' |
| 1134 | _LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s' |
| 1135 | |
| 1136 | |
| 1137 | def _setup_logging(options, offloader_type): |
| 1138 | """Set up logging. |
| 1139 | |
| 1140 | @param options: Parsed options. |
| 1141 | @param offloader_type: Type of offloader action as string. |
| 1142 | """ |
| 1143 | log_filename = _get_log_filename(options, offloader_type) |
| 1144 | log_formatter = logging.Formatter(_LOGGING_FORMAT) |
| 1145 | # Replace the default logging handler with a RotatingFileHandler. If |
| 1146 | # options.log_size is 0, the file size will not be limited. Keeps |
| 1147 | # one backup just in case. |
| 1148 | handler = logging.handlers.RotatingFileHandler( |
| 1149 | log_filename, maxBytes=1024 * options.log_size, backupCount=1) |
| 1150 | handler.setFormatter(log_formatter) |
| 1151 | logger = logging.getLogger() |
| 1152 | logger.setLevel(logging.DEBUG) |
| 1153 | logger.addHandler(handler) |
| 1154 | |
| 1155 | |
| 1156 | def _get_log_filename(options, offloader_type): |
| 1157 | """Get log filename. |
| 1158 | |
| 1159 | @param options: Parsed options. |
| 1160 | @param offloader_type: Type of offloader action as string. |
| 1161 | """ |
| 1162 | if options.log_size > 0: |
| 1163 | log_timestamp = '' |
| 1164 | else: |
| 1165 | log_timestamp = time.strftime(_LOG_TIMESTAMP_FORMAT) |
| 1166 | log_basename = _LOG_FILENAME_FORMAT % (offloader_type, log_timestamp) |
| 1167 | return os.path.join(_LOG_LOCATION, log_basename) |
| 1168 | |
| 1169 | |
Scott Zawalski | 20a9b58 | 2011-11-21 11:49:40 -0800 | [diff] [blame] | 1170 | if __name__ == '__main__': |
J. Richard Barnette | 2c41e1e | 2015-12-08 16:21:10 -0800 | [diff] [blame] | 1171 | main() |