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