| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. |
| 5 |
| 6 """ |
| 7 This is a dumb script that will: |
| 8 * compile jobsim_client (for linux-amd64) |
| 9 * upload a simple cipd package containing only jobsim_client with the 'latest' |
| 10 ref. The packaeg name is 'infra/experimental/dm/jobsim_client/linux-amd64'. |
| 11 * Print a JSONPB-encoded EnsureGraphDataReq that runs jobsim_client with the |
| 12 provided strings to calculate edit-distance(a, b, transposition?). |
| 13 """ |
| 14 |
| 15 import argparse |
| 16 import json |
| 17 import os |
| 18 import pprint |
| 19 import shutil |
| 20 import subprocess |
| 21 import tempfile |
| 22 |
| 23 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 24 |
| 25 def compile_pkg(pkg_dir, os_name, arch_name): |
| 26 print 'building jobsim_client' |
| 27 env = os.environ.copy() |
| 28 env.update( |
| 29 GOOS = os_name, |
| 30 GOARCH = arch_name, |
| 31 ) |
| 32 subprocess.check_call( |
| 33 ['go', 'build', 'github.com/luci/luci-go/dm/tools/jobsim_client'], |
| 34 cwd=pkg_dir, env=env) |
| 35 |
| 36 def upload_pkg(pkg_dir, pkg_name_prefix, os_name, arch_name): |
| 37 print 'creating jobsim_client package' |
| 38 |
| 39 pkg_name = '%s/%s-%s' % (pkg_name_prefix, os_name, arch_name) |
| 40 |
| 41 fd, tfile = tempfile.mkstemp('-cipd-output.json') |
| 42 os.close(fd) |
| 43 try: |
| 44 subprocess.check_call(['cipd', 'create', '-name', pkg_name, |
| 45 '-ref', 'latest', '-in', pkg_dir, |
| 46 '-install-mode', 'copy', '-json-output', tfile]) |
| 47 with open(tfile, 'r') as tfileData: |
| 48 out_json = json.load(tfileData) |
| 49 version = out_json[u'result'][u'instance_id'].encode('utf-8') |
| 50 finally: |
| 51 os.unlink(tfile) |
| 52 |
| 53 print 'uploaded %s:%s' % (pkg_name, version) |
| 54 |
| 55 return pkg_name, version |
| 56 |
| 57 def print_req(opts, pkg_name, version): |
| 58 def dumps(obj): |
| 59 return json.dumps(obj, sort_keys=True, separators=(',', ':')) |
| 60 |
| 61 cpu = { |
| 62 'amd64': 'x86-64', |
| 63 }[opts.arch] |
| 64 |
| 65 os_name = { |
| 66 'linux': 'Linux', |
| 67 }[opts.os] |
| 68 |
| 69 command = ['jobsim_client', 'edit-distance', '-dm-host', '${DM.HOST}', |
| 70 '-execution-auth-path', '${DM.EXECUTION.AUTH:PATH}', |
| 71 '-quest-desc-path', '${DM.QUEST.DATA.DESC:PATH}'] |
| 72 if opts.use_transposition: |
| 73 command.append('-use-transposition') |
| 74 |
| 75 distParams = { |
| 76 'scheduling': { |
| 77 'dimensions': { |
| 78 'cpu': cpu, |
| 79 'os': os_name, |
| 80 'pool': opts.pool, |
| 81 }, |
| 82 }, |
| 83 'meta': {'name_prefix': 'dm jobsim client'}, |
| 84 'job': { |
| 85 'inputs': { |
| 86 'cipd': { |
| 87 'server': 'https://chrome-infra-packages.appspot.com', |
| 88 'by_path': { |
| 89 '.': { |
| 90 'pkg': [ |
| 91 { |
| 92 'name': pkg_name, |
| 93 'version': version if opts.pin else 'latest', |
| 94 }, |
| 95 ] |
| 96 } |
| 97 } |
| 98 } |
| 99 }, |
| 100 'command': command, |
| 101 } |
| 102 } |
| 103 |
| 104 params = { |
| 105 'a': opts.a, |
| 106 'b': opts.b, |
| 107 } |
| 108 |
| 109 desc = { |
| 110 'quest': [ |
| 111 { |
| 112 'distributor_config_name': 'swarming', |
| 113 'parameters': dumps(params), |
| 114 'distributor_parameters': dumps(distParams), |
| 115 'meta': { |
| 116 'timeouts': { |
| 117 'start': '600s', |
| 118 'run': '300s', |
| 119 'stop': '300s', |
| 120 } |
| 121 }, |
| 122 } |
| 123 ], |
| 124 'quest_attempt': [ |
| 125 {'nums': [1]}, |
| 126 ] |
| 127 } |
| 128 |
| 129 print dumps(desc) |
| 130 |
| 131 def main(): |
| 132 parser = argparse.ArgumentParser( |
| 133 description=__doc__, formatter_class=argparse.RawTextHelpFormatter) |
| 134 parser.add_argument('--use-transposition', action='store_true', default=False, |
| 135 help=('Use Damerau-Levenshtein distance calculation ' |
| 136 'instead of plain Levenshtein distance.')) |
| 137 parser.add_argument('a', type=str, help='The "a" string to calculate for.') |
| 138 parser.add_argument('b', type=str, help='The "b" string to calculate for.') |
| 139 |
| 140 plat_grp = parser.add_argument_group( |
| 141 'platform', 'Options for the target platform of the job.') |
| 142 plat_grp.add_argument('--os', choices=('linux',), default='linux', |
| 143 help='The OS to compile/run on.') |
| 144 plat_grp.add_argument('--arch', choices=('amd64',), default='amd64', |
| 145 help='The Arch to compile/run on.') |
| 146 plat_grp.add_argument('--pool', type=str, default='default', |
| 147 help='The swarming pool to use.') |
| 148 plat_grp.add_argument('--pin', action='store_true', default=False, |
| 149 help='Emit the request with a pinned package version' |
| 150 ' instead of "latest".') |
| 151 |
| 152 cipd_grp = parser.add_argument_group('cipd', 'cipd packaging options') |
| 153 cipd_grp.add_argument('--cipd-service-url', default=None, |
| 154 help='The CIPD service to upload to.') |
| 155 cipd_grp.add_argument('--cipd-service-account-json', default=None, |
| 156 help='The CIPD service account JSON file to use.') |
| 157 cipd_grp.add_argument('--cipd-name', |
| 158 default='infra/experimental/dm/jobsim_client', |
| 159 help='The CIPD package name prefix to upload to. This ' |
| 160 'will be appended with the standard os-arch suffix.') |
| 161 |
| 162 opts = parser.parse_args() |
| 163 |
| 164 # Use local path for determinisim. |
| 165 pkg_dir = os.path.join(THIS_DIR, 'pkg_dir') |
| 166 shutil.rmtree(pkg_dir, ignore_errors=True) |
| 167 os.mkdir(pkg_dir) |
| 168 try: |
| 169 compile_pkg(pkg_dir, opts.os, opts.arch) |
| 170 pkg_name, version = upload_pkg(pkg_dir, opts.cipd_name, opts.os, opts.arch) |
| 171 print_req(opts, pkg_name, version) |
| 172 finally: |
| 173 shutil.rmtree(pkg_dir, ignore_errors=True) |
| 174 |
| 175 |
| 176 if __name__ == '__main__': |
| 177 main() |
| OLD | NEW |