1#!/usr/bin/env python
2# Copyright 2017 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"""
7This script runs swarming_xcode_install on the bots.  It should be run when we
8need to upgrade all the swarming testers.  It:
9  1) Packages two python files into an isolate.
10  2) Runs the isolate on swarming machines that satisfy certain dimensions.
11
12Example usage:
13  $  ./build/run_swarming_xcode_install.py  --luci_path ~/work/luci-py \
14       --swarming-server touch-swarming.appspot.com \
15       --isolate-server touch-isolate.appspot.com
16"""
17
18from __future__ import print_function
19
20import argparse
21import os
22import shutil
23import subprocess
24import sys
25import tempfile
26
27
28def main():
29  parser = argparse.ArgumentParser(
30      description='Run swarming_xcode_install on the bots.')
31  parser.add_argument('--luci_path', required=True, type=os.path.abspath)
32  parser.add_argument('--swarming-server', required=True, type=str)
33  parser.add_argument('--isolate-server', required=True, type=str)
34  parser.add_argument('--batches', type=int, default=25,
35                      help="Run xcode install in batches of size |batches|.")
36  parser.add_argument('--dimension', nargs=2, action='append')
37  args = parser.parse_args()
38
39  args.dimension = args.dimension or []
40
41  script_dir = os.path.dirname(os.path.abspath(__file__))
42  tmp_dir = tempfile.mkdtemp(prefix='swarming_xcode')
43  try:
44    print('Making isolate.')
45    shutil.copyfile(os.path.join(script_dir, 'swarming_xcode_install.py'),
46                    os.path.join(tmp_dir, 'swarming_xcode_install.py'))
47    shutil.copyfile(os.path.join(script_dir, 'mac_toolchain.py'),
48                    os.path.join(tmp_dir, 'mac_toolchain.py'))
49
50    luci_client = os.path.join(args.luci_path, 'client')
51    cmd = [
52      sys.executable, os.path.join(luci_client, 'isolateserver.py'), 'archive',
53      '-I', args.isolate_server, tmp_dir,
54    ]
55    isolate_hash = subprocess.check_output(cmd).split()[0]
56
57    print('Running swarming_xcode_install.')
58    # TODO(crbug.com/765361): The dimensions below should be updated once
59    # swarming for iOS is fleshed out, likely removing xcode_version 9 and
60    # adding different dimensions.
61    luci_tools = os.path.join(luci_client, 'tools')
62    dimensions = [['pool', 'Chrome'], ['xcode_version', '9.0']] + args.dimension
63    dim_args = []
64    for d in dimensions:
65      dim_args += ['--dimension'] + d
66    cmd = [
67      sys.executable, os.path.join(luci_tools, 'run_on_bots.py'),
68      '--swarming', args.swarming_server, '--isolate-server',
69      args.isolate_server, '--priority', '20', '--batches', str(args.batches),
70      '--tags', 'name:run_swarming_xcode_install',
71    ] + dim_args + ['--name', 'run_swarming_xcode_install', '--', isolate_hash,
72      'python', 'swarming_xcode_install.py',
73    ]
74    subprocess.check_call(cmd)
75    print('All tasks completed.')
76
77  finally:
78    shutil.rmtree(tmp_dir)
79  return 0
80
81
82if __name__ == '__main__':
83  sys.exit(main())
84