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. |
maruel | f1f5e2a | 2016-05-25 17:10:39 -0700 | [diff] [blame] | 3 | # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 | # that can be found in the LICENSE file. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 5 | |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 6 | """Runs a command with optional isolated input/output. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 7 | |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 8 | Despite name "run_isolated", can run a generic non-isolated command specified as |
| 9 | args. |
| 10 | |
| 11 | If input isolated hash is provided, fetches it, creates a tree of hard links, |
| 12 | appends args to the command in the fetched isolated and runs it. |
| 13 | To improve performance, keeps a local cache. |
| 14 | The local cache can safely be deleted. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 15 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 16 | Any ${EXECUTABLE_SUFFIX} on the command line will be replaced with ".exe" string |
| 17 | on Windows and "" on other platforms. |
| 18 | |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 19 | Any ${ISOLATED_OUTDIR} on the command line will be replaced by the location of a |
| 20 | temporary directory upon execution of the command specified in the .isolated |
| 21 | file. All content written to this directory will be uploaded upon termination |
| 22 | and the .isolated file describing this directory will be printed to stdout. |
bpastene | 447c199 | 2016-06-20 15:21:47 -0700 | [diff] [blame] | 23 | |
| 24 | Any ${SWARMING_BOT_FILE} on the command line will be replaced by the value of |
| 25 | the --bot-file parameter. This file is used by a swarming bot to communicate |
| 26 | state of the host to tasks. It is written to by the swarming bot's |
| 27 | on_before_task() hook in the swarming server's custom bot_config.py. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 28 | """ |
| 29 | |
maruel | cffa054 | 2017-04-07 08:39:20 -0700 | [diff] [blame] | 30 | __version__ = '0.9.1' |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 31 | |
aludwin | 7556e0c | 2016-10-26 08:46:10 -0700 | [diff] [blame] | 32 | import argparse |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 33 | import base64 |
iannucci | 96fcccc | 2016-08-30 15:52:22 -0700 | [diff] [blame] | 34 | import collections |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 35 | import contextlib |
aludwin | 7556e0c | 2016-10-26 08:46:10 -0700 | [diff] [blame] | 36 | import json |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 37 | import logging |
| 38 | import optparse |
| 39 | import os |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 40 | import sys |
| 41 | import tempfile |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 42 | import time |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 43 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 44 | from third_party.depot_tools import fix_encoding |
| 45 | |
Vadim Shtayura | 6b555c1 | 2014-07-23 16:22:18 -0700 | [diff] [blame] | 46 | from utils import file_path |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 47 | from utils import fs |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 48 | from utils import large |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 49 | from utils import logging_utils |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 50 | from utils import on_error |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 51 | from utils import subprocess42 |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 52 | from utils import tools |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 53 | from utils import zip_package |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 54 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 55 | import auth |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 56 | import cipd |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 57 | import isolateserver |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 58 | import named_cache |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 59 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 60 | |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 61 | # Absolute path to this file (can be None if running from zip on Mac). |
tansell | a494944 | 2016-06-23 22:34:32 -0700 | [diff] [blame] | 62 | THIS_FILE_PATH = os.path.abspath( |
| 63 | __file__.decode(sys.getfilesystemencoding())) if __file__ else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 64 | |
| 65 | # Directory that contains this file (might be inside zip package). |
tansell | a494944 | 2016-06-23 22:34:32 -0700 | [diff] [blame] | 66 | BASE_DIR = os.path.dirname(THIS_FILE_PATH) if __file__.decode( |
| 67 | sys.getfilesystemencoding()) else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 68 | |
| 69 | # Directory that contains currently running script file. |
maruel@chromium.org | 814d23f | 2013-10-01 19:08:00 +0000 | [diff] [blame] | 70 | if zip_package.get_main_script_path(): |
| 71 | MAIN_DIR = os.path.dirname( |
| 72 | os.path.abspath(zip_package.get_main_script_path())) |
| 73 | else: |
| 74 | # This happens when 'import run_isolated' is executed at the python |
| 75 | # interactive prompt, in that case __file__ is undefined. |
| 76 | MAIN_DIR = None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 77 | |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 78 | |
| 79 | # Magic variables that can be found in the isolate task command line. |
| 80 | ISOLATED_OUTDIR_PARAMETER = '${ISOLATED_OUTDIR}' |
| 81 | EXECUTABLE_SUFFIX_PARAMETER = '${EXECUTABLE_SUFFIX}' |
| 82 | SWARMING_BOT_FILE_PARAMETER = '${SWARMING_BOT_FILE}' |
| 83 | |
| 84 | |
csharp@chromium.org | ff2a466 | 2012-11-21 20:49:32 +0000 | [diff] [blame] | 85 | # The name of the log file to use. |
| 86 | RUN_ISOLATED_LOG_FILE = 'run_isolated.log' |
| 87 | |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 88 | |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 89 | # 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] | 90 | RUN_TEST_CASES_LOG = 'run_test_cases.log' |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 91 | |
vadimsh@chromium.org | 87d6326 | 2013-04-04 19:34:21 +0000 | [diff] [blame] | 92 | |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 93 | # Use short names for temporary directories. This is driven by Windows, which |
| 94 | # imposes a relatively short maximum path length of 260 characters, often |
| 95 | # referred to as MAX_PATH. It is relatively easy to create files with longer |
| 96 | # path length. A use case is with recursive depedency treesV like npm packages. |
| 97 | # |
| 98 | # It is recommended to start the script with a `root_dir` as short as |
| 99 | # possible. |
| 100 | # - ir stands for isolated_run |
| 101 | # - io stands for isolated_out |
| 102 | # - it stands for isolated_tmp |
| 103 | ISOLATED_RUN_DIR = u'ir' |
| 104 | ISOLATED_OUT_DIR = u'io' |
| 105 | ISOLATED_TMP_DIR = u'it' |
| 106 | |
| 107 | |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 108 | def get_as_zip_package(executable=True): |
| 109 | """Returns ZipPackage with this module and all its dependencies. |
| 110 | |
| 111 | If |executable| is True will store run_isolated.py as __main__.py so that |
| 112 | zip package is directly executable be python. |
| 113 | """ |
| 114 | # Building a zip package when running from another zip package is |
| 115 | # unsupported and probably unneeded. |
| 116 | assert not zip_package.is_zipped_module(sys.modules[__name__]) |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 117 | assert THIS_FILE_PATH |
| 118 | assert BASE_DIR |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 119 | package = zip_package.ZipPackage(root=BASE_DIR) |
| 120 | package.add_python_file(THIS_FILE_PATH, '__main__.py' if executable else None) |
aludwin | 8117830 | 2016-11-30 17:18:49 -0800 | [diff] [blame] | 121 | package.add_python_file(os.path.join(BASE_DIR, 'isolate_storage.py')) |
Marc-Antoine Ruel | 8bee66d | 2014-08-28 19:02:07 -0400 | [diff] [blame] | 122 | 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] | 123 | package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py')) |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 124 | package.add_python_file(os.path.join(BASE_DIR, 'auth.py')) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 125 | package.add_python_file(os.path.join(BASE_DIR, 'cipd.py')) |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 126 | package.add_python_file(os.path.join(BASE_DIR, 'named_cache.py')) |
tansell | e4288c3 | 2016-07-28 09:45:40 -0700 | [diff] [blame] | 127 | package.add_directory(os.path.join(BASE_DIR, 'libs')) |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 128 | package.add_directory(os.path.join(BASE_DIR, 'third_party')) |
| 129 | package.add_directory(os.path.join(BASE_DIR, 'utils')) |
| 130 | return package |
| 131 | |
| 132 | |
maruel | 03e1184 | 2016-07-14 10:50:16 -0700 | [diff] [blame] | 133 | def make_temp_dir(prefix, root_dir): |
| 134 | """Returns a new unique temporary directory.""" |
| 135 | return unicode(tempfile.mkdtemp(prefix=prefix, dir=root_dir)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 136 | |
| 137 | |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 138 | def change_tree_read_only(rootdir, read_only): |
| 139 | """Changes the tree read-only bits according to the read_only specification. |
| 140 | |
| 141 | The flag can be 0, 1 or 2, which will affect the possibility to modify files |
| 142 | and create or delete files. |
| 143 | """ |
| 144 | if read_only == 2: |
| 145 | # Files and directories (except on Windows) are marked read only. This |
| 146 | # inhibits modifying, creating or deleting files in the test directory, |
| 147 | # except on Windows where creating and deleting files is still possible. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 148 | file_path.make_tree_read_only(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 149 | elif read_only == 1: |
| 150 | # Files are marked read only but not the directories. This inhibits |
| 151 | # modifying files but creating or deleting files is still possible. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 152 | file_path.make_tree_files_read_only(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 153 | elif read_only in (0, None): |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 154 | # Anything can be modified. |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 155 | # TODO(maruel): This is currently dangerous as long as DiskCache.touch() |
| 156 | # is not yet changed to verify the hash of the content of the files it is |
| 157 | # looking at, so that if a test modifies an input file, the file must be |
| 158 | # deleted. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 159 | file_path.make_tree_writeable(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 160 | else: |
| 161 | raise ValueError( |
| 162 | 'change_tree_read_only(%s, %s): Unknown flag %s' % |
| 163 | (rootdir, read_only, read_only)) |
| 164 | |
| 165 | |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 166 | def process_command(command, out_dir, bot_file): |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 167 | """Replaces variables in a command line. |
| 168 | |
| 169 | Raises: |
| 170 | ValueError if a parameter is requested in |command| but its value is not |
| 171 | provided. |
| 172 | """ |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 173 | def fix(arg): |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 174 | arg = arg.replace(EXECUTABLE_SUFFIX_PARAMETER, cipd.EXECUTABLE_SUFFIX) |
| 175 | replace_slash = False |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 176 | if ISOLATED_OUTDIR_PARAMETER in arg: |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 177 | if not out_dir: |
maruel | 7f63a27 | 2016-07-12 12:40:36 -0700 | [diff] [blame] | 178 | raise ValueError( |
| 179 | 'output directory is requested in command, but not provided; ' |
| 180 | 'please specify one') |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 181 | arg = arg.replace(ISOLATED_OUTDIR_PARAMETER, out_dir) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 182 | replace_slash = True |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 183 | if SWARMING_BOT_FILE_PARAMETER in arg: |
| 184 | if bot_file: |
| 185 | arg = arg.replace(SWARMING_BOT_FILE_PARAMETER, bot_file) |
| 186 | replace_slash = True |
| 187 | else: |
| 188 | logging.warning('SWARMING_BOT_FILE_PARAMETER found in command, but no ' |
| 189 | 'bot_file specified. Leaving parameter unchanged.') |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 190 | if replace_slash: |
| 191 | # Replace slashes only if parameters are present |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 192 | # because of arguments like '${ISOLATED_OUTDIR}/foo/bar' |
| 193 | arg = arg.replace('/', os.sep) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 194 | return arg |
| 195 | |
| 196 | return [fix(arg) for arg in command] |
| 197 | |
| 198 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 199 | def get_command_env(tmp_dir, cipd_info): |
| 200 | """Returns full OS environment to run a command in. |
| 201 | |
| 202 | Sets up TEMP, puts directory with cipd binary in front of PATH, and exposes |
| 203 | CIPD_CACHE_DIR env var. |
| 204 | |
| 205 | Args: |
| 206 | tmp_dir: temp directory. |
| 207 | cipd_info: CipdInfo object is cipd client is used, None if not. |
| 208 | """ |
| 209 | def to_fs_enc(s): |
| 210 | if isinstance(s, str): |
| 211 | return s |
| 212 | return s.encode(sys.getfilesystemencoding()) |
| 213 | |
| 214 | env = os.environ.copy() |
| 215 | |
iannucci | ac0342c | 2017-02-24 05:47:01 -0800 | [diff] [blame] | 216 | # TMPDIR is specified as the POSIX standard envvar for the temp directory. |
iannucci | 460def7 | 2017-02-24 10:49:48 -0800 | [diff] [blame] | 217 | # * mktemp on linux respects $TMPDIR, not $TMP |
| 218 | # * mktemp on OS X SOMETIMES respects $TMPDIR |
iannucci | ac0342c | 2017-02-24 05:47:01 -0800 | [diff] [blame] | 219 | # * chromium's base utils respects $TMPDIR on linux, $TEMP on windows. |
| 220 | # Unfortunately at the time of writing it completely ignores all envvars |
| 221 | # on OS X. |
iannucci | 460def7 | 2017-02-24 10:49:48 -0800 | [diff] [blame] | 222 | # * python respects TMPDIR, TEMP, and TMP (regardless of platform) |
| 223 | # * golang respects TMPDIR on linux+mac, TEMP on windows. |
iannucci | ac0342c | 2017-02-24 05:47:01 -0800 | [diff] [blame] | 224 | key = {'win32': 'TEMP'}.get(sys.platform, 'TMPDIR') |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 225 | env[key] = to_fs_enc(tmp_dir) |
| 226 | |
| 227 | if cipd_info: |
| 228 | bin_dir = os.path.dirname(cipd_info.client.binary_path) |
| 229 | env['PATH'] = '%s%s%s' % (to_fs_enc(bin_dir), os.pathsep, env['PATH']) |
| 230 | env['CIPD_CACHE_DIR'] = to_fs_enc(cipd_info.cache_dir) |
| 231 | |
| 232 | return env |
| 233 | |
| 234 | |
| 235 | def run_command(command, cwd, env, hard_timeout, grace_period): |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 236 | """Runs the command. |
| 237 | |
| 238 | Returns: |
| 239 | tuple(process exit code, bool if had a hard timeout) |
| 240 | """ |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 241 | logging.info('run_command(%s, %s)' % (command, cwd)) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 242 | |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 243 | exit_code = None |
| 244 | had_hard_timeout = False |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 245 | with tools.Profiler('RunTest'): |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 246 | proc = None |
| 247 | had_signal = [] |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 248 | try: |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 249 | # TODO(maruel): This code is imperfect. It doesn't handle well signals |
| 250 | # during the download phase and there's short windows were things can go |
| 251 | # wrong. |
| 252 | def handler(signum, _frame): |
| 253 | if proc and not had_signal: |
| 254 | logging.info('Received signal %d', signum) |
| 255 | had_signal.append(True) |
maruel | 556d905 | 2015-10-05 11:12:44 -0700 | [diff] [blame] | 256 | raise subprocess42.TimeoutExpired(command, None) |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 257 | |
| 258 | proc = subprocess42.Popen(command, cwd=cwd, env=env, detached=True) |
| 259 | with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, handler): |
| 260 | try: |
| 261 | exit_code = proc.wait(hard_timeout or None) |
| 262 | except subprocess42.TimeoutExpired: |
| 263 | if not had_signal: |
| 264 | logging.warning('Hard timeout') |
| 265 | had_hard_timeout = True |
| 266 | logging.warning('Sending SIGTERM') |
| 267 | proc.terminate() |
| 268 | |
| 269 | # Ignore signals in grace period. Forcibly give the grace period to the |
| 270 | # child process. |
| 271 | if exit_code is None: |
| 272 | ignore = lambda *_: None |
| 273 | with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, ignore): |
| 274 | try: |
| 275 | exit_code = proc.wait(grace_period or None) |
| 276 | except subprocess42.TimeoutExpired: |
| 277 | # Now kill for real. The user can distinguish between the |
| 278 | # following states: |
| 279 | # - signal but process exited within grace period, |
| 280 | # hard_timed_out will be set but the process exit code will be |
| 281 | # script provided. |
| 282 | # - processed exited late, exit code will be -9 on posix. |
| 283 | logging.warning('Grace exhausted; sending SIGKILL') |
| 284 | proc.kill() |
| 285 | logging.info('Waiting for proces exit') |
| 286 | exit_code = proc.wait() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 287 | except OSError: |
| 288 | # This is not considered to be an internal error. The executable simply |
| 289 | # does not exit. |
maruel | a72f46e | 2016-02-24 11:05:45 -0800 | [diff] [blame] | 290 | sys.stderr.write( |
| 291 | '<The executable does not exist or a dependent library is missing>\n' |
| 292 | '<Check for missing .so/.dll in the .isolate or GN file>\n' |
| 293 | '<Command: %s>\n' % command) |
| 294 | if os.environ.get('SWARMING_TASK_ID'): |
| 295 | # Give an additional hint when running as a swarming task. |
| 296 | sys.stderr.write( |
| 297 | '<See the task\'s page for commands to help diagnose this issue ' |
| 298 | 'by reproducing the task locally>\n') |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 299 | exit_code = 1 |
| 300 | logging.info( |
| 301 | 'Command finished with exit code %d (%s)', |
| 302 | exit_code, hex(0xffffffff & exit_code)) |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 303 | return exit_code, had_hard_timeout |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 304 | |
| 305 | |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 306 | def fetch_and_map(isolated_hash, storage, cache, outdir, use_symlinks): |
| 307 | """Fetches an isolated tree, create the tree and returns (bundle, stats).""" |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 308 | start = time.time() |
| 309 | bundle = isolateserver.fetch_isolated( |
| 310 | isolated_hash=isolated_hash, |
| 311 | storage=storage, |
| 312 | cache=cache, |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 313 | outdir=outdir, |
| 314 | use_symlinks=use_symlinks) |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 315 | return bundle, { |
| 316 | 'duration': time.time() - start, |
| 317 | 'initial_number_items': cache.initial_number_items, |
| 318 | 'initial_size': cache.initial_size, |
| 319 | 'items_cold': base64.b64encode(large.pack(sorted(cache.added))), |
| 320 | 'items_hot': base64.b64encode( |
tansell | 9e04a8d | 2016-07-28 09:31:59 -0700 | [diff] [blame] | 321 | large.pack(sorted(set(cache.used) - set(cache.added)))), |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
aludwin | 0a8e17d | 2016-10-27 15:57:39 -0700 | [diff] [blame] | 325 | def link_outputs_to_outdir(run_dir, out_dir, outputs): |
| 326 | """Links any named outputs to out_dir so they can be uploaded. |
| 327 | |
| 328 | Raises an error if the file already exists in that directory. |
| 329 | """ |
| 330 | if not outputs: |
| 331 | return |
| 332 | isolateserver.create_directories(out_dir, outputs) |
| 333 | for o in outputs: |
| 334 | try: |
| 335 | file_path.link_file( |
| 336 | os.path.join(out_dir, o), |
| 337 | os.path.join(run_dir, o), |
| 338 | file_path.HARDLINK_WITH_FALLBACK) |
| 339 | except OSError as e: |
aludwin | 8117830 | 2016-11-30 17:18:49 -0800 | [diff] [blame] | 340 | logging.info("Couldn't collect output file %s: %s", o, e) |
aludwin | 0a8e17d | 2016-10-27 15:57:39 -0700 | [diff] [blame] | 341 | |
| 342 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 343 | def delete_and_upload(storage, out_dir, leak_temp_dir): |
| 344 | """Deletes the temporary run directory and uploads results back. |
| 345 | |
| 346 | Returns: |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 347 | tuple(outputs_ref, success, stats) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 348 | - outputs_ref: a dict referring to the results archived back to the isolated |
| 349 | server, if applicable. |
| 350 | - success: False if something occurred that means that the task must |
| 351 | forcibly be considered a failure, e.g. zombie processes were left |
| 352 | behind. |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 353 | - stats: uploading stats. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 354 | """ |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 355 | # Upload out_dir and generate a .isolated file out of this directory. It is |
| 356 | # only done if files were written in the directory. |
| 357 | outputs_ref = None |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 358 | cold = [] |
| 359 | hot = [] |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 360 | start = time.time() |
| 361 | |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 362 | if fs.isdir(out_dir) and fs.listdir(out_dir): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 363 | with tools.Profiler('ArchiveOutput'): |
| 364 | try: |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 365 | results, f_cold, f_hot = isolateserver.archive_files_to_storage( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 366 | storage, [out_dir], None) |
| 367 | outputs_ref = { |
| 368 | 'isolated': results[0][0], |
| 369 | 'isolatedserver': storage.location, |
| 370 | 'namespace': storage.namespace, |
| 371 | } |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 372 | cold = sorted(i.size for i in f_cold) |
| 373 | hot = sorted(i.size for i in f_hot) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 374 | except isolateserver.Aborted: |
| 375 | # This happens when a signal SIGTERM was received while uploading data. |
| 376 | # There is 2 causes: |
| 377 | # - The task was too slow and was about to be killed anyway due to |
| 378 | # exceeding the hard timeout. |
| 379 | # - The amount of data uploaded back is very large and took too much |
| 380 | # time to archive. |
| 381 | sys.stderr.write('Received SIGTERM while uploading') |
| 382 | # Re-raise, so it will be treated as an internal failure. |
| 383 | raise |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 384 | |
| 385 | success = False |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 386 | try: |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 387 | if (not leak_temp_dir and fs.isdir(out_dir) and |
maruel | 6eeea7d | 2015-09-16 12:17:42 -0700 | [diff] [blame] | 388 | not file_path.rmtree(out_dir)): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 389 | logging.error('Had difficulties removing out_dir %s', out_dir) |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 390 | else: |
| 391 | success = True |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 392 | except OSError as e: |
| 393 | # When this happens, it means there's a process error. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 394 | logging.exception('Had difficulties removing out_dir %s: %s', out_dir, e) |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 395 | stats = { |
| 396 | 'duration': time.time() - start, |
| 397 | 'items_cold': base64.b64encode(large.pack(cold)), |
| 398 | 'items_hot': base64.b64encode(large.pack(hot)), |
| 399 | } |
| 400 | return outputs_ref, success, stats |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 401 | |
| 402 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 403 | def map_and_run( |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 404 | command, isolated_hash, storage, isolate_cache, outputs, |
| 405 | install_named_caches, leak_temp_dir, root_dir, hard_timeout, grace_period, |
| 406 | bot_file, install_packages_fn, use_symlinks, constant_run_path): |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 407 | """Runs a command with optional isolated input/output. |
| 408 | |
| 409 | See run_tha_test for argument documentation. |
| 410 | |
| 411 | Returns metadata about the result. |
| 412 | """ |
maruel | abec63c | 2017-04-26 11:53:24 -0700 | [diff] [blame] | 413 | assert isinstance(command, list), command |
nodir | 56efa45 | 2016-10-12 12:17:39 -0700 | [diff] [blame] | 414 | assert root_dir or root_dir is None |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 415 | result = { |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 416 | 'duration': None, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 417 | 'exit_code': None, |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 418 | 'had_hard_timeout': False, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 419 | 'internal_failure': None, |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 420 | 'stats': { |
nodir | 5571571 | 2016-06-03 12:28:19 -0700 | [diff] [blame] | 421 | # 'isolated': { |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 422 | # 'cipd': { |
| 423 | # 'duration': 0., |
| 424 | # 'get_client_duration': 0., |
| 425 | # }, |
nodir | 5571571 | 2016-06-03 12:28:19 -0700 | [diff] [blame] | 426 | # 'download': { |
| 427 | # 'duration': 0., |
| 428 | # 'initial_number_items': 0, |
| 429 | # 'initial_size': 0, |
| 430 | # 'items_cold': '<large.pack()>', |
| 431 | # 'items_hot': '<large.pack()>', |
| 432 | # }, |
| 433 | # 'upload': { |
| 434 | # 'duration': 0., |
| 435 | # 'items_cold': '<large.pack()>', |
| 436 | # 'items_hot': '<large.pack()>', |
| 437 | # }, |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 438 | # }, |
| 439 | }, |
iannucci | 96fcccc | 2016-08-30 15:52:22 -0700 | [diff] [blame] | 440 | # 'cipd_pins': { |
| 441 | # 'packages': [ |
| 442 | # {'package_name': ..., 'version': ..., 'path': ...}, |
| 443 | # ... |
| 444 | # ], |
| 445 | # 'client_package': {'package_name': ..., 'version': ...}, |
| 446 | # }, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 447 | 'outputs_ref': None, |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 448 | 'version': 5, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 449 | } |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 450 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 451 | if root_dir: |
nodir | e5028a9 | 2016-04-29 14:38:21 -0700 | [diff] [blame] | 452 | file_path.ensure_tree(root_dir, 0700) |
nodir | 56efa45 | 2016-10-12 12:17:39 -0700 | [diff] [blame] | 453 | elif isolate_cache.cache_dir: |
| 454 | root_dir = os.path.dirname(isolate_cache.cache_dir) |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 455 | # See comment for these constants. |
maruel | cffa054 | 2017-04-07 08:39:20 -0700 | [diff] [blame] | 456 | # If root_dir is not specified, it is not constant. |
| 457 | # TODO(maruel): This is not obvious. Change this to become an error once we |
| 458 | # make the constant_run_path an exposed flag. |
| 459 | if constant_run_path and root_dir: |
| 460 | run_dir = os.path.join(root_dir, ISOLATED_RUN_DIR) |
maruel | 5c4eed8 | 2017-05-26 05:33:40 -0700 | [diff] [blame^] | 461 | if os.path.isdir(run_dir): |
| 462 | file_path.rmtree(run_dir) |
maruel | cffa054 | 2017-04-07 08:39:20 -0700 | [diff] [blame] | 463 | os.mkdir(run_dir) |
| 464 | else: |
| 465 | run_dir = make_temp_dir(ISOLATED_RUN_DIR, root_dir) |
maruel | 03e1184 | 2016-07-14 10:50:16 -0700 | [diff] [blame] | 466 | # storage should be normally set but don't crash if it is not. This can happen |
| 467 | # as Swarming task can run without an isolate server. |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 468 | out_dir = make_temp_dir(ISOLATED_OUT_DIR, root_dir) if storage else None |
| 469 | tmp_dir = make_temp_dir(ISOLATED_TMP_DIR, root_dir) |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 470 | cwd = run_dir |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 471 | |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 472 | try: |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 473 | with install_packages_fn(run_dir) as cipd_info: |
| 474 | if cipd_info: |
| 475 | result['stats']['cipd'] = cipd_info.stats |
| 476 | result['cipd_pins'] = cipd_info.pins |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 477 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 478 | if isolated_hash: |
| 479 | isolated_stats = result['stats'].setdefault('isolated', {}) |
| 480 | bundle, isolated_stats['download'] = fetch_and_map( |
| 481 | isolated_hash=isolated_hash, |
| 482 | storage=storage, |
| 483 | cache=isolate_cache, |
| 484 | outdir=run_dir, |
| 485 | use_symlinks=use_symlinks) |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 486 | change_tree_read_only(run_dir, bundle.read_only) |
| 487 | cwd = os.path.normpath(os.path.join(cwd, bundle.relative_cwd)) |
maruel | abec63c | 2017-04-26 11:53:24 -0700 | [diff] [blame] | 488 | # Inject the command |
| 489 | if bundle.command: |
| 490 | command = bundle.command + command |
| 491 | |
| 492 | if not command: |
| 493 | # Handle this as a task failure, not an internal failure. |
| 494 | sys.stderr.write( |
| 495 | '<No command was specified!>\n' |
| 496 | '<Please secify a command when triggering your Swarming task>\n') |
| 497 | result['exit_code'] = 1 |
| 498 | return result |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 499 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 500 | # If we have an explicit list of files to return, make sure their |
| 501 | # directories exist now. |
| 502 | if storage and outputs: |
| 503 | isolateserver.create_directories(run_dir, outputs) |
aludwin | 0a8e17d | 2016-10-27 15:57:39 -0700 | [diff] [blame] | 504 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 505 | command = tools.fix_python_path(command) |
| 506 | command = process_command(command, out_dir, bot_file) |
| 507 | file_path.ensure_command_has_abs_path(command, cwd) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 508 | |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 509 | with install_named_caches(run_dir): |
nodir | d616068 | 2017-02-02 13:03:35 -0800 | [diff] [blame] | 510 | sys.stdout.flush() |
| 511 | start = time.time() |
| 512 | try: |
| 513 | result['exit_code'], result['had_hard_timeout'] = run_command( |
| 514 | command, cwd, get_command_env(tmp_dir, cipd_info), |
| 515 | hard_timeout, grace_period) |
| 516 | finally: |
| 517 | result['duration'] = max(time.time() - start, 0) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 518 | except Exception as e: |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 519 | # An internal error occurred. Report accordingly so the swarming task will |
| 520 | # be retried automatically. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 521 | logging.exception('internal failure: %s', e) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 522 | result['internal_failure'] = str(e) |
| 523 | on_error.report(None) |
aludwin | 0a8e17d | 2016-10-27 15:57:39 -0700 | [diff] [blame] | 524 | |
| 525 | # Clean up |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 526 | finally: |
| 527 | try: |
aludwin | 0a8e17d | 2016-10-27 15:57:39 -0700 | [diff] [blame] | 528 | # Try to link files to the output directory, if specified. |
| 529 | if out_dir: |
| 530 | link_outputs_to_outdir(run_dir, out_dir, outputs) |
| 531 | |
nodir | 32a1ec1 | 2016-10-26 18:34:07 -0700 | [diff] [blame] | 532 | success = False |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 533 | if leak_temp_dir: |
nodir | 32a1ec1 | 2016-10-26 18:34:07 -0700 | [diff] [blame] | 534 | success = True |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 535 | logging.warning( |
| 536 | 'Deliberately leaking %s for later examination', run_dir) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 537 | else: |
maruel | 84537cb | 2015-10-16 14:21:28 -0700 | [diff] [blame] | 538 | # On Windows rmtree(run_dir) call above has a synchronization effect: it |
| 539 | # finishes only when all task child processes terminate (since a running |
| 540 | # process locks *.exe file). Examine out_dir only after that call |
| 541 | # completes (since child processes may write to out_dir too and we need |
| 542 | # to wait for them to finish). |
| 543 | if fs.isdir(run_dir): |
| 544 | try: |
| 545 | success = file_path.rmtree(run_dir) |
| 546 | except OSError as e: |
| 547 | logging.error('Failure with %s', e) |
| 548 | success = False |
| 549 | if not success: |
| 550 | print >> sys.stderr, ( |
qyearsley | bf6fb2b | 2017-05-09 16:17:26 -0700 | [diff] [blame] | 551 | 'Failed to delete the run directory, thus failing the task.\n' |
| 552 | 'This may be due to a subprocess outliving the main task\n' |
| 553 | 'process, holding on to resources. Please fix the task so\n' |
| 554 | 'that it releases resources and cleans up subprocesses.') |
maruel | 84537cb | 2015-10-16 14:21:28 -0700 | [diff] [blame] | 555 | if result['exit_code'] == 0: |
| 556 | result['exit_code'] = 1 |
| 557 | if fs.isdir(tmp_dir): |
| 558 | try: |
| 559 | success = file_path.rmtree(tmp_dir) |
| 560 | except OSError as e: |
| 561 | logging.error('Failure with %s', e) |
| 562 | success = False |
| 563 | if not success: |
| 564 | print >> sys.stderr, ( |
qyearsley | bf6fb2b | 2017-05-09 16:17:26 -0700 | [diff] [blame] | 565 | 'Failed to delete the temp directory, thus failing the task.\n' |
| 566 | 'This may be due to a subprocess outliving the main task\n' |
| 567 | 'process, holding on to resources. Please fix the task so\n' |
| 568 | 'that it releases resources and cleans up subprocesses.') |
maruel | 84537cb | 2015-10-16 14:21:28 -0700 | [diff] [blame] | 569 | if result['exit_code'] == 0: |
| 570 | result['exit_code'] = 1 |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 571 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 572 | # This deletes out_dir if leak_temp_dir is not set. |
nodir | 9130f07 | 2016-05-27 13:59:08 -0700 | [diff] [blame] | 573 | if out_dir: |
nodir | 5571571 | 2016-06-03 12:28:19 -0700 | [diff] [blame] | 574 | isolated_stats = result['stats'].setdefault('isolated', {}) |
| 575 | result['outputs_ref'], success, isolated_stats['upload'] = ( |
nodir | 9130f07 | 2016-05-27 13:59:08 -0700 | [diff] [blame] | 576 | delete_and_upload(storage, out_dir, leak_temp_dir)) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 577 | if not success and result['exit_code'] == 0: |
| 578 | result['exit_code'] = 1 |
| 579 | except Exception as e: |
| 580 | # Swallow any exception in the main finally clause. |
nodir | 9130f07 | 2016-05-27 13:59:08 -0700 | [diff] [blame] | 581 | if out_dir: |
| 582 | logging.exception('Leaking out_dir %s: %s', out_dir, e) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 583 | result['internal_failure'] = str(e) |
| 584 | return result |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 585 | |
| 586 | |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 587 | def run_tha_test( |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 588 | command, isolated_hash, storage, isolate_cache, outputs, |
| 589 | install_named_caches, leak_temp_dir, result_json, root_dir, hard_timeout, |
| 590 | grace_period, bot_file, install_packages_fn, use_symlinks): |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 591 | """Runs an executable and records execution metadata. |
| 592 | |
| 593 | Either command or isolated_hash must be specified. |
| 594 | |
| 595 | If isolated_hash is specified, downloads the dependencies in the cache, |
| 596 | hardlinks them into a temporary directory and runs the command specified in |
| 597 | the .isolated. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 598 | |
| 599 | A temporary directory is created to hold the output files. The content inside |
| 600 | this directory will be uploaded back to |storage| packaged as a .isolated |
| 601 | file. |
| 602 | |
| 603 | Arguments: |
maruel | abec63c | 2017-04-26 11:53:24 -0700 | [diff] [blame] | 604 | command: a list of string; the command to run OR optional arguments to add |
| 605 | to the command stated in the .isolated file if a command was |
| 606 | specified. |
Marc-Antoine Ruel | 35b5843 | 2014-12-08 17:40:40 -0500 | [diff] [blame] | 607 | 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] | 608 | recreate the tree of files to run the target executable. |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 609 | The command specified in the .isolated is executed. |
| 610 | Mutually exclusive with command argument. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 611 | storage: an isolateserver.Storage object to retrieve remote objects. This |
| 612 | object has a reference to an isolateserver.StorageApi, which does |
| 613 | the actual I/O. |
nodir | 6b94569 | 2016-10-19 19:09:06 -0700 | [diff] [blame] | 614 | isolate_cache: an isolateserver.LocalCache to keep from retrieving the |
| 615 | same objects constantly by caching the objects retrieved. |
| 616 | Can be on-disk or in-memory. |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 617 | install_named_caches: a function (run_dir) => context manager that installs |
| 618 | named caches into |run_dir|. |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 619 | leak_temp_dir: if true, the temporary directory will be deliberately leaked |
| 620 | for later examination. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 621 | result_json: file path to dump result metadata into. If set, the process |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 622 | exit code is always 0 unless an internal error occurred. |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 623 | root_dir: path to the directory to use to create the temporary directory. If |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 624 | not specified, a random temporary directory is created. |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 625 | hard_timeout: kills the process if it lasts more than this amount of |
| 626 | seconds. |
| 627 | grace_period: number of seconds to wait between SIGTERM and SIGKILL. |
iannucci | b58d10d | 2017-03-18 02:00:25 -0700 | [diff] [blame] | 628 | install_packages_fn: context manager dir => CipdInfo, see |
| 629 | install_client_and_packages. |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 630 | use_symlinks: create tree with symlinks instead of hardlinks. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 631 | |
| 632 | Returns: |
| 633 | Process exit code that should be used. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 634 | """ |
maruel | a76b9ee | 2015-12-15 06:18:08 -0800 | [diff] [blame] | 635 | if result_json: |
| 636 | # Write a json output file right away in case we get killed. |
| 637 | result = { |
| 638 | 'exit_code': None, |
| 639 | 'had_hard_timeout': False, |
| 640 | 'internal_failure': 'Was terminated before completion', |
| 641 | 'outputs_ref': None, |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 642 | 'version': 5, |
maruel | a76b9ee | 2015-12-15 06:18:08 -0800 | [diff] [blame] | 643 | } |
| 644 | tools.write_json(result_json, result, dense=True) |
| 645 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 646 | # run_isolated exit code. Depends on if result_json is used or not. |
| 647 | result = map_and_run( |
nodir | 220308c | 2017-02-01 19:32:53 -0800 | [diff] [blame] | 648 | command, isolated_hash, storage, isolate_cache, outputs, |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 649 | install_named_caches, leak_temp_dir, root_dir, hard_timeout, grace_period, |
maruel | abec63c | 2017-04-26 11:53:24 -0700 | [diff] [blame] | 650 | bot_file, install_packages_fn, use_symlinks, True) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 651 | logging.info('Result:\n%s', tools.format_json(result, dense=True)) |
bpastene | 3ae0952 | 2016-06-10 17:12:59 -0700 | [diff] [blame] | 652 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 653 | if result_json: |
maruel | 05d5a88 | 2015-09-21 13:59:02 -0700 | [diff] [blame] | 654 | # We've found tests to delete 'work' when quitting, causing an exception |
| 655 | # here. Try to recreate the directory if necessary. |
nodir | e5028a9 | 2016-04-29 14:38:21 -0700 | [diff] [blame] | 656 | file_path.ensure_tree(os.path.dirname(result_json)) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 657 | tools.write_json(result_json, result, dense=True) |
| 658 | # Only return 1 if there was an internal error. |
| 659 | return int(bool(result['internal_failure'])) |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 660 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 661 | # Marshall into old-style inline output. |
| 662 | if result['outputs_ref']: |
| 663 | data = { |
| 664 | 'hash': result['outputs_ref']['isolated'], |
| 665 | 'namespace': result['outputs_ref']['namespace'], |
| 666 | 'storage': result['outputs_ref']['isolatedserver'], |
| 667 | } |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 668 | sys.stdout.flush() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 669 | print( |
| 670 | '[run_isolated_out_hack]%s[/run_isolated_out_hack]' % |
| 671 | tools.format_json(data, dense=True)) |
maruel | b76604c | 2015-11-11 11:53:44 -0800 | [diff] [blame] | 672 | sys.stdout.flush() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 673 | return result['exit_code'] or int(bool(result['internal_failure'])) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 674 | |
| 675 | |
iannucci | b58d10d | 2017-03-18 02:00:25 -0700 | [diff] [blame] | 676 | # Yielded by 'install_client_and_packages'. |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 677 | CipdInfo = collections.namedtuple('CipdInfo', [ |
| 678 | 'client', # cipd.CipdClient object |
| 679 | 'cache_dir', # absolute path to bot-global cipd tag and instance cache |
| 680 | 'stats', # dict with stats to return to the server |
| 681 | 'pins', # dict with installed cipd pins to return to the server |
| 682 | ]) |
| 683 | |
| 684 | |
| 685 | @contextlib.contextmanager |
| 686 | def noop_install_packages(_run_dir): |
iannucci | b58d10d | 2017-03-18 02:00:25 -0700 | [diff] [blame] | 687 | """Placeholder for 'install_client_and_packages' if cipd is disabled.""" |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 688 | yield None |
| 689 | |
| 690 | |
iannucci | b58d10d | 2017-03-18 02:00:25 -0700 | [diff] [blame] | 691 | def _install_packages(run_dir, cipd_cache_dir, client, packages, timeout): |
| 692 | """Calls 'cipd ensure' for packages. |
| 693 | |
| 694 | Args: |
| 695 | run_dir (str): root of installation. |
| 696 | cipd_cache_dir (str): the directory to use for the cipd package cache. |
| 697 | client (CipdClient): the cipd client to use |
| 698 | packages: packages to install, list [(path, package_name, version), ...]. |
| 699 | timeout: max duration in seconds that this function can take. |
| 700 | |
| 701 | Returns: list of pinned packages. Looks like [ |
| 702 | { |
| 703 | 'path': 'subdirectory', |
| 704 | 'package_name': 'resolved/package/name', |
| 705 | 'version': 'deadbeef...', |
| 706 | }, |
| 707 | ... |
| 708 | ] |
| 709 | """ |
| 710 | package_pins = [None]*len(packages) |
| 711 | def insert_pin(path, name, version, idx): |
| 712 | package_pins[idx] = { |
| 713 | 'package_name': name, |
| 714 | # swarming deals with 'root' as '.' |
| 715 | 'path': path or '.', |
| 716 | 'version': version, |
| 717 | } |
| 718 | |
| 719 | by_path = collections.defaultdict(list) |
| 720 | for i, (path, name, version) in enumerate(packages): |
| 721 | # cipd deals with 'root' as '' |
| 722 | if path == '.': |
| 723 | path = '' |
| 724 | by_path[path].append((name, version, i)) |
| 725 | |
| 726 | pins = client.ensure( |
| 727 | run_dir, |
| 728 | { |
| 729 | subdir: [(name, vers) for name, vers, _ in pkgs] |
| 730 | for subdir, pkgs in by_path.iteritems() |
| 731 | }, |
| 732 | cache_dir=cipd_cache_dir, |
| 733 | timeout=timeout, |
| 734 | ) |
| 735 | |
| 736 | for subdir, pin_list in sorted(pins.iteritems()): |
| 737 | this_subdir = by_path[subdir] |
| 738 | for i, (name, version) in enumerate(pin_list): |
| 739 | insert_pin(subdir, name, version, this_subdir[i][2]) |
| 740 | |
| 741 | assert None not in package_pins |
| 742 | |
| 743 | return package_pins |
| 744 | |
| 745 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 746 | @contextlib.contextmanager |
iannucci | b58d10d | 2017-03-18 02:00:25 -0700 | [diff] [blame] | 747 | def install_client_and_packages( |
nodir | ff531b4 | 2016-06-23 13:05:06 -0700 | [diff] [blame] | 748 | run_dir, packages, service_url, client_package_name, |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 749 | client_version, cache_dir, timeout=None): |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 750 | """Bootstraps CIPD client and installs CIPD packages. |
iannucci | 96fcccc | 2016-08-30 15:52:22 -0700 | [diff] [blame] | 751 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 752 | Yields CipdClient, stats, client info and pins (as single CipdInfo object). |
| 753 | |
| 754 | Pins and the CIPD client info are in the form of: |
iannucci | 96fcccc | 2016-08-30 15:52:22 -0700 | [diff] [blame] | 755 | [ |
| 756 | { |
| 757 | "path": path, "package_name": package_name, "version": version, |
| 758 | }, |
| 759 | ... |
| 760 | ] |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 761 | (the CIPD client info is a single dictionary instead of a list) |
iannucci | 96fcccc | 2016-08-30 15:52:22 -0700 | [diff] [blame] | 762 | |
| 763 | such that they correspond 1:1 to all input package arguments from the command |
| 764 | line. These dictionaries make their all the way back to swarming, where they |
| 765 | become the arguments of CipdPackage. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 766 | |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 767 | If 'packages' list is empty, will bootstrap CIPD client, but won't install |
| 768 | any packages. |
| 769 | |
| 770 | The bootstrapped client (regardless whether 'packages' list is empty or not), |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 771 | will be made available to the task via $PATH. |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 772 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 773 | Args: |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 774 | run_dir (str): root of installation. |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 775 | packages: packages to install, list [(path, package_name, version), ...]. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 776 | service_url (str): CIPD server url, e.g. |
| 777 | "https://chrome-infra-packages.appspot.com." |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 778 | client_package_name (str): CIPD package name of CIPD client. |
| 779 | client_version (str): Version of CIPD client. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 780 | cache_dir (str): where to keep cache of cipd clients, packages and tags. |
| 781 | timeout: max duration in seconds that this function can take. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 782 | """ |
| 783 | assert cache_dir |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 784 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 785 | timeoutfn = tools.sliding_timeout(timeout) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 786 | start = time.time() |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 787 | |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 788 | cache_dir = os.path.abspath(cache_dir) |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 789 | cipd_cache_dir = os.path.join(cache_dir, 'cache') # tag and instance caches |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 790 | run_dir = os.path.abspath(run_dir) |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 791 | packages = packages or [] |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 792 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 793 | get_client_start = time.time() |
| 794 | client_manager = cipd.get_client( |
| 795 | service_url, client_package_name, client_version, cache_dir, |
| 796 | timeout=timeoutfn()) |
iannucci | 96fcccc | 2016-08-30 15:52:22 -0700 | [diff] [blame] | 797 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 798 | with client_manager as client: |
| 799 | get_client_duration = time.time() - get_client_start |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 800 | |
iannucci | b58d10d | 2017-03-18 02:00:25 -0700 | [diff] [blame] | 801 | package_pins = [] |
| 802 | if packages: |
| 803 | package_pins = _install_packages( |
| 804 | run_dir, cipd_cache_dir, client, packages, timeoutfn()) |
| 805 | |
| 806 | file_path.make_tree_files_read_only(run_dir) |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 807 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 808 | total_duration = time.time() - start |
| 809 | logging.info( |
| 810 | 'Installing CIPD client and packages took %d seconds', total_duration) |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 811 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 812 | yield CipdInfo( |
| 813 | client=client, |
| 814 | cache_dir=cipd_cache_dir, |
| 815 | stats={ |
| 816 | 'duration': total_duration, |
| 817 | 'get_client_duration': get_client_duration, |
| 818 | }, |
| 819 | pins={ |
iannucci | b58d10d | 2017-03-18 02:00:25 -0700 | [diff] [blame] | 820 | 'client_package': { |
| 821 | 'package_name': client.package_name, |
| 822 | 'version': client.instance_id, |
| 823 | }, |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 824 | 'packages': package_pins, |
| 825 | }) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 826 | |
| 827 | |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 828 | def clean_caches(options, isolate_cache, named_cache_manager): |
maruel | e6fc938 | 2017-05-04 09:03:48 -0700 | [diff] [blame] | 829 | """Trims isolated and named caches. |
| 830 | |
| 831 | The goal here is to coherently trim both caches, deleting older items |
| 832 | independent of which container they belong to. |
| 833 | """ |
| 834 | # TODO(maruel): Trim CIPD cache the same way. |
| 835 | total = 0 |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 836 | with named_cache_manager.open(): |
| 837 | oldest_isolated = isolate_cache.get_oldest() |
| 838 | oldest_named = named_cache_manager.get_oldest() |
| 839 | trimmers = [ |
| 840 | ( |
| 841 | isolate_cache.trim, |
| 842 | isolate_cache.get_timestamp(oldest_isolated) if oldest_isolated else 0, |
| 843 | ), |
| 844 | ( |
| 845 | lambda: named_cache_manager.trim(options.min_free_space), |
| 846 | named_cache_manager.get_timestamp(oldest_named) if oldest_named else 0, |
| 847 | ), |
| 848 | ] |
| 849 | trimmers.sort(key=lambda (_, ts): ts) |
maruel | e6fc938 | 2017-05-04 09:03:48 -0700 | [diff] [blame] | 850 | # TODO(maruel): This is incorrect, we want to trim 'items' that are strictly |
| 851 | # the oldest independent of in which cache they live in. Right now, the |
| 852 | # cache with the oldest item pays the price. |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 853 | for trim, _ in trimmers: |
maruel | e6fc938 | 2017-05-04 09:03:48 -0700 | [diff] [blame] | 854 | total += trim() |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 855 | isolate_cache.cleanup() |
maruel | e6fc938 | 2017-05-04 09:03:48 -0700 | [diff] [blame] | 856 | return total |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 857 | |
| 858 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 859 | def create_option_parser(): |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 860 | parser = logging_utils.OptionParserWithLogging( |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 861 | usage='%prog <options> [command to run or extra args]', |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 862 | version=__version__, |
| 863 | log_file=RUN_ISOLATED_LOG_FILE) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 864 | parser.add_option( |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 865 | '--clean', action='store_true', |
| 866 | help='Cleans the cache, trimming it necessary and remove corrupted items ' |
| 867 | 'and returns without executing anything; use with -v to know what ' |
| 868 | 'was done') |
| 869 | parser.add_option( |
maruel | 2e8d0f5 | 2016-07-16 07:51:29 -0700 | [diff] [blame] | 870 | '--no-clean', action='store_true', |
| 871 | help='Do not clean the cache automatically on startup. This is meant for ' |
| 872 | 'bots where a separate execution with --clean was done earlier so ' |
| 873 | 'doing it again is redundant') |
| 874 | parser.add_option( |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 875 | '--use-symlinks', action='store_true', |
| 876 | help='Use symlinks instead of hardlinks') |
| 877 | parser.add_option( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 878 | '--json', |
| 879 | help='dump output metadata to json file. When used, run_isolated returns ' |
| 880 | 'non-zero only on internal failure') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 881 | parser.add_option( |
maruel | 5c9e47b | 2015-12-18 13:02:30 -0800 | [diff] [blame] | 882 | '--hard-timeout', type='float', help='Enforce hard timeout in execution') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 883 | parser.add_option( |
maruel | 5c9e47b | 2015-12-18 13:02:30 -0800 | [diff] [blame] | 884 | '--grace-period', type='float', |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 885 | help='Grace period between SIGTERM and SIGKILL') |
bpastene | 3ae0952 | 2016-06-10 17:12:59 -0700 | [diff] [blame] | 886 | parser.add_option( |
| 887 | '--bot-file', |
| 888 | help='Path to a file describing the state of the host. The content is ' |
| 889 | 'defined by on_before_task() in bot_config.') |
aludwin | 7556e0c | 2016-10-26 08:46:10 -0700 | [diff] [blame] | 890 | parser.add_option( |
aludwin | 0a8e17d | 2016-10-27 15:57:39 -0700 | [diff] [blame] | 891 | '--output', action='append', |
| 892 | help='Specifies an output to return. If no outputs are specified, all ' |
| 893 | 'files located in $(ISOLATED_OUTDIR) will be returned; ' |
| 894 | 'otherwise, outputs in both $(ISOLATED_OUTDIR) and those ' |
| 895 | 'specified by --output option (there can be multiple) will be ' |
| 896 | 'returned. Note that if a file in OUT_DIR has the same path ' |
| 897 | 'as an --output option, the --output version will be returned.') |
| 898 | parser.add_option( |
aludwin | 7556e0c | 2016-10-26 08:46:10 -0700 | [diff] [blame] | 899 | '-a', '--argsfile', |
| 900 | # This is actually handled in parse_args; it's included here purely so it |
| 901 | # can make it into the help text. |
| 902 | help='Specify a file containing a JSON array of arguments to this ' |
| 903 | 'script. If --argsfile is provided, no other argument may be ' |
| 904 | 'provided on the command line.') |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 905 | data_group = optparse.OptionGroup(parser, 'Data source') |
| 906 | data_group.add_option( |
Marc-Antoine Ruel | 185ded4 | 2015-01-28 20:49:18 -0500 | [diff] [blame] | 907 | '-s', '--isolated', |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 908 | help='Hash of the .isolated to grab from the isolate server.') |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 909 | isolateserver.add_isolate_server_options(data_group) |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 910 | parser.add_option_group(data_group) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 911 | |
Marc-Antoine Ruel | a57d7db | 2014-10-15 20:31:19 -0400 | [diff] [blame] | 912 | isolateserver.add_cache_options(parser) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 913 | |
| 914 | cipd.add_cipd_options(parser) |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 915 | named_cache.add_named_cache_options(parser) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 916 | |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 917 | debug_group = optparse.OptionGroup(parser, 'Debugging') |
| 918 | debug_group.add_option( |
| 919 | '--leak-temp-dir', |
| 920 | action='store_true', |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 921 | help='Deliberately leak isolate\'s temp dir for later examination. ' |
| 922 | 'Default: %default') |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 923 | debug_group.add_option( |
| 924 | '--root-dir', help='Use a directory instead of a random one') |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 925 | parser.add_option_group(debug_group) |
| 926 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 927 | auth.add_auth_options(parser) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 928 | |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 929 | parser.set_defaults( |
| 930 | cache='cache', |
| 931 | cipd_cache='cipd_cache', |
| 932 | named_cache_root='named_caches') |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 933 | return parser |
| 934 | |
| 935 | |
aludwin | 7556e0c | 2016-10-26 08:46:10 -0700 | [diff] [blame] | 936 | def parse_args(args): |
| 937 | # Create a fake mini-parser just to get out the "-a" command. Note that |
| 938 | # it's not documented here; instead, it's documented in create_option_parser |
| 939 | # even though that parser will never actually get to parse it. This is |
| 940 | # because --argsfile is exclusive with all other options and arguments. |
| 941 | file_argparse = argparse.ArgumentParser(add_help=False) |
| 942 | file_argparse.add_argument('-a', '--argsfile') |
| 943 | (file_args, nonfile_args) = file_argparse.parse_known_args(args) |
| 944 | if file_args.argsfile: |
| 945 | if nonfile_args: |
| 946 | file_argparse.error('Can\'t specify --argsfile with' |
| 947 | 'any other arguments (%s)' % nonfile_args) |
| 948 | try: |
| 949 | with open(file_args.argsfile, 'r') as f: |
| 950 | args = json.load(f) |
| 951 | except (IOError, OSError, ValueError) as e: |
| 952 | # We don't need to error out here - "args" is now empty, |
| 953 | # so the call below to parser.parse_args(args) will fail |
| 954 | # and print the full help text. |
| 955 | print >> sys.stderr, 'Couldn\'t read arguments: %s' % e |
| 956 | |
| 957 | # Even if we failed to read the args, just call the normal parser now since it |
| 958 | # will print the correct help message. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 959 | parser = create_option_parser() |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 960 | options, args = parser.parse_args(args) |
aludwin | 7556e0c | 2016-10-26 08:46:10 -0700 | [diff] [blame] | 961 | return (parser, options, args) |
| 962 | |
| 963 | |
| 964 | def main(args): |
| 965 | (parser, options, args) = parse_args(args) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 966 | |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 967 | isolate_cache = isolateserver.process_cache_options(options, trim=False) |
| 968 | named_cache_manager = named_cache.process_named_cache_options(parser, options) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 969 | if options.clean: |
| 970 | if options.isolated: |
| 971 | parser.error('Can\'t use --isolated with --clean.') |
| 972 | if options.isolate_server: |
| 973 | parser.error('Can\'t use --isolate-server with --clean.') |
| 974 | if options.json: |
| 975 | parser.error('Can\'t use --json with --clean.') |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 976 | if options.named_caches: |
| 977 | parser.error('Can\t use --named-cache with --clean.') |
| 978 | clean_caches(options, isolate_cache, named_cache_manager) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 979 | return 0 |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 980 | |
maruel | 2e8d0f5 | 2016-07-16 07:51:29 -0700 | [diff] [blame] | 981 | if not options.no_clean: |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 982 | clean_caches(options, isolate_cache, named_cache_manager) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 983 | |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 984 | if not options.isolated and not args: |
| 985 | parser.error('--isolated or command to run is required.') |
| 986 | |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 987 | auth.process_auth_options(parser, options) |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 988 | |
| 989 | isolateserver.process_isolate_server_options( |
| 990 | parser, options, True, False) |
| 991 | if not options.isolate_server: |
| 992 | if options.isolated: |
| 993 | parser.error('--isolated requires --isolate-server') |
| 994 | if ISOLATED_OUTDIR_PARAMETER in args: |
| 995 | parser.error( |
| 996 | '%s in args requires --isolate-server' % ISOLATED_OUTDIR_PARAMETER) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 997 | |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 998 | if options.root_dir: |
| 999 | options.root_dir = unicode(os.path.abspath(options.root_dir)) |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 1000 | if options.json: |
| 1001 | options.json = unicode(os.path.abspath(options.json)) |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 1002 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 1003 | cipd.validate_cipd_options(parser, options) |
| 1004 | |
vadimsh | 232f5a8 | 2017-01-20 19:23:44 -0800 | [diff] [blame] | 1005 | install_packages_fn = noop_install_packages |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 1006 | if options.cipd_enabled: |
iannucci | b58d10d | 2017-03-18 02:00:25 -0700 | [diff] [blame] | 1007 | install_packages_fn = lambda run_dir: install_client_and_packages( |
vadimsh | 902948e | 2017-01-20 15:57:32 -0800 | [diff] [blame] | 1008 | run_dir, cipd.parse_package_args(options.cipd_packages), |
| 1009 | options.cipd_server, options.cipd_client_package, |
| 1010 | options.cipd_client_version, cache_dir=options.cipd_cache) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 1011 | |
nodir | d616068 | 2017-02-02 13:03:35 -0800 | [diff] [blame] | 1012 | @contextlib.contextmanager |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 1013 | def install_named_caches(run_dir): |
nodir | d616068 | 2017-02-02 13:03:35 -0800 | [diff] [blame] | 1014 | # WARNING: this function depends on "options" variable defined in the outer |
| 1015 | # function. |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 1016 | caches = [ |
| 1017 | (os.path.join(run_dir, unicode(relpath)), name) |
| 1018 | for name, relpath in options.named_caches |
| 1019 | ] |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1020 | with named_cache_manager.open(): |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 1021 | for path, name in caches: |
| 1022 | named_cache_manager.install(path, name) |
nodir | d616068 | 2017-02-02 13:03:35 -0800 | [diff] [blame] | 1023 | try: |
| 1024 | yield |
| 1025 | finally: |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 1026 | with named_cache_manager.open(): |
| 1027 | for path, name in caches: |
| 1028 | named_cache_manager.uninstall(path, name) |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1029 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 1030 | try: |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 1031 | if options.isolate_server: |
| 1032 | storage = isolateserver.get_storage( |
| 1033 | options.isolate_server, options.namespace) |
| 1034 | with storage: |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1035 | # Hashing schemes used by |storage| and |isolate_cache| MUST match. |
| 1036 | assert storage.hash_algo == isolate_cache.hash_algo |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 1037 | return run_tha_test( |
maruel | abec63c | 2017-04-26 11:53:24 -0700 | [diff] [blame] | 1038 | args, |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1039 | options.isolated, |
| 1040 | storage, |
| 1041 | isolate_cache, |
aludwin | 0a8e17d | 2016-10-27 15:57:39 -0700 | [diff] [blame] | 1042 | options.output, |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 1043 | install_named_caches, |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1044 | options.leak_temp_dir, |
| 1045 | options.json, options.root_dir, |
| 1046 | options.hard_timeout, |
| 1047 | options.grace_period, |
maruel | abec63c | 2017-04-26 11:53:24 -0700 | [diff] [blame] | 1048 | options.bot_file, |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1049 | install_packages_fn, |
| 1050 | options.use_symlinks) |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 1051 | return run_tha_test( |
maruel | abec63c | 2017-04-26 11:53:24 -0700 | [diff] [blame] | 1052 | args, |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1053 | options.isolated, |
| 1054 | None, |
| 1055 | isolate_cache, |
aludwin | 0a8e17d | 2016-10-27 15:57:39 -0700 | [diff] [blame] | 1056 | options.output, |
nodir | 0ae98b3 | 2017-05-11 13:21:53 -0700 | [diff] [blame] | 1057 | install_named_caches, |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1058 | options.leak_temp_dir, |
| 1059 | options.json, |
| 1060 | options.root_dir, |
| 1061 | options.hard_timeout, |
| 1062 | options.grace_period, |
maruel | abec63c | 2017-04-26 11:53:24 -0700 | [diff] [blame] | 1063 | options.bot_file, |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1064 | install_packages_fn, |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 1065 | options.use_symlinks) |
nodir | f33b8d6 | 2016-10-26 22:34:58 -0700 | [diff] [blame] | 1066 | except (cipd.Error, named_cache.Error) as ex: |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 1067 | print >> sys.stderr, ex.message |
| 1068 | return 1 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 1069 | |
| 1070 | |
| 1071 | if __name__ == '__main__': |
maruel | 8e4e40c | 2016-05-30 06:21:07 -0700 | [diff] [blame] | 1072 | subprocess42.inhibit_os_error_reporting() |
csharp@chromium.org | bfb9874 | 2013-03-26 20:28:36 +0000 | [diff] [blame] | 1073 | # Ensure that we are always running with the correct encoding. |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 1074 | fix_encoding.fix_encoding() |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 1075 | file_path.enable_symlink() |
aludwin | 7556e0c | 2016-10-26 08:46:10 -0700 | [diff] [blame] | 1076 | |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 1077 | sys.exit(main(sys.argv[1:])) |