1# Copyright 2020 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Runs a single action remotely with RBE.""" 16 17import argparse 18import os 19import rbe 20import subprocess 21import sys 22 23 24def main(): 25 parser = argparse.ArgumentParser( 26 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 27 parser.add_argument( 28 '--command', 29 default='echo RBE check successful.', 30 help='Command to run remotely with RBE.') 31 parser.add_argument( 32 '--print', '-p', 33 action='store_true', 34 help='Prints the executed commands') 35 args = parser.parse_args() 36 37 cleanup = rbe.setup([], sys.stdout if args.print else subprocess.DEVNULL) 38 src_root = os.path.normpath( 39 os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../..')) 40 env = rbe.env_array_to_dict(rbe.prepare_env([])) 41 env['PATH'] = os.getenv('PATH') 42 for d in ['FLAG_log_dir', 'RBE_output_dir', 'RBE_proxy_log_dir']: 43 env[d] = '/tmp' # We want the logs in /tmp instead of out. 44 try: 45 # Bootstrap the RBE proxy. 46 bootstrap_cmd = rbe.get_nsjail_bin_wrapper() + \ 47 [os.path.join(rbe.TOOLS_DIR, 'bootstrap')] 48 shell_env = ' '.join(['%s=%s' % (k,v) for k, v in env.items()]) 49 if args.print: 50 print('Bootstrap RBE reproxy:') 51 print('cd ' + src_root) 52 print('%s %s' % (shell_env, ' '.join(bootstrap_cmd))) 53 subprocess.check_call( 54 bootstrap_cmd, env=env, cwd=src_root, stdout=subprocess.DEVNULL) 55 # Execute the remote command. 56 rewrapper_cmd = rbe.get_nsjail_bin_wrapper() + [ 57 os.path.join(rbe.TOOLS_DIR, 'rewrapper'), 58 '--platform=container-image=docker://gcr.io/androidbuild-re-dockerimage/android-build-remoteexec-image@sha256:582efb38f0c229ea39952fff9e132ccbe183e14869b39888010dacf56b360d62', \ 59 '--labels=type=tool', 60 '--exec_strategy=remote', 61 '--dial_timeout=5s', 62 '--exec_root=' + src_root, 63 '--', 64 ] + args.command.split() 65 if args.print: 66 print('Run remote command with RBE:') 67 print('%s %s' % (shell_env, ' '.join(rewrapper_cmd))) 68 subprocess.check_call(rewrapper_cmd, env=env, cwd=src_root) 69 finally: 70 # Shut down the RBE proxy. 71 if args.print: 72 print('RBE proxy shutdown:') 73 print('killall reproxy') 74 subprocess.call( 75 ['killall', 'reproxy'], 76 stdout=subprocess.DEVNULL, 77 stderr=subprocess.DEVNULL) 78 cleanup() 79 80 81if __name__ == '__main__': 82 main() 83