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 | |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 17 | __version__ = '0.6.0' |
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 | |
| 225 | def delete_and_upload(storage, out_dir, leak_temp_dir): |
| 226 | """Deletes the temporary run directory and uploads results back. |
| 227 | |
| 228 | Returns: |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 229 | tuple(outputs_ref, success, cold, hot) |
| 230 | - outputs_ref: a dict referring to the results archived back to the isolated |
| 231 | server, if applicable. |
| 232 | - success: False if something occurred that means that the task must |
| 233 | forcibly be considered a failure, e.g. zombie processes were left |
| 234 | behind. |
| 235 | - cold: list of size of cold items, they had to be uploaded. |
| 236 | - hot: list of size of hot items, they didn't have to be uploaded. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 237 | """ |
| 238 | |
| 239 | # Upload out_dir and generate a .isolated file out of this directory. It is |
| 240 | # only done if files were written in the directory. |
| 241 | outputs_ref = None |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 242 | cold = [] |
| 243 | hot = [] |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 244 | if fs.isdir(out_dir) and fs.listdir(out_dir): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 245 | with tools.Profiler('ArchiveOutput'): |
| 246 | try: |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 247 | results, f_cold, f_hot = isolateserver.archive_files_to_storage( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 248 | storage, [out_dir], None) |
| 249 | outputs_ref = { |
| 250 | 'isolated': results[0][0], |
| 251 | 'isolatedserver': storage.location, |
| 252 | 'namespace': storage.namespace, |
| 253 | } |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 254 | cold = sorted(i.size for i in f_cold) |
| 255 | hot = sorted(i.size for i in f_hot) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 256 | except isolateserver.Aborted: |
| 257 | # This happens when a signal SIGTERM was received while uploading data. |
| 258 | # There is 2 causes: |
| 259 | # - The task was too slow and was about to be killed anyway due to |
| 260 | # exceeding the hard timeout. |
| 261 | # - The amount of data uploaded back is very large and took too much |
| 262 | # time to archive. |
| 263 | sys.stderr.write('Received SIGTERM while uploading') |
| 264 | # Re-raise, so it will be treated as an internal failure. |
| 265 | raise |
| 266 | try: |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 267 | if (not leak_temp_dir and fs.isdir(out_dir) and |
maruel | 6eeea7d | 2015-09-16 12:17:42 -0700 | [diff] [blame] | 268 | not file_path.rmtree(out_dir)): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 269 | logging.error('Had difficulties removing out_dir %s', out_dir) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 270 | return outputs_ref, False, cold, hot |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 271 | except OSError as e: |
| 272 | # When this happens, it means there's a process error. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 273 | logging.exception('Had difficulties removing out_dir %s: %s', out_dir, e) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 274 | return outputs_ref, False, cold, hot |
| 275 | return outputs_ref, True, cold, hot |
| 276 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 277 | |
| 278 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 279 | def map_and_run( |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 280 | isolated_hash, storage, cache, leak_temp_dir, root_dir, hard_timeout, |
| 281 | grace_period, extra_args): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 282 | """Maps and run the command. Returns metadata about the result.""" |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 283 | result = { |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 284 | 'duration': None, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 285 | 'exit_code': None, |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 286 | 'had_hard_timeout': False, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 287 | 'internal_failure': None, |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 288 | 'stats': { |
| 289 | # 'download': { |
| 290 | # 'duration': 0., |
| 291 | # 'initial_number_items': 0, |
| 292 | # 'initial_size': 0, |
| 293 | # 'items_cold': '<large.pack()>', |
| 294 | # 'items_hot': '<large.pack()>', |
| 295 | # }, |
| 296 | # 'upload': { |
| 297 | # 'duration': 0., |
| 298 | # 'items_cold': '<large.pack()>', |
| 299 | # 'items_hot': '<large.pack()>', |
| 300 | # }, |
| 301 | }, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 302 | 'outputs_ref': None, |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 303 | 'version': 3, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 304 | } |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 305 | if root_dir: |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 306 | if not fs.isdir(root_dir): |
| 307 | fs.makedirs(root_dir, 0700) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 308 | prefix = u'' |
| 309 | else: |
| 310 | root_dir = os.path.dirname(cache.cache_dir) if cache.cache_dir else None |
| 311 | prefix = u'isolated_' |
| 312 | run_dir = make_temp_dir(prefix + u'run', root_dir) |
| 313 | out_dir = make_temp_dir(prefix + u'out', root_dir) |
| 314 | tmp_dir = make_temp_dir(prefix + u'tmp', root_dir) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 315 | try: |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 316 | start = time.time() |
maruel | b8d88d1 | 2016-04-08 12:54:01 -0700 | [diff] [blame] | 317 | bundle = isolateserver.fetch_isolated( |
| 318 | isolated_hash=isolated_hash, |
| 319 | storage=storage, |
| 320 | cache=cache, |
| 321 | outdir=run_dir) |
| 322 | if not bundle.command: |
maruel | a72f46e | 2016-02-24 11:05:45 -0800 | [diff] [blame] | 323 | # Handle this as a task failure, not an internal failure. |
| 324 | sys.stderr.write( |
| 325 | '<The .isolated doesn\'t declare any command to run!>\n' |
| 326 | '<Check your .isolate for missing \'command\' variable>\n') |
| 327 | if os.environ.get('SWARMING_TASK_ID'): |
| 328 | # Give an additional hint when running as a swarming task. |
| 329 | sys.stderr.write('<This occurs at the \'isolate\' step>\n') |
| 330 | result['exit_code'] = 1 |
| 331 | return result |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 332 | result['stats']['download'] = { |
| 333 | 'duration': time.time() - start, |
| 334 | 'initial_number_items': cache.initial_number_items, |
| 335 | 'initial_size': cache.initial_size, |
| 336 | 'items_cold': base64.b64encode(large.pack(sorted(cache.added))), |
| 337 | 'items_hot': base64.b64encode( |
| 338 | large.pack(sorted(set(cache.linked) - set(cache.added)))), |
| 339 | } |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 340 | |
| 341 | change_tree_read_only(run_dir, bundle.read_only) |
| 342 | cwd = os.path.normpath(os.path.join(run_dir, bundle.relative_cwd)) |
| 343 | command = bundle.command + extra_args |
| 344 | file_path.ensure_command_has_abs_path(command, cwd) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 345 | sys.stdout.flush() |
| 346 | start = time.time() |
| 347 | try: |
| 348 | result['exit_code'], result['had_hard_timeout'] = run_command( |
| 349 | process_command(command, out_dir), cwd, tmp_dir, hard_timeout, |
| 350 | grace_period) |
| 351 | finally: |
| 352 | result['duration'] = max(time.time() - start, 0) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 353 | except Exception as e: |
| 354 | # An internal error occured. Report accordingly so the swarming task will be |
| 355 | # retried automatically. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 356 | logging.exception('internal failure: %s', e) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 357 | result['internal_failure'] = str(e) |
| 358 | on_error.report(None) |
| 359 | finally: |
| 360 | try: |
| 361 | if leak_temp_dir: |
| 362 | logging.warning( |
| 363 | 'Deliberately leaking %s for later examination', run_dir) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 364 | else: |
maruel | 84537cb | 2015-10-16 14:21:28 -0700 | [diff] [blame] | 365 | # On Windows rmtree(run_dir) call above has a synchronization effect: it |
| 366 | # finishes only when all task child processes terminate (since a running |
| 367 | # process locks *.exe file). Examine out_dir only after that call |
| 368 | # completes (since child processes may write to out_dir too and we need |
| 369 | # to wait for them to finish). |
| 370 | if fs.isdir(run_dir): |
| 371 | try: |
| 372 | success = file_path.rmtree(run_dir) |
| 373 | except OSError as e: |
| 374 | logging.error('Failure with %s', e) |
| 375 | success = False |
| 376 | if not success: |
| 377 | print >> sys.stderr, ( |
| 378 | 'Failed to delete the run directory, forcibly failing\n' |
| 379 | 'the task because of it. No zombie process can outlive a\n' |
| 380 | 'successful task run and still be marked as successful.\n' |
| 381 | 'Fix your stuff.') |
| 382 | if result['exit_code'] == 0: |
| 383 | result['exit_code'] = 1 |
| 384 | if fs.isdir(tmp_dir): |
| 385 | try: |
| 386 | success = file_path.rmtree(tmp_dir) |
| 387 | except OSError as e: |
| 388 | logging.error('Failure with %s', e) |
| 389 | success = False |
| 390 | if not success: |
| 391 | print >> sys.stderr, ( |
| 392 | 'Failed to delete the temporary directory, forcibly failing\n' |
| 393 | 'the task because of it. No zombie process can outlive a\n' |
| 394 | 'successful task run and still be marked as successful.\n' |
| 395 | 'Fix your stuff.') |
| 396 | if result['exit_code'] == 0: |
| 397 | result['exit_code'] = 1 |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 398 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 399 | # This deletes out_dir if leak_temp_dir is not set. |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 400 | start = time.time() |
| 401 | result['outputs_ref'], success, cold, hot = delete_and_upload( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 402 | storage, out_dir, leak_temp_dir) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 403 | result['stats']['upload'] = { |
| 404 | 'duration': time.time() - start, |
| 405 | 'items_cold': base64.b64encode(large.pack(cold)), |
| 406 | 'items_hot': base64.b64encode(large.pack(hot)), |
| 407 | } |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 408 | if not success and result['exit_code'] == 0: |
| 409 | result['exit_code'] = 1 |
| 410 | except Exception as e: |
| 411 | # Swallow any exception in the main finally clause. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 412 | logging.exception('Leaking out_dir %s: %s', out_dir, e) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 413 | result['internal_failure'] = str(e) |
| 414 | return result |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 415 | |
| 416 | |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 417 | def run_tha_test( |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 418 | isolated_hash, storage, cache, leak_temp_dir, result_json, root_dir, |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 419 | hard_timeout, grace_period, extra_args): |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 420 | """Downloads the dependencies in the cache, hardlinks them into a temporary |
| 421 | directory and runs the executable from there. |
| 422 | |
| 423 | A temporary directory is created to hold the output files. The content inside |
| 424 | this directory will be uploaded back to |storage| packaged as a .isolated |
| 425 | file. |
| 426 | |
| 427 | Arguments: |
Marc-Antoine Ruel | 35b5843 | 2014-12-08 17:40:40 -0500 | [diff] [blame] | 428 | 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] | 429 | recreate the tree of files to run the target executable. |
| 430 | storage: an isolateserver.Storage object to retrieve remote objects. This |
| 431 | object has a reference to an isolateserver.StorageApi, which does |
| 432 | the actual I/O. |
| 433 | cache: an isolateserver.LocalCache to keep from retrieving the same objects |
| 434 | constantly by caching the objects retrieved. Can be on-disk or |
| 435 | in-memory. |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 436 | leak_temp_dir: if true, the temporary directory will be deliberately leaked |
| 437 | for later examination. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 438 | result_json: file path to dump result metadata into. If set, the process |
| 439 | exit code is always 0 unless an internal error occured. |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 440 | root_dir: directory to the path to use to create the temporary directory. If |
| 441 | not specified, a random temporary directory is created. |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 442 | hard_timeout: kills the process if it lasts more than this amount of |
| 443 | seconds. |
| 444 | grace_period: number of seconds to wait between SIGTERM and SIGKILL. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 445 | extra_args: optional arguments to add to the command stated in the .isolate |
| 446 | file. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 447 | |
| 448 | Returns: |
| 449 | Process exit code that should be used. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 450 | """ |
maruel | a76b9ee | 2015-12-15 06:18:08 -0800 | [diff] [blame] | 451 | if result_json: |
| 452 | # Write a json output file right away in case we get killed. |
| 453 | result = { |
| 454 | 'exit_code': None, |
| 455 | 'had_hard_timeout': False, |
| 456 | 'internal_failure': 'Was terminated before completion', |
| 457 | 'outputs_ref': None, |
| 458 | 'version': 2, |
| 459 | } |
| 460 | tools.write_json(result_json, result, dense=True) |
| 461 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 462 | # run_isolated exit code. Depends on if result_json is used or not. |
| 463 | result = map_and_run( |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 464 | isolated_hash, storage, cache, leak_temp_dir, root_dir, hard_timeout, |
| 465 | grace_period, extra_args) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 466 | logging.info('Result:\n%s', tools.format_json(result, dense=True)) |
| 467 | if result_json: |
maruel | 05d5a88 | 2015-09-21 13:59:02 -0700 | [diff] [blame] | 468 | # We've found tests to delete 'work' when quitting, causing an exception |
| 469 | # here. Try to recreate the directory if necessary. |
| 470 | work_dir = os.path.dirname(result_json) |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 471 | if not fs.isdir(work_dir): |
| 472 | fs.mkdir(work_dir) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 473 | tools.write_json(result_json, result, dense=True) |
| 474 | # Only return 1 if there was an internal error. |
| 475 | return int(bool(result['internal_failure'])) |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 476 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 477 | # Marshall into old-style inline output. |
| 478 | if result['outputs_ref']: |
| 479 | data = { |
| 480 | 'hash': result['outputs_ref']['isolated'], |
| 481 | 'namespace': result['outputs_ref']['namespace'], |
| 482 | 'storage': result['outputs_ref']['isolatedserver'], |
| 483 | } |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 484 | sys.stdout.flush() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 485 | print( |
| 486 | '[run_isolated_out_hack]%s[/run_isolated_out_hack]' % |
| 487 | tools.format_json(data, dense=True)) |
maruel | b76604c | 2015-11-11 11:53:44 -0800 | [diff] [blame] | 488 | sys.stdout.flush() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 489 | return result['exit_code'] or int(bool(result['internal_failure'])) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 490 | |
| 491 | |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 492 | def main(args): |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 493 | parser = logging_utils.OptionParserWithLogging( |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 494 | usage='%prog <options>', |
| 495 | version=__version__, |
| 496 | log_file=RUN_ISOLATED_LOG_FILE) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 497 | parser.add_option( |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame^] | 498 | '--clean', action='store_true', |
| 499 | help='Cleans the cache, trimming it necessary and remove corrupted items ' |
| 500 | 'and returns without executing anything; use with -v to know what ' |
| 501 | 'was done') |
| 502 | parser.add_option( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 503 | '--json', |
| 504 | help='dump output metadata to json file. When used, run_isolated returns ' |
| 505 | 'non-zero only on internal failure') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 506 | parser.add_option( |
maruel | 5c9e47b | 2015-12-18 13:02:30 -0800 | [diff] [blame] | 507 | '--hard-timeout', type='float', help='Enforce hard timeout in execution') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 508 | parser.add_option( |
maruel | 5c9e47b | 2015-12-18 13:02:30 -0800 | [diff] [blame] | 509 | '--grace-period', type='float', |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 510 | help='Grace period between SIGTERM and SIGKILL') |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 511 | data_group = optparse.OptionGroup(parser, 'Data source') |
| 512 | data_group.add_option( |
Marc-Antoine Ruel | 185ded4 | 2015-01-28 20:49:18 -0500 | [diff] [blame] | 513 | '-s', '--isolated', |
| 514 | help='Hash of the .isolated to grab from the isolate server') |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 515 | isolateserver.add_isolate_server_options(data_group) |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 516 | parser.add_option_group(data_group) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 517 | |
Marc-Antoine Ruel | a57d7db | 2014-10-15 20:31:19 -0400 | [diff] [blame] | 518 | isolateserver.add_cache_options(parser) |
| 519 | parser.set_defaults(cache='cache') |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 520 | |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 521 | debug_group = optparse.OptionGroup(parser, 'Debugging') |
| 522 | debug_group.add_option( |
| 523 | '--leak-temp-dir', |
| 524 | action='store_true', |
| 525 | help='Deliberately leak isolate\'s temp dir for later examination ' |
| 526 | '[default: %default]') |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 527 | debug_group.add_option( |
| 528 | '--root-dir', help='Use a directory instead of a random one') |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 529 | parser.add_option_group(debug_group) |
| 530 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 531 | auth.add_auth_options(parser) |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 532 | options, args = parser.parse_args(args) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame^] | 533 | |
| 534 | cache = isolateserver.process_cache_options(options) |
| 535 | if options.clean: |
| 536 | if options.isolated: |
| 537 | parser.error('Can\'t use --isolated with --clean.') |
| 538 | if options.isolate_server: |
| 539 | parser.error('Can\'t use --isolate-server with --clean.') |
| 540 | if options.json: |
| 541 | parser.error('Can\'t use --json with --clean.') |
| 542 | cache.cleanup() |
| 543 | return 0 |
| 544 | |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 545 | auth.process_auth_options(parser, options) |
Marc-Antoine Ruel | e290ada | 2014-12-10 19:48:49 -0500 | [diff] [blame] | 546 | isolateserver.process_isolate_server_options(parser, options, True) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 547 | |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 548 | if options.root_dir: |
| 549 | options.root_dir = unicode(os.path.abspath(options.root_dir)) |
| 550 | if options.json: |
| 551 | options.json = unicode(os.path.abspath(options.json)) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame^] | 552 | if not options.isolated: |
| 553 | parser.error('--isolated is required.') |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 554 | with isolateserver.get_storage( |
| 555 | options.isolate_server, options.namespace) as storage: |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 556 | # Hashing schemes used by |storage| and |cache| MUST match. |
| 557 | assert storage.hash_algo == cache.hash_algo |
| 558 | return run_tha_test( |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 559 | options.isolated, storage, cache, options.leak_temp_dir, options.json, |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 560 | options.root_dir, options.hard_timeout, options.grace_period, args) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 561 | |
| 562 | |
| 563 | if __name__ == '__main__': |
csharp@chromium.org | bfb9874 | 2013-03-26 20:28:36 +0000 | [diff] [blame] | 564 | # Ensure that we are always running with the correct encoding. |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 565 | fix_encoding.fix_encoding() |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 566 | sys.exit(main(sys.argv[1:])) |