OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 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 """Runs hello_world.py, through hello_world.isolate, remotely on a Swarm slave. | |
7 """ | |
8 | |
9 import datetime | |
10 import getpass | |
11 import hashlib | |
12 import optparse | |
13 import os | |
14 import shutil | |
15 import subprocess | |
16 import sys | |
17 import tempfile | |
18 | |
19 | |
20 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
21 | |
22 # Mapping of the sys.platform value into Swarm OS value. | |
23 OSES = {'win32': 'win', 'linux2': 'linux', 'darwin': 'mac'} | |
24 | |
25 | |
26 def run(cmd, verbose): | |
27 cmd = cmd[:] | |
28 cmd.extend(['--verbose'] * verbose) | |
29 print('Running: %s' % ' '.join(cmd)) | |
30 cmd = [sys.executable, os.path.join(ROOT_DIR, '..', cmd[0])] + cmd[1:] | |
31 if sys.platform != 'win32': | |
32 cmd = ['time', '-p'] + cmd | |
33 subprocess.check_call(cmd) | |
34 | |
35 | |
36 def simple(isolate_server, swarming_server, prefix, os_slave, verbose): | |
37 try: | |
38 # All the files are put in a temporary directory. This is optional and | |
39 # simply done so the current directory doesn't have the following files | |
40 # created: | |
41 # - hello_world.isolated | |
42 # - hello_world.isolated.state | |
43 tempdir = tempfile.mkdtemp(prefix='hello_world') | |
44 isolated = os.path.join(tempdir, 'hello_world.isolated') | |
45 | |
46 run( | |
47 [ | |
48 'isolate.py', | |
49 'check', | |
50 '--isolate', os.path.join(ROOT_DIR, 'hello_world.isolate'), | |
51 '--isolated', isolated, | |
52 # Manually override the OS. | |
53 '--variable', 'OS', OSES[os_slave], | |
54 ], | |
55 verbose) | |
56 | |
57 run( | |
58 [ | |
59 'swarming.py', | |
60 'run', | |
61 '--os', os_slave, | |
62 '--swarming', swarming_server, | |
63 '--task-prefix', prefix, | |
64 '--isolate-server', isolate_server, | |
65 isolated, | |
66 ], | |
67 verbose) | |
68 return 0 | |
69 finally: | |
70 shutil.rmtree(tempdir) | |
71 | |
72 | |
73 def involved(isolate_server, swarming_server, prefix, os_slave, verbose): | |
74 """Runs all the steps involved individually, for demonstration purposes.""" | |
75 try: | |
76 # All the files are put in a temporary directory. This is optional and | |
77 # simply done so the current directory doesn't have the following files | |
78 # created: | |
79 # - hello_world.isolated | |
80 # - hello_world.isolated.state | |
81 tempdir = tempfile.mkdtemp(prefix='hello_world') | |
82 isolated = os.path.join(tempdir, 'hello_world.isolated') | |
83 | |
84 print('Archiving') | |
85 run( | |
86 [ | |
87 'isolate.py', | |
88 'archive', | |
89 '--isolate', os.path.join(ROOT_DIR, 'hello_world.isolate'), | |
90 '--isolated', isolated, | |
91 '--outdir', isolate_server, | |
92 # Manually override the OS. | |
93 '--variable', 'OS', OSES[os_slave], | |
94 ], | |
95 verbose) | |
96 hashval = hashlib.sha1(open(isolated, 'rb').read()).hexdigest() | |
97 finally: | |
98 shutil.rmtree(tempdir) | |
99 | |
100 print('\nRunning') | |
101 run( | |
102 [ | |
103 'swarming.py', | |
104 'trigger', | |
105 '--os', os_slave, | |
106 '--isolate-server', isolate_server, | |
107 '--swarming', swarming_server, | |
108 '--task-prefix', prefix, | |
109 '--task', | |
110 hashval, | |
111 'hello_world', | |
112 # Number of shards. | |
113 '1', | |
114 '*', | |
115 ], | |
116 verbose) | |
117 | |
118 print('\nGetting results') | |
119 run( | |
120 [ | |
121 'swarming.py', | |
122 'collect', | |
123 '--swarming', swarming_server, | |
124 prefix + 'hello_world', | |
125 ], | |
126 verbose) | |
127 return 0 | |
128 | |
129 | |
130 def main(): | |
131 parser = optparse.OptionParser(description=sys.modules[__name__].__doc__) | |
132 parser.add_option( | |
133 '-I', '--isolate-server', | |
134 metavar='URL', default='', | |
135 help='Isolate server to use') | |
136 parser.add_option( | |
137 '-S', '--swarming', | |
138 metavar='URL', default='', | |
139 help='Swarming server to use') | |
140 parser.add_option('-v', '--verbose', action='count', default=0) | |
141 parser.add_option( | |
142 '-o', '--os', default=sys.platform, | |
143 help='Swarm OS image to request. Should be one of the valid sys.platform ' | |
144 'values like darwin, linux2 or win32 default: %default.') | |
145 parser.add_option( | |
146 '--short', action='store_true', | |
147 help='Use \'swarming.py run\' instead of running each step manually') | |
148 options, args = parser.parse_args() | |
149 if args: | |
150 parser.error('Unsupported argument %s' % args) | |
151 if not options.isolate_server: | |
152 parser.error('--isolate-server is required.') | |
153 if not options.swarming: | |
154 parser.error('--swarming is required.') | |
155 | |
156 prefix = getpass.getuser() + '-' + datetime.datetime.now().isoformat() + '-' | |
157 try: | |
158 if options.short: | |
159 return simple( | |
160 options.isolate_server, | |
161 options.swarming, | |
162 prefix, | |
163 options.os, | |
164 options.verbose) | |
165 else: | |
166 return involved( | |
167 options.isolate_server, | |
168 options.swarming, | |
169 prefix, | |
170 options.os, | |
171 options.verbose) | |
172 except subprocess.CalledProcessError as e: | |
173 print e.returncode or 1 | |
174 | |
175 | |
176 if __name__ == '__main__': | |
177 sys.exit(main()) | |
OLD | NEW |