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