blob: 188e47bd2253b1535fe4ab18fe7c1f0c3151afa2 [file] [log] [blame]
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001#!/usr/bin/env python
maruelea586f32016-04-05 11:11:33 -07002# Copyright 2012 The LUCI Authors. All rights reserved.
maruelf1f5e2a2016-05-25 17:10:39 -07003# 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.org9c72d4e2012-09-28 19:20:25 +00005
nodir55be77b2016-05-03 09:39:57 -07006"""Runs a command with optional isolated input/output.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00007
Marc-Antoine Rueleed2f3a2019-03-14 00:00:40 +00008run_isolated takes cares of setting up a temporary environment, running a
9command, and tearing it down.
nodir55be77b2016-05-03 09:39:57 -070010
Marc-Antoine Rueleed2f3a2019-03-14 00:00:40 +000011It handles downloading and uploading isolated files, mapping CIPD packages and
12reusing stateful named caches.
13
14The isolated files, CIPD packages and named caches are kept as a global LRU
15cache.
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -050016
Roberto Carrillo71ade6d2018-10-08 22:30:24 +000017Any ${EXECUTABLE_SUFFIX} on the command line or the environment variables passed
18with the --env option will be replaced with ".exe" string on Windows and "" on
19other platforms.
nodirbe642ff2016-06-09 15:51:51 -070020
Roberto Carrillo71ade6d2018-10-08 22:30:24 +000021Any ${ISOLATED_OUTDIR} on the command line or the environment variables passed
22with the --env option will be replaced by the location of a temporary directory
23upon execution of the command specified in the .isolated file. All content
24written to this directory will be uploaded upon termination and the .isolated
25file describing this directory will be printed to stdout.
bpastene447c1992016-06-20 15:21:47 -070026
Marc-Antoine Rueleed2f3a2019-03-14 00:00:40 +000027Any ${SWARMING_BOT_FILE} on the command line or the environment variables passed
28with the --env option will be replaced by the value of the --bot-file parameter.
29This file is used by a swarming bot to communicate state of the host to tasks.
30It is written to by the swarming bot's on_before_task() hook in the swarming
31server's custom bot_config.py.
32
33See
34https://chromium.googlesource.com/infra/luci/luci-py.git/+/master/appengine/swarming/doc/Magic-Values.md
35for all the variables.
36
37See
38https://chromium.googlesource.com/infra/luci/luci-py.git/+/master/appengine/swarming/swarming_bot/config/bot_config.py
39for more information about bot_config.py.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000040"""
41
Marc-Antoine Ruelf899c482019-10-10 23:32:06 +000042from __future__ import print_function
43
44__version__ = '1.0.1'
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000045
aludwin7556e0c2016-10-26 08:46:10 -070046import argparse
maruel064c0a32016-04-05 11:47:15 -070047import base64
iannucci96fcccc2016-08-30 15:52:22 -070048import collections
vadimsh232f5a82017-01-20 19:23:44 -080049import contextlib
Sadaf Matinkhoo10743a62018-03-29 16:28:58 -040050import errno
aludwin7556e0c2016-10-26 08:46:10 -070051import json
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000052import logging
53import optparse
54import os
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -040055import re
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000056import sys
57import tempfile
maruel064c0a32016-04-05 11:47:15 -070058import time
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000059
Marc-Antoine Ruel016c7602019-04-02 18:31:13 +000060from utils import tools
61tools.force_local_third_party()
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000062
Marc-Antoine Ruel016c7602019-04-02 18:31:13 +000063# third_party/
64from depot_tools import fix_encoding
Takuto Ikuta6e2ff962019-10-29 12:35:27 +000065import six
Marc-Antoine Ruel016c7602019-04-02 18:31:13 +000066
67# pylint: disable=ungrouped-imports
68import auth
69import cipd
70import isolate_storage
71import isolateserver
72import local_caching
73from libs import luci_context
Vadim Shtayura6b555c12014-07-23 16:22:18 -070074from utils import file_path
maruel12e30012015-10-09 11:55:35 -070075from utils import fs
maruel064c0a32016-04-05 11:47:15 -070076from utils import large
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -040077from utils import logging_utils
Marc-Antoine Ruelcfb60852014-07-02 15:22:00 -040078from utils import on_error
Marc-Antoine Ruelc44f5722015-01-08 16:10:01 -050079from utils import subprocess42
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000080
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000081
maruele2f2cb82016-07-13 14:41:03 -070082# Magic variables that can be found in the isolate task command line.
83ISOLATED_OUTDIR_PARAMETER = '${ISOLATED_OUTDIR}'
84EXECUTABLE_SUFFIX_PARAMETER = '${EXECUTABLE_SUFFIX}'
85SWARMING_BOT_FILE_PARAMETER = '${SWARMING_BOT_FILE}'
86
87
csharp@chromium.orgff2a4662012-11-21 20:49:32 +000088# The name of the log file to use.
89RUN_ISOLATED_LOG_FILE = 'run_isolated.log'
90
maruele2f2cb82016-07-13 14:41:03 -070091
csharp@chromium.orge217f302012-11-22 16:51:53 +000092# The name of the log to use for the run_test_cases.py command
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000093RUN_TEST_CASES_LOG = 'run_test_cases.log'
csharp@chromium.orge217f302012-11-22 16:51:53 +000094
vadimsh@chromium.org87d63262013-04-04 19:34:21 +000095
maruele2f2cb82016-07-13 14:41:03 -070096# Use short names for temporary directories. This is driven by Windows, which
97# imposes a relatively short maximum path length of 260 characters, often
98# referred to as MAX_PATH. It is relatively easy to create files with longer
Marc-Antoine Ruel793bff32019-04-18 17:50:48 +000099# path length. A use case is with recursive dependency trees like npm packages.
maruele2f2cb82016-07-13 14:41:03 -0700100#
101# It is recommended to start the script with a `root_dir` as short as
102# possible.
103# - ir stands for isolated_run
104# - io stands for isolated_out
105# - it stands for isolated_tmp
Takuto Ikutab7ce0e32019-11-27 23:26:18 +0000106# - ic stands for isolated_client
maruele2f2cb82016-07-13 14:41:03 -0700107ISOLATED_RUN_DIR = u'ir'
108ISOLATED_OUT_DIR = u'io'
109ISOLATED_TMP_DIR = u'it'
Takuto Ikutab7ce0e32019-11-27 23:26:18 +0000110ISOLATED_CLIENT_DIR = u'ic'
maruele2f2cb82016-07-13 14:41:03 -0700111
Takuto Ikuta02edca22019-11-29 10:04:51 +0000112# TODO(tikuta): take these parameter from luci-config?
Takuto Ikutab7ce0e32019-11-27 23:26:18 +0000113# Take revision from
114# https://ci.chromium.org/p/infra-internal/g/infra-packagers/console
Takuto Ikuta02edca22019-11-29 10:04:51 +0000115ISOLATED_PACKAGE = 'infra/tools/luci/isolated/${platform}'
Takuto Ikuta4129e822019-12-03 04:04:26 +0000116ISOLATED_REVISION = 'git_revision:1190afd45e12e2adbdde61b24a14365f0dc415e3'
maruele2f2cb82016-07-13 14:41:03 -0700117
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -0400118# Keep synced with task_request.py
Lei Leife202df2019-06-11 17:33:34 +0000119CACHE_NAME_RE = re.compile(r'^[a-z0-9_]{1,4096}$')
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -0400120
121
marueld928c862017-06-08 08:20:04 -0700122OUTLIVING_ZOMBIE_MSG = """\
123*** Swarming tried multiple times to delete the %s directory and failed ***
124*** Hard failing the task ***
125
126Swarming detected that your testing script ran an executable, which may have
127started a child executable, and the main script returned early, leaving the
128children executables playing around unguided.
129
130You don't want to leave children processes outliving the task on the Swarming
131bot, do you? The Swarming bot doesn't.
132
133How to fix?
134- For any process that starts children processes, make sure all children
135 processes terminated properly before each parent process exits. This is
136 especially important in very deep process trees.
137 - This must be done properly both in normal successful task and in case of
138 task failure. Cleanup is very important.
139- The Swarming bot sends a SIGTERM in case of timeout.
140 - You have %s seconds to comply after the signal was sent to the process
141 before the process is forcibly killed.
142- To achieve not leaking children processes in case of signals on timeout, you
143 MUST handle signals in each executable / python script and propagate them to
144 children processes.
145 - When your test script (python or binary) receives a signal like SIGTERM or
146 CTRL_BREAK_EVENT on Windows), send it to all children processes and wait for
147 them to terminate before quitting.
148
149See
Marc-Antoine Ruelc7243592018-05-24 17:04:04 -0400150https://chromium.googlesource.com/infra/luci/luci-py.git/+/master/appengine/swarming/doc/Bot.md#Graceful-termination_aka-the-SIGTERM-and-SIGKILL-dance
marueld928c862017-06-08 08:20:04 -0700151for more information.
152
153*** May the SIGKILL force be with you ***
154"""
155
156
Marc-Antoine Ruel5d7606b2018-06-15 19:06:12 +0000157# Currently hardcoded. Eventually could be exposed as a flag once there's value.
158# 3 weeks
159MAX_AGE_SECS = 21*24*60*60
160
161
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500162TaskData = collections.namedtuple(
Marc-Antoine Ruel03c6fd12019-04-30 12:12:55 +0000163 'TaskData',
164 [
Takuto Ikuta9a319502019-11-26 07:40:14 +0000165 # List of strings; the command line to use, independent of what was
166 # specified in the isolated file.
167 'command',
168 # Relative directory to start command into.
169 'relative_cwd',
170 # List of strings; the arguments to add to the command specified in the
171 # isolated file.
172 'extra_args',
173 # Hash of the .isolated file that must be retrieved to recreate the tree
174 # of files to run the target executable. The command specified in the
175 # .isolated is executed. Mutually exclusive with command argument.
176 'isolated_hash',
177 # isolateserver.Storage instance to retrieve remote objects. This object
178 # has a reference to an isolateserver.StorageApi, which does the actual
179 # I/O.
180 'storage',
181 # isolateserver.LocalCache instance to keep from retrieving the same
182 # objects constantly by caching the objects retrieved. Can be on-disk or
183 # in-memory.
184 'isolate_cache',
185 # List of paths relative to root_dir to put into the output isolated
186 # bundle upon task completion (see link_outputs_to_outdir).
187 'outputs',
188 # Function (run_dir) => context manager that installs named caches into
189 # |run_dir|.
190 'install_named_caches',
191 # If True, the temporary directory will be deliberately leaked for later
192 # examination.
193 'leak_temp_dir',
194 # Path to the directory to use to create the temporary directory. If not
195 # specified, a random temporary directory is created.
196 'root_dir',
197 # Kills the process if it lasts more than this amount of seconds.
198 'hard_timeout',
199 # Number of seconds to wait between SIGTERM and SIGKILL.
200 'grace_period',
201 # Path to a file with bot state, used in place of ${SWARMING_BOT_FILE}
202 # task command line argument.
203 'bot_file',
204 # Logical account to switch LUCI_CONTEXT into.
205 'switch_to_account',
206 # Context manager dir => CipdInfo, see install_client_and_packages.
207 'install_packages_fn',
208 # Create tree with symlinks instead of hardlinks.
209 'use_symlinks',
Takuto Ikutad03ffcc2019-12-02 01:04:23 +0000210 # Use go isolated client.
211 'use_go_isolated',
Takuto Ikuta057c5342019-12-03 04:05:05 +0000212 # Cache directory for go isolated client.
213 'go_cache_dir',
Takuto Ikuta9a319502019-11-26 07:40:14 +0000214 # Environment variables to set.
215 'env',
216 # Environment variables to mutate with relative directories.
217 # Example: {"ENV_KEY": ['relative', 'paths', 'to', 'prepend']}
218 'env_prefix',
219 # Lowers the task process priority.
220 'lower_priority',
221 # subprocess42.Containment instance. Can be None.
222 'containment',
Marc-Antoine Ruel03c6fd12019-04-30 12:12:55 +0000223 ])
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500224
225
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -0500226def _to_str(s):
227 """Downgrades a unicode instance to str. Pass str through as-is."""
228 if isinstance(s, str):
229 return s
230 # This is technically incorrect, especially on Windows. In theory
231 # sys.getfilesystemencoding() should be used to use the right 'ANSI code
232 # page' on Windows, but that causes other problems, as the character set
233 # is very limited.
234 return s.encode('utf-8')
235
236
Marc-Antoine Ruel7a68f712017-12-01 18:45:18 -0500237def _to_unicode(s):
238 """Upgrades a str instance to unicode. Pass unicode through as-is."""
Takuto Ikuta95459dd2019-10-29 12:39:47 +0000239 if isinstance(s, six.text_type) or s is None:
Marc-Antoine Ruel7a68f712017-12-01 18:45:18 -0500240 return s
241 return s.decode('utf-8')
242
243
maruel03e11842016-07-14 10:50:16 -0700244def make_temp_dir(prefix, root_dir):
245 """Returns a new unique temporary directory."""
Takuto Ikuta6e2ff962019-10-29 12:35:27 +0000246 return six.text_type(tempfile.mkdtemp(prefix=prefix, dir=root_dir))
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000247
248
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500249def change_tree_read_only(rootdir, read_only):
250 """Changes the tree read-only bits according to the read_only specification.
251
252 The flag can be 0, 1 or 2, which will affect the possibility to modify files
253 and create or delete files.
254 """
255 if read_only == 2:
256 # Files and directories (except on Windows) are marked read only. This
257 # inhibits modifying, creating or deleting files in the test directory,
258 # except on Windows where creating and deleting files is still possible.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400259 file_path.make_tree_read_only(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500260 elif read_only == 1:
261 # Files are marked read only but not the directories. This inhibits
262 # modifying files but creating or deleting files is still possible.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400263 file_path.make_tree_files_read_only(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500264 elif read_only in (0, None):
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -0500265 # Anything can be modified.
Marc-Antoine Ruel2666d9c2018-05-18 13:52:02 -0400266 # TODO(maruel): This is currently dangerous as long as
267 # DiskContentAddressedCache.touch() is not yet changed to verify the hash of
268 # the content of the files it is looking at, so that if a test modifies an
269 # input file, the file must be deleted.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400270 file_path.make_tree_writeable(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500271 else:
272 raise ValueError(
273 'change_tree_read_only(%s, %s): Unknown flag %s' %
274 (rootdir, read_only, read_only))
275
276
vadimsh9c54b2c2017-07-25 14:08:29 -0700277@contextlib.contextmanager
278def set_luci_context_account(account, tmp_dir):
279 """Sets LUCI_CONTEXT account to be used by the task.
280
281 If 'account' is None or '', does nothing at all. This happens when
282 run_isolated.py is called without '--switch-to-account' flag. In this case,
283 if run_isolated.py is running in some LUCI_CONTEXT environment, the task will
Takuto Ikuta33e2ff32019-09-30 12:44:03 +0000284 just inherit whatever account is already set. This may happen if users invoke
vadimsh9c54b2c2017-07-25 14:08:29 -0700285 run_isolated.py explicitly from their code.
286
287 If the requested account is not defined in the context, switches to
288 non-authenticated access. This happens for Swarming tasks that don't use
289 'task' service accounts.
290
291 If not using LUCI_CONTEXT-based auth, does nothing.
292 If already running as requested account, does nothing.
293 """
294 if not account:
295 # Not actually switching.
296 yield
297 return
298
299 local_auth = luci_context.read('local_auth')
300 if not local_auth:
301 # Not using LUCI_CONTEXT auth at all.
302 yield
303 return
304
305 # See LUCI_CONTEXT.md for the format of 'local_auth'.
306 if local_auth.get('default_account_id') == account:
307 # Already set, no need to switch.
308 yield
309 return
310
311 available = {a['id'] for a in local_auth.get('accounts') or []}
312 if account in available:
313 logging.info('Switching default LUCI_CONTEXT account to %r', account)
314 local_auth['default_account_id'] = account
315 else:
316 logging.warning(
317 'Requested LUCI_CONTEXT account %r is not available (have only %r), '
318 'disabling authentication', account, sorted(available))
319 local_auth.pop('default_account_id', None)
320
321 with luci_context.write(_tmpdir=tmp_dir, local_auth=local_auth):
322 yield
323
324
nodir90bc8dc2016-06-15 13:35:21 -0700325def process_command(command, out_dir, bot_file):
Roberto Carrillo71ade6d2018-10-08 22:30:24 +0000326 """Replaces parameters in a command line.
nodirbe642ff2016-06-09 15:51:51 -0700327
328 Raises:
329 ValueError if a parameter is requested in |command| but its value is not
330 provided.
331 """
Roberto Carrillo71ade6d2018-10-08 22:30:24 +0000332 return [replace_parameters(arg, out_dir, bot_file) for arg in command]
333
334
335def replace_parameters(arg, out_dir, bot_file):
336 """Replaces parameter tokens with appropriate values in a string.
337
338 Raises:
339 ValueError if a parameter is requested in |arg| but its value is not
340 provided.
341 """
342 arg = arg.replace(EXECUTABLE_SUFFIX_PARAMETER, cipd.EXECUTABLE_SUFFIX)
343 replace_slash = False
344 if ISOLATED_OUTDIR_PARAMETER in arg:
345 if not out_dir:
346 raise ValueError(
347 'output directory is requested in command or env var, but not '
348 'provided; please specify one')
349 arg = arg.replace(ISOLATED_OUTDIR_PARAMETER, out_dir)
350 replace_slash = True
351 if SWARMING_BOT_FILE_PARAMETER in arg:
352 if bot_file:
353 arg = arg.replace(SWARMING_BOT_FILE_PARAMETER, bot_file)
nodirbe642ff2016-06-09 15:51:51 -0700354 replace_slash = True
Roberto Carrillo71ade6d2018-10-08 22:30:24 +0000355 else:
356 logging.warning('SWARMING_BOT_FILE_PARAMETER found in command or env '
357 'var, but no bot_file specified. Leaving parameter '
358 'unchanged.')
359 if replace_slash:
360 # Replace slashes only if parameters are present
361 # because of arguments like '${ISOLATED_OUTDIR}/foo/bar'
362 arg = arg.replace('/', os.sep)
363 return arg
maruela9cfd6f2015-09-15 11:03:15 -0700364
365
Roberto Carrillo71ade6d2018-10-08 22:30:24 +0000366
367def get_command_env(tmp_dir, cipd_info, run_dir, env, env_prefixes, out_dir,
368 bot_file):
vadimsh232f5a82017-01-20 19:23:44 -0800369 """Returns full OS environment to run a command in.
370
Robert Iannuccibf5f84c2017-11-22 12:56:50 -0800371 Sets up TEMP, puts directory with cipd binary in front of PATH, exposes
372 CIPD_CACHE_DIR env var, and installs all env_prefixes.
vadimsh232f5a82017-01-20 19:23:44 -0800373
374 Args:
375 tmp_dir: temp directory.
376 cipd_info: CipdInfo object is cipd client is used, None if not.
Marc-Antoine Ruel9ec1e9f2017-12-20 16:36:54 -0500377 run_dir: The root directory the isolated tree is mapped in.
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500378 env: environment variables to use
Robert Iannuccibf5f84c2017-11-22 12:56:50 -0800379 env_prefixes: {"ENV_KEY": ['cwd', 'relative', 'paths', 'to', 'prepend']}
Roberto Carrillo71ade6d2018-10-08 22:30:24 +0000380 out_dir: Isolated output directory. Required to be != None if any of the
381 env vars contain ISOLATED_OUTDIR_PARAMETER.
382 bot_file: Required to be != None if any of the env vars contain
383 SWARMING_BOT_FILE_PARAMETER.
vadimsh232f5a82017-01-20 19:23:44 -0800384 """
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500385 out = os.environ.copy()
Marc-Antoine Ruel04903a32019-10-09 21:09:25 +0000386 for k, v in env.items():
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500387 if not v:
Marc-Antoine Ruel9ec1e9f2017-12-20 16:36:54 -0500388 out.pop(k, None)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500389 else:
Roberto Carrillo71ade6d2018-10-08 22:30:24 +0000390 out[k] = replace_parameters(v, out_dir, bot_file)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500391
392 if cipd_info:
393 bin_dir = os.path.dirname(cipd_info.client.binary_path)
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -0500394 out['PATH'] = '%s%s%s' % (_to_str(bin_dir), os.pathsep, out['PATH'])
395 out['CIPD_CACHE_DIR'] = _to_str(cipd_info.cache_dir)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500396
Marc-Antoine Ruel04903a32019-10-09 21:09:25 +0000397 for key, paths in env_prefixes.items():
Marc-Antoine Ruel9ec1e9f2017-12-20 16:36:54 -0500398 assert isinstance(paths, list), paths
399 paths = [os.path.normpath(os.path.join(run_dir, p)) for p in paths]
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500400 cur = out.get(key)
401 if cur:
402 paths.append(cur)
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -0500403 out[key] = _to_str(os.path.pathsep.join(paths))
vadimsh232f5a82017-01-20 19:23:44 -0800404
Marc-Antoine Ruelefb30b12018-07-25 18:34:36 +0000405 tmp_dir = _to_str(tmp_dir)
406 # pylint: disable=line-too-long
407 # * python respects $TMPDIR, $TEMP, and $TMP in this order, regardless of
408 # platform. So $TMPDIR must be set on all platforms.
409 # https://github.com/python/cpython/blob/2.7/Lib/tempfile.py#L155
410 out['TMPDIR'] = tmp_dir
411 if sys.platform == 'win32':
412 # * chromium's base utils uses GetTempPath().
413 # https://cs.chromium.org/chromium/src/base/files/file_util_win.cc?q=GetTempPath
414 # * Go uses GetTempPath().
415 # * GetTempDir() uses %TMP%, then %TEMP%, then other stuff. So %TMP% must be
416 # set.
417 # https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-gettemppathw
418 out['TMP'] = tmp_dir
419 # https://blogs.msdn.microsoft.com/oldnewthing/20150417-00/?p=44213
420 out['TEMP'] = tmp_dir
421 elif sys.platform == 'darwin':
422 # * Chromium uses an hack on macOS before calling into
423 # NSTemporaryDirectory().
424 # https://cs.chromium.org/chromium/src/base/files/file_util_mac.mm?q=GetTempDir
425 # https://developer.apple.com/documentation/foundation/1409211-nstemporarydirectory
426 out['MAC_CHROMIUM_TMPDIR'] = tmp_dir
427 else:
428 # TMPDIR is specified as the POSIX standard envvar for the temp directory.
429 # http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
430 # * mktemp on linux respects $TMPDIR.
431 # * Chromium respects $TMPDIR on linux.
432 # https://cs.chromium.org/chromium/src/base/files/file_util_posix.cc?q=GetTempDir
433 # * Go uses $TMPDIR.
434 # https://go.googlesource.com/go/+/go1.10.3/src/os/file_unix.go#307
435 pass
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500436 return out
vadimsh232f5a82017-01-20 19:23:44 -0800437
438
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +0000439def run_command(
440 command, cwd, env, hard_timeout, grace_period, lower_priority, containment):
maruel6be7f9e2015-10-01 12:25:30 -0700441 """Runs the command.
442
443 Returns:
444 tuple(process exit code, bool if had a hard timeout)
445 """
Marc-Antoine Ruel03c6fd12019-04-30 12:12:55 +0000446 logging.info(
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +0000447 'run_command(%s, %s, %s, %s, %s, %s)',
448 command, cwd, hard_timeout, grace_period, lower_priority, containment)
marueleb5fbee2015-09-17 13:01:36 -0700449
maruel6be7f9e2015-10-01 12:25:30 -0700450 exit_code = None
451 had_hard_timeout = False
maruela9cfd6f2015-09-15 11:03:15 -0700452 with tools.Profiler('RunTest'):
maruel6be7f9e2015-10-01 12:25:30 -0700453 proc = None
454 had_signal = []
maruela9cfd6f2015-09-15 11:03:15 -0700455 try:
maruel6be7f9e2015-10-01 12:25:30 -0700456 # TODO(maruel): This code is imperfect. It doesn't handle well signals
457 # during the download phase and there's short windows were things can go
458 # wrong.
459 def handler(signum, _frame):
460 if proc and not had_signal:
461 logging.info('Received signal %d', signum)
462 had_signal.append(True)
maruel556d9052015-10-05 11:12:44 -0700463 raise subprocess42.TimeoutExpired(command, None)
maruel6be7f9e2015-10-01 12:25:30 -0700464
Marc-Antoine Ruel30b80fe2019-02-08 13:51:31 +0000465 proc = subprocess42.Popen(
Marc-Antoine Ruel03c6fd12019-04-30 12:12:55 +0000466 command, cwd=cwd, env=env, detached=True, close_fds=True,
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +0000467 lower_priority=lower_priority, containment=containment)
maruel6be7f9e2015-10-01 12:25:30 -0700468 with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, handler):
469 try:
John Budorickc398f092019-06-10 22:49:44 +0000470 exit_code = proc.wait(hard_timeout or None)
maruel6be7f9e2015-10-01 12:25:30 -0700471 except subprocess42.TimeoutExpired:
472 if not had_signal:
473 logging.warning('Hard timeout')
474 had_hard_timeout = True
475 logging.warning('Sending SIGTERM')
476 proc.terminate()
477
478 # Ignore signals in grace period. Forcibly give the grace period to the
479 # child process.
480 if exit_code is None:
481 ignore = lambda *_: None
482 with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, ignore):
483 try:
484 exit_code = proc.wait(grace_period or None)
485 except subprocess42.TimeoutExpired:
486 # Now kill for real. The user can distinguish between the
487 # following states:
488 # - signal but process exited within grace period,
489 # hard_timed_out will be set but the process exit code will be
490 # script provided.
491 # - processed exited late, exit code will be -9 on posix.
492 logging.warning('Grace exhausted; sending SIGKILL')
493 proc.kill()
martiniss5c8043e2017-08-01 17:09:43 -0700494 logging.info('Waiting for process exit')
maruel6be7f9e2015-10-01 12:25:30 -0700495 exit_code = proc.wait()
maruela9cfd6f2015-09-15 11:03:15 -0700496 except OSError:
497 # This is not considered to be an internal error. The executable simply
498 # does not exit.
maruela72f46e2016-02-24 11:05:45 -0800499 sys.stderr.write(
tikuta2d678212019-09-23 23:12:08 +0000500 '<The executable does not exist, a dependent library is missing or '
501 'the command line is too long>\n'
502 '<Check for missing .so/.dll in the .isolate or GN file or length of '
503 'command line args>\n'
maruela72f46e2016-02-24 11:05:45 -0800504 '<Command: %s>\n' % command)
505 if os.environ.get('SWARMING_TASK_ID'):
506 # Give an additional hint when running as a swarming task.
507 sys.stderr.write(
508 '<See the task\'s page for commands to help diagnose this issue '
509 'by reproducing the task locally>\n')
maruela9cfd6f2015-09-15 11:03:15 -0700510 exit_code = 1
511 logging.info(
512 'Command finished with exit code %d (%s)',
513 exit_code, hex(0xffffffff & exit_code))
maruel6be7f9e2015-10-01 12:25:30 -0700514 return exit_code, had_hard_timeout
maruela9cfd6f2015-09-15 11:03:15 -0700515
516
Takuto Ikuta057c5342019-12-03 04:05:05 +0000517def _fetch_and_map_with_go(isolated_hash, storage, cache, outdir, go_cache_dir,
Takuto Ikutad03ffcc2019-12-02 01:04:23 +0000518 isolated_client):
519 """
520 Fetches an isolated tree using go client, create the tree and returns
521 (bundle, stats).
522 """
523 start = time.time()
524 server_ref = storage.server_ref
525 policies = cache.policies
526 result_json_handle, result_json_path = tempfile.mkstemp(
527 prefix=u'fetch-and-map-result-', suffix=u'.json')
528 os.close(result_json_handle)
529 try:
530 subprocess42.check_call([
531 isolated_client,
532 'download',
533 '-isolate-server',
534 server_ref.url,
535 '-namespace',
536 server_ref.namespace,
537 '-isolated',
538 isolated_hash,
539
540 # flags for cache
541 '-cache-dir',
Takuto Ikuta057c5342019-12-03 04:05:05 +0000542 go_cache_dir,
Takuto Ikutad03ffcc2019-12-02 01:04:23 +0000543 '-cache-max-items',
Takuto Ikuta50bc0552019-12-03 03:26:46 +0000544 str(policies.max_items),
Takuto Ikutad03ffcc2019-12-02 01:04:23 +0000545 '-cache-max-size',
Takuto Ikuta50bc0552019-12-03 03:26:46 +0000546 str(policies.max_cache_size),
Takuto Ikutad03ffcc2019-12-02 01:04:23 +0000547 '-cache-min-free-space',
Takuto Ikuta50bc0552019-12-03 03:26:46 +0000548 str(policies.min_free_space),
Takuto Ikutad03ffcc2019-12-02 01:04:23 +0000549
550 # flags for output
551 '-output-dir',
552 outdir,
553 '-fetch-and-map-result-json',
554 result_json_path,
555 ])
556 with open(result_json_path) as json_file:
557 result_json = json.load(json_file)
558
559 isolated = result_json['isolated']
560 bundle = isolateserver.IsolatedBundle(filter_cb=None)
561 # Only following properties are used in caller.
562 bundle.command = isolated.get('command')
563 bundle.read_only = isolated.get('read_only')
564 bundle.relative_cwd = isolated.get('relative_cwd')
565
566 return bundle, {
567 'duration': time.time() - start,
568 'items_cold': result_json['items_cold'],
569 'items_hot': result_json['items_hot'],
570 }
571 finally:
572 fs.remove(result_json_path)
573
574
575# TODO(crbug.com/932396): remove this function.
maruel4409e302016-07-19 14:25:51 -0700576def fetch_and_map(isolated_hash, storage, cache, outdir, use_symlinks):
577 """Fetches an isolated tree, create the tree and returns (bundle, stats)."""
nodir6f801882016-04-29 14:41:50 -0700578 start = time.time()
579 bundle = isolateserver.fetch_isolated(
580 isolated_hash=isolated_hash,
581 storage=storage,
582 cache=cache,
maruel4409e302016-07-19 14:25:51 -0700583 outdir=outdir,
584 use_symlinks=use_symlinks)
Takuto Ikuta2b9640e2019-06-19 00:53:23 +0000585 hot = (collections.Counter(cache.used) -
586 collections.Counter(cache.added)).elements()
nodir6f801882016-04-29 14:41:50 -0700587 return bundle, {
588 'duration': time.time() - start,
nodir6f801882016-04-29 14:41:50 -0700589 'items_cold': base64.b64encode(large.pack(sorted(cache.added))),
Takuto Ikuta2b9640e2019-06-19 00:53:23 +0000590 'items_hot': base64.b64encode(large.pack(sorted(hot))),
nodir6f801882016-04-29 14:41:50 -0700591 }
592
593
aludwin0a8e17d2016-10-27 15:57:39 -0700594def link_outputs_to_outdir(run_dir, out_dir, outputs):
595 """Links any named outputs to out_dir so they can be uploaded.
596
597 Raises an error if the file already exists in that directory.
598 """
599 if not outputs:
600 return
601 isolateserver.create_directories(out_dir, outputs)
602 for o in outputs:
Sadaf Matinkhoo10743a62018-03-29 16:28:58 -0400603 copy_recursively(os.path.join(run_dir, o), os.path.join(out_dir, o))
604
605
606def copy_recursively(src, dst):
607 """Efficiently copies a file or directory from src_dir to dst_dir.
608
609 `item` may be a file, directory, or a symlink to a file or directory.
610 All symlinks are replaced with their targets, so the resulting
611 directory structure in dst_dir will never have any symlinks.
612
613 To increase speed, copy_recursively hardlinks individual files into the
614 (newly created) directory structure if possible, unlike Python's
615 shutil.copytree().
616 """
617 orig_src = src
618 try:
619 # Replace symlinks with their final target.
620 while fs.islink(src):
621 res = fs.readlink(src)
622 src = os.path.join(os.path.dirname(src), res)
623 # TODO(sadafm): Explicitly handle cyclic symlinks.
624
625 # Note that fs.isfile (which is a wrapper around os.path.isfile) throws
626 # an exception if src does not exist. A warning will be logged in that case.
627 if fs.isfile(src):
628 file_path.link_file(dst, src, file_path.HARDLINK_WITH_FALLBACK)
629 return
630
631 if not fs.exists(dst):
632 os.makedirs(dst)
633
634 for child in fs.listdir(src):
635 copy_recursively(os.path.join(src, child), os.path.join(dst, child))
636
637 except OSError as e:
638 if e.errno == errno.ENOENT:
639 logging.warning('Path %s does not exist or %s is a broken symlink',
640 src, orig_src)
641 else:
642 logging.info("Couldn't collect output file %s: %s", src, e)
aludwin0a8e17d2016-10-27 15:57:39 -0700643
644
Takuto Ikutab4aa8662019-09-17 05:54:36 +0000645def upload_then_delete(storage, out_dir, leak_temp_dir):
maruela9cfd6f2015-09-15 11:03:15 -0700646 """Deletes the temporary run directory and uploads results back.
647
648 Returns:
nodir6f801882016-04-29 14:41:50 -0700649 tuple(outputs_ref, success, stats)
maruel064c0a32016-04-05 11:47:15 -0700650 - outputs_ref: a dict referring to the results archived back to the isolated
651 server, if applicable.
652 - success: False if something occurred that means that the task must
653 forcibly be considered a failure, e.g. zombie processes were left
654 behind.
nodir6f801882016-04-29 14:41:50 -0700655 - stats: uploading stats.
maruela9cfd6f2015-09-15 11:03:15 -0700656 """
maruela9cfd6f2015-09-15 11:03:15 -0700657 # Upload out_dir and generate a .isolated file out of this directory. It is
658 # only done if files were written in the directory.
659 outputs_ref = None
maruel064c0a32016-04-05 11:47:15 -0700660 cold = []
661 hot = []
nodir6f801882016-04-29 14:41:50 -0700662 start = time.time()
663
maruel12e30012015-10-09 11:55:35 -0700664 if fs.isdir(out_dir) and fs.listdir(out_dir):
maruela9cfd6f2015-09-15 11:03:15 -0700665 with tools.Profiler('ArchiveOutput'):
666 try:
maruel064c0a32016-04-05 11:47:15 -0700667 results, f_cold, f_hot = isolateserver.archive_files_to_storage(
maruela9cfd6f2015-09-15 11:03:15 -0700668 storage, [out_dir], None)
669 outputs_ref = {
Marc-Antoine Rueld0868ec2018-11-28 20:47:29 +0000670 'isolated': results.values()[0],
Marc-Antoine Ruelb8513132018-11-20 19:48:53 +0000671 'isolatedserver': storage.server_ref.url,
672 'namespace': storage.server_ref.namespace,
maruela9cfd6f2015-09-15 11:03:15 -0700673 }
maruel064c0a32016-04-05 11:47:15 -0700674 cold = sorted(i.size for i in f_cold)
675 hot = sorted(i.size for i in f_hot)
maruela9cfd6f2015-09-15 11:03:15 -0700676 except isolateserver.Aborted:
677 # This happens when a signal SIGTERM was received while uploading data.
678 # There is 2 causes:
679 # - The task was too slow and was about to be killed anyway due to
680 # exceeding the hard timeout.
681 # - The amount of data uploaded back is very large and took too much
682 # time to archive.
683 sys.stderr.write('Received SIGTERM while uploading')
684 # Re-raise, so it will be treated as an internal failure.
685 raise
nodir6f801882016-04-29 14:41:50 -0700686
687 success = False
maruela9cfd6f2015-09-15 11:03:15 -0700688 try:
maruel12e30012015-10-09 11:55:35 -0700689 if (not leak_temp_dir and fs.isdir(out_dir) and
maruel6eeea7d2015-09-16 12:17:42 -0700690 not file_path.rmtree(out_dir)):
maruela9cfd6f2015-09-15 11:03:15 -0700691 logging.error('Had difficulties removing out_dir %s', out_dir)
nodir6f801882016-04-29 14:41:50 -0700692 else:
693 success = True
maruela9cfd6f2015-09-15 11:03:15 -0700694 except OSError as e:
695 # When this happens, it means there's a process error.
maruel12e30012015-10-09 11:55:35 -0700696 logging.exception('Had difficulties removing out_dir %s: %s', out_dir, e)
nodir6f801882016-04-29 14:41:50 -0700697 stats = {
698 'duration': time.time() - start,
699 'items_cold': base64.b64encode(large.pack(cold)),
700 'items_hot': base64.b64encode(large.pack(hot)),
701 }
702 return outputs_ref, success, stats
maruela9cfd6f2015-09-15 11:03:15 -0700703
704
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500705def map_and_run(data, constant_run_path):
nodir55be77b2016-05-03 09:39:57 -0700706 """Runs a command with optional isolated input/output.
707
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500708 Arguments:
709 - data: TaskData instance.
710 - constant_run_path: TODO
nodir55be77b2016-05-03 09:39:57 -0700711
712 Returns metadata about the result.
713 """
maruela9cfd6f2015-09-15 11:03:15 -0700714 result = {
Takuto Ikuta5ed62ad2019-09-26 09:16:00 +0000715 'duration': None,
716 'exit_code': None,
717 'had_hard_timeout': False,
718 'internal_failure': 'run_isolated did not complete properly',
719 'stats': {
720 #'cipd': {
721 # 'duration': 0.,
722 # 'get_client_duration': 0.,
723 #},
724 'isolated': {
725 'download': {
726 #'duration': 0.,
727 'initial_number_items': len(data.isolate_cache),
728 'initial_size': data.isolate_cache.total_size,
729 #'items_cold': '<large.pack()>',
730 #'items_hot': '<large.pack()>',
731 },
732 #'upload': {
733 # 'duration': 0.,
734 # 'items_cold': '<large.pack()>',
735 # 'items_hot': '<large.pack()>',
736 #},
737 },
Marc-Antoine Ruel5d7606b2018-06-15 19:06:12 +0000738 },
Takuto Ikuta5ed62ad2019-09-26 09:16:00 +0000739 #'cipd_pins': {
740 # 'packages': [
741 # {'package_name': ..., 'version': ..., 'path': ...},
742 # ...
743 # ],
744 # 'client_package': {'package_name': ..., 'version': ...},
745 #},
746 'outputs_ref': None,
747 'version': 5,
maruela9cfd6f2015-09-15 11:03:15 -0700748 }
nodirbe642ff2016-06-09 15:51:51 -0700749
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500750 if data.root_dir:
Lei Leife202df2019-06-11 17:33:34 +0000751 file_path.ensure_tree(data.root_dir, 0o700)
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500752 elif data.isolate_cache.cache_dir:
753 data = data._replace(
754 root_dir=os.path.dirname(data.isolate_cache.cache_dir))
maruele2f2cb82016-07-13 14:41:03 -0700755 # See comment for these constants.
maruelcffa0542017-04-07 08:39:20 -0700756 # If root_dir is not specified, it is not constant.
757 # TODO(maruel): This is not obvious. Change this to become an error once we
758 # make the constant_run_path an exposed flag.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500759 if constant_run_path and data.root_dir:
760 run_dir = os.path.join(data.root_dir, ISOLATED_RUN_DIR)
maruel5c4eed82017-05-26 05:33:40 -0700761 if os.path.isdir(run_dir):
762 file_path.rmtree(run_dir)
Lei Leife202df2019-06-11 17:33:34 +0000763 os.mkdir(run_dir, 0o700)
maruelcffa0542017-04-07 08:39:20 -0700764 else:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500765 run_dir = make_temp_dir(ISOLATED_RUN_DIR, data.root_dir)
maruel03e11842016-07-14 10:50:16 -0700766 # storage should be normally set but don't crash if it is not. This can happen
767 # as Swarming task can run without an isolate server.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500768 out_dir = make_temp_dir(
769 ISOLATED_OUT_DIR, data.root_dir) if data.storage else None
770 tmp_dir = make_temp_dir(ISOLATED_TMP_DIR, data.root_dir)
Takuto Ikutab7ce0e32019-11-27 23:26:18 +0000771 isolated_client_dir = make_temp_dir(ISOLATED_CLIENT_DIR, data.root_dir)
nodir55be77b2016-05-03 09:39:57 -0700772 cwd = run_dir
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -0500773 if data.relative_cwd:
774 cwd = os.path.normpath(os.path.join(cwd, data.relative_cwd))
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500775 command = data.command
nodir55be77b2016-05-03 09:39:57 -0700776 try:
Takuto Ikutab7ce0e32019-11-27 23:26:18 +0000777 with data.install_packages_fn(run_dir, isolated_client_dir) as cipd_info:
vadimsh232f5a82017-01-20 19:23:44 -0800778 if cipd_info:
779 result['stats']['cipd'] = cipd_info.stats
780 result['cipd_pins'] = cipd_info.pins
nodir90bc8dc2016-06-15 13:35:21 -0700781
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500782 if data.isolated_hash:
vadimsh232f5a82017-01-20 19:23:44 -0800783 isolated_stats = result['stats'].setdefault('isolated', {})
Takuto Ikutad03ffcc2019-12-02 01:04:23 +0000784 if data.use_go_isolated:
785 bundle, stats = _fetch_and_map_with_go(
786 isolated_hash=data.isolated_hash,
787 storage=data.storage,
788 cache=data.isolate_cache,
789 outdir=run_dir,
Takuto Ikuta057c5342019-12-03 04:05:05 +0000790 go_cache_dir=data.go_cache_dir,
Takuto Ikutad03ffcc2019-12-02 01:04:23 +0000791 isolated_client=os.path.join(isolated_client_dir,
792 'isolated' + cipd.EXECUTABLE_SUFFIX))
793 else:
794 bundle, stats = fetch_and_map(
795 isolated_hash=data.isolated_hash,
796 storage=data.storage,
797 cache=data.isolate_cache,
798 outdir=run_dir,
799 use_symlinks=data.use_symlinks)
Marc-Antoine Ruel5d7606b2018-06-15 19:06:12 +0000800 isolated_stats['download'].update(stats)
vadimsh232f5a82017-01-20 19:23:44 -0800801 change_tree_read_only(run_dir, bundle.read_only)
maruelabec63c2017-04-26 11:53:24 -0700802 # Inject the command
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500803 if not command and bundle.command:
804 command = bundle.command + data.extra_args
Marc-Antoine Rueld704a1f2017-10-31 10:51:23 -0400805 # Only set the relative directory if the isolated file specified a
806 # command, and no raw command was specified.
807 if bundle.relative_cwd:
808 cwd = os.path.normpath(os.path.join(cwd, bundle.relative_cwd))
maruelabec63c2017-04-26 11:53:24 -0700809
810 if not command:
811 # Handle this as a task failure, not an internal failure.
812 sys.stderr.write(
813 '<No command was specified!>\n'
814 '<Please secify a command when triggering your Swarming task>\n')
815 result['exit_code'] = 1
816 return result
nodirbe642ff2016-06-09 15:51:51 -0700817
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -0500818 if not cwd.startswith(run_dir):
819 # Handle this as a task failure, not an internal failure. This is a
820 # 'last chance' way to gate against directory escape.
821 sys.stderr.write('<Relative CWD is outside of run directory!>\n')
822 result['exit_code'] = 1
823 return result
824
825 if not os.path.isdir(cwd):
826 # Accepts relative_cwd that does not exist.
Lei Leife202df2019-06-11 17:33:34 +0000827 os.makedirs(cwd, 0o700)
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -0500828
vadimsh232f5a82017-01-20 19:23:44 -0800829 # If we have an explicit list of files to return, make sure their
830 # directories exist now.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500831 if data.storage and data.outputs:
832 isolateserver.create_directories(run_dir, data.outputs)
aludwin0a8e17d2016-10-27 15:57:39 -0700833
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500834 with data.install_named_caches(run_dir):
nodird6160682017-02-02 13:03:35 -0800835 sys.stdout.flush()
836 start = time.time()
837 try:
vadimsh9c54b2c2017-07-25 14:08:29 -0700838 # Need to switch the default account before 'get_command_env' call,
839 # so it can grab correct value of LUCI_CONTEXT env var.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500840 with set_luci_context_account(data.switch_to_account, tmp_dir):
841 env = get_command_env(
Roberto Carrillo71ade6d2018-10-08 22:30:24 +0000842 tmp_dir, cipd_info, run_dir, data.env, data.env_prefix, out_dir,
843 data.bot_file)
Brian Sheedy7a761172019-08-30 22:55:14 +0000844 command = tools.find_executable(command, env)
Robert Iannucci24ae76a2018-02-26 12:51:18 -0800845 command = process_command(command, out_dir, data.bot_file)
846 file_path.ensure_command_has_abs_path(command, cwd)
847
vadimsh9c54b2c2017-07-25 14:08:29 -0700848 result['exit_code'], result['had_hard_timeout'] = run_command(
Marc-Antoine Ruel03c6fd12019-04-30 12:12:55 +0000849 command, cwd, env, data.hard_timeout, data.grace_period,
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +0000850 data.lower_priority, data.containment)
nodird6160682017-02-02 13:03:35 -0800851 finally:
852 result['duration'] = max(time.time() - start, 0)
Seth Koehler49139812017-12-19 13:59:33 -0500853
854 # We successfully ran the command, set internal_failure back to
855 # None (even if the command failed, it's not an internal error).
856 result['internal_failure'] = None
maruela9cfd6f2015-09-15 11:03:15 -0700857 except Exception as e:
nodir90bc8dc2016-06-15 13:35:21 -0700858 # An internal error occurred. Report accordingly so the swarming task will
859 # be retried automatically.
maruel12e30012015-10-09 11:55:35 -0700860 logging.exception('internal failure: %s', e)
maruela9cfd6f2015-09-15 11:03:15 -0700861 result['internal_failure'] = str(e)
862 on_error.report(None)
aludwin0a8e17d2016-10-27 15:57:39 -0700863
864 # Clean up
maruela9cfd6f2015-09-15 11:03:15 -0700865 finally:
866 try:
aludwin0a8e17d2016-10-27 15:57:39 -0700867 # Try to link files to the output directory, if specified.
868 if out_dir:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500869 link_outputs_to_outdir(run_dir, out_dir, data.outputs)
aludwin0a8e17d2016-10-27 15:57:39 -0700870
nodir32a1ec12016-10-26 18:34:07 -0700871 success = False
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500872 if data.leak_temp_dir:
nodir32a1ec12016-10-26 18:34:07 -0700873 success = True
maruela9cfd6f2015-09-15 11:03:15 -0700874 logging.warning(
875 'Deliberately leaking %s for later examination', run_dir)
marueleb5fbee2015-09-17 13:01:36 -0700876 else:
maruel84537cb2015-10-16 14:21:28 -0700877 # On Windows rmtree(run_dir) call above has a synchronization effect: it
878 # finishes only when all task child processes terminate (since a running
879 # process locks *.exe file). Examine out_dir only after that call
880 # completes (since child processes may write to out_dir too and we need
881 # to wait for them to finish).
Takuto Ikutab7ce0e32019-11-27 23:26:18 +0000882 for directory in (run_dir, tmp_dir, isolated_client_dir):
Takuto Ikuta69c0d662019-11-27 01:18:08 +0000883 if not fs.isdir(directory):
884 continue
maruel84537cb2015-10-16 14:21:28 -0700885 try:
Takuto Ikuta69c0d662019-11-27 01:18:08 +0000886 success = file_path.rmtree(directory)
maruel84537cb2015-10-16 14:21:28 -0700887 except OSError as e:
Takuto Ikuta69c0d662019-11-27 01:18:08 +0000888 logging.error('rmtree(%r) failed: %s', directory, e)
maruel84537cb2015-10-16 14:21:28 -0700889 success = False
890 if not success:
Takuto Ikuta69c0d662019-11-27 01:18:08 +0000891 sys.stderr.write(
892 OUTLIVING_ZOMBIE_MSG % (directory, data.grace_period))
maruel84537cb2015-10-16 14:21:28 -0700893 if result['exit_code'] == 0:
894 result['exit_code'] = 1
maruela9cfd6f2015-09-15 11:03:15 -0700895
marueleb5fbee2015-09-17 13:01:36 -0700896 # This deletes out_dir if leak_temp_dir is not set.
nodir9130f072016-05-27 13:59:08 -0700897 if out_dir:
nodir55715712016-06-03 12:28:19 -0700898 isolated_stats = result['stats'].setdefault('isolated', {})
899 result['outputs_ref'], success, isolated_stats['upload'] = (
Takuto Ikutab4aa8662019-09-17 05:54:36 +0000900 upload_then_delete(data.storage, out_dir, data.leak_temp_dir))
maruela9cfd6f2015-09-15 11:03:15 -0700901 if not success and result['exit_code'] == 0:
902 result['exit_code'] = 1
903 except Exception as e:
904 # Swallow any exception in the main finally clause.
nodir9130f072016-05-27 13:59:08 -0700905 if out_dir:
906 logging.exception('Leaking out_dir %s: %s', out_dir, e)
maruela9cfd6f2015-09-15 11:03:15 -0700907 result['internal_failure'] = str(e)
908 return result
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500909
910
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500911def run_tha_test(data, result_json):
nodir55be77b2016-05-03 09:39:57 -0700912 """Runs an executable and records execution metadata.
913
nodir55be77b2016-05-03 09:39:57 -0700914 If isolated_hash is specified, downloads the dependencies in the cache,
915 hardlinks them into a temporary directory and runs the command specified in
916 the .isolated.
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500917
918 A temporary directory is created to hold the output files. The content inside
919 this directory will be uploaded back to |storage| packaged as a .isolated
920 file.
921
922 Arguments:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500923 - data: TaskData instance.
924 - result_json: File path to dump result metadata into. If set, the process
925 exit code is always 0 unless an internal error occurred.
maruela9cfd6f2015-09-15 11:03:15 -0700926
927 Returns:
928 Process exit code that should be used.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000929 """
maruela76b9ee2015-12-15 06:18:08 -0800930 if result_json:
931 # Write a json output file right away in case we get killed.
932 result = {
933 'exit_code': None,
934 'had_hard_timeout': False,
935 'internal_failure': 'Was terminated before completion',
936 'outputs_ref': None,
nodirbe642ff2016-06-09 15:51:51 -0700937 'version': 5,
maruela76b9ee2015-12-15 06:18:08 -0800938 }
939 tools.write_json(result_json, result, dense=True)
940
maruela9cfd6f2015-09-15 11:03:15 -0700941 # run_isolated exit code. Depends on if result_json is used or not.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500942 result = map_and_run(data, True)
maruela9cfd6f2015-09-15 11:03:15 -0700943 logging.info('Result:\n%s', tools.format_json(result, dense=True))
bpastene3ae09522016-06-10 17:12:59 -0700944
maruela9cfd6f2015-09-15 11:03:15 -0700945 if result_json:
maruel05d5a882015-09-21 13:59:02 -0700946 # We've found tests to delete 'work' when quitting, causing an exception
947 # here. Try to recreate the directory if necessary.
nodire5028a92016-04-29 14:38:21 -0700948 file_path.ensure_tree(os.path.dirname(result_json))
maruela9cfd6f2015-09-15 11:03:15 -0700949 tools.write_json(result_json, result, dense=True)
950 # Only return 1 if there was an internal error.
951 return int(bool(result['internal_failure']))
maruel@chromium.org781ccf62013-09-17 19:39:47 +0000952
maruela9cfd6f2015-09-15 11:03:15 -0700953 # Marshall into old-style inline output.
954 if result['outputs_ref']:
Marc-Antoine Ruel793bff32019-04-18 17:50:48 +0000955 # pylint: disable=unsubscriptable-object
maruela9cfd6f2015-09-15 11:03:15 -0700956 data = {
957 'hash': result['outputs_ref']['isolated'],
958 'namespace': result['outputs_ref']['namespace'],
959 'storage': result['outputs_ref']['isolatedserver'],
960 }
Marc-Antoine Ruelc44f5722015-01-08 16:10:01 -0500961 sys.stdout.flush()
maruela9cfd6f2015-09-15 11:03:15 -0700962 print(
963 '[run_isolated_out_hack]%s[/run_isolated_out_hack]' %
964 tools.format_json(data, dense=True))
maruelb76604c2015-11-11 11:53:44 -0800965 sys.stdout.flush()
maruela9cfd6f2015-09-15 11:03:15 -0700966 return result['exit_code'] or int(bool(result['internal_failure']))
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000967
968
iannuccib58d10d2017-03-18 02:00:25 -0700969# Yielded by 'install_client_and_packages'.
vadimsh232f5a82017-01-20 19:23:44 -0800970CipdInfo = collections.namedtuple('CipdInfo', [
971 'client', # cipd.CipdClient object
972 'cache_dir', # absolute path to bot-global cipd tag and instance cache
973 'stats', # dict with stats to return to the server
974 'pins', # dict with installed cipd pins to return to the server
975])
976
977
978@contextlib.contextmanager
Takuto Ikutab7ce0e32019-11-27 23:26:18 +0000979def noop_install_packages(_run_dir, _isolated_dir):
iannuccib58d10d2017-03-18 02:00:25 -0700980 """Placeholder for 'install_client_and_packages' if cipd is disabled."""
vadimsh232f5a82017-01-20 19:23:44 -0800981 yield None
982
983
Takuto Ikuta2efc7792019-11-27 14:33:34 +0000984def _install_packages(run_dir, cipd_cache_dir, client, packages):
iannuccib58d10d2017-03-18 02:00:25 -0700985 """Calls 'cipd ensure' for packages.
986
987 Args:
988 run_dir (str): root of installation.
989 cipd_cache_dir (str): the directory to use for the cipd package cache.
990 client (CipdClient): the cipd client to use
991 packages: packages to install, list [(path, package_name, version), ...].
iannuccib58d10d2017-03-18 02:00:25 -0700992
993 Returns: list of pinned packages. Looks like [
994 {
995 'path': 'subdirectory',
996 'package_name': 'resolved/package/name',
997 'version': 'deadbeef...',
998 },
999 ...
1000 ]
1001 """
1002 package_pins = [None]*len(packages)
1003 def insert_pin(path, name, version, idx):
1004 package_pins[idx] = {
1005 'package_name': name,
1006 # swarming deals with 'root' as '.'
1007 'path': path or '.',
1008 'version': version,
1009 }
1010
1011 by_path = collections.defaultdict(list)
1012 for i, (path, name, version) in enumerate(packages):
1013 # cipd deals with 'root' as ''
1014 if path == '.':
1015 path = ''
1016 by_path[path].append((name, version, i))
1017
1018 pins = client.ensure(
Takuto Ikuta2efc7792019-11-27 14:33:34 +00001019 run_dir,
1020 {
1021 subdir: [(name, vers) for name, vers, _ in pkgs
1022 ] for subdir, pkgs in by_path.items()
1023 },
1024 cache_dir=cipd_cache_dir,
iannuccib58d10d2017-03-18 02:00:25 -07001025 )
1026
Marc-Antoine Ruel04903a32019-10-09 21:09:25 +00001027 for subdir, pin_list in sorted(pins.items()):
iannuccib58d10d2017-03-18 02:00:25 -07001028 this_subdir = by_path[subdir]
1029 for i, (name, version) in enumerate(pin_list):
1030 insert_pin(subdir, name, version, this_subdir[i][2])
1031
Robert Iannucci461b30d2017-12-13 11:34:03 -08001032 assert None not in package_pins, (packages, pins, package_pins)
iannuccib58d10d2017-03-18 02:00:25 -07001033
1034 return package_pins
1035
1036
vadimsh232f5a82017-01-20 19:23:44 -08001037@contextlib.contextmanager
Takuto Ikuta2efc7792019-11-27 14:33:34 +00001038def install_client_and_packages(run_dir, packages, service_url,
Takuto Ikutab7ce0e32019-11-27 23:26:18 +00001039 client_package_name, client_version, cache_dir,
1040 isolated_dir):
vadimsh902948e2017-01-20 15:57:32 -08001041 """Bootstraps CIPD client and installs CIPD packages.
iannucci96fcccc2016-08-30 15:52:22 -07001042
vadimsh232f5a82017-01-20 19:23:44 -08001043 Yields CipdClient, stats, client info and pins (as single CipdInfo object).
1044
1045 Pins and the CIPD client info are in the form of:
iannucci96fcccc2016-08-30 15:52:22 -07001046 [
1047 {
1048 "path": path, "package_name": package_name, "version": version,
1049 },
1050 ...
1051 ]
vadimsh902948e2017-01-20 15:57:32 -08001052 (the CIPD client info is a single dictionary instead of a list)
iannucci96fcccc2016-08-30 15:52:22 -07001053
1054 such that they correspond 1:1 to all input package arguments from the command
1055 line. These dictionaries make their all the way back to swarming, where they
1056 become the arguments of CipdPackage.
nodirbe642ff2016-06-09 15:51:51 -07001057
vadimsh902948e2017-01-20 15:57:32 -08001058 If 'packages' list is empty, will bootstrap CIPD client, but won't install
1059 any packages.
1060
1061 The bootstrapped client (regardless whether 'packages' list is empty or not),
vadimsh232f5a82017-01-20 19:23:44 -08001062 will be made available to the task via $PATH.
vadimsh902948e2017-01-20 15:57:32 -08001063
nodirbe642ff2016-06-09 15:51:51 -07001064 Args:
nodir90bc8dc2016-06-15 13:35:21 -07001065 run_dir (str): root of installation.
vadimsh902948e2017-01-20 15:57:32 -08001066 packages: packages to install, list [(path, package_name, version), ...].
nodirbe642ff2016-06-09 15:51:51 -07001067 service_url (str): CIPD server url, e.g.
1068 "https://chrome-infra-packages.appspot.com."
nodir90bc8dc2016-06-15 13:35:21 -07001069 client_package_name (str): CIPD package name of CIPD client.
1070 client_version (str): Version of CIPD client.
nodirbe642ff2016-06-09 15:51:51 -07001071 cache_dir (str): where to keep cache of cipd clients, packages and tags.
Takuto Ikutab7ce0e32019-11-27 23:26:18 +00001072 isolated_dir (str): where to download isolated client.
nodirbe642ff2016-06-09 15:51:51 -07001073 """
1074 assert cache_dir
nodir90bc8dc2016-06-15 13:35:21 -07001075
nodirbe642ff2016-06-09 15:51:51 -07001076 start = time.time()
nodirbe642ff2016-06-09 15:51:51 -07001077
vadimsh902948e2017-01-20 15:57:32 -08001078 cache_dir = os.path.abspath(cache_dir)
vadimsh232f5a82017-01-20 19:23:44 -08001079 cipd_cache_dir = os.path.join(cache_dir, 'cache') # tag and instance caches
nodir90bc8dc2016-06-15 13:35:21 -07001080 run_dir = os.path.abspath(run_dir)
vadimsh902948e2017-01-20 15:57:32 -08001081 packages = packages or []
nodir90bc8dc2016-06-15 13:35:21 -07001082
nodirbe642ff2016-06-09 15:51:51 -07001083 get_client_start = time.time()
Takuto Ikuta2efc7792019-11-27 14:33:34 +00001084 client_manager = cipd.get_client(service_url, client_package_name,
1085 client_version, cache_dir)
iannucci96fcccc2016-08-30 15:52:22 -07001086
nodirbe642ff2016-06-09 15:51:51 -07001087 with client_manager as client:
1088 get_client_duration = time.time() - get_client_start
nodir90bc8dc2016-06-15 13:35:21 -07001089
iannuccib58d10d2017-03-18 02:00:25 -07001090 package_pins = []
1091 if packages:
Takuto Ikuta2efc7792019-11-27 14:33:34 +00001092 package_pins = _install_packages(run_dir, cipd_cache_dir, client,
1093 packages)
iannuccib58d10d2017-03-18 02:00:25 -07001094
Takuto Ikutab7ce0e32019-11-27 23:26:18 +00001095 # Install isolated client to |isolated_dir|.
Takuto Ikuta02edca22019-11-29 10:04:51 +00001096 _install_packages(isolated_dir, cipd_cache_dir, client,
1097 [('', ISOLATED_PACKAGE, ISOLATED_REVISION)])
Takuto Ikutab7ce0e32019-11-27 23:26:18 +00001098
iannuccib58d10d2017-03-18 02:00:25 -07001099 file_path.make_tree_files_read_only(run_dir)
nodir90bc8dc2016-06-15 13:35:21 -07001100
vadimsh232f5a82017-01-20 19:23:44 -08001101 total_duration = time.time() - start
1102 logging.info(
1103 'Installing CIPD client and packages took %d seconds', total_duration)
nodir90bc8dc2016-06-15 13:35:21 -07001104
vadimsh232f5a82017-01-20 19:23:44 -08001105 yield CipdInfo(
1106 client=client,
1107 cache_dir=cipd_cache_dir,
1108 stats={
1109 'duration': total_duration,
1110 'get_client_duration': get_client_duration,
1111 },
1112 pins={
iannuccib58d10d2017-03-18 02:00:25 -07001113 'client_package': {
1114 'package_name': client.package_name,
1115 'version': client.instance_id,
1116 },
vadimsh232f5a82017-01-20 19:23:44 -08001117 'packages': package_pins,
1118 })
nodirbe642ff2016-06-09 15:51:51 -07001119
1120
1121def create_option_parser():
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -04001122 parser = logging_utils.OptionParserWithLogging(
nodir55be77b2016-05-03 09:39:57 -07001123 usage='%prog <options> [command to run or extra args]',
maruel@chromium.orgdedbf492013-09-12 20:42:11 +00001124 version=__version__,
1125 log_file=RUN_ISOLATED_LOG_FILE)
maruela9cfd6f2015-09-15 11:03:15 -07001126 parser.add_option(
maruel36a963d2016-04-08 17:15:49 -07001127 '--clean', action='store_true',
1128 help='Cleans the cache, trimming it necessary and remove corrupted items '
1129 'and returns without executing anything; use with -v to know what '
1130 'was done')
1131 parser.add_option(
maruel4409e302016-07-19 14:25:51 -07001132 '--use-symlinks', action='store_true',
1133 help='Use symlinks instead of hardlinks')
1134 parser.add_option(
Takuto Ikutad03ffcc2019-12-02 01:04:23 +00001135 '--use-go-isolated',
1136 action='store_true',
1137 help='Use go isolated instead of python implementation')
1138 parser.add_option(
Takuto Ikuta057c5342019-12-03 04:05:05 +00001139 '--go-cache-dir', help='Cache directory used for go isolated client')
1140 parser.add_option(
maruela9cfd6f2015-09-15 11:03:15 -07001141 '--json',
1142 help='dump output metadata to json file. When used, run_isolated returns '
1143 'non-zero only on internal failure')
maruel6be7f9e2015-10-01 12:25:30 -07001144 parser.add_option(
maruel5c9e47b2015-12-18 13:02:30 -08001145 '--hard-timeout', type='float', help='Enforce hard timeout in execution')
maruel6be7f9e2015-10-01 12:25:30 -07001146 parser.add_option(
maruel5c9e47b2015-12-18 13:02:30 -08001147 '--grace-period', type='float',
maruel6be7f9e2015-10-01 12:25:30 -07001148 help='Grace period between SIGTERM and SIGKILL')
bpastene3ae09522016-06-10 17:12:59 -07001149 parser.add_option(
Marc-Antoine Ruel49e347d2017-10-24 16:52:02 -07001150 '--raw-cmd', action='store_true',
1151 help='Ignore the isolated command, use the one supplied at the command '
1152 'line')
1153 parser.add_option(
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -05001154 '--relative-cwd',
1155 help='Ignore the isolated \'relative_cwd\' and use this one instead; '
1156 'requires --raw-cmd')
1157 parser.add_option(
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -05001158 '--env', default=[], action='append',
1159 help='Environment variables to set for the child process')
1160 parser.add_option(
1161 '--env-prefix', default=[], action='append',
Robert Iannuccibf5f84c2017-11-22 12:56:50 -08001162 help='Specify a VAR=./path/fragment to put in the environment variable '
1163 'before executing the command. The path fragment must be relative '
1164 'to the isolated run directory, and must not contain a `..` token. '
1165 'The path will be made absolute and prepended to the indicated '
1166 '$VAR using the OS\'s path separator. Multiple items for the same '
1167 '$VAR will be prepended in order.')
1168 parser.add_option(
bpastene3ae09522016-06-10 17:12:59 -07001169 '--bot-file',
1170 help='Path to a file describing the state of the host. The content is '
1171 'defined by on_before_task() in bot_config.')
aludwin7556e0c2016-10-26 08:46:10 -07001172 parser.add_option(
vadimsh9c54b2c2017-07-25 14:08:29 -07001173 '--switch-to-account',
1174 help='If given, switches LUCI_CONTEXT to given logical service account '
1175 '(e.g. "task" or "system") before launching the isolated process.')
1176 parser.add_option(
aludwin0a8e17d2016-10-27 15:57:39 -07001177 '--output', action='append',
1178 help='Specifies an output to return. If no outputs are specified, all '
1179 'files located in $(ISOLATED_OUTDIR) will be returned; '
1180 'otherwise, outputs in both $(ISOLATED_OUTDIR) and those '
1181 'specified by --output option (there can be multiple) will be '
1182 'returned. Note that if a file in OUT_DIR has the same path '
1183 'as an --output option, the --output version will be returned.')
1184 parser.add_option(
aludwin7556e0c2016-10-26 08:46:10 -07001185 '-a', '--argsfile',
1186 # This is actually handled in parse_args; it's included here purely so it
1187 # can make it into the help text.
1188 help='Specify a file containing a JSON array of arguments to this '
1189 'script. If --argsfile is provided, no other argument may be '
1190 'provided on the command line.')
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +00001191
1192 group = optparse.OptionGroup(parser, 'Data source')
1193 group.add_option(
Marc-Antoine Ruel185ded42015-01-28 20:49:18 -05001194 '-s', '--isolated',
nodir55be77b2016-05-03 09:39:57 -07001195 help='Hash of the .isolated to grab from the isolate server.')
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +00001196 isolateserver.add_isolate_server_options(group)
1197 parser.add_option_group(group)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001198
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04001199 isolateserver.add_cache_options(parser)
nodirbe642ff2016-06-09 15:51:51 -07001200
1201 cipd.add_cipd_options(parser)
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001202
1203 group = optparse.OptionGroup(parser, 'Named caches')
1204 group.add_option(
1205 '--named-cache',
1206 dest='named_caches',
1207 action='append',
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001208 nargs=3,
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001209 default=[],
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001210 help='A named cache to request. Accepts 3 arguments: name, path, hint. '
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001211 'name identifies the cache, must match regex [a-z0-9_]{1,4096}. '
1212 'path is a path relative to the run dir where the cache directory '
1213 'must be put to. '
1214 'This option can be specified more than once.')
1215 group.add_option(
1216 '--named-cache-root', default='named_caches',
1217 help='Cache root directory. Default=%default')
1218 parser.add_option_group(group)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001219
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +00001220 group = optparse.OptionGroup(parser, 'Process containment')
1221 parser.add_option(
1222 '--lower-priority', action='store_true',
1223 help='Lowers the child process priority')
1224 parser.add_option(
1225 '--containment-type', choices=('NONE', 'AUTO', 'JOB_OBJECT'),
1226 default='NONE',
1227 help='Type of container to use')
1228 parser.add_option(
1229 '--limit-processes', type='int', default=0,
1230 help='Maximum number of active processes in the containment')
1231 parser.add_option(
1232 '--limit-total-committed-memory', type='int', default=0,
1233 help='Maximum sum of committed memory in the containment')
1234 parser.add_option_group(group)
1235
1236 group = optparse.OptionGroup(parser, 'Debugging')
1237 group.add_option(
Kenneth Russell61d42352014-09-15 11:41:16 -07001238 '--leak-temp-dir',
1239 action='store_true',
nodirbe642ff2016-06-09 15:51:51 -07001240 help='Deliberately leak isolate\'s temp dir for later examination. '
1241 'Default: %default')
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +00001242 group.add_option(
marueleb5fbee2015-09-17 13:01:36 -07001243 '--root-dir', help='Use a directory instead of a random one')
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +00001244 parser.add_option_group(group)
Kenneth Russell61d42352014-09-15 11:41:16 -07001245
Vadim Shtayurae34e13a2014-02-02 11:23:26 -08001246 auth.add_auth_options(parser)
nodirbe642ff2016-06-09 15:51:51 -07001247
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001248 parser.set_defaults(cache='cache', cipd_cache='cipd_cache')
nodirbe642ff2016-06-09 15:51:51 -07001249 return parser
1250
1251
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001252def process_named_cache_options(parser, options, time_fn=None):
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001253 """Validates named cache options and returns a CacheManager."""
1254 if options.named_caches and not options.named_cache_root:
1255 parser.error('--named-cache is specified, but --named-cache-root is empty')
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001256 for name, path, hint in options.named_caches:
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001257 if not CACHE_NAME_RE.match(name):
1258 parser.error(
1259 'cache name %r does not match %r' % (name, CACHE_NAME_RE.pattern))
1260 if not path:
1261 parser.error('cache path cannot be empty')
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001262 try:
1263 long(hint)
1264 except ValueError:
1265 parser.error('cache hint must be a number')
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001266 if options.named_cache_root:
1267 # Make these configurable later if there is use case but for now it's fairly
1268 # safe values.
1269 # In practice, a fair chunk of bots are already recycled on a daily schedule
1270 # so this code doesn't have any effect to them, unless they are preloaded
1271 # with a really old cache.
1272 policies = local_caching.CachePolicies(
1273 # 1TiB.
1274 max_cache_size=1024*1024*1024*1024,
1275 min_free_space=options.min_free_space,
1276 max_items=50,
Marc-Antoine Ruel5d7606b2018-06-15 19:06:12 +00001277 max_age_secs=MAX_AGE_SECS)
Takuto Ikuta6e2ff962019-10-29 12:35:27 +00001278 root_dir = six.text_type(os.path.abspath(options.named_cache_root))
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001279 return local_caching.NamedCache(root_dir, policies, time_fn=time_fn)
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001280 return None
1281
1282
aludwin7556e0c2016-10-26 08:46:10 -07001283def parse_args(args):
1284 # Create a fake mini-parser just to get out the "-a" command. Note that
1285 # it's not documented here; instead, it's documented in create_option_parser
1286 # even though that parser will never actually get to parse it. This is
1287 # because --argsfile is exclusive with all other options and arguments.
1288 file_argparse = argparse.ArgumentParser(add_help=False)
1289 file_argparse.add_argument('-a', '--argsfile')
1290 (file_args, nonfile_args) = file_argparse.parse_known_args(args)
1291 if file_args.argsfile:
1292 if nonfile_args:
1293 file_argparse.error('Can\'t specify --argsfile with'
1294 'any other arguments (%s)' % nonfile_args)
1295 try:
1296 with open(file_args.argsfile, 'r') as f:
1297 args = json.load(f)
1298 except (IOError, OSError, ValueError) as e:
1299 # We don't need to error out here - "args" is now empty,
1300 # so the call below to parser.parse_args(args) will fail
1301 # and print the full help text.
Marc-Antoine Ruelf899c482019-10-10 23:32:06 +00001302 print('Couldn\'t read arguments: %s' % e, file=sys.stderr)
aludwin7556e0c2016-10-26 08:46:10 -07001303
1304 # Even if we failed to read the args, just call the normal parser now since it
1305 # will print the correct help message.
nodirbe642ff2016-06-09 15:51:51 -07001306 parser = create_option_parser()
Marc-Antoine Ruel90c98162013-12-18 15:11:57 -05001307 options, args = parser.parse_args(args)
aludwin7556e0c2016-10-26 08:46:10 -07001308 return (parser, options, args)
1309
1310
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001311def _calc_named_cache_hint(named_cache, named_caches):
1312 """Returns the expected size of the missing named caches."""
1313 present = named_cache.available
1314 size = 0
1315 for name, _, hint in named_caches:
1316 if name not in present:
1317 hint = long(hint)
1318 if hint > 0:
1319 size += hint
1320 return size
1321
1322
aludwin7556e0c2016-10-26 08:46:10 -07001323def main(args):
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -05001324 # Warning: when --argsfile is used, the strings are unicode instances, when
1325 # parsed normally, the strings are str instances.
aludwin7556e0c2016-10-26 08:46:10 -07001326 (parser, options, args) = parse_args(args)
maruel36a963d2016-04-08 17:15:49 -07001327
Marc-Antoine Ruel5028ba22017-08-25 17:37:51 -04001328 if not file_path.enable_symlink():
Marc-Antoine Ruel5a024272019-01-15 20:11:16 +00001329 logging.warning('Symlink support is not enabled')
Marc-Antoine Ruel5028ba22017-08-25 17:37:51 -04001330
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001331 named_cache = process_named_cache_options(parser, options)
Marc-Antoine Ruel0d8b0f62018-09-10 14:40:35 +00001332 # hint is 0 if there's no named cache.
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001333 hint = _calc_named_cache_hint(named_cache, options.named_caches)
1334 if hint:
1335 # Increase the --min-free-space value by the hint, and recreate the
1336 # NamedCache instance so it gets the updated CachePolicy.
1337 options.min_free_space += hint
1338 named_cache = process_named_cache_options(parser, options)
1339
Marc-Antoine Ruel7139d912018-06-15 20:04:42 +00001340 # TODO(maruel): CIPD caches should be defined at an higher level here too, so
1341 # they can be cleaned the same way.
nodirf33b8d62016-10-26 22:34:58 -07001342 isolate_cache = isolateserver.process_cache_options(options, trim=False)
Marc-Antoine Ruel7139d912018-06-15 20:04:42 +00001343 caches = []
1344 if isolate_cache:
1345 caches.append(isolate_cache)
1346 if named_cache:
1347 caches.append(named_cache)
Takuto Ikuta6e2ff962019-10-29 12:35:27 +00001348 root = caches[0].cache_dir if caches else six.text_type(os.getcwd())
maruel36a963d2016-04-08 17:15:49 -07001349 if options.clean:
1350 if options.isolated:
1351 parser.error('Can\'t use --isolated with --clean.')
1352 if options.isolate_server:
1353 parser.error('Can\'t use --isolate-server with --clean.')
1354 if options.json:
1355 parser.error('Can\'t use --json with --clean.')
nodirf33b8d62016-10-26 22:34:58 -07001356 if options.named_caches:
1357 parser.error('Can\t use --named-cache with --clean.')
Marc-Antoine Ruel7139d912018-06-15 20:04:42 +00001358 # Trim first, then clean.
1359 local_caching.trim_caches(
1360 caches,
1361 root,
1362 min_free_space=options.min_free_space,
1363 max_age_secs=MAX_AGE_SECS)
1364 for c in caches:
Marc-Antoine Ruel87fc2222018-06-18 13:09:24 +00001365 c.cleanup()
maruel36a963d2016-04-08 17:15:49 -07001366 return 0
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001367
1368 # Trim must still be done for the following case:
1369 # - named-cache was used
1370 # - some entries, with a large hint, where missing
1371 # - --min-free-space was increased accordingly, thus trimming is needed
1372 # Otherwise, this will have no effect, as bot_main calls run_isolated with
1373 # --clean after each task.
1374 if hint:
1375 logging.info('Additional trimming of %d bytes', hint)
Marc-Antoine Ruel0d8b0f62018-09-10 14:40:35 +00001376 local_caching.trim_caches(
1377 caches,
1378 root,
1379 min_free_space=options.min_free_space,
1380 max_age_secs=MAX_AGE_SECS)
maruel36a963d2016-04-08 17:15:49 -07001381
nodir55be77b2016-05-03 09:39:57 -07001382 if not options.isolated and not args:
1383 parser.error('--isolated or command to run is required.')
1384
Vadim Shtayura5d1efce2014-02-04 10:55:43 -08001385 auth.process_auth_options(parser, options)
nodir55be77b2016-05-03 09:39:57 -07001386
1387 isolateserver.process_isolate_server_options(
Marc-Antoine Ruel5028ba22017-08-25 17:37:51 -04001388 parser, options, True, False)
nodir55be77b2016-05-03 09:39:57 -07001389 if not options.isolate_server:
1390 if options.isolated:
1391 parser.error('--isolated requires --isolate-server')
1392 if ISOLATED_OUTDIR_PARAMETER in args:
1393 parser.error(
1394 '%s in args requires --isolate-server' % ISOLATED_OUTDIR_PARAMETER)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001395
nodir90bc8dc2016-06-15 13:35:21 -07001396 if options.root_dir:
Takuto Ikuta6e2ff962019-10-29 12:35:27 +00001397 options.root_dir = six.text_type(os.path.abspath(options.root_dir))
maruel12e30012015-10-09 11:55:35 -07001398 if options.json:
Takuto Ikuta6e2ff962019-10-29 12:35:27 +00001399 options.json = six.text_type(os.path.abspath(options.json))
nodir55be77b2016-05-03 09:39:57 -07001400
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -05001401 if any('=' not in i for i in options.env):
1402 parser.error(
1403 '--env required key=value form. value can be skipped to delete '
1404 'the variable')
Marc-Antoine Ruel7a68f712017-12-01 18:45:18 -05001405 options.env = dict(i.split('=', 1) for i in options.env)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -05001406
1407 prefixes = {}
1408 cwd = os.path.realpath(os.getcwd())
1409 for item in options.env_prefix:
1410 if '=' not in item:
1411 parser.error(
1412 '--env-prefix %r is malformed, must be in the form `VAR=./path`'
1413 % item)
Marc-Antoine Ruel7a68f712017-12-01 18:45:18 -05001414 key, opath = item.split('=', 1)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -05001415 if os.path.isabs(opath):
1416 parser.error('--env-prefix %r path is bad, must be relative.' % opath)
1417 opath = os.path.normpath(opath)
1418 if not os.path.realpath(os.path.join(cwd, opath)).startswith(cwd):
1419 parser.error(
1420 '--env-prefix %r path is bad, must be relative and not contain `..`.'
1421 % opath)
1422 prefixes.setdefault(key, []).append(opath)
1423 options.env_prefix = prefixes
Robert Iannuccibf5f84c2017-11-22 12:56:50 -08001424
nodirbe642ff2016-06-09 15:51:51 -07001425 cipd.validate_cipd_options(parser, options)
Takuto Ikuta7f1471c2019-12-02 04:34:03 +00001426 if options.use_go_isolated and not options.cipd_enabled:
1427 parser.error('--cipd-enabled should be set if --use-go-isolated is set.')
Takuto Ikuta057c5342019-12-03 04:05:05 +00001428 if options.use_go_isolated and not options.go_cache_dir:
1429 parser.error('--go-cache-dir should be set if --use-go-isolated is set.')
nodirbe642ff2016-06-09 15:51:51 -07001430
vadimsh232f5a82017-01-20 19:23:44 -08001431 install_packages_fn = noop_install_packages
vadimsh902948e2017-01-20 15:57:32 -08001432 if options.cipd_enabled:
Takuto Ikutab7ce0e32019-11-27 23:26:18 +00001433 install_packages_fn = (
1434 lambda run_dir, isolated_dir: install_client_and_packages(
vadimsh902948e2017-01-20 15:57:32 -08001435 run_dir, cipd.parse_package_args(options.cipd_packages),
1436 options.cipd_server, options.cipd_client_package,
Takuto Ikutab7ce0e32019-11-27 23:26:18 +00001437 options.cipd_client_version, cache_dir=options.cipd_cache,
1438 isolated_dir=isolated_dir))
nodirbe642ff2016-06-09 15:51:51 -07001439
nodird6160682017-02-02 13:03:35 -08001440 @contextlib.contextmanager
nodir0ae98b32017-05-11 13:21:53 -07001441 def install_named_caches(run_dir):
nodird6160682017-02-02 13:03:35 -08001442 # WARNING: this function depends on "options" variable defined in the outer
1443 # function.
Takuto Ikuta6e2ff962019-10-29 12:35:27 +00001444 assert six.text_type(run_dir), repr(run_dir)
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001445 assert os.path.isabs(run_dir), run_dir
Takuto Ikuta6e2ff962019-10-29 12:35:27 +00001446 named_caches = [(os.path.join(run_dir, six.text_type(relpath)), name)
1447 for name, relpath, _ in options.named_caches]
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001448 for path, name in named_caches:
Marc-Antoine Ruele79ddbf2018-06-13 18:33:07 +00001449 named_cache.install(path, name)
nodird6160682017-02-02 13:03:35 -08001450 try:
1451 yield
1452 finally:
dnje289d132017-07-07 11:16:44 -07001453 # Uninstall each named cache, returning it to the cache pool. If an
1454 # uninstall fails for a given cache, it will remain in the task's
1455 # temporary space, get cleaned up by the Swarming bot, and be lost.
1456 #
1457 # If the Swarming bot cannot clean up the cache, it will handle it like
1458 # any other bot file that could not be removed.
Marc-Antoine Ruelc7a704b2018-08-29 19:02:23 +00001459 for path, name in reversed(named_caches):
Marc-Antoine Ruele79ddbf2018-06-13 18:33:07 +00001460 try:
Marc-Antoine Ruele9558372018-08-03 03:41:22 +00001461 # uninstall() doesn't trim but does call save() implicitly. Trimming
1462 # *must* be done manually via periodic 'run_isolated.py --clean'.
Marc-Antoine Ruele79ddbf2018-06-13 18:33:07 +00001463 named_cache.uninstall(path, name)
1464 except local_caching.NamedCacheError:
1465 logging.exception('Error while removing named cache %r at %r. '
1466 'The cache will be lost.', path, name)
nodirf33b8d62016-10-26 22:34:58 -07001467
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001468 extra_args = []
1469 command = []
1470 if options.raw_cmd:
1471 command = args
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -05001472 if options.relative_cwd:
1473 a = os.path.normpath(os.path.abspath(options.relative_cwd))
1474 if not a.startswith(os.getcwd()):
1475 parser.error(
1476 '--relative-cwd must not try to escape the working directory')
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001477 else:
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -05001478 if options.relative_cwd:
1479 parser.error('--relative-cwd requires --raw-cmd')
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001480 extra_args = args
1481
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +00001482 containment_type = subprocess42.Containment.NONE
1483 if options.containment_type == 'AUTO':
1484 containment_type = subprocess42.Containment.AUTO
1485 if options.containment_type == 'JOB_OBJECT':
1486 containment_type = subprocess42.Containment.JOB_OBJECT
1487 containment = subprocess42.Containment(
1488 containment_type=containment_type,
1489 limit_processes=options.limit_processes,
1490 limit_total_committed_memory=options.limit_total_committed_memory)
1491
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001492 data = TaskData(
1493 command=command,
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -05001494 relative_cwd=options.relative_cwd,
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001495 extra_args=extra_args,
1496 isolated_hash=options.isolated,
1497 storage=None,
1498 isolate_cache=isolate_cache,
1499 outputs=options.output,
1500 install_named_caches=install_named_caches,
1501 leak_temp_dir=options.leak_temp_dir,
1502 root_dir=_to_unicode(options.root_dir),
1503 hard_timeout=options.hard_timeout,
1504 grace_period=options.grace_period,
1505 bot_file=options.bot_file,
1506 switch_to_account=options.switch_to_account,
1507 install_packages_fn=install_packages_fn,
Marc-Antoine Ruel03c6fd12019-04-30 12:12:55 +00001508 use_symlinks=bool(options.use_symlinks),
Takuto Ikutad03ffcc2019-12-02 01:04:23 +00001509 use_go_isolated=bool(options.use_go_isolated),
Takuto Ikuta057c5342019-12-03 04:05:05 +00001510 go_cache_dir=options.go_cache_dir,
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001511 env=options.env,
Marc-Antoine Ruel03c6fd12019-04-30 12:12:55 +00001512 env_prefix=options.env_prefix,
Marc-Antoine Ruel1b65f4e2019-05-02 21:56:58 +00001513 lower_priority=bool(options.lower_priority),
1514 containment=containment)
nodirbe642ff2016-06-09 15:51:51 -07001515 try:
nodir90bc8dc2016-06-15 13:35:21 -07001516 if options.isolate_server:
Marc-Antoine Ruelb8513132018-11-20 19:48:53 +00001517 server_ref = isolate_storage.ServerRef(
nodir90bc8dc2016-06-15 13:35:21 -07001518 options.isolate_server, options.namespace)
Marc-Antoine Ruelb8513132018-11-20 19:48:53 +00001519 storage = isolateserver.get_storage(server_ref)
nodir90bc8dc2016-06-15 13:35:21 -07001520 with storage:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001521 data = data._replace(storage=storage)
nodirf33b8d62016-10-26 22:34:58 -07001522 # Hashing schemes used by |storage| and |isolate_cache| MUST match.
Marc-Antoine Ruelb8513132018-11-20 19:48:53 +00001523 assert storage.server_ref.hash_algo == server_ref.hash_algo
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001524 return run_tha_test(data, options.json)
1525 return run_tha_test(data, options.json)
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001526 except (
1527 cipd.Error,
1528 local_caching.NamedCacheError,
Marc-Antoine Ruelb6e9e232018-11-20 00:12:33 +00001529 local_caching.NoMoreSpace) as ex:
Marc-Antoine Ruelf899c482019-10-10 23:32:06 +00001530 print(ex.message, file=sys.stderr)
nodirbe642ff2016-06-09 15:51:51 -07001531 return 1
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001532
1533
1534if __name__ == '__main__':
maruel8e4e40c2016-05-30 06:21:07 -07001535 subprocess42.inhibit_os_error_reporting()
csharp@chromium.orgbfb98742013-03-26 20:28:36 +00001536 # Ensure that we are always running with the correct encoding.
vadimsh@chromium.orga4326472013-08-24 02:05:41 +00001537 fix_encoding.fix_encoding()
Marc-Antoine Ruel90c98162013-12-18 15:11:57 -05001538 sys.exit(main(sys.argv[1:]))