OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Wrapper for adding logdog streaming support to swarming tasks.""" | 6 """Wrapper for adding logdog streaming support to swarming tasks.""" |
7 | 7 |
8 import argparse | 8 import argparse |
9 import logging | 9 import logging |
10 import os | 10 import os |
11 import signal | 11 import signal |
12 import subprocess | 12 import subprocess |
13 import sys | 13 import sys |
| 14 import urllib |
14 | 15 |
15 _SRC_PATH = os.path.abspath(os.path.join( | |
16 os.path.dirname(__file__), '..', '..', '..')) | |
17 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) | |
18 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'common', | |
19 'py_utils')) | |
20 | |
21 from devil.utils import signal_handler | |
22 from py_utils import tempfile_ext | |
23 | |
24 PROJECT = 'chromium' | |
25 OUTPUT = 'logdog' | |
26 COORDINATOR_HOST = 'luci-logdog.appspot.com' | |
27 SERVICE_ACCOUNT_JSON = ('/creds/service_accounts' | |
28 '/service-account-luci-logdog-publisher.json') | |
29 | 16 |
30 def CommandParser(): | 17 def CommandParser(): |
31 # Parses the command line arguments being passed in | 18 # Parses the command line arguments being passed in |
32 parser = argparse.ArgumentParser() | 19 parser = argparse.ArgumentParser() |
33 parser.add_argument('--target', required=True, | |
34 help='The test target to be run.') | |
35 parser.add_argument('--logdog-bin-cmd', required=True, | 20 parser.add_argument('--logdog-bin-cmd', required=True, |
36 help='The logdog bin cmd.') | 21 help='Command for running logdog butler binary') |
| 22 parser.add_argument('--project', required=True, |
| 23 help='Name of logdog project') |
| 24 parser.add_argument('--logdog-server', |
| 25 default='services-dot-luci-logdog.appspot.com', |
| 26 help='URL of logdog server, https:// is assumed.') |
| 27 parser.add_argument('--service-account-json', required=True, |
| 28 help='Location of authentication json') |
| 29 parser.add_argument('--prefix', required=True, |
| 30 help='Prefix to be used for logdog stream') |
| 31 parser.add_argument('--source', required=True, |
| 32 help='Location of file for logdog to stream') |
| 33 parser.add_argument('--name', required=True, |
| 34 help='Name to be used for logdog stream') |
37 return parser | 35 return parser |
38 | 36 |
39 def CreateStopTestsMethod(proc): | 37 |
40 def StopTests(signum, _frame): | 38 def CreateUrl(server, project, prefix, name): |
| 39 stream_name = '%s/%s/+/%s' % (project, prefix, name) |
| 40 return 'https://%s/v/?s=%s' % (server, urllib.quote_plus(stream_name)) |
| 41 |
| 42 |
| 43 def CreateSignalForwarder(proc): |
| 44 def handler(signum, _frame): |
41 logging.error('Forwarding signal %s to test process', str(signum)) | 45 logging.error('Forwarding signal %s to test process', str(signum)) |
42 proc.send_signal(signum) | 46 proc.send_signal(signum) |
43 return StopTests | 47 |
| 48 return handler |
| 49 |
44 | 50 |
45 def main(): | 51 def main(): |
46 parser = CommandParser() | 52 parser = CommandParser() |
47 args, extra_cmd_args = parser.parse_known_args(sys.argv[1:]) | 53 args, test_cmd = parser.parse_known_args(sys.argv[1:]) |
| 54 logging.basicConfig(level=logging.INFO) |
| 55 if not test_cmd: |
| 56 parser.error('Must specify command to run after the logdog flags') |
| 57 test_proc = subprocess.Popen(test_cmd) |
| 58 original_sigterm_handler = signal.signal( |
| 59 signal.SIGTERM, CreateSignalForwarder(test_proc)) |
| 60 try: |
| 61 result = test_proc.wait() |
| 62 finally: |
| 63 signal.signal(signal.SIGTERM, original_sigterm_handler) |
| 64 if '${SWARMING_TASK_ID}' in args.prefix: |
| 65 args.prefix = args.prefix.replace('${SWARMING_TASK_ID}', |
| 66 os.environ.get('SWARMING_TASK_ID')) |
| 67 url = CreateUrl('luci-logdog.appspot.com', args.project, args.prefix, |
| 68 args.name) |
| 69 logdog_cmd = [args.logdog_bin_cmd, '-project', args.project, |
| 70 '-output', 'logdog,host=%s' % args.logdog_server, |
| 71 '-prefix', args.prefix, |
| 72 '-service-account-json', args.service_account_json, |
| 73 'stream', '-source', args.source, |
| 74 '-stream', '-name=%s' % args.name] |
48 | 75 |
49 logging.basicConfig(level=logging.INFO) | 76 if not os.path.exists(args.logdog_bin_cmd): |
50 with tempfile_ext.NamedTemporaryDirectory() as logcat_output_dir: | 77 logging.error( |
51 test_cmd = [ | 78 'Logdog binary %s unavailable. Unable to upload logcats.', |
52 os.path.join('bin', 'run_%s' % args.target), | 79 args.logdog_bin_cmd) |
53 '--logcat-output-file', os.path.join(logcat_output_dir, 'logcats'), | 80 elif not os.path.exists(args.source): |
54 '--upload-logcats-file', | 81 logging.error( |
55 '--target-devices-file', '${SWARMING_BOT_FILE}', | 82 'Logcat sources not found at %s. Unable to upload logcats.', |
56 '-v'] + extra_cmd_args | 83 args.source) |
| 84 else: |
| 85 subprocess.call(logdog_cmd) |
| 86 logging.info('Logcats are located at: %s', url) |
| 87 return result |
57 | 88 |
58 with tempfile_ext.NamedTemporaryDirectory( | |
59 prefix='tmp_android_logdog_wrapper') as temp_directory: | |
60 if not os.path.exists(args.logdog_bin_cmd): | |
61 logging.error( | |
62 'Logdog binary %s unavailable. Unable to create logdog client', | |
63 args.logdog_bin_cmd) | |
64 else: | |
65 streamserver_uri = 'unix:%s' % os.path.join(temp_directory, | |
66 'butler.sock') | |
67 prefix = os.path.join('android', 'swarming', 'logcats', | |
68 os.environ.get('SWARMING_TASK_ID')) | |
69 | |
70 # Call test_cmdline through logdog butler subcommand. | |
71 test_cmd = [ | |
72 args.logdog_bin_cmd, '-project', PROJECT, | |
73 '-output', OUTPUT, | |
74 '-prefix', prefix, | |
75 '--service-account-json', SERVICE_ACCOUNT_JSON, | |
76 '-coordinator-host', COORDINATOR_HOST, | |
77 'run', '-streamserver-uri', streamserver_uri, '--'] + test_cmd | |
78 | |
79 test_proc = subprocess.Popen(test_cmd) | |
80 with signal_handler.SignalHandler(signal.SIGTERM, | |
81 CreateStopTestsMethod(test_proc)): | |
82 result = test_proc.wait() | |
83 return result | |
84 | 89 |
85 if __name__ == '__main__': | 90 if __name__ == '__main__': |
86 sys.exit(main()) | 91 sys.exit(main()) |
OLD | NEW |