"""Run with sudo on the server; never prints the service token. Uses a simulated wrapper, not a real Android APK build. """ import base64 import hashlib import json import os import pwd import sys from pathlib import Path import time import urllib.request token = next(line.split('=', 1)[1] for line in Path('/etc/remotecompile/environment').read_text().splitlines() if line.startswith('REMOTE_BUILD_TOKEN=')) def api(method, route, body=None): data = json.dumps(body).encode() if body is not None else None request = urllib.request.Request('http://127.0.0.1:8765/api/v1' + route, data=data, method=method, headers={'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'}) with urllib.request.urlopen(request, timeout=30) as response: value = response.read() return value if '/artifacts/' in route else json.loads(value) project = api('POST', '/projects')['id'] wrapper = b'#!/bin/sh\nset -eu\n"$JAVA_HOME/bin/java" -version\nmkdir -p app/build/outputs/apk/debug\nprintf remote-service-smoke > app/build/outputs/apk/debug/app-debug.apk\n' api('POST', f'/projects/{project}/sync', {'revision': 'deployment-smoke', 'files': {'gradlew': { 'hash': hashlib.sha256(wrapper).hexdigest(), 'data': base64.b64encode(wrapper).decode()}}}) build_id = api('POST', f'/projects/{project}/builds', {'revision': 'deployment-smoke', 'task': ':app:assembleDebug'})['id'] for _ in range(120): build = api('GET', f'/builds/{build_id}') if build['status'] not in ('queued', 'running'): break time.sleep(.25) assert build['status'] == 'success', build artifact = build['artifacts'][0] content = api('GET', f'/builds/{build_id}/artifacts/' + artifact['name']) assert content == b'remote-service-smoke' assert hashlib.sha256(content).hexdigest() == artifact['hash'] print(json.dumps({'status': 'passed', 'project': project, 'build': build_id, 'checks': ['sync', 'service-account-java17', 'build', 'download', 'sha256'], 'realAndroidBuild': False})) if len(sys.argv) == 3 and sys.argv[1] == '--export-for': user = pwd.getpwnam(sys.argv[2]) folder = Path(user.pw_dir) / '.config' / 'remotecompile' folder.mkdir(mode=0o700, parents=True, exist_ok=True) os.chown(folder, user.pw_uid, user.pw_gid) target = folder / 'token' with target.open('x', encoding='utf-8') as output: os.fchmod(output.fileno(), 0o600) output.write(token + '\n') os.fchown(output.fileno(), user.pw_uid, user.pw_gid) print('Client credential exported (contents hidden).')