blob: 60f2f4a2e388bd006a12f4d14a1513f79b15ea32 [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
nodir55be77b2016-05-03 09:39:57 -07008Despite name "run_isolated", can run a generic non-isolated command specified as
9args.
10
11If input isolated hash is provided, fetches it, creates a tree of hard links,
12appends args to the command in the fetched isolated and runs it.
13To improve performance, keeps a local cache.
14The local cache can safely be deleted.
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -050015
nodirbe642ff2016-06-09 15:51:51 -070016Any ${EXECUTABLE_SUFFIX} on the command line will be replaced with ".exe" string
17on Windows and "" on other platforms.
18
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -050019Any ${ISOLATED_OUTDIR} on the command line will be replaced by the location of a
20temporary directory upon execution of the command specified in the .isolated
21file. All content written to this directory will be uploaded upon termination
22and the .isolated file describing this directory will be printed to stdout.
bpastene447c1992016-06-20 15:21:47 -070023
24Any ${SWARMING_BOT_FILE} on the command line will be replaced by the value of
25the --bot-file parameter. This file is used by a swarming bot to communicate
26state of the host to tasks. It is written to by the swarming bot's
27on_before_task() hook in the swarming server's custom bot_config.py.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000028"""
29
Sadaf Matinkhoo10743a62018-03-29 16:28:58 -040030__version__ = '0.10.5'
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000031
aludwin7556e0c2016-10-26 08:46:10 -070032import argparse
maruel064c0a32016-04-05 11:47:15 -070033import base64
iannucci96fcccc2016-08-30 15:52:22 -070034import collections
vadimsh232f5a82017-01-20 19:23:44 -080035import contextlib
Sadaf Matinkhoo10743a62018-03-29 16:28:58 -040036import errno
aludwin7556e0c2016-10-26 08:46:10 -070037import json
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000038import logging
39import optparse
40import os
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -040041import re
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000042import sys
43import tempfile
maruel064c0a32016-04-05 11:47:15 -070044import time
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000045
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000046from third_party.depot_tools import fix_encoding
47
Vadim Shtayura6b555c12014-07-23 16:22:18 -070048from utils import file_path
maruel12e30012015-10-09 11:55:35 -070049from utils import fs
maruel064c0a32016-04-05 11:47:15 -070050from utils import large
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -040051from utils import logging_utils
Marc-Antoine Ruelcfb60852014-07-02 15:22:00 -040052from utils import on_error
Marc-Antoine Ruelc44f5722015-01-08 16:10:01 -050053from utils import subprocess42
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000054from utils import tools
vadimsh@chromium.org3e97deb2013-08-24 00:56:44 +000055from utils import zip_package
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000056
vadimsh9c54b2c2017-07-25 14:08:29 -070057from libs import luci_context
58
Vadim Shtayurae34e13a2014-02-02 11:23:26 -080059import auth
nodirbe642ff2016-06-09 15:51:51 -070060import cipd
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000061import isolateserver
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -040062import local_caching
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000063
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000064
vadimsh@chromium.org85071062013-08-21 23:37:45 +000065# Absolute path to this file (can be None if running from zip on Mac).
tansella4949442016-06-23 22:34:32 -070066THIS_FILE_PATH = os.path.abspath(
67 __file__.decode(sys.getfilesystemencoding())) if __file__ else None
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000068
69# Directory that contains this file (might be inside zip package).
tansella4949442016-06-23 22:34:32 -070070BASE_DIR = os.path.dirname(THIS_FILE_PATH) if __file__.decode(
71 sys.getfilesystemencoding()) else None
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000072
73# Directory that contains currently running script file.
maruel@chromium.org814d23f2013-10-01 19:08:00 +000074if zip_package.get_main_script_path():
75 MAIN_DIR = os.path.dirname(
76 os.path.abspath(zip_package.get_main_script_path()))
77else:
78 # This happens when 'import run_isolated' is executed at the python
79 # interactive prompt, in that case __file__ is undefined.
80 MAIN_DIR = None
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000081
maruele2f2cb82016-07-13 14:41:03 -070082
83# Magic variables that can be found in the isolate task command line.
84ISOLATED_OUTDIR_PARAMETER = '${ISOLATED_OUTDIR}'
85EXECUTABLE_SUFFIX_PARAMETER = '${EXECUTABLE_SUFFIX}'
86SWARMING_BOT_FILE_PARAMETER = '${SWARMING_BOT_FILE}'
87
88
csharp@chromium.orgff2a4662012-11-21 20:49:32 +000089# The name of the log file to use.
90RUN_ISOLATED_LOG_FILE = 'run_isolated.log'
91
maruele2f2cb82016-07-13 14:41:03 -070092
csharp@chromium.orge217f302012-11-22 16:51:53 +000093# The name of the log to use for the run_test_cases.py command
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000094RUN_TEST_CASES_LOG = 'run_test_cases.log'
csharp@chromium.orge217f302012-11-22 16:51:53 +000095
vadimsh@chromium.org87d63262013-04-04 19:34:21 +000096
maruele2f2cb82016-07-13 14:41:03 -070097# Use short names for temporary directories. This is driven by Windows, which
98# imposes a relatively short maximum path length of 260 characters, often
99# referred to as MAX_PATH. It is relatively easy to create files with longer
100# path length. A use case is with recursive depedency treesV like npm packages.
101#
102# It is recommended to start the script with a `root_dir` as short as
103# possible.
104# - ir stands for isolated_run
105# - io stands for isolated_out
106# - it stands for isolated_tmp
107ISOLATED_RUN_DIR = u'ir'
108ISOLATED_OUT_DIR = u'io'
109ISOLATED_TMP_DIR = u'it'
110
111
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -0400112# Keep synced with task_request.py
113CACHE_NAME_RE = re.compile(ur'^[a-z0-9_]{1,4096}$')
114
115
marueld928c862017-06-08 08:20:04 -0700116OUTLIVING_ZOMBIE_MSG = """\
117*** Swarming tried multiple times to delete the %s directory and failed ***
118*** Hard failing the task ***
119
120Swarming detected that your testing script ran an executable, which may have
121started a child executable, and the main script returned early, leaving the
122children executables playing around unguided.
123
124You don't want to leave children processes outliving the task on the Swarming
125bot, do you? The Swarming bot doesn't.
126
127How to fix?
128- For any process that starts children processes, make sure all children
129 processes terminated properly before each parent process exits. This is
130 especially important in very deep process trees.
131 - This must be done properly both in normal successful task and in case of
132 task failure. Cleanup is very important.
133- The Swarming bot sends a SIGTERM in case of timeout.
134 - You have %s seconds to comply after the signal was sent to the process
135 before the process is forcibly killed.
136- To achieve not leaking children processes in case of signals on timeout, you
137 MUST handle signals in each executable / python script and propagate them to
138 children processes.
139 - When your test script (python or binary) receives a signal like SIGTERM or
140 CTRL_BREAK_EVENT on Windows), send it to all children processes and wait for
141 them to terminate before quitting.
142
143See
Marc-Antoine Ruelc7243592018-05-24 17:04:04 -0400144https://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 -0700145for more information.
146
147*** May the SIGKILL force be with you ***
148"""
149
150
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500151TaskData = collections.namedtuple(
152 'TaskData', [
153 # List of strings; the command line to use, independent of what was
154 # specified in the isolated file.
155 'command',
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -0500156 # Relative directory to start command into.
157 'relative_cwd',
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500158 # List of strings; the arguments to add to the command specified in the
159 # isolated file.
160 'extra_args',
161 # Hash of the .isolated file that must be retrieved to recreate the tree
162 # of files to run the target executable. The command specified in the
163 # .isolated is executed. Mutually exclusive with command argument.
164 'isolated_hash',
165 # isolateserver.Storage instance to retrieve remote objects. This object
166 # has a reference to an isolateserver.StorageApi, which does the actual
167 # I/O.
168 'storage',
169 # isolateserver.LocalCache instance to keep from retrieving the same
170 # objects constantly by caching the objects retrieved. Can be on-disk or
171 # in-memory.
172 'isolate_cache',
173 # List of paths relative to root_dir to put into the output isolated
174 # bundle upon task completion (see link_outputs_to_outdir).
175 'outputs',
176 # Function (run_dir) => context manager that installs named caches into
177 # |run_dir|.
178 'install_named_caches',
179 # If True, the temporary directory will be deliberately leaked for later
180 # examination.
181 'leak_temp_dir',
182 # Path to the directory to use to create the temporary directory. If not
183 # specified, a random temporary directory is created.
184 'root_dir',
185 # Kills the process if it lasts more than this amount of seconds.
186 'hard_timeout',
187 # Number of seconds to wait between SIGTERM and SIGKILL.
188 'grace_period',
189 # Path to a file with bot state, used in place of ${SWARMING_BOT_FILE}
190 # task command line argument.
191 'bot_file',
192 # Logical account to switch LUCI_CONTEXT into.
193 'switch_to_account',
194 # Context manager dir => CipdInfo, see install_client_and_packages.
195 'install_packages_fn',
196 # Create tree with symlinks instead of hardlinks.
197 'use_symlinks',
198 # Environment variables to set.
199 'env',
200 # Environment variables to mutate with relative directories.
201 # Example: {"ENV_KEY": ['relative', 'paths', 'to', 'prepend']}
202 'env_prefix'])
203
204
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +0000205def get_as_zip_package(executable=True):
206 """Returns ZipPackage with this module and all its dependencies.
207
208 If |executable| is True will store run_isolated.py as __main__.py so that
209 zip package is directly executable be python.
210 """
211 # Building a zip package when running from another zip package is
212 # unsupported and probably unneeded.
213 assert not zip_package.is_zipped_module(sys.modules[__name__])
vadimsh@chromium.org85071062013-08-21 23:37:45 +0000214 assert THIS_FILE_PATH
215 assert BASE_DIR
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +0000216 package = zip_package.ZipPackage(root=BASE_DIR)
217 package.add_python_file(THIS_FILE_PATH, '__main__.py' if executable else None)
aludwin81178302016-11-30 17:18:49 -0800218 package.add_python_file(os.path.join(BASE_DIR, 'isolate_storage.py'))
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -0400219 package.add_python_file(os.path.join(BASE_DIR, 'isolated_format.py'))
maruel@chromium.orgdedbf492013-09-12 20:42:11 +0000220 package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py'))
Vadim Shtayurae34e13a2014-02-02 11:23:26 -0800221 package.add_python_file(os.path.join(BASE_DIR, 'auth.py'))
nodirbe642ff2016-06-09 15:51:51 -0700222 package.add_python_file(os.path.join(BASE_DIR, 'cipd.py'))
Marc-Antoine Ruel34f5f282018-05-16 16:04:31 -0400223 package.add_python_file(os.path.join(BASE_DIR, 'local_caching.py'))
tanselle4288c32016-07-28 09:45:40 -0700224 package.add_directory(os.path.join(BASE_DIR, 'libs'))
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +0000225 package.add_directory(os.path.join(BASE_DIR, 'third_party'))
226 package.add_directory(os.path.join(BASE_DIR, 'utils'))
227 return package
228
229
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -0500230def _to_str(s):
231 """Downgrades a unicode instance to str. Pass str through as-is."""
232 if isinstance(s, str):
233 return s
234 # This is technically incorrect, especially on Windows. In theory
235 # sys.getfilesystemencoding() should be used to use the right 'ANSI code
236 # page' on Windows, but that causes other problems, as the character set
237 # is very limited.
238 return s.encode('utf-8')
239
240
Marc-Antoine Ruel7a68f712017-12-01 18:45:18 -0500241def _to_unicode(s):
242 """Upgrades a str instance to unicode. Pass unicode through as-is."""
243 if isinstance(s, unicode) or s is None:
244 return s
245 return s.decode('utf-8')
246
247
maruel03e11842016-07-14 10:50:16 -0700248def make_temp_dir(prefix, root_dir):
249 """Returns a new unique temporary directory."""
250 return unicode(tempfile.mkdtemp(prefix=prefix, dir=root_dir))
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000251
252
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500253def change_tree_read_only(rootdir, read_only):
254 """Changes the tree read-only bits according to the read_only specification.
255
256 The flag can be 0, 1 or 2, which will affect the possibility to modify files
257 and create or delete files.
258 """
259 if read_only == 2:
260 # Files and directories (except on Windows) are marked read only. This
261 # inhibits modifying, creating or deleting files in the test directory,
262 # except on Windows where creating and deleting files is still possible.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400263 file_path.make_tree_read_only(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500264 elif read_only == 1:
265 # Files are marked read only but not the directories. This inhibits
266 # modifying files but creating or deleting files is still possible.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400267 file_path.make_tree_files_read_only(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500268 elif read_only in (0, None):
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -0500269 # Anything can be modified.
Marc-Antoine Ruel2666d9c2018-05-18 13:52:02 -0400270 # TODO(maruel): This is currently dangerous as long as
271 # DiskContentAddressedCache.touch() is not yet changed to verify the hash of
272 # the content of the files it is looking at, so that if a test modifies an
273 # input file, the file must be deleted.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400274 file_path.make_tree_writeable(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500275 else:
276 raise ValueError(
277 'change_tree_read_only(%s, %s): Unknown flag %s' %
278 (rootdir, read_only, read_only))
279
280
vadimsh9c54b2c2017-07-25 14:08:29 -0700281@contextlib.contextmanager
282def set_luci_context_account(account, tmp_dir):
283 """Sets LUCI_CONTEXT account to be used by the task.
284
285 If 'account' is None or '', does nothing at all. This happens when
286 run_isolated.py is called without '--switch-to-account' flag. In this case,
287 if run_isolated.py is running in some LUCI_CONTEXT environment, the task will
288 just inherit whatever account is already set. This may happen is users invoke
289 run_isolated.py explicitly from their code.
290
291 If the requested account is not defined in the context, switches to
292 non-authenticated access. This happens for Swarming tasks that don't use
293 'task' service accounts.
294
295 If not using LUCI_CONTEXT-based auth, does nothing.
296 If already running as requested account, does nothing.
297 """
298 if not account:
299 # Not actually switching.
300 yield
301 return
302
303 local_auth = luci_context.read('local_auth')
304 if not local_auth:
305 # Not using LUCI_CONTEXT auth at all.
306 yield
307 return
308
309 # See LUCI_CONTEXT.md for the format of 'local_auth'.
310 if local_auth.get('default_account_id') == account:
311 # Already set, no need to switch.
312 yield
313 return
314
315 available = {a['id'] for a in local_auth.get('accounts') or []}
316 if account in available:
317 logging.info('Switching default LUCI_CONTEXT account to %r', account)
318 local_auth['default_account_id'] = account
319 else:
320 logging.warning(
321 'Requested LUCI_CONTEXT account %r is not available (have only %r), '
322 'disabling authentication', account, sorted(available))
323 local_auth.pop('default_account_id', None)
324
325 with luci_context.write(_tmpdir=tmp_dir, local_auth=local_auth):
326 yield
327
328
nodir90bc8dc2016-06-15 13:35:21 -0700329def process_command(command, out_dir, bot_file):
nodirbe642ff2016-06-09 15:51:51 -0700330 """Replaces variables in a command line.
331
332 Raises:
333 ValueError if a parameter is requested in |command| but its value is not
334 provided.
335 """
maruela9cfd6f2015-09-15 11:03:15 -0700336 def fix(arg):
nodirbe642ff2016-06-09 15:51:51 -0700337 arg = arg.replace(EXECUTABLE_SUFFIX_PARAMETER, cipd.EXECUTABLE_SUFFIX)
338 replace_slash = False
nodir55be77b2016-05-03 09:39:57 -0700339 if ISOLATED_OUTDIR_PARAMETER in arg:
nodirbe642ff2016-06-09 15:51:51 -0700340 if not out_dir:
maruel7f63a272016-07-12 12:40:36 -0700341 raise ValueError(
342 'output directory is requested in command, but not provided; '
343 'please specify one')
nodir55be77b2016-05-03 09:39:57 -0700344 arg = arg.replace(ISOLATED_OUTDIR_PARAMETER, out_dir)
nodirbe642ff2016-06-09 15:51:51 -0700345 replace_slash = True
nodir90bc8dc2016-06-15 13:35:21 -0700346 if SWARMING_BOT_FILE_PARAMETER in arg:
347 if bot_file:
348 arg = arg.replace(SWARMING_BOT_FILE_PARAMETER, bot_file)
349 replace_slash = True
350 else:
351 logging.warning('SWARMING_BOT_FILE_PARAMETER found in command, but no '
352 'bot_file specified. Leaving parameter unchanged.')
nodirbe642ff2016-06-09 15:51:51 -0700353 if replace_slash:
354 # Replace slashes only if parameters are present
nodir55be77b2016-05-03 09:39:57 -0700355 # because of arguments like '${ISOLATED_OUTDIR}/foo/bar'
356 arg = arg.replace('/', os.sep)
maruela9cfd6f2015-09-15 11:03:15 -0700357 return arg
358
359 return [fix(arg) for arg in command]
360
361
Marc-Antoine Ruel9ec1e9f2017-12-20 16:36:54 -0500362def get_command_env(tmp_dir, cipd_info, run_dir, env, env_prefixes):
vadimsh232f5a82017-01-20 19:23:44 -0800363 """Returns full OS environment to run a command in.
364
Robert Iannuccibf5f84c2017-11-22 12:56:50 -0800365 Sets up TEMP, puts directory with cipd binary in front of PATH, exposes
366 CIPD_CACHE_DIR env var, and installs all env_prefixes.
vadimsh232f5a82017-01-20 19:23:44 -0800367
368 Args:
369 tmp_dir: temp directory.
370 cipd_info: CipdInfo object is cipd client is used, None if not.
Marc-Antoine Ruel9ec1e9f2017-12-20 16:36:54 -0500371 run_dir: The root directory the isolated tree is mapped in.
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500372 env: environment variables to use
Robert Iannuccibf5f84c2017-11-22 12:56:50 -0800373 env_prefixes: {"ENV_KEY": ['cwd', 'relative', 'paths', 'to', 'prepend']}
vadimsh232f5a82017-01-20 19:23:44 -0800374 """
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500375 out = os.environ.copy()
376 for k, v in env.iteritems():
377 if not v:
Marc-Antoine Ruel9ec1e9f2017-12-20 16:36:54 -0500378 out.pop(k, None)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500379 else:
380 out[k] = v
381
382 if cipd_info:
383 bin_dir = os.path.dirname(cipd_info.client.binary_path)
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -0500384 out['PATH'] = '%s%s%s' % (_to_str(bin_dir), os.pathsep, out['PATH'])
385 out['CIPD_CACHE_DIR'] = _to_str(cipd_info.cache_dir)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500386
387 for key, paths in env_prefixes.iteritems():
Marc-Antoine Ruel9ec1e9f2017-12-20 16:36:54 -0500388 assert isinstance(paths, list), paths
389 paths = [os.path.normpath(os.path.join(run_dir, p)) for p in paths]
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500390 cur = out.get(key)
391 if cur:
392 paths.append(cur)
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -0500393 out[key] = _to_str(os.path.pathsep.join(paths))
vadimsh232f5a82017-01-20 19:23:44 -0800394
iannucciac0342c2017-02-24 05:47:01 -0800395 # TMPDIR is specified as the POSIX standard envvar for the temp directory.
iannucci460def72017-02-24 10:49:48 -0800396 # * mktemp on linux respects $TMPDIR, not $TMP
397 # * mktemp on OS X SOMETIMES respects $TMPDIR
iannucciac0342c2017-02-24 05:47:01 -0800398 # * chromium's base utils respects $TMPDIR on linux, $TEMP on windows.
399 # Unfortunately at the time of writing it completely ignores all envvars
400 # on OS X.
iannucci460def72017-02-24 10:49:48 -0800401 # * python respects TMPDIR, TEMP, and TMP (regardless of platform)
402 # * golang respects TMPDIR on linux+mac, TEMP on windows.
iannucciac0342c2017-02-24 05:47:01 -0800403 key = {'win32': 'TEMP'}.get(sys.platform, 'TMPDIR')
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -0500404 out[key] = _to_str(tmp_dir)
vadimsh232f5a82017-01-20 19:23:44 -0800405
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -0500406 return out
vadimsh232f5a82017-01-20 19:23:44 -0800407
408
409def run_command(command, cwd, env, hard_timeout, grace_period):
maruel6be7f9e2015-10-01 12:25:30 -0700410 """Runs the command.
411
412 Returns:
413 tuple(process exit code, bool if had a hard timeout)
414 """
maruela9cfd6f2015-09-15 11:03:15 -0700415 logging.info('run_command(%s, %s)' % (command, cwd))
marueleb5fbee2015-09-17 13:01:36 -0700416
maruel6be7f9e2015-10-01 12:25:30 -0700417 exit_code = None
418 had_hard_timeout = False
maruela9cfd6f2015-09-15 11:03:15 -0700419 with tools.Profiler('RunTest'):
maruel6be7f9e2015-10-01 12:25:30 -0700420 proc = None
421 had_signal = []
maruela9cfd6f2015-09-15 11:03:15 -0700422 try:
maruel6be7f9e2015-10-01 12:25:30 -0700423 # TODO(maruel): This code is imperfect. It doesn't handle well signals
424 # during the download phase and there's short windows were things can go
425 # wrong.
426 def handler(signum, _frame):
427 if proc and not had_signal:
428 logging.info('Received signal %d', signum)
429 had_signal.append(True)
maruel556d9052015-10-05 11:12:44 -0700430 raise subprocess42.TimeoutExpired(command, None)
maruel6be7f9e2015-10-01 12:25:30 -0700431
432 proc = subprocess42.Popen(command, cwd=cwd, env=env, detached=True)
433 with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, handler):
434 try:
435 exit_code = proc.wait(hard_timeout or None)
436 except subprocess42.TimeoutExpired:
437 if not had_signal:
438 logging.warning('Hard timeout')
439 had_hard_timeout = True
440 logging.warning('Sending SIGTERM')
441 proc.terminate()
442
443 # Ignore signals in grace period. Forcibly give the grace period to the
444 # child process.
445 if exit_code is None:
446 ignore = lambda *_: None
447 with subprocess42.set_signal_handler(subprocess42.STOP_SIGNALS, ignore):
448 try:
449 exit_code = proc.wait(grace_period or None)
450 except subprocess42.TimeoutExpired:
451 # Now kill for real. The user can distinguish between the
452 # following states:
453 # - signal but process exited within grace period,
454 # hard_timed_out will be set but the process exit code will be
455 # script provided.
456 # - processed exited late, exit code will be -9 on posix.
457 logging.warning('Grace exhausted; sending SIGKILL')
458 proc.kill()
martiniss5c8043e2017-08-01 17:09:43 -0700459 logging.info('Waiting for process exit')
maruel6be7f9e2015-10-01 12:25:30 -0700460 exit_code = proc.wait()
maruela9cfd6f2015-09-15 11:03:15 -0700461 except OSError:
462 # This is not considered to be an internal error. The executable simply
463 # does not exit.
maruela72f46e2016-02-24 11:05:45 -0800464 sys.stderr.write(
465 '<The executable does not exist or a dependent library is missing>\n'
466 '<Check for missing .so/.dll in the .isolate or GN file>\n'
467 '<Command: %s>\n' % command)
468 if os.environ.get('SWARMING_TASK_ID'):
469 # Give an additional hint when running as a swarming task.
470 sys.stderr.write(
471 '<See the task\'s page for commands to help diagnose this issue '
472 'by reproducing the task locally>\n')
maruela9cfd6f2015-09-15 11:03:15 -0700473 exit_code = 1
474 logging.info(
475 'Command finished with exit code %d (%s)',
476 exit_code, hex(0xffffffff & exit_code))
maruel6be7f9e2015-10-01 12:25:30 -0700477 return exit_code, had_hard_timeout
maruela9cfd6f2015-09-15 11:03:15 -0700478
479
maruel4409e302016-07-19 14:25:51 -0700480def fetch_and_map(isolated_hash, storage, cache, outdir, use_symlinks):
481 """Fetches an isolated tree, create the tree and returns (bundle, stats)."""
nodir6f801882016-04-29 14:41:50 -0700482 start = time.time()
483 bundle = isolateserver.fetch_isolated(
484 isolated_hash=isolated_hash,
485 storage=storage,
486 cache=cache,
maruel4409e302016-07-19 14:25:51 -0700487 outdir=outdir,
488 use_symlinks=use_symlinks)
nodir6f801882016-04-29 14:41:50 -0700489 return bundle, {
490 'duration': time.time() - start,
491 'initial_number_items': cache.initial_number_items,
492 'initial_size': cache.initial_size,
493 'items_cold': base64.b64encode(large.pack(sorted(cache.added))),
494 'items_hot': base64.b64encode(
tansell9e04a8d2016-07-28 09:31:59 -0700495 large.pack(sorted(set(cache.used) - set(cache.added)))),
nodir6f801882016-04-29 14:41:50 -0700496 }
497
498
aludwin0a8e17d2016-10-27 15:57:39 -0700499def link_outputs_to_outdir(run_dir, out_dir, outputs):
500 """Links any named outputs to out_dir so they can be uploaded.
501
502 Raises an error if the file already exists in that directory.
503 """
504 if not outputs:
505 return
506 isolateserver.create_directories(out_dir, outputs)
507 for o in outputs:
Sadaf Matinkhoo10743a62018-03-29 16:28:58 -0400508 copy_recursively(os.path.join(run_dir, o), os.path.join(out_dir, o))
509
510
511def copy_recursively(src, dst):
512 """Efficiently copies a file or directory from src_dir to dst_dir.
513
514 `item` may be a file, directory, or a symlink to a file or directory.
515 All symlinks are replaced with their targets, so the resulting
516 directory structure in dst_dir will never have any symlinks.
517
518 To increase speed, copy_recursively hardlinks individual files into the
519 (newly created) directory structure if possible, unlike Python's
520 shutil.copytree().
521 """
522 orig_src = src
523 try:
524 # Replace symlinks with their final target.
525 while fs.islink(src):
526 res = fs.readlink(src)
527 src = os.path.join(os.path.dirname(src), res)
528 # TODO(sadafm): Explicitly handle cyclic symlinks.
529
530 # Note that fs.isfile (which is a wrapper around os.path.isfile) throws
531 # an exception if src does not exist. A warning will be logged in that case.
532 if fs.isfile(src):
533 file_path.link_file(dst, src, file_path.HARDLINK_WITH_FALLBACK)
534 return
535
536 if not fs.exists(dst):
537 os.makedirs(dst)
538
539 for child in fs.listdir(src):
540 copy_recursively(os.path.join(src, child), os.path.join(dst, child))
541
542 except OSError as e:
543 if e.errno == errno.ENOENT:
544 logging.warning('Path %s does not exist or %s is a broken symlink',
545 src, orig_src)
546 else:
547 logging.info("Couldn't collect output file %s: %s", src, e)
aludwin0a8e17d2016-10-27 15:57:39 -0700548
549
maruela9cfd6f2015-09-15 11:03:15 -0700550def delete_and_upload(storage, out_dir, leak_temp_dir):
551 """Deletes the temporary run directory and uploads results back.
552
553 Returns:
nodir6f801882016-04-29 14:41:50 -0700554 tuple(outputs_ref, success, stats)
maruel064c0a32016-04-05 11:47:15 -0700555 - outputs_ref: a dict referring to the results archived back to the isolated
556 server, if applicable.
557 - success: False if something occurred that means that the task must
558 forcibly be considered a failure, e.g. zombie processes were left
559 behind.
nodir6f801882016-04-29 14:41:50 -0700560 - stats: uploading stats.
maruela9cfd6f2015-09-15 11:03:15 -0700561 """
maruela9cfd6f2015-09-15 11:03:15 -0700562 # Upload out_dir and generate a .isolated file out of this directory. It is
563 # only done if files were written in the directory.
564 outputs_ref = None
maruel064c0a32016-04-05 11:47:15 -0700565 cold = []
566 hot = []
nodir6f801882016-04-29 14:41:50 -0700567 start = time.time()
568
maruel12e30012015-10-09 11:55:35 -0700569 if fs.isdir(out_dir) and fs.listdir(out_dir):
maruela9cfd6f2015-09-15 11:03:15 -0700570 with tools.Profiler('ArchiveOutput'):
571 try:
maruel064c0a32016-04-05 11:47:15 -0700572 results, f_cold, f_hot = isolateserver.archive_files_to_storage(
maruela9cfd6f2015-09-15 11:03:15 -0700573 storage, [out_dir], None)
574 outputs_ref = {
575 'isolated': results[0][0],
576 'isolatedserver': storage.location,
577 'namespace': storage.namespace,
578 }
maruel064c0a32016-04-05 11:47:15 -0700579 cold = sorted(i.size for i in f_cold)
580 hot = sorted(i.size for i in f_hot)
maruela9cfd6f2015-09-15 11:03:15 -0700581 except isolateserver.Aborted:
582 # This happens when a signal SIGTERM was received while uploading data.
583 # There is 2 causes:
584 # - The task was too slow and was about to be killed anyway due to
585 # exceeding the hard timeout.
586 # - The amount of data uploaded back is very large and took too much
587 # time to archive.
588 sys.stderr.write('Received SIGTERM while uploading')
589 # Re-raise, so it will be treated as an internal failure.
590 raise
nodir6f801882016-04-29 14:41:50 -0700591
592 success = False
maruela9cfd6f2015-09-15 11:03:15 -0700593 try:
maruel12e30012015-10-09 11:55:35 -0700594 if (not leak_temp_dir and fs.isdir(out_dir) and
maruel6eeea7d2015-09-16 12:17:42 -0700595 not file_path.rmtree(out_dir)):
maruela9cfd6f2015-09-15 11:03:15 -0700596 logging.error('Had difficulties removing out_dir %s', out_dir)
nodir6f801882016-04-29 14:41:50 -0700597 else:
598 success = True
maruela9cfd6f2015-09-15 11:03:15 -0700599 except OSError as e:
600 # When this happens, it means there's a process error.
maruel12e30012015-10-09 11:55:35 -0700601 logging.exception('Had difficulties removing out_dir %s: %s', out_dir, e)
nodir6f801882016-04-29 14:41:50 -0700602 stats = {
603 'duration': time.time() - start,
604 'items_cold': base64.b64encode(large.pack(cold)),
605 'items_hot': base64.b64encode(large.pack(hot)),
606 }
607 return outputs_ref, success, stats
maruela9cfd6f2015-09-15 11:03:15 -0700608
609
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500610def map_and_run(data, constant_run_path):
nodir55be77b2016-05-03 09:39:57 -0700611 """Runs a command with optional isolated input/output.
612
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500613 Arguments:
614 - data: TaskData instance.
615 - constant_run_path: TODO
nodir55be77b2016-05-03 09:39:57 -0700616
617 Returns metadata about the result.
618 """
maruela9cfd6f2015-09-15 11:03:15 -0700619 result = {
maruel064c0a32016-04-05 11:47:15 -0700620 'duration': None,
maruela9cfd6f2015-09-15 11:03:15 -0700621 'exit_code': None,
maruel6be7f9e2015-10-01 12:25:30 -0700622 'had_hard_timeout': False,
Seth Koehler49139812017-12-19 13:59:33 -0500623 'internal_failure': 'run_isolated did not complete properly',
maruel064c0a32016-04-05 11:47:15 -0700624 'stats': {
nodir55715712016-06-03 12:28:19 -0700625 # 'isolated': {
nodirbe642ff2016-06-09 15:51:51 -0700626 # 'cipd': {
627 # 'duration': 0.,
628 # 'get_client_duration': 0.,
629 # },
nodir55715712016-06-03 12:28:19 -0700630 # 'download': {
631 # 'duration': 0.,
632 # 'initial_number_items': 0,
633 # 'initial_size': 0,
634 # 'items_cold': '<large.pack()>',
635 # 'items_hot': '<large.pack()>',
636 # },
637 # 'upload': {
638 # 'duration': 0.,
639 # 'items_cold': '<large.pack()>',
640 # 'items_hot': '<large.pack()>',
641 # },
maruel064c0a32016-04-05 11:47:15 -0700642 # },
643 },
iannucci96fcccc2016-08-30 15:52:22 -0700644 # 'cipd_pins': {
645 # 'packages': [
646 # {'package_name': ..., 'version': ..., 'path': ...},
647 # ...
648 # ],
649 # 'client_package': {'package_name': ..., 'version': ...},
650 # },
maruela9cfd6f2015-09-15 11:03:15 -0700651 'outputs_ref': None,
nodirbe642ff2016-06-09 15:51:51 -0700652 'version': 5,
maruela9cfd6f2015-09-15 11:03:15 -0700653 }
nodirbe642ff2016-06-09 15:51:51 -0700654
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500655 if data.root_dir:
656 file_path.ensure_tree(data.root_dir, 0700)
657 elif data.isolate_cache.cache_dir:
658 data = data._replace(
659 root_dir=os.path.dirname(data.isolate_cache.cache_dir))
maruele2f2cb82016-07-13 14:41:03 -0700660 # See comment for these constants.
maruelcffa0542017-04-07 08:39:20 -0700661 # If root_dir is not specified, it is not constant.
662 # TODO(maruel): This is not obvious. Change this to become an error once we
663 # make the constant_run_path an exposed flag.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500664 if constant_run_path and data.root_dir:
665 run_dir = os.path.join(data.root_dir, ISOLATED_RUN_DIR)
maruel5c4eed82017-05-26 05:33:40 -0700666 if os.path.isdir(run_dir):
667 file_path.rmtree(run_dir)
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -0500668 os.mkdir(run_dir, 0700)
maruelcffa0542017-04-07 08:39:20 -0700669 else:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500670 run_dir = make_temp_dir(ISOLATED_RUN_DIR, data.root_dir)
maruel03e11842016-07-14 10:50:16 -0700671 # storage should be normally set but don't crash if it is not. This can happen
672 # as Swarming task can run without an isolate server.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500673 out_dir = make_temp_dir(
674 ISOLATED_OUT_DIR, data.root_dir) if data.storage else None
675 tmp_dir = make_temp_dir(ISOLATED_TMP_DIR, data.root_dir)
nodir55be77b2016-05-03 09:39:57 -0700676 cwd = run_dir
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -0500677 if data.relative_cwd:
678 cwd = os.path.normpath(os.path.join(cwd, data.relative_cwd))
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500679 command = data.command
nodir55be77b2016-05-03 09:39:57 -0700680 try:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500681 with data.install_packages_fn(run_dir) as cipd_info:
vadimsh232f5a82017-01-20 19:23:44 -0800682 if cipd_info:
683 result['stats']['cipd'] = cipd_info.stats
684 result['cipd_pins'] = cipd_info.pins
nodir90bc8dc2016-06-15 13:35:21 -0700685
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500686 if data.isolated_hash:
vadimsh232f5a82017-01-20 19:23:44 -0800687 isolated_stats = result['stats'].setdefault('isolated', {})
688 bundle, isolated_stats['download'] = fetch_and_map(
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500689 isolated_hash=data.isolated_hash,
690 storage=data.storage,
691 cache=data.isolate_cache,
vadimsh232f5a82017-01-20 19:23:44 -0800692 outdir=run_dir,
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500693 use_symlinks=data.use_symlinks)
vadimsh232f5a82017-01-20 19:23:44 -0800694 change_tree_read_only(run_dir, bundle.read_only)
maruelabec63c2017-04-26 11:53:24 -0700695 # Inject the command
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500696 if not command and bundle.command:
697 command = bundle.command + data.extra_args
Marc-Antoine Rueld704a1f2017-10-31 10:51:23 -0400698 # Only set the relative directory if the isolated file specified a
699 # command, and no raw command was specified.
700 if bundle.relative_cwd:
701 cwd = os.path.normpath(os.path.join(cwd, bundle.relative_cwd))
maruelabec63c2017-04-26 11:53:24 -0700702
703 if not command:
704 # Handle this as a task failure, not an internal failure.
705 sys.stderr.write(
706 '<No command was specified!>\n'
707 '<Please secify a command when triggering your Swarming task>\n')
708 result['exit_code'] = 1
709 return result
nodirbe642ff2016-06-09 15:51:51 -0700710
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -0500711 if not cwd.startswith(run_dir):
712 # Handle this as a task failure, not an internal failure. This is a
713 # 'last chance' way to gate against directory escape.
714 sys.stderr.write('<Relative CWD is outside of run directory!>\n')
715 result['exit_code'] = 1
716 return result
717
718 if not os.path.isdir(cwd):
719 # Accepts relative_cwd that does not exist.
720 os.makedirs(cwd, 0700)
721
vadimsh232f5a82017-01-20 19:23:44 -0800722 # If we have an explicit list of files to return, make sure their
723 # directories exist now.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500724 if data.storage and data.outputs:
725 isolateserver.create_directories(run_dir, data.outputs)
aludwin0a8e17d2016-10-27 15:57:39 -0700726
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500727 with data.install_named_caches(run_dir):
nodird6160682017-02-02 13:03:35 -0800728 sys.stdout.flush()
729 start = time.time()
730 try:
vadimsh9c54b2c2017-07-25 14:08:29 -0700731 # Need to switch the default account before 'get_command_env' call,
732 # so it can grab correct value of LUCI_CONTEXT env var.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500733 with set_luci_context_account(data.switch_to_account, tmp_dir):
734 env = get_command_env(
Marc-Antoine Ruel9ec1e9f2017-12-20 16:36:54 -0500735 tmp_dir, cipd_info, run_dir, data.env, data.env_prefix)
Robert Iannucci24ae76a2018-02-26 12:51:18 -0800736 command = tools.fix_python_cmd(command, env)
737 command = process_command(command, out_dir, data.bot_file)
738 file_path.ensure_command_has_abs_path(command, cwd)
739
vadimsh9c54b2c2017-07-25 14:08:29 -0700740 result['exit_code'], result['had_hard_timeout'] = run_command(
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500741 command, cwd, env, data.hard_timeout, data.grace_period)
nodird6160682017-02-02 13:03:35 -0800742 finally:
743 result['duration'] = max(time.time() - start, 0)
Seth Koehler49139812017-12-19 13:59:33 -0500744
745 # We successfully ran the command, set internal_failure back to
746 # None (even if the command failed, it's not an internal error).
747 result['internal_failure'] = None
maruela9cfd6f2015-09-15 11:03:15 -0700748 except Exception as e:
nodir90bc8dc2016-06-15 13:35:21 -0700749 # An internal error occurred. Report accordingly so the swarming task will
750 # be retried automatically.
maruel12e30012015-10-09 11:55:35 -0700751 logging.exception('internal failure: %s', e)
maruela9cfd6f2015-09-15 11:03:15 -0700752 result['internal_failure'] = str(e)
753 on_error.report(None)
aludwin0a8e17d2016-10-27 15:57:39 -0700754
755 # Clean up
maruela9cfd6f2015-09-15 11:03:15 -0700756 finally:
757 try:
aludwin0a8e17d2016-10-27 15:57:39 -0700758 # Try to link files to the output directory, if specified.
759 if out_dir:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500760 link_outputs_to_outdir(run_dir, out_dir, data.outputs)
aludwin0a8e17d2016-10-27 15:57:39 -0700761
nodir32a1ec12016-10-26 18:34:07 -0700762 success = False
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500763 if data.leak_temp_dir:
nodir32a1ec12016-10-26 18:34:07 -0700764 success = True
maruela9cfd6f2015-09-15 11:03:15 -0700765 logging.warning(
766 'Deliberately leaking %s for later examination', run_dir)
marueleb5fbee2015-09-17 13:01:36 -0700767 else:
maruel84537cb2015-10-16 14:21:28 -0700768 # On Windows rmtree(run_dir) call above has a synchronization effect: it
769 # finishes only when all task child processes terminate (since a running
770 # process locks *.exe file). Examine out_dir only after that call
771 # completes (since child processes may write to out_dir too and we need
772 # to wait for them to finish).
773 if fs.isdir(run_dir):
774 try:
775 success = file_path.rmtree(run_dir)
776 except OSError as e:
777 logging.error('Failure with %s', e)
778 success = False
779 if not success:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500780 sys.stderr.write(OUTLIVING_ZOMBIE_MSG % ('run', data.grace_period))
maruel84537cb2015-10-16 14:21:28 -0700781 if result['exit_code'] == 0:
782 result['exit_code'] = 1
783 if fs.isdir(tmp_dir):
784 try:
785 success = file_path.rmtree(tmp_dir)
786 except OSError as e:
787 logging.error('Failure with %s', e)
788 success = False
789 if not success:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500790 sys.stderr.write(OUTLIVING_ZOMBIE_MSG % ('temp', data.grace_period))
maruel84537cb2015-10-16 14:21:28 -0700791 if result['exit_code'] == 0:
792 result['exit_code'] = 1
maruela9cfd6f2015-09-15 11:03:15 -0700793
marueleb5fbee2015-09-17 13:01:36 -0700794 # This deletes out_dir if leak_temp_dir is not set.
nodir9130f072016-05-27 13:59:08 -0700795 if out_dir:
nodir55715712016-06-03 12:28:19 -0700796 isolated_stats = result['stats'].setdefault('isolated', {})
797 result['outputs_ref'], success, isolated_stats['upload'] = (
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500798 delete_and_upload(data.storage, out_dir, data.leak_temp_dir))
maruela9cfd6f2015-09-15 11:03:15 -0700799 if not success and result['exit_code'] == 0:
800 result['exit_code'] = 1
801 except Exception as e:
802 # Swallow any exception in the main finally clause.
nodir9130f072016-05-27 13:59:08 -0700803 if out_dir:
804 logging.exception('Leaking out_dir %s: %s', out_dir, e)
maruela9cfd6f2015-09-15 11:03:15 -0700805 result['internal_failure'] = str(e)
806 return result
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500807
808
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500809def run_tha_test(data, result_json):
nodir55be77b2016-05-03 09:39:57 -0700810 """Runs an executable and records execution metadata.
811
nodir55be77b2016-05-03 09:39:57 -0700812 If isolated_hash is specified, downloads the dependencies in the cache,
813 hardlinks them into a temporary directory and runs the command specified in
814 the .isolated.
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500815
816 A temporary directory is created to hold the output files. The content inside
817 this directory will be uploaded back to |storage| packaged as a .isolated
818 file.
819
820 Arguments:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500821 - data: TaskData instance.
822 - result_json: File path to dump result metadata into. If set, the process
823 exit code is always 0 unless an internal error occurred.
maruela9cfd6f2015-09-15 11:03:15 -0700824
825 Returns:
826 Process exit code that should be used.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000827 """
maruela76b9ee2015-12-15 06:18:08 -0800828 if result_json:
829 # Write a json output file right away in case we get killed.
830 result = {
831 'exit_code': None,
832 'had_hard_timeout': False,
833 'internal_failure': 'Was terminated before completion',
834 'outputs_ref': None,
nodirbe642ff2016-06-09 15:51:51 -0700835 'version': 5,
maruela76b9ee2015-12-15 06:18:08 -0800836 }
837 tools.write_json(result_json, result, dense=True)
838
maruela9cfd6f2015-09-15 11:03:15 -0700839 # run_isolated exit code. Depends on if result_json is used or not.
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -0500840 result = map_and_run(data, True)
maruela9cfd6f2015-09-15 11:03:15 -0700841 logging.info('Result:\n%s', tools.format_json(result, dense=True))
bpastene3ae09522016-06-10 17:12:59 -0700842
maruela9cfd6f2015-09-15 11:03:15 -0700843 if result_json:
maruel05d5a882015-09-21 13:59:02 -0700844 # We've found tests to delete 'work' when quitting, causing an exception
845 # here. Try to recreate the directory if necessary.
nodire5028a92016-04-29 14:38:21 -0700846 file_path.ensure_tree(os.path.dirname(result_json))
maruela9cfd6f2015-09-15 11:03:15 -0700847 tools.write_json(result_json, result, dense=True)
848 # Only return 1 if there was an internal error.
849 return int(bool(result['internal_failure']))
maruel@chromium.org781ccf62013-09-17 19:39:47 +0000850
maruela9cfd6f2015-09-15 11:03:15 -0700851 # Marshall into old-style inline output.
852 if result['outputs_ref']:
853 data = {
854 'hash': result['outputs_ref']['isolated'],
855 'namespace': result['outputs_ref']['namespace'],
856 'storage': result['outputs_ref']['isolatedserver'],
857 }
Marc-Antoine Ruelc44f5722015-01-08 16:10:01 -0500858 sys.stdout.flush()
maruela9cfd6f2015-09-15 11:03:15 -0700859 print(
860 '[run_isolated_out_hack]%s[/run_isolated_out_hack]' %
861 tools.format_json(data, dense=True))
maruelb76604c2015-11-11 11:53:44 -0800862 sys.stdout.flush()
maruela9cfd6f2015-09-15 11:03:15 -0700863 return result['exit_code'] or int(bool(result['internal_failure']))
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000864
865
iannuccib58d10d2017-03-18 02:00:25 -0700866# Yielded by 'install_client_and_packages'.
vadimsh232f5a82017-01-20 19:23:44 -0800867CipdInfo = collections.namedtuple('CipdInfo', [
868 'client', # cipd.CipdClient object
869 'cache_dir', # absolute path to bot-global cipd tag and instance cache
870 'stats', # dict with stats to return to the server
871 'pins', # dict with installed cipd pins to return to the server
872])
873
874
875@contextlib.contextmanager
876def noop_install_packages(_run_dir):
iannuccib58d10d2017-03-18 02:00:25 -0700877 """Placeholder for 'install_client_and_packages' if cipd is disabled."""
vadimsh232f5a82017-01-20 19:23:44 -0800878 yield None
879
880
iannuccib58d10d2017-03-18 02:00:25 -0700881def _install_packages(run_dir, cipd_cache_dir, client, packages, timeout):
882 """Calls 'cipd ensure' for packages.
883
884 Args:
885 run_dir (str): root of installation.
886 cipd_cache_dir (str): the directory to use for the cipd package cache.
887 client (CipdClient): the cipd client to use
888 packages: packages to install, list [(path, package_name, version), ...].
889 timeout: max duration in seconds that this function can take.
890
891 Returns: list of pinned packages. Looks like [
892 {
893 'path': 'subdirectory',
894 'package_name': 'resolved/package/name',
895 'version': 'deadbeef...',
896 },
897 ...
898 ]
899 """
900 package_pins = [None]*len(packages)
901 def insert_pin(path, name, version, idx):
902 package_pins[idx] = {
903 'package_name': name,
904 # swarming deals with 'root' as '.'
905 'path': path or '.',
906 'version': version,
907 }
908
909 by_path = collections.defaultdict(list)
910 for i, (path, name, version) in enumerate(packages):
911 # cipd deals with 'root' as ''
912 if path == '.':
913 path = ''
914 by_path[path].append((name, version, i))
915
916 pins = client.ensure(
917 run_dir,
918 {
919 subdir: [(name, vers) for name, vers, _ in pkgs]
920 for subdir, pkgs in by_path.iteritems()
921 },
922 cache_dir=cipd_cache_dir,
923 timeout=timeout,
924 )
925
926 for subdir, pin_list in sorted(pins.iteritems()):
927 this_subdir = by_path[subdir]
928 for i, (name, version) in enumerate(pin_list):
929 insert_pin(subdir, name, version, this_subdir[i][2])
930
Robert Iannucci461b30d2017-12-13 11:34:03 -0800931 assert None not in package_pins, (packages, pins, package_pins)
iannuccib58d10d2017-03-18 02:00:25 -0700932
933 return package_pins
934
935
vadimsh232f5a82017-01-20 19:23:44 -0800936@contextlib.contextmanager
iannuccib58d10d2017-03-18 02:00:25 -0700937def install_client_and_packages(
nodirff531b42016-06-23 13:05:06 -0700938 run_dir, packages, service_url, client_package_name,
vadimsh232f5a82017-01-20 19:23:44 -0800939 client_version, cache_dir, timeout=None):
vadimsh902948e2017-01-20 15:57:32 -0800940 """Bootstraps CIPD client and installs CIPD packages.
iannucci96fcccc2016-08-30 15:52:22 -0700941
vadimsh232f5a82017-01-20 19:23:44 -0800942 Yields CipdClient, stats, client info and pins (as single CipdInfo object).
943
944 Pins and the CIPD client info are in the form of:
iannucci96fcccc2016-08-30 15:52:22 -0700945 [
946 {
947 "path": path, "package_name": package_name, "version": version,
948 },
949 ...
950 ]
vadimsh902948e2017-01-20 15:57:32 -0800951 (the CIPD client info is a single dictionary instead of a list)
iannucci96fcccc2016-08-30 15:52:22 -0700952
953 such that they correspond 1:1 to all input package arguments from the command
954 line. These dictionaries make their all the way back to swarming, where they
955 become the arguments of CipdPackage.
nodirbe642ff2016-06-09 15:51:51 -0700956
vadimsh902948e2017-01-20 15:57:32 -0800957 If 'packages' list is empty, will bootstrap CIPD client, but won't install
958 any packages.
959
960 The bootstrapped client (regardless whether 'packages' list is empty or not),
vadimsh232f5a82017-01-20 19:23:44 -0800961 will be made available to the task via $PATH.
vadimsh902948e2017-01-20 15:57:32 -0800962
nodirbe642ff2016-06-09 15:51:51 -0700963 Args:
nodir90bc8dc2016-06-15 13:35:21 -0700964 run_dir (str): root of installation.
vadimsh902948e2017-01-20 15:57:32 -0800965 packages: packages to install, list [(path, package_name, version), ...].
nodirbe642ff2016-06-09 15:51:51 -0700966 service_url (str): CIPD server url, e.g.
967 "https://chrome-infra-packages.appspot.com."
nodir90bc8dc2016-06-15 13:35:21 -0700968 client_package_name (str): CIPD package name of CIPD client.
969 client_version (str): Version of CIPD client.
nodirbe642ff2016-06-09 15:51:51 -0700970 cache_dir (str): where to keep cache of cipd clients, packages and tags.
971 timeout: max duration in seconds that this function can take.
nodirbe642ff2016-06-09 15:51:51 -0700972 """
973 assert cache_dir
nodir90bc8dc2016-06-15 13:35:21 -0700974
nodirbe642ff2016-06-09 15:51:51 -0700975 timeoutfn = tools.sliding_timeout(timeout)
nodirbe642ff2016-06-09 15:51:51 -0700976 start = time.time()
nodirbe642ff2016-06-09 15:51:51 -0700977
vadimsh902948e2017-01-20 15:57:32 -0800978 cache_dir = os.path.abspath(cache_dir)
vadimsh232f5a82017-01-20 19:23:44 -0800979 cipd_cache_dir = os.path.join(cache_dir, 'cache') # tag and instance caches
nodir90bc8dc2016-06-15 13:35:21 -0700980 run_dir = os.path.abspath(run_dir)
vadimsh902948e2017-01-20 15:57:32 -0800981 packages = packages or []
nodir90bc8dc2016-06-15 13:35:21 -0700982
nodirbe642ff2016-06-09 15:51:51 -0700983 get_client_start = time.time()
984 client_manager = cipd.get_client(
985 service_url, client_package_name, client_version, cache_dir,
986 timeout=timeoutfn())
iannucci96fcccc2016-08-30 15:52:22 -0700987
nodirbe642ff2016-06-09 15:51:51 -0700988 with client_manager as client:
989 get_client_duration = time.time() - get_client_start
nodir90bc8dc2016-06-15 13:35:21 -0700990
iannuccib58d10d2017-03-18 02:00:25 -0700991 package_pins = []
992 if packages:
993 package_pins = _install_packages(
994 run_dir, cipd_cache_dir, client, packages, timeoutfn())
995
996 file_path.make_tree_files_read_only(run_dir)
nodir90bc8dc2016-06-15 13:35:21 -0700997
vadimsh232f5a82017-01-20 19:23:44 -0800998 total_duration = time.time() - start
999 logging.info(
1000 'Installing CIPD client and packages took %d seconds', total_duration)
nodir90bc8dc2016-06-15 13:35:21 -07001001
vadimsh232f5a82017-01-20 19:23:44 -08001002 yield CipdInfo(
1003 client=client,
1004 cache_dir=cipd_cache_dir,
1005 stats={
1006 'duration': total_duration,
1007 'get_client_duration': get_client_duration,
1008 },
1009 pins={
iannuccib58d10d2017-03-18 02:00:25 -07001010 'client_package': {
1011 'package_name': client.package_name,
1012 'version': client.instance_id,
1013 },
vadimsh232f5a82017-01-20 19:23:44 -08001014 'packages': package_pins,
1015 })
nodirbe642ff2016-06-09 15:51:51 -07001016
1017
Marc-Antoine Ruel34f5f282018-05-16 16:04:31 -04001018def clean_caches(isolate_cache, named_cache_manager):
maruele6fc9382017-05-04 09:03:48 -07001019 """Trims isolated and named caches.
1020
1021 The goal here is to coherently trim both caches, deleting older items
1022 independent of which container they belong to.
1023 """
1024 # TODO(maruel): Trim CIPD cache the same way.
1025 total = 0
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001026 with named_cache_manager:
nodirf33b8d62016-10-26 22:34:58 -07001027 oldest_isolated = isolate_cache.get_oldest()
1028 oldest_named = named_cache_manager.get_oldest()
1029 trimmers = [
1030 (
1031 isolate_cache.trim,
1032 isolate_cache.get_timestamp(oldest_isolated) if oldest_isolated else 0,
1033 ),
1034 (
Marc-Antoine Ruel34f5f282018-05-16 16:04:31 -04001035 named_cache_manager.trim,
nodirf33b8d62016-10-26 22:34:58 -07001036 named_cache_manager.get_timestamp(oldest_named) if oldest_named else 0,
1037 ),
1038 ]
1039 trimmers.sort(key=lambda (_, ts): ts)
maruele6fc9382017-05-04 09:03:48 -07001040 # TODO(maruel): This is incorrect, we want to trim 'items' that are strictly
1041 # the oldest independent of in which cache they live in. Right now, the
1042 # cache with the oldest item pays the price.
nodirf33b8d62016-10-26 22:34:58 -07001043 for trim, _ in trimmers:
maruele6fc9382017-05-04 09:03:48 -07001044 total += trim()
nodirf33b8d62016-10-26 22:34:58 -07001045 isolate_cache.cleanup()
maruele6fc9382017-05-04 09:03:48 -07001046 return total
nodirf33b8d62016-10-26 22:34:58 -07001047
1048
nodirbe642ff2016-06-09 15:51:51 -07001049def create_option_parser():
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -04001050 parser = logging_utils.OptionParserWithLogging(
nodir55be77b2016-05-03 09:39:57 -07001051 usage='%prog <options> [command to run or extra args]',
maruel@chromium.orgdedbf492013-09-12 20:42:11 +00001052 version=__version__,
1053 log_file=RUN_ISOLATED_LOG_FILE)
maruela9cfd6f2015-09-15 11:03:15 -07001054 parser.add_option(
maruel36a963d2016-04-08 17:15:49 -07001055 '--clean', action='store_true',
1056 help='Cleans the cache, trimming it necessary and remove corrupted items '
1057 'and returns without executing anything; use with -v to know what '
1058 'was done')
1059 parser.add_option(
maruel2e8d0f52016-07-16 07:51:29 -07001060 '--no-clean', action='store_true',
1061 help='Do not clean the cache automatically on startup. This is meant for '
1062 'bots where a separate execution with --clean was done earlier so '
1063 'doing it again is redundant')
1064 parser.add_option(
maruel4409e302016-07-19 14:25:51 -07001065 '--use-symlinks', action='store_true',
1066 help='Use symlinks instead of hardlinks')
1067 parser.add_option(
maruela9cfd6f2015-09-15 11:03:15 -07001068 '--json',
1069 help='dump output metadata to json file. When used, run_isolated returns '
1070 'non-zero only on internal failure')
maruel6be7f9e2015-10-01 12:25:30 -07001071 parser.add_option(
maruel5c9e47b2015-12-18 13:02:30 -08001072 '--hard-timeout', type='float', help='Enforce hard timeout in execution')
maruel6be7f9e2015-10-01 12:25:30 -07001073 parser.add_option(
maruel5c9e47b2015-12-18 13:02:30 -08001074 '--grace-period', type='float',
maruel6be7f9e2015-10-01 12:25:30 -07001075 help='Grace period between SIGTERM and SIGKILL')
bpastene3ae09522016-06-10 17:12:59 -07001076 parser.add_option(
Marc-Antoine Ruel49e347d2017-10-24 16:52:02 -07001077 '--raw-cmd', action='store_true',
1078 help='Ignore the isolated command, use the one supplied at the command '
1079 'line')
1080 parser.add_option(
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -05001081 '--relative-cwd',
1082 help='Ignore the isolated \'relative_cwd\' and use this one instead; '
1083 'requires --raw-cmd')
1084 parser.add_option(
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -05001085 '--env', default=[], action='append',
1086 help='Environment variables to set for the child process')
1087 parser.add_option(
1088 '--env-prefix', default=[], action='append',
Robert Iannuccibf5f84c2017-11-22 12:56:50 -08001089 help='Specify a VAR=./path/fragment to put in the environment variable '
1090 'before executing the command. The path fragment must be relative '
1091 'to the isolated run directory, and must not contain a `..` token. '
1092 'The path will be made absolute and prepended to the indicated '
1093 '$VAR using the OS\'s path separator. Multiple items for the same '
1094 '$VAR will be prepended in order.')
1095 parser.add_option(
bpastene3ae09522016-06-10 17:12:59 -07001096 '--bot-file',
1097 help='Path to a file describing the state of the host. The content is '
1098 'defined by on_before_task() in bot_config.')
aludwin7556e0c2016-10-26 08:46:10 -07001099 parser.add_option(
vadimsh9c54b2c2017-07-25 14:08:29 -07001100 '--switch-to-account',
1101 help='If given, switches LUCI_CONTEXT to given logical service account '
1102 '(e.g. "task" or "system") before launching the isolated process.')
1103 parser.add_option(
aludwin0a8e17d2016-10-27 15:57:39 -07001104 '--output', action='append',
1105 help='Specifies an output to return. If no outputs are specified, all '
1106 'files located in $(ISOLATED_OUTDIR) will be returned; '
1107 'otherwise, outputs in both $(ISOLATED_OUTDIR) and those '
1108 'specified by --output option (there can be multiple) will be '
1109 'returned. Note that if a file in OUT_DIR has the same path '
1110 'as an --output option, the --output version will be returned.')
1111 parser.add_option(
aludwin7556e0c2016-10-26 08:46:10 -07001112 '-a', '--argsfile',
1113 # This is actually handled in parse_args; it's included here purely so it
1114 # can make it into the help text.
1115 help='Specify a file containing a JSON array of arguments to this '
1116 'script. If --argsfile is provided, no other argument may be '
1117 'provided on the command line.')
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05001118 data_group = optparse.OptionGroup(parser, 'Data source')
1119 data_group.add_option(
Marc-Antoine Ruel185ded42015-01-28 20:49:18 -05001120 '-s', '--isolated',
nodir55be77b2016-05-03 09:39:57 -07001121 help='Hash of the .isolated to grab from the isolate server.')
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -05001122 isolateserver.add_isolate_server_options(data_group)
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -05001123 parser.add_option_group(data_group)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001124
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -04001125 isolateserver.add_cache_options(parser)
nodirbe642ff2016-06-09 15:51:51 -07001126
1127 cipd.add_cipd_options(parser)
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001128
1129 group = optparse.OptionGroup(parser, 'Named caches')
1130 group.add_option(
1131 '--named-cache',
1132 dest='named_caches',
1133 action='append',
1134 nargs=2,
1135 default=[],
1136 help='A named cache to request. Accepts two arguments, name and path. '
1137 'name identifies the cache, must match regex [a-z0-9_]{1,4096}. '
1138 'path is a path relative to the run dir where the cache directory '
1139 'must be put to. '
1140 'This option can be specified more than once.')
1141 group.add_option(
1142 '--named-cache-root', default='named_caches',
1143 help='Cache root directory. Default=%default')
1144 parser.add_option_group(group)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001145
Kenneth Russell61d42352014-09-15 11:41:16 -07001146 debug_group = optparse.OptionGroup(parser, 'Debugging')
1147 debug_group.add_option(
1148 '--leak-temp-dir',
1149 action='store_true',
nodirbe642ff2016-06-09 15:51:51 -07001150 help='Deliberately leak isolate\'s temp dir for later examination. '
1151 'Default: %default')
marueleb5fbee2015-09-17 13:01:36 -07001152 debug_group.add_option(
1153 '--root-dir', help='Use a directory instead of a random one')
Kenneth Russell61d42352014-09-15 11:41:16 -07001154 parser.add_option_group(debug_group)
1155
Vadim Shtayurae34e13a2014-02-02 11:23:26 -08001156 auth.add_auth_options(parser)
nodirbe642ff2016-06-09 15:51:51 -07001157
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001158 parser.set_defaults(cache='cache', cipd_cache='cipd_cache')
nodirbe642ff2016-06-09 15:51:51 -07001159 return parser
1160
1161
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001162def process_named_cache_options(parser, options, time_fn=None):
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001163 """Validates named cache options and returns a CacheManager."""
1164 if options.named_caches and not options.named_cache_root:
1165 parser.error('--named-cache is specified, but --named-cache-root is empty')
1166 for name, path in options.named_caches:
1167 if not CACHE_NAME_RE.match(name):
1168 parser.error(
1169 'cache name %r does not match %r' % (name, CACHE_NAME_RE.pattern))
1170 if not path:
1171 parser.error('cache path cannot be empty')
1172 if options.named_cache_root:
1173 # Make these configurable later if there is use case but for now it's fairly
1174 # safe values.
1175 # In practice, a fair chunk of bots are already recycled on a daily schedule
1176 # so this code doesn't have any effect to them, unless they are preloaded
1177 # with a really old cache.
1178 policies = local_caching.CachePolicies(
1179 # 1TiB.
1180 max_cache_size=1024*1024*1024*1024,
1181 min_free_space=options.min_free_space,
1182 max_items=50,
1183 # 3 weeks.
1184 max_age_secs=21*24*60*60)
1185 root_dir = unicode(os.path.abspath(options.named_cache_root))
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001186 return local_caching.NamedCache(root_dir, policies, time_fn=time_fn)
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001187 return None
1188
1189
aludwin7556e0c2016-10-26 08:46:10 -07001190def parse_args(args):
1191 # Create a fake mini-parser just to get out the "-a" command. Note that
1192 # it's not documented here; instead, it's documented in create_option_parser
1193 # even though that parser will never actually get to parse it. This is
1194 # because --argsfile is exclusive with all other options and arguments.
1195 file_argparse = argparse.ArgumentParser(add_help=False)
1196 file_argparse.add_argument('-a', '--argsfile')
1197 (file_args, nonfile_args) = file_argparse.parse_known_args(args)
1198 if file_args.argsfile:
1199 if nonfile_args:
1200 file_argparse.error('Can\'t specify --argsfile with'
1201 'any other arguments (%s)' % nonfile_args)
1202 try:
1203 with open(file_args.argsfile, 'r') as f:
1204 args = json.load(f)
1205 except (IOError, OSError, ValueError) as e:
1206 # We don't need to error out here - "args" is now empty,
1207 # so the call below to parser.parse_args(args) will fail
1208 # and print the full help text.
1209 print >> sys.stderr, 'Couldn\'t read arguments: %s' % e
1210
1211 # Even if we failed to read the args, just call the normal parser now since it
1212 # will print the correct help message.
nodirbe642ff2016-06-09 15:51:51 -07001213 parser = create_option_parser()
Marc-Antoine Ruel90c98162013-12-18 15:11:57 -05001214 options, args = parser.parse_args(args)
aludwin7556e0c2016-10-26 08:46:10 -07001215 return (parser, options, args)
1216
1217
1218def main(args):
Marc-Antoine Ruelee6ca622017-11-29 11:19:16 -05001219 # Warning: when --argsfile is used, the strings are unicode instances, when
1220 # parsed normally, the strings are str instances.
aludwin7556e0c2016-10-26 08:46:10 -07001221 (parser, options, args) = parse_args(args)
maruel36a963d2016-04-08 17:15:49 -07001222
Marc-Antoine Ruel5028ba22017-08-25 17:37:51 -04001223 if not file_path.enable_symlink():
1224 logging.error('Symlink support is not enabled')
1225
nodirf33b8d62016-10-26 22:34:58 -07001226 isolate_cache = isolateserver.process_cache_options(options, trim=False)
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001227 named_cache_manager = process_named_cache_options(parser, options)
maruel36a963d2016-04-08 17:15:49 -07001228 if options.clean:
1229 if options.isolated:
1230 parser.error('Can\'t use --isolated with --clean.')
1231 if options.isolate_server:
1232 parser.error('Can\'t use --isolate-server with --clean.')
1233 if options.json:
1234 parser.error('Can\'t use --json with --clean.')
nodirf33b8d62016-10-26 22:34:58 -07001235 if options.named_caches:
1236 parser.error('Can\t use --named-cache with --clean.')
Marc-Antoine Ruel34f5f282018-05-16 16:04:31 -04001237 clean_caches(isolate_cache, named_cache_manager)
maruel36a963d2016-04-08 17:15:49 -07001238 return 0
nodirf33b8d62016-10-26 22:34:58 -07001239
maruel2e8d0f52016-07-16 07:51:29 -07001240 if not options.no_clean:
Marc-Antoine Ruel34f5f282018-05-16 16:04:31 -04001241 clean_caches(isolate_cache, named_cache_manager)
maruel36a963d2016-04-08 17:15:49 -07001242
nodir55be77b2016-05-03 09:39:57 -07001243 if not options.isolated and not args:
1244 parser.error('--isolated or command to run is required.')
1245
Vadim Shtayura5d1efce2014-02-04 10:55:43 -08001246 auth.process_auth_options(parser, options)
nodir55be77b2016-05-03 09:39:57 -07001247
1248 isolateserver.process_isolate_server_options(
Marc-Antoine Ruel5028ba22017-08-25 17:37:51 -04001249 parser, options, True, False)
nodir55be77b2016-05-03 09:39:57 -07001250 if not options.isolate_server:
1251 if options.isolated:
1252 parser.error('--isolated requires --isolate-server')
1253 if ISOLATED_OUTDIR_PARAMETER in args:
1254 parser.error(
1255 '%s in args requires --isolate-server' % ISOLATED_OUTDIR_PARAMETER)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001256
nodir90bc8dc2016-06-15 13:35:21 -07001257 if options.root_dir:
1258 options.root_dir = unicode(os.path.abspath(options.root_dir))
maruel12e30012015-10-09 11:55:35 -07001259 if options.json:
1260 options.json = unicode(os.path.abspath(options.json))
nodir55be77b2016-05-03 09:39:57 -07001261
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -05001262 if any('=' not in i for i in options.env):
1263 parser.error(
1264 '--env required key=value form. value can be skipped to delete '
1265 'the variable')
Marc-Antoine Ruel7a68f712017-12-01 18:45:18 -05001266 options.env = dict(i.split('=', 1) for i in options.env)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -05001267
1268 prefixes = {}
1269 cwd = os.path.realpath(os.getcwd())
1270 for item in options.env_prefix:
1271 if '=' not in item:
1272 parser.error(
1273 '--env-prefix %r is malformed, must be in the form `VAR=./path`'
1274 % item)
Marc-Antoine Ruel7a68f712017-12-01 18:45:18 -05001275 key, opath = item.split('=', 1)
Marc-Antoine Ruel19dd8872017-11-28 18:33:39 -05001276 if os.path.isabs(opath):
1277 parser.error('--env-prefix %r path is bad, must be relative.' % opath)
1278 opath = os.path.normpath(opath)
1279 if not os.path.realpath(os.path.join(cwd, opath)).startswith(cwd):
1280 parser.error(
1281 '--env-prefix %r path is bad, must be relative and not contain `..`.'
1282 % opath)
1283 prefixes.setdefault(key, []).append(opath)
1284 options.env_prefix = prefixes
Robert Iannuccibf5f84c2017-11-22 12:56:50 -08001285
nodirbe642ff2016-06-09 15:51:51 -07001286 cipd.validate_cipd_options(parser, options)
1287
vadimsh232f5a82017-01-20 19:23:44 -08001288 install_packages_fn = noop_install_packages
vadimsh902948e2017-01-20 15:57:32 -08001289 if options.cipd_enabled:
iannuccib58d10d2017-03-18 02:00:25 -07001290 install_packages_fn = lambda run_dir: install_client_and_packages(
vadimsh902948e2017-01-20 15:57:32 -08001291 run_dir, cipd.parse_package_args(options.cipd_packages),
1292 options.cipd_server, options.cipd_client_package,
1293 options.cipd_client_version, cache_dir=options.cipd_cache)
nodirbe642ff2016-06-09 15:51:51 -07001294
nodird6160682017-02-02 13:03:35 -08001295 @contextlib.contextmanager
nodir0ae98b32017-05-11 13:21:53 -07001296 def install_named_caches(run_dir):
nodird6160682017-02-02 13:03:35 -08001297 # WARNING: this function depends on "options" variable defined in the outer
1298 # function.
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001299 assert unicode(run_dir), repr(run_dir)
1300 assert os.path.isabs(run_dir), run_dir
nodir0ae98b32017-05-11 13:21:53 -07001301 caches = [
1302 (os.path.join(run_dir, unicode(relpath)), name)
1303 for name, relpath in options.named_caches
1304 ]
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001305 with named_cache_manager:
nodir0ae98b32017-05-11 13:21:53 -07001306 for path, name in caches:
1307 named_cache_manager.install(path, name)
nodird6160682017-02-02 13:03:35 -08001308 try:
1309 yield
1310 finally:
dnje289d132017-07-07 11:16:44 -07001311 # Uninstall each named cache, returning it to the cache pool. If an
1312 # uninstall fails for a given cache, it will remain in the task's
1313 # temporary space, get cleaned up by the Swarming bot, and be lost.
1314 #
1315 # If the Swarming bot cannot clean up the cache, it will handle it like
1316 # any other bot file that could not be removed.
Marc-Antoine Ruel49f9f8d2018-05-24 15:57:06 -04001317 with named_cache_manager:
nodir0ae98b32017-05-11 13:21:53 -07001318 for path, name in caches:
dnje289d132017-07-07 11:16:44 -07001319 try:
1320 named_cache_manager.uninstall(path, name)
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001321 except local_caching.NamedCacheError:
dnje289d132017-07-07 11:16:44 -07001322 logging.exception('Error while removing named cache %r at %r. '
1323 'The cache will be lost.', path, name)
nodirf33b8d62016-10-26 22:34:58 -07001324
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001325 extra_args = []
1326 command = []
1327 if options.raw_cmd:
1328 command = args
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -05001329 if options.relative_cwd:
1330 a = os.path.normpath(os.path.abspath(options.relative_cwd))
1331 if not a.startswith(os.getcwd()):
1332 parser.error(
1333 '--relative-cwd must not try to escape the working directory')
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001334 else:
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -05001335 if options.relative_cwd:
1336 parser.error('--relative-cwd requires --raw-cmd')
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001337 extra_args = args
1338
1339 data = TaskData(
1340 command=command,
Marc-Antoine Ruel95068cf2017-12-07 21:35:05 -05001341 relative_cwd=options.relative_cwd,
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001342 extra_args=extra_args,
1343 isolated_hash=options.isolated,
1344 storage=None,
1345 isolate_cache=isolate_cache,
1346 outputs=options.output,
1347 install_named_caches=install_named_caches,
1348 leak_temp_dir=options.leak_temp_dir,
1349 root_dir=_to_unicode(options.root_dir),
1350 hard_timeout=options.hard_timeout,
1351 grace_period=options.grace_period,
1352 bot_file=options.bot_file,
1353 switch_to_account=options.switch_to_account,
1354 install_packages_fn=install_packages_fn,
1355 use_symlinks=options.use_symlinks,
1356 env=options.env,
1357 env_prefix=options.env_prefix)
nodirbe642ff2016-06-09 15:51:51 -07001358 try:
nodir90bc8dc2016-06-15 13:35:21 -07001359 if options.isolate_server:
1360 storage = isolateserver.get_storage(
1361 options.isolate_server, options.namespace)
1362 with storage:
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001363 data = data._replace(storage=storage)
nodirf33b8d62016-10-26 22:34:58 -07001364 # Hashing schemes used by |storage| and |isolate_cache| MUST match.
1365 assert storage.hash_algo == isolate_cache.hash_algo
Marc-Antoine Ruel7de52592017-12-07 10:41:12 -05001366 return run_tha_test(data, options.json)
1367 return run_tha_test(data, options.json)
Marc-Antoine Ruel8b11dbd2018-05-18 14:31:22 -04001368 except (
1369 cipd.Error,
1370 local_caching.NamedCacheError,
1371 local_caching.NotFoundError) as ex:
nodirbe642ff2016-06-09 15:51:51 -07001372 print >> sys.stderr, ex.message
1373 return 1
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001374
1375
1376if __name__ == '__main__':
maruel8e4e40c2016-05-30 06:21:07 -07001377 subprocess42.inhibit_os_error_reporting()
csharp@chromium.orgbfb98742013-03-26 20:28:36 +00001378 # Ensure that we are always running with the correct encoding.
vadimsh@chromium.orga4326472013-08-24 02:05:41 +00001379 fix_encoding.fix_encoding()
Marc-Antoine Ruel90c98162013-12-18 15:11:57 -05001380 sys.exit(main(sys.argv[1:]))