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 env = [] 38 cleanup = rbe.setup(env, sys.stdout if args.print else subprocess.DEVNULL) 39 src_root = os.path.normpath( 40 os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../..')) 41 env = rbe.env_array_to_dict(rbe.prepare_env(env)) 42 env['PATH'] = os.getenv('PATH') 43 for d in ['FLAG_log_dir', 'RBE_output_dir', 'RBE_proxy_log_dir']: 44 env[d] = '/tmp' # We want the logs in /tmp instead of out. 45 try: 46 # Bootstrap the RBE proxy. 47 bootstrap_cmd = rbe.get_nsjail_bin_wrapper() + \ 48 [os.path.join(rbe.TOOLS_DIR, 'bootstrap')] 49 shell_env = ' '.join(['%s=%s' % (k,v) for k, v in env.items()]) 50 if args.print: 51 print('Bootstrap RBE reproxy:') 52 print('cd ' + src_root) 53 print('%s %s' % (shell_env, ' '.join(bootstrap_cmd))) 54 subprocess.check_call( 55 bootstrap_cmd, env=env, cwd=src_root, stdout=subprocess.DEVNULL) 56 # Execute the remote command. 57 rewrapper_cmd = rbe.get_nsjail_bin_wrapper() + [ 58 os.path.join(rbe.TOOLS_DIR, 'rewrapper'), 59 '--platform=container-image=docker://gcr.io/androidbuild-re-dockerimage/android-build-remoteexec-image@sha256:582efb38f0c229ea39952fff9e132ccbe183e14869b39888010dacf56b360d62', \ 60 '--labels=type=tool', 61 '--exec_strategy=remote', 62 '--dial_timeout=5s', 63 '--exec_root=' + src_root, 64 '--', 65 ] + args.command.split() 66 if args.print: 67 print('Run remote command with RBE:') 68 print('%s %s' % (shell_env, ' '.join(rewrapper_cmd))) 69 subprocess.check_call(rewrapper_cmd, env=env, cwd=src_root) 70 finally: 71 # Shut down the RBE proxy. 72 if args.print: 73 print('RBE proxy shutdown:') 74 print('killall reproxy') 75 subprocess.call( 76 ['killall', 'reproxy'], 77 stdout=subprocess.DEVNULL, 78 stderr=subprocess.DEVNULL) 79 cleanup() 80 81 82if __name__ == '__main__': 83 main() 84