Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: client/example/3_swarming_trigger_collect.py

Issue 1337633002: Reapply "Isolated task support in Endpoints API: client side (3/3)" and fixes" (Closed) Base URL: git@github.com:luci/luci-py.git@master
Patch Set: We did it, again Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2012 The Swarming Authors. All rights reserved. 2 # Copyright 2012 The Swarming Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 that 3 # Use of this source code is governed under the Apache License, Version 2.0 that
4 # can be found in the LICENSE file. 4 # can be found in the LICENSE file.
5 5
6 """Runs hello_world.py, through hello_world.isolate, remotely on a Swarming 6 """Runs hello_world.py, through hello_world.isolate, remotely on a Swarming
7 slave. 7 slave.
8 8
9 It compiles and archives via 'isolate.py archive', then discard the local files. 9 It compiles and archives via 'isolate.py archive', then discard the local files.
10 After, it triggers and finally collects the results. 10 After, it triggers and finally collects the results.
11
12 Creates 2 shards and instructs the script to produce a file in the output
13 directory.
11 """ 14 """
12 15
13 import hashlib
14 import os 16 import os
15 import shutil 17 import shutil
16 import subprocess 18 import subprocess
17 import sys 19 import sys
18 import tempfile 20 import tempfile
19 21
20 # Pylint can't find common.py that's in the same directory as this file. 22 # Pylint can't find common.py that's in the same directory as this file.
21 # pylint: disable=F0401 23 # pylint: disable=F0401
22 import common 24 import common
23 25
24 26
25 def main(): 27 def main():
26 options = common.parse_args(use_isolate_server=True, use_swarming=True) 28 options = common.parse_args(use_isolate_server=True, use_swarming=True)
27 try: 29 try:
28 tempdir = tempfile.mkdtemp(prefix=u'hello_world') 30 tempdir = tempfile.mkdtemp(prefix=u'hello_world')
29 try: 31 try:
30 # All the files are put in a temporary directory. This is optional and 32 _, hashval = common.isolate(
31 # simply done so the current directory doesn't have the following files 33 tempdir, options.isolate_server, options.swarming_os, options.verbose)
32 # created:
33 # - hello_world.isolated
34 # - hello_world.isolated.state
35 isolated = os.path.join(tempdir, 'hello_world.isolated')
36 common.note('Archiving to %s' % options.isolate_server)
37 common.run(
38 [
39 'isolate.py',
40 'archive',
41 '--isolate', os.path.join('payload', 'hello_world.isolate'),
42 '--isolated', isolated,
43 '--isolate-server', options.isolate_server,
44 '--config-variable', 'OS', options.swarming_os,
45 ], options.verbose)
46 with open(isolated, 'rb') as f:
47 hashval = hashlib.sha1(f.read()).hexdigest()
48 34
49 json_file = os.path.join(tempdir, 'task.json') 35 json_file = os.path.join(tempdir, 'task.json')
50 common.note('Running on %s' % options.swarming) 36 common.note('Running on %s' % options.swarming)
51 cmd = [ 37 cmd = [
52 'swarming.py', 38 'swarming.py',
53 'trigger', 39 'trigger',
54 '--swarming', options.swarming, 40 '--swarming', options.swarming,
55 '--isolate-server', options.isolate_server, 41 '--isolate-server', options.isolate_server,
56 '--dimension', 'os', options.swarming_os, 42 '--dimension', 'os', options.swarming_os,
57 '--task-name', options.task_name, 43 '--task-name', options.task_name,
58 '--dump-json', json_file, 44 '--dump-json', json_file,
59 '--isolated', hashval, 45 '--isolated', hashval,
46 '--shards', '2',
60 ] 47 ]
61 if options.idempotent: 48 if options.idempotent:
62 cmd.append('--idempotent') 49 cmd.append('--idempotent')
63 if options.priority is not None: 50 if options.priority is not None:
64 cmd.extend(('--priority', str(options.priority))) 51 cmd.extend(('--priority', str(options.priority)))
52 cmd.extend(('--', '${ISOLATED_OUTDIR}'))
65 common.run(cmd, options.verbose) 53 common.run(cmd, options.verbose)
66 54
67 common.note('Getting results from %s' % options.swarming) 55 common.note('Getting results from %s' % options.swarming)
68 common.run( 56 common.run(
69 [ 57 [
70 'swarming.py', 58 'swarming.py',
71 'collect', 59 'collect',
72 '--swarming', options.swarming, 60 '--swarming', options.swarming,
73 '--json', json_file, 61 '--json', json_file,
62 '--task-output-dir', 'example_result',
74 ], options.verbose) 63 ], options.verbose)
64 for root, _, files in os.walk('example_result'):
65 for name in files:
66 p = os.path.join(root, name)
67 with open(p, 'rb') as f:
68 print('%s content:' % p)
69 print(f.read())
75 return 0 70 return 0
76 finally: 71 finally:
77 shutil.rmtree(tempdir) 72 shutil.rmtree(tempdir)
78 except subprocess.CalledProcessError as e: 73 except subprocess.CalledProcessError as e:
79 print e.returncode or 1 74 return e.returncode
80 75
81 76
82 if __name__ == '__main__': 77 if __name__ == '__main__':
83 sys.exit(main()) 78 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698