| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import argparse |
| 7 import json |
| 8 import logging |
| 9 import os |
| 10 import subprocess |
| 11 import sys |
| 12 import tempfile |
| 13 |
| 14 |
| 15 # Install Infra build environment. |
| 16 BUILD_ROOT = os.path.dirname(os.path.dirname(os.path.dirname( |
| 17 os.path.abspath(__file__)))) |
| 18 sys.path.insert(0, os.path.join(BUILD_ROOT, 'scripts')) |
| 19 |
| 20 from common import chromium_utils |
| 21 from common import env |
| 22 from slave import cipd |
| 23 from slave import infra_platform |
| 24 from slave import robust_tempdir |
| 25 from slave import update_scripts |
| 26 |
| 27 |
| 28 LOGGER = logging.getLogger('kitchen_run') |
| 29 |
| 30 |
| 31 KITCHEN_CIPD_VERSION = 'latest' |
| 32 |
| 33 |
| 34 CIPD_BINARIES = { |
| 35 ('linux', 32): cipd.CipdBinary( |
| 36 cipd.CipdPackage('infra/tools/luci/kitchen/linux-386', |
| 37 KITCHEN_CIPD_VERSION), |
| 38 'kitchen'), |
| 39 ('linux', 64): cipd.CipdBinary( |
| 40 cipd.CipdPackage('infra/tools/luci/kitchen/linux-amd64', |
| 41 KITCHEN_CIPD_VERSION), |
| 42 'kitchen'), |
| 43 ('mac', 64): cipd.CipdBinary( |
| 44 cipd.CipdPackage('infra/tools/luci/kitchen/mac-amd64', |
| 45 KITCHEN_CIPD_VERSION), |
| 46 'kitchen'), |
| 47 ('win', 32): cipd.CipdBinary( |
| 48 cipd.CipdPackage('infra/tools/luci/kitchen/windows-386', |
| 49 KITCHEN_CIPD_VERSION), |
| 50 'kitchen.exe'), |
| 51 ('win', 64): cipd.CipdBinary( |
| 52 cipd.CipdPackage('infra/tools/luci/kitchen/windows-amd64', |
| 53 KITCHEN_CIPD_VERSION), |
| 54 'kitchen.exe'), |
| 55 } |
| 56 |
| 57 |
| 58 def _call(cmd, **kwargs): |
| 59 LOGGER.info('Executing command: %s', cmd) |
| 60 exit_code = subprocess.call(cmd, **kwargs) |
| 61 LOGGER.info('Command %s finished with exit code %d.', cmd, exit_code) |
| 62 return exit_code |
| 63 |
| 64 |
| 65 def _install_cipd_packages(path, *binaries): |
| 66 """Bootstraps CIPD in |path| and installs requested |binaries|. |
| 67 |
| 68 Args: |
| 69 path (str): The CIPD installation root. |
| 70 binaries (list of CipdBinary): The set of CIPD binaries to install. |
| 71 |
| 72 Returns (list): The paths to the binaries. |
| 73 """ |
| 74 cmd = [ |
| 75 sys.executable, |
| 76 os.path.join(env.Build, 'scripts', 'slave', 'cipd.py'), |
| 77 '--dest-directory', path, |
| 78 '-vv' if logging.getLogger().level == logging.DEBUG else '-v', |
| 79 ] |
| 80 for b in binaries: |
| 81 cmd += ['-P', '%s@%s' % (b.package.name, b.package.version)] |
| 82 |
| 83 exit_code = _call(cmd) |
| 84 if exit_code != 0: |
| 85 raise Exception('Failed to install CIPD packages.') |
| 86 return [os.path.join(path, b.relpath) for b in binaries] |
| 87 |
| 88 |
| 89 def main(args): |
| 90 basedir = os.getcwd() |
| 91 cipd_path = os.path.join(basedir, '.kitchen_cipd') |
| 92 (kitchen,) = _install_cipd_packages( |
| 93 cipd_path, CIPD_BINARIES[infra_platform.get()]) |
| 94 |
| 95 with robust_tempdir.RobustTempdir( |
| 96 prefix='.kitchen_run', leak=args.leak) as rt: |
| 97 # Explicitly clean up possibly leaked temporary directories |
| 98 # from previous runs. |
| 99 rt.cleanup(basedir) |
| 100 |
| 101 tempdir = rt.tempdir(basedir) |
| 102 LOGGER.info('Using temporary directory: [%s].', tempdir) |
| 103 |
| 104 properties_file = os.path.join(tempdir, 'kitchen_properties.json') |
| 105 with open(properties_file, 'w') as f: |
| 106 json.dump(args.build_properties, f) |
| 107 |
| 108 return _call([ |
| 109 kitchen, 'cook', |
| 110 '-repository', args.repository, |
| 111 '-revision', args.revision, |
| 112 '-recipe', args.recipe, |
| 113 '-properties-file', properties_file, |
| 114 '-workdir', tempdir, |
| 115 ]) |
| 116 |
| 117 |
| 118 def shell_main(argv): |
| 119 parser = argparse.ArgumentParser() |
| 120 parser.add_argument('--repository', required=True, |
| 121 help='URL of a git repository to fetch.') |
| 122 parser.add_argument('--revision', |
| 123 help='Git commit hash to check out.') |
| 124 parser.add_argument('--recipe', required=True, |
| 125 help='Name of the recipe to run') |
| 126 parser.add_argument('--build-properties-gz', dest='build_properties', |
| 127 type=chromium_utils.convert_gz_json_type, default={}, |
| 128 help='Build properties in b64 gz JSON format') |
| 129 parser.add_argument('--leak', action='store_true', |
| 130 help='Refrain from cleaning up generated artifacts.') |
| 131 parser.add_argument('--verbose', action='store_true') |
| 132 args = parser.parse_args(argv[1:]) |
| 133 |
| 134 logging.basicConfig(level=(logging.DEBUG if args.verbose else logging.INFO)) |
| 135 |
| 136 if update_scripts.update_scripts(): |
| 137 # Re-execute with the updated kitchen_run.py. |
| 138 return _call([sys.executable] + argv) |
| 139 |
| 140 return main(args) |
| 141 |
| 142 |
| 143 if __name__ == '__main__': |
| 144 sys.exit(shell_main(sys.argv)) |
| OLD | NEW |