feat: 初始化跨平台远程编译服务与 Android Studio 插件
This commit is contained in:
commit
9eba4556db
44 files changed
+2802
No files matched your search
@@ -0,0 +1,25 @@
|
||||
# Ubuntu 22.04 部署
|
||||
|
||||
`install.sh` 将 Linux amd64 服务安装到 `/opt/remotecompile/bin/`,使用 `remotecompile` 系统账户运行,由 systemd 管理并设为开机启动。脚本会重启同名服务;不会安装或修改 Android SDK。
|
||||
|
||||
```sh
|
||||
sudo bash install.sh /tmp/remotecompile-linux-amd64
|
||||
```
|
||||
|
||||
可在首次安装时通过 `sudo env JAVA_HOME=... ANDROID_HOME=... bash ...` 指定服务器已有的 JDK/SDK。已有配置不覆盖;后续修改 `/etc/remotecompile/environment` 后重启。
|
||||
|
||||
- 配置和随机 Token:`/etc/remotecompile/environment`,仅 root 可读。
|
||||
- 数据及缓存:`/var/lib/remotecompile/`,服务账户可写。
|
||||
- 日志:`journalctl -u remotecompile -f`。
|
||||
- 重启:`sudo systemctl restart remotecompile`。
|
||||
- 仅监听 `127.0.0.1:8765`;通过本机 SSH 隧道或 HTTPS 代理访问。
|
||||
- systemd 启用 ProtectHome,SDK 不应放在 `/root` 或 `/home`;建议工具链放在服务可读的 `/opt` 下。按需安装 SDK 组件时需要明确调整工具链目录权限,服务只对自身数据目录拥有写权限。
|
||||
- SDK 自动下载、licenses 和签名材料需要单独准备。服务健康检查通过不代表真实项目已完成构建。
|
||||
|
||||
本机连接示例:
|
||||
|
||||
```sh
|
||||
ssh -N -L 8765:127.0.0.1:8765 root@192.168.5.224
|
||||
```
|
||||
|
||||
插件填 `http://127.0.0.1:8765`;Token 从服务器配置中读取后填入 IDE PasswordSafe。不要把配置提交仓库。
|
||||
Executable
+78
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env bash
|
||||
# Usage: sudo bash install.sh /path/to/remotecompile-linux-amd64
|
||||
# Optional: JAVA_HOME, ANDROID_HOME, ANDROID_SDK_ROOT (existing readable toolchains).
|
||||
set -euo pipefail
|
||||
if [[ $(id -u) != 0 ]]; then echo 'Run as root or through sudo.' >&2; exit 1; fi
|
||||
if [[ $(uname -m) != x86_64 ]]; then echo 'This package requires x86_64 Linux.' >&2; exit 1; fi
|
||||
binary=${1:?Provide the uploaded Linux amd64 executable}
|
||||
[[ -f "$binary" ]] || { echo 'Binary does not exist' >&2; exit 1; }
|
||||
if ! id remotecompile >/dev/null 2>&1; then
|
||||
useradd --system --create-home --home-dir /var/lib/remotecompile --shell /usr/sbin/nologin remotecompile
|
||||
fi
|
||||
install -d -m 0755 /opt/remotecompile/bin
|
||||
install -d -m 0700 /etc/remotecompile
|
||||
install -d -o remotecompile -g remotecompile -m 0700 /var/lib/remotecompile /var/lib/remotecompile/data
|
||||
if [[ ! -f /etc/remotecompile/environment ]]; then
|
||||
umask 077
|
||||
token=$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n')
|
||||
printf 'REMOTE_BUILD_TOKEN=%s\nHOME=/var/lib/remotecompile\n' "$token" > /etc/remotecompile/environment
|
||||
for key in JAVA_HOME ANDROID_HOME ANDROID_SDK_ROOT; do
|
||||
value=${!key:-}
|
||||
if [[ -n "$value" ]]; then
|
||||
[[ "$value" != *$'\n'* && "$value" != *'"'* && "$value" != *'\\'* ]] || { echo "Unsupported characters in $key" >&2; exit 1; }
|
||||
printf '%s="%s"\n' "$key" "$value" >> /etc/remotecompile/environment
|
||||
fi
|
||||
done
|
||||
fi
|
||||
chmod 0600 /etc/remotecompile/environment
|
||||
# Stop through systemd so existing workers can finish cancellation and persist state.
|
||||
systemctl stop remotecompile.service 2>/dev/null || true
|
||||
install -m 0755 "$binary" /opt/remotecompile/bin/remotecompile
|
||||
cat > /etc/systemd/system/remotecompile.service <<'UNIT'
|
||||
[Unit]
|
||||
Description=VibeCoding Android Remote Compilation
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=remotecompile
|
||||
Group=remotecompile
|
||||
WorkingDirectory=/var/lib/remotecompile
|
||||
EnvironmentFile=/etc/remotecompile/environment
|
||||
ExecStart=/opt/remotecompile/bin/remotecompile -listen 127.0.0.1:8765 -data /var/lib/remotecompile/data -tasks :app:assembleDebug
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
TimeoutStopSec=40
|
||||
KillMode=control-group
|
||||
UMask=0077
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ReadWritePaths=/var/lib/remotecompile
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
UNIT
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now remotecompile.service
|
||||
for _ in {1..20}; do
|
||||
if systemctl is-active --quiet remotecompile.service; then
|
||||
if python3 - <<'PY'
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
config = Path('/etc/remotecompile/environment').read_text()
|
||||
token = next(line.split('=', 1)[1] for line in config.splitlines() if line.startswith('REMOTE_BUILD_TOKEN='))
|
||||
request = urllib.request.Request('http://127.0.0.1:8765/api/v1/health', headers={'Authorization': 'Bearer ' + token})
|
||||
with urllib.request.urlopen(request, timeout=2) as response:
|
||||
print('Health:', response.read().decode())
|
||||
PY
|
||||
then
|
||||
systemctl --no-pager --full status remotecompile.service
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
journalctl -u remotecompile.service --no-pager -n 30
|
||||
exit 1
|
||||
@@ -0,0 +1,50 @@
|
||||
"""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).')
|
||||
Reference in new issue
Block a user