maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
maruel | ea586f3 | 2016-04-05 11:11:33 -0700 | [diff] [blame] | 2 | # Copyright 2012 The LUCI Authors. All rights reserved. |
| 3 | # Use of this source code is governed by the Apache v2.0 license that can be |
| 4 | # found in the LICENSE file. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 5 | |
maruel@chromium.org | 0cd0b18 | 2012-10-22 13:34:15 +0000 | [diff] [blame] | 6 | """Reads a .isolated, creates a tree of hardlinks and runs the test. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 7 | |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 8 | To improve performance, it keeps a local cache. The local cache can safely be |
| 9 | deleted. |
| 10 | |
| 11 | Any ${ISOLATED_OUTDIR} on the command line will be replaced by the location of a |
| 12 | temporary directory upon execution of the command specified in the .isolated |
| 13 | file. All content written to this directory will be uploaded upon termination |
| 14 | and the .isolated file describing this directory will be printed to stdout. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 15 | """ |
| 16 | |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 17 | __version__ = '0.6.1' |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 18 | |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 19 | import base64 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 20 | import logging |
| 21 | import optparse |
| 22 | import os |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 23 | import sys |
| 24 | import tempfile |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 25 | import time |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 26 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 27 | from third_party.depot_tools import fix_encoding |
| 28 | |
Vadim Shtayura | 6b555c1 | 2014-07-23 16:22:18 -0700 | [diff] [blame] | 29 | from utils import file_path |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 30 | from utils import fs |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 31 | from utils import large |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 32 | from utils import logging_utils |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 33 | from utils import on_error |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 34 | from utils import subprocess42 |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 35 | from utils import tools |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 36 | from utils import zip_package |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 37 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 38 | import auth |
Marc-Antoine Ruel | 8bee66d | 2014-08-28 19:02:07 -0400 | [diff] [blame] | 39 | import isolated_format |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 40 | import isolateserver |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 41 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 42 | |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 43 | # Absolute path to this file (can be None if running from zip on Mac). |
| 44 | THIS_FILE_PATH = os.path.abspath(__file__) if __file__ else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 45 | |
| 46 | # Directory that contains this file (might be inside zip package). |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 47 | BASE_DIR = os.path.dirname(THIS_FILE_PATH) if __file__ else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 48 | |
| 49 | # Directory that contains currently running script file. |
maruel@chromium.org | 814d23f | 2013-10-01 19:08:00 +0000 | [diff] [blame] | 50 | if zip_package.get_main_script_path(): |
| 51 | MAIN_DIR = os.path.dirname( |
| 52 | os.path.abspath(zip_package.get_main_script_path())) |
| 53 | else: |
| 54 | # This happens when 'import run_isolated' is executed at the python |
| 55 | # interactive prompt, in that case __file__ is undefined. |
| 56 | MAIN_DIR = None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 57 | |
csharp@chromium.org | ff2a466 | 2012-11-21 20:49:32 +0000 | [diff] [blame] | 58 | # The name of the log file to use. |
| 59 | RUN_ISOLATED_LOG_FILE = 'run_isolated.log' |
| 60 | |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 61 | # The name of the log to use for the run_test_cases.py command |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 62 | RUN_TEST_CASES_LOG = 'run_test_cases.log' |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 63 | |
vadimsh@chromium.org | 87d6326 | 2013-04-04 19:34:21 +0000 | [diff] [blame] | 64 | |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 65 | def get_as_zip_package(executable=True): |
| 66 | """Returns ZipPackage with this module and all its dependencies. |
| 67 | |
| 68 | If |executable| is True will store run_isolated.py as __main__.py so that |
| 69 | zip package is directly executable be python. |
| 70 | """ |
| 71 | # Building a zip package when running from another zip package is |
| 72 | # unsupported and probably unneeded. |
| 73 | assert not zip_package.is_zipped_module(sys.modules[__name__]) |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 74 | assert THIS_FILE_PATH |
| 75 | assert BASE_DIR |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 76 | package = zip_package.ZipPackage(root=BASE_DIR) |
| 77 | package.add_python_file(THIS_FILE_PATH, '__main__.py' if executable else None) |
Marc-Antoine Ruel | 8bee66d | 2014-08-28 19:02:07 -0400 | [diff] [blame] | 78 | package.add_python_file(os.path.join(BASE_DIR, 'isolated_format.py')) |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 79 | package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py')) |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 80 | package.add_python_file(os.path.join(BASE_DIR, 'auth.py')) |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 81 | package.add_directory(os.path.join(BASE_DIR, 'third_party')) |
| 82 | package.add_directory(os.path.join(BASE_DIR, 'utils')) |
| 83 | return package |
| 84 | |
| 85 | |
Vadim Shtayura | cb0b743 | 2015-07-31 13:26:50 -0700 | [diff] [blame] | 86 | def make_temp_dir(prefix, root_dir=None): |
| 87 | """Returns a temporary directory. |
| 88 | |
| 89 | If root_dir is given and /tmp is on same file system as root_dir, uses /tmp. |
| 90 | Otherwise makes a new temp directory under root_dir. |
maruel | 79d5e06 | 2016-04-08 13:39:57 -0700 | [diff] [blame] | 91 | |
| 92 | Except on OSX, because it's dangerous to create hardlinks in $TMPDIR on OSX! |
| 93 | /System/Library/LaunchDaemons/com.apple.bsd.dirhelper.plist runs every day at |
| 94 | 3:35am and deletes all files older than 3 days in $TMPDIR, but hardlinks do |
| 95 | not have the inode modification time updated, so they tend to be old, thus |
| 96 | they get deleted. |
Vadim Shtayura | cb0b743 | 2015-07-31 13:26:50 -0700 | [diff] [blame] | 97 | """ |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 98 | base_temp_dir = None |
maruel | 79d5e06 | 2016-04-08 13:39:57 -0700 | [diff] [blame] | 99 | real_temp_dir = unicode(tempfile.gettempdir()) |
| 100 | if sys.platform == 'darwin': |
| 101 | # Nope! Nope! Nope! |
| 102 | assert root_dir, 'It is unsafe to create hardlinks in $TMPDIR' |
| 103 | base_temp_dir = root_dir |
| 104 | elif root_dir and not file_path.is_same_filesystem(root_dir, real_temp_dir): |
Paweł Hajdan, Jr | f7d5872 | 2015-04-27 14:54:42 +0200 | [diff] [blame] | 105 | base_temp_dir = root_dir |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 106 | return unicode(tempfile.mkdtemp(prefix=prefix, dir=base_temp_dir)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 107 | |
| 108 | |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 109 | def change_tree_read_only(rootdir, read_only): |
| 110 | """Changes the tree read-only bits according to the read_only specification. |
| 111 | |
| 112 | The flag can be 0, 1 or 2, which will affect the possibility to modify files |
| 113 | and create or delete files. |
| 114 | """ |
| 115 | if read_only == 2: |
| 116 | # Files and directories (except on Windows) are marked read only. This |
| 117 | # inhibits modifying, creating or deleting files in the test directory, |
| 118 | # except on Windows where creating and deleting files is still possible. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 119 | file_path.make_tree_read_only(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 120 | elif read_only == 1: |
| 121 | # Files are marked read only but not the directories. This inhibits |
| 122 | # modifying files but creating or deleting files is still possible. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 123 | file_path.make_tree_files_read_only(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 124 | elif read_only in (0, None): |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 125 | # Anything can be modified. |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 126 | # TODO(maruel): This is currently dangerous as long as DiskCache.touch() |
| 127 | # is not yet changed to verify the hash of the content of the files it is |
| 128 | # looking at, so that if a test modifies an input file, the file must be |
| 129 | # deleted. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 130 | file_path.make_tree_writeable(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 131 | else: |
| 132 | raise ValueError( |
| 133 | 'change_tree_read_only(%s, %s): Unknown flag %s' % |
| 134 | (rootdir, read_only, read_only)) |
| 135 | |
| 136 | |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 137 | def process_command(command, out_dir): |
| 138 | """Replaces isolated specific variables in a command line.""" |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 139 | def fix(arg): |
Vadim Shtayura | 51aba36 | 2014-05-14 15:39:23 -0700 | [diff] [blame] | 140 | if '${ISOLATED_OUTDIR}' in arg: |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 141 | return arg.replace('${ISOLATED_OUTDIR}', out_dir).replace('/', os.sep) |
| 142 | return arg |
| 143 | |
| 144 | return [fix(arg) for arg in command] |
| 145 | |
| 146 | |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 147 | def run_command(command, cwd, tmp_dir, hard_timeout, grace_period): |
| 148 | """Runs the command. |
| 149 | |
| 150 | Returns: |
| 151 | tuple(process exit code, bool if had a hard timeout) |
| 152 | """ |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 153 | logging.info('run_command(%s, %s)' % (command, cwd)) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 154 | |
| 155 | env = os.environ.copy() |
| 156 | if sys.platform == 'darwin': |
| 157 | env['TMPDIR'] = tmp_dir.encode('ascii') |
| 158 | elif sys.platform == 'win32': |
maruel | df2329b | 2016-01-19 15:33:23 -0800 | [diff] [blame] | 159 | env['TEMP'] = tmp_dir.encode('ascii') |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 160 | else: |
| 161 | env['TMP'] = tmp_dir.encode('ascii') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 162 | exit_code = None |
| 163 | had_hard_timeout = False |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 164 | with tools.Profiler('RunTest'): |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 165 | proc = None |
| 166 | had_signal = [] |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 167 | try: |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 168 | # TODO(maruel): This code is imperfect. It doesn't handle well signals |
| 169 | # during the download phase and there's short windows were things can go |
| 170 | # wrong. |
| 171 | def handler(signum, _frame): |
| 172 | if proc and not had_signal: |
| 173 | logging.info('Received signal %d', signum) |
| 174 | had_signal.append(True) |
maruel | 556d905 | 2015-10-05 11:12:44 -0700 | [diff] [blame] | 175 | raise subprocess42.TimeoutExpired(command, None) |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 176 | |
| 177 | proc = subprocess42.Popen(command, cwd=cwd, env=env, detached=True) |
| 178 | with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, handler): |
| 179 | try: |
| 180 | exit_code = proc.wait(hard_timeout or None) |
| 181 | except subprocess42.TimeoutExpired: |
| 182 | if not had_signal: |
| 183 | logging.warning('Hard timeout') |
| 184 | had_hard_timeout = True |
| 185 | logging.warning('Sending SIGTERM') |
| 186 | proc.terminate() |
| 187 | |
| 188 | # Ignore signals in grace period. Forcibly give the grace period to the |
| 189 | # child process. |
| 190 | if exit_code is None: |
| 191 | ignore = lambda *_: None |
| 192 | with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, ignore): |
| 193 | try: |
| 194 | exit_code = proc.wait(grace_period or None) |
| 195 | except subprocess42.TimeoutExpired: |
| 196 | # Now kill for real. The user can distinguish between the |
| 197 | # following states: |
| 198 | # - signal but process exited within grace period, |
| 199 | # hard_timed_out will be set but the process exit code will be |
| 200 | # script provided. |
| 201 | # - processed exited late, exit code will be -9 on posix. |
| 202 | logging.warning('Grace exhausted; sending SIGKILL') |
| 203 | proc.kill() |
| 204 | logging.info('Waiting for proces exit') |
| 205 | exit_code = proc.wait() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 206 | except OSError: |
| 207 | # This is not considered to be an internal error. The executable simply |
| 208 | # does not exit. |
maruel | a72f46e | 2016-02-24 11:05:45 -0800 | [diff] [blame] | 209 | sys.stderr.write( |
| 210 | '<The executable does not exist or a dependent library is missing>\n' |
| 211 | '<Check for missing .so/.dll in the .isolate or GN file>\n' |
| 212 | '<Command: %s>\n' % command) |
| 213 | if os.environ.get('SWARMING_TASK_ID'): |
| 214 | # Give an additional hint when running as a swarming task. |
| 215 | sys.stderr.write( |
| 216 | '<See the task\'s page for commands to help diagnose this issue ' |
| 217 | 'by reproducing the task locally>\n') |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 218 | exit_code = 1 |
| 219 | logging.info( |
| 220 | 'Command finished with exit code %d (%s)', |
| 221 | exit_code, hex(0xffffffff & exit_code)) |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 222 | return exit_code, had_hard_timeout |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 223 | |
| 224 | |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 225 | def fetch_and_measure(isolated_hash, storage, cache, outdir): |
| 226 | """Fetches an isolated and returns (bundle, stats).""" |
| 227 | start = time.time() |
| 228 | bundle = isolateserver.fetch_isolated( |
| 229 | isolated_hash=isolated_hash, |
| 230 | storage=storage, |
| 231 | cache=cache, |
| 232 | outdir=outdir) |
| 233 | return bundle, { |
| 234 | 'duration': time.time() - start, |
| 235 | 'initial_number_items': cache.initial_number_items, |
| 236 | 'initial_size': cache.initial_size, |
| 237 | 'items_cold': base64.b64encode(large.pack(sorted(cache.added))), |
| 238 | 'items_hot': base64.b64encode( |
| 239 | large.pack(sorted(set(cache.linked) - set(cache.added)))), |
| 240 | } |
| 241 | |
| 242 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 243 | def delete_and_upload(storage, out_dir, leak_temp_dir): |
| 244 | """Deletes the temporary run directory and uploads results back. |
| 245 | |
| 246 | Returns: |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 247 | tuple(outputs_ref, success, stats) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 248 | - outputs_ref: a dict referring to the results archived back to the isolated |
| 249 | server, if applicable. |
| 250 | - success: False if something occurred that means that the task must |
| 251 | forcibly be considered a failure, e.g. zombie processes were left |
| 252 | behind. |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 253 | - stats: uploading stats. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 254 | """ |
| 255 | |
| 256 | # Upload out_dir and generate a .isolated file out of this directory. It is |
| 257 | # only done if files were written in the directory. |
| 258 | outputs_ref = None |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 259 | cold = [] |
| 260 | hot = [] |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 261 | start = time.time() |
| 262 | |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 263 | if fs.isdir(out_dir) and fs.listdir(out_dir): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 264 | with tools.Profiler('ArchiveOutput'): |
| 265 | try: |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 266 | results, f_cold, f_hot = isolateserver.archive_files_to_storage( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 267 | storage, [out_dir], None) |
| 268 | outputs_ref = { |
| 269 | 'isolated': results[0][0], |
| 270 | 'isolatedserver': storage.location, |
| 271 | 'namespace': storage.namespace, |
| 272 | } |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 273 | cold = sorted(i.size for i in f_cold) |
| 274 | hot = sorted(i.size for i in f_hot) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 275 | except isolateserver.Aborted: |
| 276 | # This happens when a signal SIGTERM was received while uploading data. |
| 277 | # There is 2 causes: |
| 278 | # - The task was too slow and was about to be killed anyway due to |
| 279 | # exceeding the hard timeout. |
| 280 | # - The amount of data uploaded back is very large and took too much |
| 281 | # time to archive. |
| 282 | sys.stderr.write('Received SIGTERM while uploading') |
| 283 | # Re-raise, so it will be treated as an internal failure. |
| 284 | raise |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 285 | |
| 286 | success = False |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 287 | try: |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 288 | if (not leak_temp_dir and fs.isdir(out_dir) and |
maruel | 6eeea7d | 2015-09-16 12:17:42 -0700 | [diff] [blame] | 289 | not file_path.rmtree(out_dir)): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 290 | logging.error('Had difficulties removing out_dir %s', out_dir) |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 291 | else: |
| 292 | success = True |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 293 | except OSError as e: |
| 294 | # When this happens, it means there's a process error. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 295 | logging.exception('Had difficulties removing out_dir %s: %s', out_dir, e) |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 296 | stats = { |
| 297 | 'duration': time.time() - start, |
| 298 | 'items_cold': base64.b64encode(large.pack(cold)), |
| 299 | 'items_hot': base64.b64encode(large.pack(hot)), |
| 300 | } |
| 301 | return outputs_ref, success, stats |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 302 | |
| 303 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 304 | def map_and_run( |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 305 | isolated_hash, storage, cache, leak_temp_dir, root_dir, hard_timeout, |
| 306 | grace_period, extra_args): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 307 | """Maps and run the command. Returns metadata about the result.""" |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 308 | result = { |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 309 | 'duration': None, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 310 | 'exit_code': None, |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 311 | 'had_hard_timeout': False, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 312 | 'internal_failure': None, |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 313 | 'stats': { |
| 314 | # 'download': { |
| 315 | # 'duration': 0., |
| 316 | # 'initial_number_items': 0, |
| 317 | # 'initial_size': 0, |
| 318 | # 'items_cold': '<large.pack()>', |
| 319 | # 'items_hot': '<large.pack()>', |
| 320 | # }, |
| 321 | # 'upload': { |
| 322 | # 'duration': 0., |
| 323 | # 'items_cold': '<large.pack()>', |
| 324 | # 'items_hot': '<large.pack()>', |
| 325 | # }, |
| 326 | }, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 327 | 'outputs_ref': None, |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 328 | 'version': 3, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 329 | } |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 330 | if root_dir: |
nodir | e5028a9 | 2016-04-29 14:38:21 -0700 | [diff] [blame] | 331 | file_path.ensure_tree(root_dir, 0700) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 332 | prefix = u'' |
| 333 | else: |
| 334 | root_dir = os.path.dirname(cache.cache_dir) if cache.cache_dir else None |
| 335 | prefix = u'isolated_' |
| 336 | run_dir = make_temp_dir(prefix + u'run', root_dir) |
| 337 | out_dir = make_temp_dir(prefix + u'out', root_dir) |
| 338 | tmp_dir = make_temp_dir(prefix + u'tmp', root_dir) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 339 | try: |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 340 | bundle, result['stats']['download'] = fetch_and_measure( |
maruel | b8d88d1 | 2016-04-08 12:54:01 -0700 | [diff] [blame] | 341 | isolated_hash=isolated_hash, |
| 342 | storage=storage, |
| 343 | cache=cache, |
| 344 | outdir=run_dir) |
| 345 | if not bundle.command: |
maruel | a72f46e | 2016-02-24 11:05:45 -0800 | [diff] [blame] | 346 | # Handle this as a task failure, not an internal failure. |
| 347 | sys.stderr.write( |
| 348 | '<The .isolated doesn\'t declare any command to run!>\n' |
| 349 | '<Check your .isolate for missing \'command\' variable>\n') |
| 350 | if os.environ.get('SWARMING_TASK_ID'): |
| 351 | # Give an additional hint when running as a swarming task. |
| 352 | sys.stderr.write('<This occurs at the \'isolate\' step>\n') |
| 353 | result['exit_code'] = 1 |
| 354 | return result |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 355 | |
| 356 | change_tree_read_only(run_dir, bundle.read_only) |
| 357 | cwd = os.path.normpath(os.path.join(run_dir, bundle.relative_cwd)) |
| 358 | command = bundle.command + extra_args |
| 359 | file_path.ensure_command_has_abs_path(command, cwd) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 360 | sys.stdout.flush() |
| 361 | start = time.time() |
| 362 | try: |
| 363 | result['exit_code'], result['had_hard_timeout'] = run_command( |
| 364 | process_command(command, out_dir), cwd, tmp_dir, hard_timeout, |
| 365 | grace_period) |
| 366 | finally: |
| 367 | result['duration'] = max(time.time() - start, 0) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 368 | except Exception as e: |
| 369 | # An internal error occured. Report accordingly so the swarming task will be |
| 370 | # retried automatically. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 371 | logging.exception('internal failure: %s', e) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 372 | result['internal_failure'] = str(e) |
| 373 | on_error.report(None) |
| 374 | finally: |
| 375 | try: |
| 376 | if leak_temp_dir: |
| 377 | logging.warning( |
| 378 | 'Deliberately leaking %s for later examination', run_dir) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 379 | else: |
maruel | 84537cb | 2015-10-16 14:21:28 -0700 | [diff] [blame] | 380 | # On Windows rmtree(run_dir) call above has a synchronization effect: it |
| 381 | # finishes only when all task child processes terminate (since a running |
| 382 | # process locks *.exe file). Examine out_dir only after that call |
| 383 | # completes (since child processes may write to out_dir too and we need |
| 384 | # to wait for them to finish). |
| 385 | if fs.isdir(run_dir): |
| 386 | try: |
| 387 | success = file_path.rmtree(run_dir) |
| 388 | except OSError as e: |
| 389 | logging.error('Failure with %s', e) |
| 390 | success = False |
| 391 | if not success: |
| 392 | print >> sys.stderr, ( |
| 393 | 'Failed to delete the run directory, forcibly failing\n' |
| 394 | 'the task because of it. No zombie process can outlive a\n' |
| 395 | 'successful task run and still be marked as successful.\n' |
| 396 | 'Fix your stuff.') |
| 397 | if result['exit_code'] == 0: |
| 398 | result['exit_code'] = 1 |
| 399 | if fs.isdir(tmp_dir): |
| 400 | try: |
| 401 | success = file_path.rmtree(tmp_dir) |
| 402 | except OSError as e: |
| 403 | logging.error('Failure with %s', e) |
| 404 | success = False |
| 405 | if not success: |
| 406 | print >> sys.stderr, ( |
| 407 | 'Failed to delete the temporary directory, forcibly failing\n' |
| 408 | 'the task because of it. No zombie process can outlive a\n' |
| 409 | 'successful task run and still be marked as successful.\n' |
| 410 | 'Fix your stuff.') |
| 411 | if result['exit_code'] == 0: |
| 412 | result['exit_code'] = 1 |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 413 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 414 | # This deletes out_dir if leak_temp_dir is not set. |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame^] | 415 | result['outputs_ref'], success, result['stats']['upload'] = ( |
| 416 | delete_and_upload(storage, out_dir, leak_temp_dir)) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 417 | if not success and result['exit_code'] == 0: |
| 418 | result['exit_code'] = 1 |
| 419 | except Exception as e: |
| 420 | # Swallow any exception in the main finally clause. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 421 | logging.exception('Leaking out_dir %s: %s', out_dir, e) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 422 | result['internal_failure'] = str(e) |
| 423 | return result |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 424 | |
| 425 | |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 426 | def run_tha_test( |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 427 | isolated_hash, storage, cache, leak_temp_dir, result_json, root_dir, |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 428 | hard_timeout, grace_period, extra_args): |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 429 | """Downloads the dependencies in the cache, hardlinks them into a temporary |
| 430 | directory and runs the executable from there. |
| 431 | |
| 432 | A temporary directory is created to hold the output files. The content inside |
| 433 | this directory will be uploaded back to |storage| packaged as a .isolated |
| 434 | file. |
| 435 | |
| 436 | Arguments: |
Marc-Antoine Ruel | 35b5843 | 2014-12-08 17:40:40 -0500 | [diff] [blame] | 437 | isolated_hash: the SHA-1 of the .isolated file that must be retrieved to |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 438 | recreate the tree of files to run the target executable. |
| 439 | storage: an isolateserver.Storage object to retrieve remote objects. This |
| 440 | object has a reference to an isolateserver.StorageApi, which does |
| 441 | the actual I/O. |
| 442 | cache: an isolateserver.LocalCache to keep from retrieving the same objects |
| 443 | constantly by caching the objects retrieved. Can be on-disk or |
| 444 | in-memory. |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 445 | leak_temp_dir: if true, the temporary directory will be deliberately leaked |
| 446 | for later examination. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 447 | result_json: file path to dump result metadata into. If set, the process |
| 448 | exit code is always 0 unless an internal error occured. |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 449 | root_dir: directory to the path to use to create the temporary directory. If |
| 450 | not specified, a random temporary directory is created. |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 451 | hard_timeout: kills the process if it lasts more than this amount of |
| 452 | seconds. |
| 453 | grace_period: number of seconds to wait between SIGTERM and SIGKILL. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 454 | extra_args: optional arguments to add to the command stated in the .isolate |
| 455 | file. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 456 | |
| 457 | Returns: |
| 458 | Process exit code that should be used. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 459 | """ |
maruel | a76b9ee | 2015-12-15 06:18:08 -0800 | [diff] [blame] | 460 | if result_json: |
| 461 | # Write a json output file right away in case we get killed. |
| 462 | result = { |
| 463 | 'exit_code': None, |
| 464 | 'had_hard_timeout': False, |
| 465 | 'internal_failure': 'Was terminated before completion', |
| 466 | 'outputs_ref': None, |
| 467 | 'version': 2, |
| 468 | } |
| 469 | tools.write_json(result_json, result, dense=True) |
| 470 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 471 | # run_isolated exit code. Depends on if result_json is used or not. |
| 472 | result = map_and_run( |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 473 | isolated_hash, storage, cache, leak_temp_dir, root_dir, hard_timeout, |
| 474 | grace_period, extra_args) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 475 | logging.info('Result:\n%s', tools.format_json(result, dense=True)) |
| 476 | if result_json: |
maruel | 05d5a88 | 2015-09-21 13:59:02 -0700 | [diff] [blame] | 477 | # We've found tests to delete 'work' when quitting, causing an exception |
| 478 | # here. Try to recreate the directory if necessary. |
nodir | e5028a9 | 2016-04-29 14:38:21 -0700 | [diff] [blame] | 479 | file_path.ensure_tree(os.path.dirname(result_json)) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 480 | tools.write_json(result_json, result, dense=True) |
| 481 | # Only return 1 if there was an internal error. |
| 482 | return int(bool(result['internal_failure'])) |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 483 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 484 | # Marshall into old-style inline output. |
| 485 | if result['outputs_ref']: |
| 486 | data = { |
| 487 | 'hash': result['outputs_ref']['isolated'], |
| 488 | 'namespace': result['outputs_ref']['namespace'], |
| 489 | 'storage': result['outputs_ref']['isolatedserver'], |
| 490 | } |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 491 | sys.stdout.flush() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 492 | print( |
| 493 | '[run_isolated_out_hack]%s[/run_isolated_out_hack]' % |
| 494 | tools.format_json(data, dense=True)) |
maruel | b76604c | 2015-11-11 11:53:44 -0800 | [diff] [blame] | 495 | sys.stdout.flush() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 496 | return result['exit_code'] or int(bool(result['internal_failure'])) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 497 | |
| 498 | |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 499 | def main(args): |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 500 | parser = logging_utils.OptionParserWithLogging( |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 501 | usage='%prog <options>', |
| 502 | version=__version__, |
| 503 | log_file=RUN_ISOLATED_LOG_FILE) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 504 | parser.add_option( |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 505 | '--clean', action='store_true', |
| 506 | help='Cleans the cache, trimming it necessary and remove corrupted items ' |
| 507 | 'and returns without executing anything; use with -v to know what ' |
| 508 | 'was done') |
| 509 | parser.add_option( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 510 | '--json', |
| 511 | help='dump output metadata to json file. When used, run_isolated returns ' |
| 512 | 'non-zero only on internal failure') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 513 | parser.add_option( |
maruel | 5c9e47b | 2015-12-18 13:02:30 -0800 | [diff] [blame] | 514 | '--hard-timeout', type='float', help='Enforce hard timeout in execution') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 515 | parser.add_option( |
maruel | 5c9e47b | 2015-12-18 13:02:30 -0800 | [diff] [blame] | 516 | '--grace-period', type='float', |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 517 | help='Grace period between SIGTERM and SIGKILL') |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 518 | data_group = optparse.OptionGroup(parser, 'Data source') |
| 519 | data_group.add_option( |
Marc-Antoine Ruel | 185ded4 | 2015-01-28 20:49:18 -0500 | [diff] [blame] | 520 | '-s', '--isolated', |
| 521 | help='Hash of the .isolated to grab from the isolate server') |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 522 | isolateserver.add_isolate_server_options(data_group) |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 523 | parser.add_option_group(data_group) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 524 | |
Marc-Antoine Ruel | a57d7db | 2014-10-15 20:31:19 -0400 | [diff] [blame] | 525 | isolateserver.add_cache_options(parser) |
| 526 | parser.set_defaults(cache='cache') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 527 | |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 528 | debug_group = optparse.OptionGroup(parser, 'Debugging') |
| 529 | debug_group.add_option( |
| 530 | '--leak-temp-dir', |
| 531 | action='store_true', |
| 532 | help='Deliberately leak isolate\'s temp dir for later examination ' |
| 533 | '[default: %default]') |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 534 | debug_group.add_option( |
| 535 | '--root-dir', help='Use a directory instead of a random one') |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 536 | parser.add_option_group(debug_group) |
| 537 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 538 | auth.add_auth_options(parser) |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 539 | options, args = parser.parse_args(args) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 540 | |
| 541 | cache = isolateserver.process_cache_options(options) |
| 542 | if options.clean: |
| 543 | if options.isolated: |
| 544 | parser.error('Can\'t use --isolated with --clean.') |
| 545 | if options.isolate_server: |
| 546 | parser.error('Can\'t use --isolate-server with --clean.') |
| 547 | if options.json: |
| 548 | parser.error('Can\'t use --json with --clean.') |
| 549 | cache.cleanup() |
| 550 | return 0 |
| 551 | |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 552 | auth.process_auth_options(parser, options) |
Marc-Antoine Ruel | e290ada | 2014-12-10 19:48:49 -0500 | [diff] [blame] | 553 | isolateserver.process_isolate_server_options(parser, options, True) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 554 | |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 555 | if options.root_dir: |
| 556 | options.root_dir = unicode(os.path.abspath(options.root_dir)) |
| 557 | if options.json: |
| 558 | options.json = unicode(os.path.abspath(options.json)) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 559 | if not options.isolated: |
| 560 | parser.error('--isolated is required.') |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 561 | with isolateserver.get_storage( |
| 562 | options.isolate_server, options.namespace) as storage: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 563 | # Hashing schemes used by |storage| and |cache| MUST match. |
| 564 | assert storage.hash_algo == cache.hash_algo |
| 565 | return run_tha_test( |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 566 | options.isolated, storage, cache, options.leak_temp_dir, options.json, |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 567 | options.root_dir, options.hard_timeout, options.grace_period, args) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 568 | |
| 569 | |
| 570 | if __name__ == '__main__': |
csharp@chromium.org | bfb9874 | 2013-03-26 20:28:36 +0000 | [diff] [blame] | 571 | # Ensure that we are always running with the correct encoding. |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 572 | fix_encoding.fix_encoding() |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 573 | sys.exit(main(sys.argv[1:])) |