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 | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 30 | __version__ = '0.8.4' |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 31 | |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 32 | import base64 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 33 | import logging |
| 34 | import optparse |
| 35 | import os |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 36 | import sys |
| 37 | import tempfile |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 38 | import time |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 39 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 40 | from third_party.depot_tools import fix_encoding |
| 41 | |
Vadim Shtayura | 6b555c1 | 2014-07-23 16:22:18 -0700 | [diff] [blame] | 42 | from utils import file_path |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 43 | from utils import fs |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 44 | from utils import large |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 45 | from utils import logging_utils |
Marc-Antoine Ruel | cfb6085 | 2014-07-02 15:22:00 -0400 | [diff] [blame] | 46 | from utils import on_error |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 47 | from utils import subprocess42 |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 48 | from utils import tools |
vadimsh@chromium.org | 3e97deb | 2013-08-24 00:56:44 +0000 | [diff] [blame] | 49 | from utils import zip_package |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 50 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 51 | import auth |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 52 | import cipd |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 53 | import isolateserver |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 54 | |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 55 | |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 56 | # 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] | 57 | THIS_FILE_PATH = os.path.abspath( |
| 58 | __file__.decode(sys.getfilesystemencoding())) if __file__ else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 59 | |
| 60 | # Directory that contains this file (might be inside zip package). |
tansell | a494944 | 2016-06-23 22:34:32 -0700 | [diff] [blame] | 61 | BASE_DIR = os.path.dirname(THIS_FILE_PATH) if __file__.decode( |
| 62 | sys.getfilesystemencoding()) else None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 63 | |
| 64 | # Directory that contains currently running script file. |
maruel@chromium.org | 814d23f | 2013-10-01 19:08:00 +0000 | [diff] [blame] | 65 | if zip_package.get_main_script_path(): |
| 66 | MAIN_DIR = os.path.dirname( |
| 67 | os.path.abspath(zip_package.get_main_script_path())) |
| 68 | else: |
| 69 | # This happens when 'import run_isolated' is executed at the python |
| 70 | # interactive prompt, in that case __file__ is undefined. |
| 71 | MAIN_DIR = None |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 72 | |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 73 | |
| 74 | # Magic variables that can be found in the isolate task command line. |
| 75 | ISOLATED_OUTDIR_PARAMETER = '${ISOLATED_OUTDIR}' |
| 76 | EXECUTABLE_SUFFIX_PARAMETER = '${EXECUTABLE_SUFFIX}' |
| 77 | SWARMING_BOT_FILE_PARAMETER = '${SWARMING_BOT_FILE}' |
| 78 | |
| 79 | |
csharp@chromium.org | ff2a466 | 2012-11-21 20:49:32 +0000 | [diff] [blame] | 80 | # The name of the log file to use. |
| 81 | RUN_ISOLATED_LOG_FILE = 'run_isolated.log' |
| 82 | |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 83 | |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 84 | # 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] | 85 | RUN_TEST_CASES_LOG = 'run_test_cases.log' |
csharp@chromium.org | e217f30 | 2012-11-22 16:51:53 +0000 | [diff] [blame] | 86 | |
vadimsh@chromium.org | 87d6326 | 2013-04-04 19:34:21 +0000 | [diff] [blame] | 87 | |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 88 | # Use short names for temporary directories. This is driven by Windows, which |
| 89 | # imposes a relatively short maximum path length of 260 characters, often |
| 90 | # referred to as MAX_PATH. It is relatively easy to create files with longer |
| 91 | # path length. A use case is with recursive depedency treesV like npm packages. |
| 92 | # |
| 93 | # It is recommended to start the script with a `root_dir` as short as |
| 94 | # possible. |
| 95 | # - ir stands for isolated_run |
| 96 | # - io stands for isolated_out |
| 97 | # - it stands for isolated_tmp |
| 98 | ISOLATED_RUN_DIR = u'ir' |
| 99 | ISOLATED_OUT_DIR = u'io' |
| 100 | ISOLATED_TMP_DIR = u'it' |
| 101 | |
| 102 | |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 103 | def get_as_zip_package(executable=True): |
| 104 | """Returns ZipPackage with this module and all its dependencies. |
| 105 | |
| 106 | If |executable| is True will store run_isolated.py as __main__.py so that |
| 107 | zip package is directly executable be python. |
| 108 | """ |
| 109 | # Building a zip package when running from another zip package is |
| 110 | # unsupported and probably unneeded. |
| 111 | assert not zip_package.is_zipped_module(sys.modules[__name__]) |
vadimsh@chromium.org | 8507106 | 2013-08-21 23:37:45 +0000 | [diff] [blame] | 112 | assert THIS_FILE_PATH |
| 113 | assert BASE_DIR |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 114 | package = zip_package.ZipPackage(root=BASE_DIR) |
| 115 | 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] | 116 | 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] | 117 | package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py')) |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 118 | package.add_python_file(os.path.join(BASE_DIR, 'auth.py')) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 119 | package.add_python_file(os.path.join(BASE_DIR, 'cipd.py')) |
tansell | e4288c3 | 2016-07-28 09:45:40 -0700 | [diff] [blame^] | 120 | package.add_directory(os.path.join(BASE_DIR, 'libs')) |
vadimsh@chromium.org | 8b9d56b | 2013-08-21 22:24:35 +0000 | [diff] [blame] | 121 | package.add_directory(os.path.join(BASE_DIR, 'third_party')) |
| 122 | package.add_directory(os.path.join(BASE_DIR, 'utils')) |
| 123 | return package |
| 124 | |
| 125 | |
maruel | 03e1184 | 2016-07-14 10:50:16 -0700 | [diff] [blame] | 126 | def make_temp_dir(prefix, root_dir): |
| 127 | """Returns a new unique temporary directory.""" |
| 128 | return unicode(tempfile.mkdtemp(prefix=prefix, dir=root_dir)) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 129 | |
| 130 | |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 131 | def change_tree_read_only(rootdir, read_only): |
| 132 | """Changes the tree read-only bits according to the read_only specification. |
| 133 | |
| 134 | The flag can be 0, 1 or 2, which will affect the possibility to modify files |
| 135 | and create or delete files. |
| 136 | """ |
| 137 | if read_only == 2: |
| 138 | # Files and directories (except on Windows) are marked read only. This |
| 139 | # inhibits modifying, creating or deleting files in the test directory, |
| 140 | # except on Windows where creating and deleting files is still possible. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 141 | file_path.make_tree_read_only(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 142 | elif read_only == 1: |
| 143 | # Files are marked read only but not the directories. This inhibits |
| 144 | # modifying files but creating or deleting files is still possible. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 145 | file_path.make_tree_files_read_only(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 146 | elif read_only in (0, None): |
Marc-Antoine Ruel | f1d827c | 2014-11-24 15:22:25 -0500 | [diff] [blame] | 147 | # Anything can be modified. |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 148 | # TODO(maruel): This is currently dangerous as long as DiskCache.touch() |
| 149 | # is not yet changed to verify the hash of the content of the files it is |
| 150 | # looking at, so that if a test modifies an input file, the file must be |
| 151 | # deleted. |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 152 | file_path.make_tree_writeable(rootdir) |
Marc-Antoine Ruel | 7124e39 | 2014-01-09 11:49:21 -0500 | [diff] [blame] | 153 | else: |
| 154 | raise ValueError( |
| 155 | 'change_tree_read_only(%s, %s): Unknown flag %s' % |
| 156 | (rootdir, read_only, read_only)) |
| 157 | |
| 158 | |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 159 | def process_command(command, out_dir, bot_file): |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 160 | """Replaces variables in a command line. |
| 161 | |
| 162 | Raises: |
| 163 | ValueError if a parameter is requested in |command| but its value is not |
| 164 | provided. |
| 165 | """ |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 166 | def fix(arg): |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 167 | arg = arg.replace(EXECUTABLE_SUFFIX_PARAMETER, cipd.EXECUTABLE_SUFFIX) |
| 168 | replace_slash = False |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 169 | if ISOLATED_OUTDIR_PARAMETER in arg: |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 170 | if not out_dir: |
maruel | 7f63a27 | 2016-07-12 12:40:36 -0700 | [diff] [blame] | 171 | raise ValueError( |
| 172 | 'output directory is requested in command, but not provided; ' |
| 173 | 'please specify one') |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 174 | arg = arg.replace(ISOLATED_OUTDIR_PARAMETER, out_dir) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 175 | replace_slash = True |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 176 | if SWARMING_BOT_FILE_PARAMETER in arg: |
| 177 | if bot_file: |
| 178 | arg = arg.replace(SWARMING_BOT_FILE_PARAMETER, bot_file) |
| 179 | replace_slash = True |
| 180 | else: |
| 181 | logging.warning('SWARMING_BOT_FILE_PARAMETER found in command, but no ' |
| 182 | 'bot_file specified. Leaving parameter unchanged.') |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 183 | if replace_slash: |
| 184 | # Replace slashes only if parameters are present |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 185 | # because of arguments like '${ISOLATED_OUTDIR}/foo/bar' |
| 186 | arg = arg.replace('/', os.sep) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 187 | return arg |
| 188 | |
| 189 | return [fix(arg) for arg in command] |
| 190 | |
| 191 | |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 192 | def run_command(command, cwd, tmp_dir, hard_timeout, grace_period): |
| 193 | """Runs the command. |
| 194 | |
| 195 | Returns: |
| 196 | tuple(process exit code, bool if had a hard timeout) |
| 197 | """ |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 198 | logging.info('run_command(%s, %s)' % (command, cwd)) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 199 | |
| 200 | env = os.environ.copy() |
| 201 | if sys.platform == 'darwin': |
tansell | a494944 | 2016-06-23 22:34:32 -0700 | [diff] [blame] | 202 | env['TMPDIR'] = tmp_dir.encode(sys.getfilesystemencoding()) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 203 | elif sys.platform == 'win32': |
tansell | a494944 | 2016-06-23 22:34:32 -0700 | [diff] [blame] | 204 | env['TEMP'] = tmp_dir.encode(sys.getfilesystemencoding()) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 205 | else: |
tansell | a494944 | 2016-06-23 22:34:32 -0700 | [diff] [blame] | 206 | env['TMP'] = tmp_dir.encode(sys.getfilesystemencoding()) |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 207 | exit_code = None |
| 208 | had_hard_timeout = False |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 209 | with tools.Profiler('RunTest'): |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 210 | proc = None |
| 211 | had_signal = [] |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 212 | try: |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 213 | # TODO(maruel): This code is imperfect. It doesn't handle well signals |
| 214 | # during the download phase and there's short windows were things can go |
| 215 | # wrong. |
| 216 | def handler(signum, _frame): |
| 217 | if proc and not had_signal: |
| 218 | logging.info('Received signal %d', signum) |
| 219 | had_signal.append(True) |
maruel | 556d905 | 2015-10-05 11:12:44 -0700 | [diff] [blame] | 220 | raise subprocess42.TimeoutExpired(command, None) |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 221 | |
| 222 | proc = subprocess42.Popen(command, cwd=cwd, env=env, detached=True) |
| 223 | with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, handler): |
| 224 | try: |
| 225 | exit_code = proc.wait(hard_timeout or None) |
| 226 | except subprocess42.TimeoutExpired: |
| 227 | if not had_signal: |
| 228 | logging.warning('Hard timeout') |
| 229 | had_hard_timeout = True |
| 230 | logging.warning('Sending SIGTERM') |
| 231 | proc.terminate() |
| 232 | |
| 233 | # Ignore signals in grace period. Forcibly give the grace period to the |
| 234 | # child process. |
| 235 | if exit_code is None: |
| 236 | ignore = lambda *_: None |
| 237 | with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, ignore): |
| 238 | try: |
| 239 | exit_code = proc.wait(grace_period or None) |
| 240 | except subprocess42.TimeoutExpired: |
| 241 | # Now kill for real. The user can distinguish between the |
| 242 | # following states: |
| 243 | # - signal but process exited within grace period, |
| 244 | # hard_timed_out will be set but the process exit code will be |
| 245 | # script provided. |
| 246 | # - processed exited late, exit code will be -9 on posix. |
| 247 | logging.warning('Grace exhausted; sending SIGKILL') |
| 248 | proc.kill() |
| 249 | logging.info('Waiting for proces exit') |
| 250 | exit_code = proc.wait() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 251 | except OSError: |
| 252 | # This is not considered to be an internal error. The executable simply |
| 253 | # does not exit. |
maruel | a72f46e | 2016-02-24 11:05:45 -0800 | [diff] [blame] | 254 | sys.stderr.write( |
| 255 | '<The executable does not exist or a dependent library is missing>\n' |
| 256 | '<Check for missing .so/.dll in the .isolate or GN file>\n' |
| 257 | '<Command: %s>\n' % command) |
| 258 | if os.environ.get('SWARMING_TASK_ID'): |
| 259 | # Give an additional hint when running as a swarming task. |
| 260 | sys.stderr.write( |
| 261 | '<See the task\'s page for commands to help diagnose this issue ' |
| 262 | 'by reproducing the task locally>\n') |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 263 | exit_code = 1 |
| 264 | logging.info( |
| 265 | 'Command finished with exit code %d (%s)', |
| 266 | exit_code, hex(0xffffffff & exit_code)) |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 267 | return exit_code, had_hard_timeout |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 268 | |
| 269 | |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 270 | def fetch_and_map(isolated_hash, storage, cache, outdir, use_symlinks): |
| 271 | """Fetches an isolated tree, create the tree and returns (bundle, stats).""" |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 272 | start = time.time() |
| 273 | bundle = isolateserver.fetch_isolated( |
| 274 | isolated_hash=isolated_hash, |
| 275 | storage=storage, |
| 276 | cache=cache, |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 277 | outdir=outdir, |
| 278 | use_symlinks=use_symlinks) |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 279 | return bundle, { |
| 280 | 'duration': time.time() - start, |
| 281 | 'initial_number_items': cache.initial_number_items, |
| 282 | 'initial_size': cache.initial_size, |
| 283 | 'items_cold': base64.b64encode(large.pack(sorted(cache.added))), |
| 284 | 'items_hot': base64.b64encode( |
tansell | 9e04a8d | 2016-07-28 09:31:59 -0700 | [diff] [blame] | 285 | large.pack(sorted(set(cache.used) - set(cache.added)))), |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 289 | def delete_and_upload(storage, out_dir, leak_temp_dir): |
| 290 | """Deletes the temporary run directory and uploads results back. |
| 291 | |
| 292 | Returns: |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 293 | tuple(outputs_ref, success, stats) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 294 | - outputs_ref: a dict referring to the results archived back to the isolated |
| 295 | server, if applicable. |
| 296 | - success: False if something occurred that means that the task must |
| 297 | forcibly be considered a failure, e.g. zombie processes were left |
| 298 | behind. |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 299 | - stats: uploading stats. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 300 | """ |
| 301 | |
| 302 | # Upload out_dir and generate a .isolated file out of this directory. It is |
| 303 | # only done if files were written in the directory. |
| 304 | outputs_ref = None |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 305 | cold = [] |
| 306 | hot = [] |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 307 | start = time.time() |
| 308 | |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 309 | if fs.isdir(out_dir) and fs.listdir(out_dir): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 310 | with tools.Profiler('ArchiveOutput'): |
| 311 | try: |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 312 | results, f_cold, f_hot = isolateserver.archive_files_to_storage( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 313 | storage, [out_dir], None) |
| 314 | outputs_ref = { |
| 315 | 'isolated': results[0][0], |
| 316 | 'isolatedserver': storage.location, |
| 317 | 'namespace': storage.namespace, |
| 318 | } |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 319 | cold = sorted(i.size for i in f_cold) |
| 320 | hot = sorted(i.size for i in f_hot) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 321 | except isolateserver.Aborted: |
| 322 | # This happens when a signal SIGTERM was received while uploading data. |
| 323 | # There is 2 causes: |
| 324 | # - The task was too slow and was about to be killed anyway due to |
| 325 | # exceeding the hard timeout. |
| 326 | # - The amount of data uploaded back is very large and took too much |
| 327 | # time to archive. |
| 328 | sys.stderr.write('Received SIGTERM while uploading') |
| 329 | # Re-raise, so it will be treated as an internal failure. |
| 330 | raise |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 331 | |
| 332 | success = False |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 333 | try: |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 334 | if (not leak_temp_dir and fs.isdir(out_dir) and |
maruel | 6eeea7d | 2015-09-16 12:17:42 -0700 | [diff] [blame] | 335 | not file_path.rmtree(out_dir)): |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 336 | logging.error('Had difficulties removing out_dir %s', out_dir) |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 337 | else: |
| 338 | success = True |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 339 | except OSError as e: |
| 340 | # When this happens, it means there's a process error. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 341 | logging.exception('Had difficulties removing out_dir %s: %s', out_dir, e) |
nodir | 6f80188 | 2016-04-29 14:41:50 -0700 | [diff] [blame] | 342 | stats = { |
| 343 | 'duration': time.time() - start, |
| 344 | 'items_cold': base64.b64encode(large.pack(cold)), |
| 345 | 'items_hot': base64.b64encode(large.pack(hot)), |
| 346 | } |
| 347 | return outputs_ref, success, stats |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 348 | |
| 349 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 350 | def map_and_run( |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 351 | command, isolated_hash, storage, cache, leak_temp_dir, root_dir, |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 352 | hard_timeout, grace_period, bot_file, extra_args, install_packages_fn, |
| 353 | use_symlinks): |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 354 | """Runs a command with optional isolated input/output. |
| 355 | |
| 356 | See run_tha_test for argument documentation. |
| 357 | |
| 358 | Returns metadata about the result. |
| 359 | """ |
| 360 | assert bool(command) ^ bool(isolated_hash) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 361 | result = { |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 362 | 'duration': None, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 363 | 'exit_code': None, |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 364 | 'had_hard_timeout': False, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 365 | 'internal_failure': None, |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 366 | 'stats': { |
nodir | 5571571 | 2016-06-03 12:28:19 -0700 | [diff] [blame] | 367 | # 'isolated': { |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 368 | # 'cipd': { |
| 369 | # 'duration': 0., |
| 370 | # 'get_client_duration': 0., |
| 371 | # }, |
nodir | 5571571 | 2016-06-03 12:28:19 -0700 | [diff] [blame] | 372 | # 'download': { |
| 373 | # 'duration': 0., |
| 374 | # 'initial_number_items': 0, |
| 375 | # 'initial_size': 0, |
| 376 | # 'items_cold': '<large.pack()>', |
| 377 | # 'items_hot': '<large.pack()>', |
| 378 | # }, |
| 379 | # 'upload': { |
| 380 | # 'duration': 0., |
| 381 | # 'items_cold': '<large.pack()>', |
| 382 | # 'items_hot': '<large.pack()>', |
| 383 | # }, |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 384 | # }, |
| 385 | }, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 386 | 'outputs_ref': None, |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 387 | 'version': 5, |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 388 | } |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 389 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 390 | if root_dir: |
nodir | e5028a9 | 2016-04-29 14:38:21 -0700 | [diff] [blame] | 391 | file_path.ensure_tree(root_dir, 0700) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 392 | else: |
maruel | 2e8d0f5 | 2016-07-16 07:51:29 -0700 | [diff] [blame] | 393 | root_dir = os.path.dirname(cache.cache_dir) if cache.cache_dir else None |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 394 | # See comment for these constants. |
| 395 | run_dir = make_temp_dir(ISOLATED_RUN_DIR, root_dir) |
maruel | 03e1184 | 2016-07-14 10:50:16 -0700 | [diff] [blame] | 396 | # storage should be normally set but don't crash if it is not. This can happen |
| 397 | # as Swarming task can run without an isolate server. |
maruel | e2f2cb8 | 2016-07-13 14:41:03 -0700 | [diff] [blame] | 398 | out_dir = make_temp_dir(ISOLATED_OUT_DIR, root_dir) if storage else None |
| 399 | tmp_dir = make_temp_dir(ISOLATED_TMP_DIR, root_dir) |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 400 | cwd = run_dir |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 401 | |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 402 | try: |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 403 | cipd_stats = install_packages_fn(run_dir) |
| 404 | if cipd_stats: |
| 405 | result['stats']['cipd'] = cipd_stats |
| 406 | |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 407 | if isolated_hash: |
nodir | 5571571 | 2016-06-03 12:28:19 -0700 | [diff] [blame] | 408 | isolated_stats = result['stats'].setdefault('isolated', {}) |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 409 | bundle, isolated_stats['download'] = fetch_and_map( |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 410 | isolated_hash=isolated_hash, |
| 411 | storage=storage, |
| 412 | cache=cache, |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 413 | outdir=run_dir, |
| 414 | use_symlinks=use_symlinks) |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 415 | if not bundle.command: |
| 416 | # Handle this as a task failure, not an internal failure. |
| 417 | sys.stderr.write( |
| 418 | '<The .isolated doesn\'t declare any command to run!>\n' |
| 419 | '<Check your .isolate for missing \'command\' variable>\n') |
| 420 | if os.environ.get('SWARMING_TASK_ID'): |
| 421 | # Give an additional hint when running as a swarming task. |
| 422 | sys.stderr.write('<This occurs at the \'isolate\' step>\n') |
| 423 | result['exit_code'] = 1 |
| 424 | return result |
| 425 | |
| 426 | change_tree_read_only(run_dir, bundle.read_only) |
| 427 | cwd = os.path.normpath(os.path.join(cwd, bundle.relative_cwd)) |
| 428 | command = bundle.command + extra_args |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 429 | |
nodir | 34d673c | 2016-05-24 09:30:48 -0700 | [diff] [blame] | 430 | command = tools.fix_python_path(command) |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 431 | command = process_command(command, out_dir, bot_file) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 432 | file_path.ensure_command_has_abs_path(command, cwd) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 433 | |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 434 | sys.stdout.flush() |
| 435 | start = time.time() |
| 436 | try: |
| 437 | result['exit_code'], result['had_hard_timeout'] = run_command( |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 438 | command, cwd, tmp_dir, hard_timeout, grace_period) |
maruel | 064c0a3 | 2016-04-05 11:47:15 -0700 | [diff] [blame] | 439 | finally: |
| 440 | result['duration'] = max(time.time() - start, 0) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 441 | except Exception as e: |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 442 | # An internal error occurred. Report accordingly so the swarming task will |
| 443 | # be retried automatically. |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 444 | logging.exception('internal failure: %s', e) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 445 | result['internal_failure'] = str(e) |
| 446 | on_error.report(None) |
| 447 | finally: |
| 448 | try: |
| 449 | if leak_temp_dir: |
| 450 | logging.warning( |
| 451 | 'Deliberately leaking %s for later examination', run_dir) |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 452 | else: |
maruel | 84537cb | 2015-10-16 14:21:28 -0700 | [diff] [blame] | 453 | # On Windows rmtree(run_dir) call above has a synchronization effect: it |
| 454 | # finishes only when all task child processes terminate (since a running |
| 455 | # process locks *.exe file). Examine out_dir only after that call |
| 456 | # completes (since child processes may write to out_dir too and we need |
| 457 | # to wait for them to finish). |
| 458 | if fs.isdir(run_dir): |
| 459 | try: |
| 460 | success = file_path.rmtree(run_dir) |
| 461 | except OSError as e: |
| 462 | logging.error('Failure with %s', e) |
| 463 | success = False |
| 464 | if not success: |
| 465 | print >> sys.stderr, ( |
| 466 | 'Failed to delete the run directory, forcibly failing\n' |
| 467 | 'the task because of it. No zombie process can outlive a\n' |
| 468 | 'successful task run and still be marked as successful.\n' |
| 469 | 'Fix your stuff.') |
| 470 | if result['exit_code'] == 0: |
| 471 | result['exit_code'] = 1 |
| 472 | if fs.isdir(tmp_dir): |
| 473 | try: |
| 474 | success = file_path.rmtree(tmp_dir) |
| 475 | except OSError as e: |
| 476 | logging.error('Failure with %s', e) |
| 477 | success = False |
| 478 | if not success: |
| 479 | print >> sys.stderr, ( |
| 480 | 'Failed to delete the temporary directory, forcibly failing\n' |
| 481 | 'the task because of it. No zombie process can outlive a\n' |
| 482 | 'successful task run and still be marked as successful.\n' |
| 483 | 'Fix your stuff.') |
| 484 | if result['exit_code'] == 0: |
| 485 | result['exit_code'] = 1 |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 486 | |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 487 | # This deletes out_dir if leak_temp_dir is not set. |
nodir | 9130f07 | 2016-05-27 13:59:08 -0700 | [diff] [blame] | 488 | if out_dir: |
nodir | 5571571 | 2016-06-03 12:28:19 -0700 | [diff] [blame] | 489 | isolated_stats = result['stats'].setdefault('isolated', {}) |
| 490 | result['outputs_ref'], success, isolated_stats['upload'] = ( |
nodir | 9130f07 | 2016-05-27 13:59:08 -0700 | [diff] [blame] | 491 | delete_and_upload(storage, out_dir, leak_temp_dir)) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 492 | if not success and result['exit_code'] == 0: |
| 493 | result['exit_code'] = 1 |
| 494 | except Exception as e: |
| 495 | # Swallow any exception in the main finally clause. |
nodir | 9130f07 | 2016-05-27 13:59:08 -0700 | [diff] [blame] | 496 | if out_dir: |
| 497 | logging.exception('Leaking out_dir %s: %s', out_dir, e) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 498 | result['internal_failure'] = str(e) |
| 499 | return result |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 500 | |
| 501 | |
Marc-Antoine Ruel | 0ec868b | 2015-08-12 14:12:46 -0400 | [diff] [blame] | 502 | def run_tha_test( |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 503 | command, isolated_hash, storage, cache, leak_temp_dir, result_json, |
bpastene | 3ae0952 | 2016-06-10 17:12:59 -0700 | [diff] [blame] | 504 | root_dir, hard_timeout, grace_period, bot_file, extra_args, |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 505 | install_packages_fn, use_symlinks): |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 506 | """Runs an executable and records execution metadata. |
| 507 | |
| 508 | Either command or isolated_hash must be specified. |
| 509 | |
| 510 | If isolated_hash is specified, downloads the dependencies in the cache, |
| 511 | hardlinks them into a temporary directory and runs the command specified in |
| 512 | the .isolated. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 513 | |
| 514 | A temporary directory is created to hold the output files. The content inside |
| 515 | this directory will be uploaded back to |storage| packaged as a .isolated |
| 516 | file. |
| 517 | |
| 518 | Arguments: |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 519 | command: the command to run, a list of strings. Mutually exclusive with |
| 520 | isolated_hash. |
Marc-Antoine Ruel | 35b5843 | 2014-12-08 17:40:40 -0500 | [diff] [blame] | 521 | 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] | 522 | recreate the tree of files to run the target executable. |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 523 | The command specified in the .isolated is executed. |
| 524 | Mutually exclusive with command argument. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 525 | storage: an isolateserver.Storage object to retrieve remote objects. This |
| 526 | object has a reference to an isolateserver.StorageApi, which does |
| 527 | the actual I/O. |
| 528 | cache: an isolateserver.LocalCache to keep from retrieving the same objects |
| 529 | constantly by caching the objects retrieved. Can be on-disk or |
| 530 | in-memory. |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 531 | leak_temp_dir: if true, the temporary directory will be deliberately leaked |
| 532 | for later examination. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 533 | result_json: file path to dump result metadata into. If set, the process |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 534 | exit code is always 0 unless an internal error occurred. |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 535 | 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] | 536 | not specified, a random temporary directory is created. |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 537 | hard_timeout: kills the process if it lasts more than this amount of |
| 538 | seconds. |
| 539 | grace_period: number of seconds to wait between SIGTERM and SIGKILL. |
Marc-Antoine Ruel | 2283ad1 | 2014-02-09 11:14:57 -0500 | [diff] [blame] | 540 | extra_args: optional arguments to add to the command stated in the .isolate |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 541 | file. Ignored if isolate_hash is empty. |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 542 | install_packages_fn: function (dir) => cipd_stats. Installs packages. |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 543 | use_symlinks: create tree with symlinks instead of hardlinks. |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 544 | |
| 545 | Returns: |
| 546 | Process exit code that should be used. |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 547 | """ |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 548 | assert bool(command) ^ bool(isolated_hash) |
| 549 | extra_args = extra_args or [] |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 550 | |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 551 | if any(ISOLATED_OUTDIR_PARAMETER in a for a in (command or extra_args)): |
| 552 | assert storage is not None, 'storage is None although outdir is specified' |
| 553 | |
maruel | a76b9ee | 2015-12-15 06:18:08 -0800 | [diff] [blame] | 554 | if result_json: |
| 555 | # Write a json output file right away in case we get killed. |
| 556 | result = { |
| 557 | 'exit_code': None, |
| 558 | 'had_hard_timeout': False, |
| 559 | 'internal_failure': 'Was terminated before completion', |
| 560 | 'outputs_ref': None, |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 561 | 'version': 5, |
maruel | a76b9ee | 2015-12-15 06:18:08 -0800 | [diff] [blame] | 562 | } |
| 563 | tools.write_json(result_json, result, dense=True) |
| 564 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 565 | # run_isolated exit code. Depends on if result_json is used or not. |
| 566 | result = map_and_run( |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 567 | command, isolated_hash, storage, cache, leak_temp_dir, root_dir, |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 568 | hard_timeout, grace_period, bot_file, extra_args, install_packages_fn, |
| 569 | use_symlinks) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 570 | logging.info('Result:\n%s', tools.format_json(result, dense=True)) |
bpastene | 3ae0952 | 2016-06-10 17:12:59 -0700 | [diff] [blame] | 571 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 572 | if result_json: |
maruel | 05d5a88 | 2015-09-21 13:59:02 -0700 | [diff] [blame] | 573 | # We've found tests to delete 'work' when quitting, causing an exception |
| 574 | # here. Try to recreate the directory if necessary. |
nodir | e5028a9 | 2016-04-29 14:38:21 -0700 | [diff] [blame] | 575 | file_path.ensure_tree(os.path.dirname(result_json)) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 576 | tools.write_json(result_json, result, dense=True) |
| 577 | # Only return 1 if there was an internal error. |
| 578 | return int(bool(result['internal_failure'])) |
maruel@chromium.org | 781ccf6 | 2013-09-17 19:39:47 +0000 | [diff] [blame] | 579 | |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 580 | # Marshall into old-style inline output. |
| 581 | if result['outputs_ref']: |
| 582 | data = { |
| 583 | 'hash': result['outputs_ref']['isolated'], |
| 584 | 'namespace': result['outputs_ref']['namespace'], |
| 585 | 'storage': result['outputs_ref']['isolatedserver'], |
| 586 | } |
Marc-Antoine Ruel | c44f572 | 2015-01-08 16:10:01 -0500 | [diff] [blame] | 587 | sys.stdout.flush() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 588 | print( |
| 589 | '[run_isolated_out_hack]%s[/run_isolated_out_hack]' % |
| 590 | tools.format_json(data, dense=True)) |
maruel | b76604c | 2015-11-11 11:53:44 -0800 | [diff] [blame] | 591 | sys.stdout.flush() |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 592 | return result['exit_code'] or int(bool(result['internal_failure'])) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 593 | |
| 594 | |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 595 | def install_packages( |
nodir | ff531b4 | 2016-06-23 13:05:06 -0700 | [diff] [blame] | 596 | run_dir, packages, service_url, client_package_name, |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 597 | client_version, cache_dir=None, timeout=None): |
| 598 | """Installs packages. Returns stats. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 599 | |
| 600 | Args: |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 601 | run_dir (str): root of installation. |
nodir | ff531b4 | 2016-06-23 13:05:06 -0700 | [diff] [blame] | 602 | packages: packages to install, dict {path: [(package_name, version)]. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 603 | service_url (str): CIPD server url, e.g. |
| 604 | "https://chrome-infra-packages.appspot.com." |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 605 | client_package_name (str): CIPD package name of CIPD client. |
| 606 | client_version (str): Version of CIPD client. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 607 | cache_dir (str): where to keep cache of cipd clients, packages and tags. |
| 608 | timeout: max duration in seconds that this function can take. |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 609 | """ |
| 610 | assert cache_dir |
nodir | ff531b4 | 2016-06-23 13:05:06 -0700 | [diff] [blame] | 611 | if not packages: |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 612 | return None |
| 613 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 614 | timeoutfn = tools.sliding_timeout(timeout) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 615 | start = time.time() |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 616 | cache_dir = os.path.abspath(cache_dir) |
| 617 | |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 618 | run_dir = os.path.abspath(run_dir) |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 619 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 620 | get_client_start = time.time() |
| 621 | client_manager = cipd.get_client( |
| 622 | service_url, client_package_name, client_version, cache_dir, |
| 623 | timeout=timeoutfn()) |
| 624 | with client_manager as client: |
| 625 | get_client_duration = time.time() - get_client_start |
nodir | ff531b4 | 2016-06-23 13:05:06 -0700 | [diff] [blame] | 626 | for path, packages in sorted(packages.iteritems()): |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 627 | site_root = os.path.abspath(os.path.join(run_dir, path)) |
| 628 | if not site_root.startswith(run_dir): |
| 629 | raise cipd.Error('Invalid CIPD package path "%s"' % path) |
| 630 | |
| 631 | # Do not clean site_root before installation because it may contain other |
| 632 | # site roots. |
| 633 | file_path.ensure_tree(site_root, 0770) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 634 | client.ensure( |
| 635 | site_root, packages, |
| 636 | cache_dir=os.path.join(cache_dir, 'cipd_internal'), |
| 637 | timeout=timeoutfn()) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 638 | file_path.make_tree_files_read_only(site_root) |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 639 | |
| 640 | total_duration = time.time() - start |
| 641 | logging.info( |
| 642 | 'Installing CIPD client and packages took %d seconds', total_duration) |
| 643 | |
| 644 | return { |
| 645 | 'duration': total_duration, |
| 646 | 'get_client_duration': get_client_duration, |
| 647 | } |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 648 | |
| 649 | |
| 650 | def create_option_parser(): |
Marc-Antoine Ruel | f74cffe | 2015-07-15 15:21:34 -0400 | [diff] [blame] | 651 | parser = logging_utils.OptionParserWithLogging( |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 652 | usage='%prog <options> [command to run or extra args]', |
maruel@chromium.org | dedbf49 | 2013-09-12 20:42:11 +0000 | [diff] [blame] | 653 | version=__version__, |
| 654 | log_file=RUN_ISOLATED_LOG_FILE) |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 655 | parser.add_option( |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 656 | '--clean', action='store_true', |
| 657 | help='Cleans the cache, trimming it necessary and remove corrupted items ' |
| 658 | 'and returns without executing anything; use with -v to know what ' |
| 659 | 'was done') |
| 660 | parser.add_option( |
maruel | 2e8d0f5 | 2016-07-16 07:51:29 -0700 | [diff] [blame] | 661 | '--no-clean', action='store_true', |
| 662 | help='Do not clean the cache automatically on startup. This is meant for ' |
| 663 | 'bots where a separate execution with --clean was done earlier so ' |
| 664 | 'doing it again is redundant') |
| 665 | parser.add_option( |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 666 | '--use-symlinks', action='store_true', |
| 667 | help='Use symlinks instead of hardlinks') |
| 668 | parser.add_option( |
maruel | a9cfd6f | 2015-09-15 11:03:15 -0700 | [diff] [blame] | 669 | '--json', |
| 670 | help='dump output metadata to json file. When used, run_isolated returns ' |
| 671 | 'non-zero only on internal failure') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 672 | parser.add_option( |
maruel | 5c9e47b | 2015-12-18 13:02:30 -0800 | [diff] [blame] | 673 | '--hard-timeout', type='float', help='Enforce hard timeout in execution') |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 674 | parser.add_option( |
maruel | 5c9e47b | 2015-12-18 13:02:30 -0800 | [diff] [blame] | 675 | '--grace-period', type='float', |
maruel | 6be7f9e | 2015-10-01 12:25:30 -0700 | [diff] [blame] | 676 | help='Grace period between SIGTERM and SIGKILL') |
bpastene | 3ae0952 | 2016-06-10 17:12:59 -0700 | [diff] [blame] | 677 | parser.add_option( |
| 678 | '--bot-file', |
| 679 | help='Path to a file describing the state of the host. The content is ' |
| 680 | 'defined by on_before_task() in bot_config.') |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 681 | data_group = optparse.OptionGroup(parser, 'Data source') |
| 682 | data_group.add_option( |
Marc-Antoine Ruel | 185ded4 | 2015-01-28 20:49:18 -0500 | [diff] [blame] | 683 | '-s', '--isolated', |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 684 | help='Hash of the .isolated to grab from the isolate server.') |
Marc-Antoine Ruel | f7d737d | 2014-12-10 15:36:29 -0500 | [diff] [blame] | 685 | isolateserver.add_isolate_server_options(data_group) |
Marc-Antoine Ruel | 1687b5e | 2014-02-06 17:47:53 -0500 | [diff] [blame] | 686 | parser.add_option_group(data_group) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 687 | |
Marc-Antoine Ruel | a57d7db | 2014-10-15 20:31:19 -0400 | [diff] [blame] | 688 | isolateserver.add_cache_options(parser) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 689 | |
| 690 | cipd.add_cipd_options(parser) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 691 | |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 692 | debug_group = optparse.OptionGroup(parser, 'Debugging') |
| 693 | debug_group.add_option( |
| 694 | '--leak-temp-dir', |
| 695 | action='store_true', |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 696 | help='Deliberately leak isolate\'s temp dir for later examination. ' |
| 697 | 'Default: %default') |
maruel | eb5fbee | 2015-09-17 13:01:36 -0700 | [diff] [blame] | 698 | debug_group.add_option( |
| 699 | '--root-dir', help='Use a directory instead of a random one') |
Kenneth Russell | 61d4235 | 2014-09-15 11:41:16 -0700 | [diff] [blame] | 700 | parser.add_option_group(debug_group) |
| 701 | |
Vadim Shtayura | e34e13a | 2014-02-02 11:23:26 -0800 | [diff] [blame] | 702 | auth.add_auth_options(parser) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 703 | |
| 704 | parser.set_defaults(cache='cache', cipd_cache='cipd_cache') |
| 705 | return parser |
| 706 | |
| 707 | |
| 708 | def main(args): |
| 709 | parser = create_option_parser() |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 710 | options, args = parser.parse_args(args) |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 711 | |
| 712 | cache = isolateserver.process_cache_options(options) |
| 713 | if options.clean: |
| 714 | if options.isolated: |
| 715 | parser.error('Can\'t use --isolated with --clean.') |
| 716 | if options.isolate_server: |
| 717 | parser.error('Can\'t use --isolate-server with --clean.') |
| 718 | if options.json: |
| 719 | parser.error('Can\'t use --json with --clean.') |
| 720 | cache.cleanup() |
| 721 | return 0 |
maruel | 2e8d0f5 | 2016-07-16 07:51:29 -0700 | [diff] [blame] | 722 | if not options.no_clean: |
| 723 | cache.cleanup() |
maruel | 36a963d | 2016-04-08 17:15:49 -0700 | [diff] [blame] | 724 | |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 725 | if not options.isolated and not args: |
| 726 | parser.error('--isolated or command to run is required.') |
| 727 | |
Vadim Shtayura | 5d1efce | 2014-02-04 10:55:43 -0800 | [diff] [blame] | 728 | auth.process_auth_options(parser, options) |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 729 | |
| 730 | isolateserver.process_isolate_server_options( |
| 731 | parser, options, True, False) |
| 732 | if not options.isolate_server: |
| 733 | if options.isolated: |
| 734 | parser.error('--isolated requires --isolate-server') |
| 735 | if ISOLATED_OUTDIR_PARAMETER in args: |
| 736 | parser.error( |
| 737 | '%s in args requires --isolate-server' % ISOLATED_OUTDIR_PARAMETER) |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 738 | |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 739 | if options.root_dir: |
| 740 | options.root_dir = unicode(os.path.abspath(options.root_dir)) |
maruel | 12e3001 | 2015-10-09 11:55:35 -0700 | [diff] [blame] | 741 | if options.json: |
| 742 | options.json = unicode(os.path.abspath(options.json)) |
nodir | 55be77b | 2016-05-03 09:39:57 -0700 | [diff] [blame] | 743 | |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 744 | cipd.validate_cipd_options(parser, options) |
| 745 | |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 746 | install_packages_fn = lambda run_dir: install_packages( |
nodir | ff531b4 | 2016-06-23 13:05:06 -0700 | [diff] [blame] | 747 | run_dir, cipd.parse_package_args(options.cipd_packages), |
| 748 | options.cipd_server, options.cipd_client_package, |
| 749 | options.cipd_client_version, cache_dir=options.cipd_cache) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 750 | |
| 751 | try: |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 752 | command = [] if options.isolated else args |
| 753 | if options.isolate_server: |
| 754 | storage = isolateserver.get_storage( |
| 755 | options.isolate_server, options.namespace) |
| 756 | with storage: |
| 757 | # Hashing schemes used by |storage| and |cache| MUST match. |
| 758 | assert storage.hash_algo == cache.hash_algo |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 759 | return run_tha_test( |
nodir | 90bc8dc | 2016-06-15 13:35:21 -0700 | [diff] [blame] | 760 | command, options.isolated, storage, cache, options.leak_temp_dir, |
| 761 | options.json, options.root_dir, options.hard_timeout, |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 762 | options.grace_period, options.bot_file, args, install_packages_fn, |
| 763 | options.use_symlinks) |
| 764 | return run_tha_test( |
| 765 | command, options.isolated, None, cache, options.leak_temp_dir, |
| 766 | options.json, options.root_dir, options.hard_timeout, |
| 767 | options.grace_period, options.bot_file, args, install_packages_fn, |
| 768 | options.use_symlinks) |
nodir | be642ff | 2016-06-09 15:51:51 -0700 | [diff] [blame] | 769 | except cipd.Error as ex: |
| 770 | print >> sys.stderr, ex.message |
| 771 | return 1 |
maruel@chromium.org | 9c72d4e | 2012-09-28 19:20:25 +0000 | [diff] [blame] | 772 | |
| 773 | |
| 774 | if __name__ == '__main__': |
maruel | 8e4e40c | 2016-05-30 06:21:07 -0700 | [diff] [blame] | 775 | subprocess42.inhibit_os_error_reporting() |
csharp@chromium.org | bfb9874 | 2013-03-26 20:28:36 +0000 | [diff] [blame] | 776 | # Ensure that we are always running with the correct encoding. |
vadimsh@chromium.org | a432647 | 2013-08-24 02:05:41 +0000 | [diff] [blame] | 777 | fix_encoding.fix_encoding() |
maruel | 4409e30 | 2016-07-19 14:25:51 -0700 | [diff] [blame] | 778 | file_path.enable_symlink() |
Marc-Antoine Ruel | 90c9816 | 2013-12-18 15:11:57 -0500 | [diff] [blame] | 779 | sys.exit(main(sys.argv[1:])) |