packServer/backend/scripts/deploy/deploy-ops.cjs
liufumingren 228723e0c3 设备端扫码上报 + 桌面端/Android 正式包发布脚手架
AndroidStation(原中控改造为纯设备端)
- 只做三件事:UDP 48899 被桌面端发现、TCP 8087 推送 USB 扫码结果、离线语音播报
- 扫码不再经过 UI:去掉逐键重绘,50ms 自动提交,同一码重复扫码也一律上报
- 离线语音种子(数字/字母/“开始打包”/欢迎语)随包安装,MediaPlayer 播放,启动把音量拉满
- 启动播报「欢迎使用溯源系统,初始化已经完成,扫码即可开始打包」
- 移除 Device Owner / 设备管理员 / lock task / 网络与 IP 设置等非普通应用能力(仅保留开机自启)
- 正式签名 keystore + release 签名配置,versionName/versionCode 可由打包脚本覆盖

发布脚手架
- backend/android_client:Android 正式包构建(build.ps1 + package.config.json)
- backend/scripts/deploy/publish-android.cjs + npm run android:build / android:publish
- 桌面端安装包改为 download/desktop,Android 为 download/android_client;
  version.json 仍走 /download/version.json(老客户端写死),新增 /download/android_client/version.json
- 谷歌插件随桌面端 MSI 安装到 <安装目录>\extension,工具菜单新增「复制谷歌插件地址」
- git-safety-check 增加仓库级禁区(keystore.properties / *.jks / local.properties)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 05:53:27 +08:00

161 lines
4.9 KiB
JavaScript

'use strict';
const { loadDeployConfig, quoteForBash, runSsh } = require('./deploy-lib.cjs');
const COMMANDS = new Set([ 'inspect', 'smoke', 'verify', 'cleanup-backups' ]);
const DEFAULT_SMOKE_PATHS = [ '/', '/health', '/download/version.json' ];
function parseArgs(argv) {
const options = { envName: 'production', command: '', args: [] };
const tokens = [ ...argv ];
if (tokens.length === 0) {
throw new Error('缺少命令: inspect | smoke | verify | cleanup-backups');
}
if (!COMMANDS.has(tokens[0])) {
options.envName = tokens[0];
tokens.shift();
}
if (tokens.length === 0) {
throw new Error('缺少命令: inspect | smoke | verify | cleanup-backups');
}
options.command = tokens[0];
options.args = tokens.slice(1);
if (!COMMANDS.has(options.command)) {
throw new Error(`不支持的命令: ${options.command}`);
}
return options;
}
function getActivePortInfo(config) {
const currentMdPath = `${config.deployRoot}/current.md`;
const output = runSsh(config, `cat ${quoteForBash(currentMdPath)} 2>/dev/null || true`, { stdio: 'pipe' });
const rawContent = String(output || '').trim();
if (!rawContent) {
throw new Error(`无法读取远端 ${currentMdPath},内容为空或文件不存在。`);
}
const version = rawContent.replace(/\r/g, '').trim();
if (version !== 'v1' && version !== 'v2') {
throw new Error(`远端 current.md 内容无效: "${rawContent}",期望值为 v1 或 v2。`);
}
return { port: config.ports[version], version, rawContent };
}
function inspectState(config) {
const latestBackupFile = `${config.stateDir}/latest-backup.txt`;
const remoteScript = [
'set -euo pipefail',
`STATE_DIR=${quoteForBash(config.stateDir)}`,
`BACKUP_DIR=${quoteForBash(config.backupDir)}`,
`LATEST_FILE=${quoteForBash(latestBackupFile)}`,
'printf "[STATE]\\n"',
'if [ -d "$STATE_DIR" ]; then ls -la "$STATE_DIR"; else echo MISSING; fi',
'printf "\\n[LATEST_BACKUP]\\n"',
'if [ -f "$LATEST_FILE" ]; then cat "$LATEST_FILE"; else echo MISSING; fi',
'printf "\\n[BACKUPS]\\n"',
'if [ -d "$BACKUP_DIR" ]; then ls -1t "$BACKUP_DIR"; else echo MISSING; fi',
].join('\n');
return runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
}
function smokeRoutes(config) {
const { port } = getActivePortInfo(config);
const paths = DEFAULT_SMOKE_PATHS.map(quoteForBash).join(' ');
const useNginx = Boolean(config.serverName);
const baseUrl = useNginx ? 'http://127.0.0.1' : `http://127.0.0.1:${port}`;
const remoteScript = [
'set -euo pipefail',
`BASE_URL=${quoteForBash(baseUrl)}`,
`SERVER_NAME=${quoteForBash(config.serverName || '')}`,
`for path in ${paths}; do`,
' if [ -n "$SERVER_NAME" ]; then',
' code=$(curl -sS -o /dev/null -w "%{http_code}" -H "Host: $SERVER_NAME" "$BASE_URL$path" || true)',
' else',
' code=$(curl -sS -o /dev/null -w "%{http_code}" "$BASE_URL$path" || true)',
' fi',
' printf "%s %s\\n" "$path" "$code"',
'done',
].join('\n');
return runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
}
function cleanupBackups(config, names) {
if (!names.length) {
throw new Error('cleanup-backups 需要至少一个备份文件名');
}
const remoteScript = [
'set -euo pipefail',
`BACKUP_DIR=${quoteForBash(config.backupDir)}`,
...names.map((name, index) => `FILE_${index}=${quoteForBash(name)}`),
...names.map((_, index) => `if [ -f "$BACKUP_DIR/$FILE_${index}" ]; then rm -f "$BACKUP_DIR/$FILE_${index}"; echo "REMOVED $FILE_${index}"; else echo "MISS $FILE_${index}"; fi`),
].join('\n');
return runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
}
function showActiveInfo(config) {
let activeInfo;
try {
activeInfo = getActivePortInfo(config);
} catch (err) {
console.log(`[CURRENT_MD] 读取失败: ${err.message}`);
console.log('');
return;
}
console.log(`[CURRENT_MD] 内容: ${activeInfo.rawContent}`);
console.log(`[ACTIVE] 端口: ${activeInfo.port} (${activeInfo.version})`);
console.log('');
}
function main() {
const options = parseArgs(process.argv.slice(2));
const config = loadDeployConfig(options.envName);
if (options.command === 'inspect') {
showActiveInfo(config);
console.log(inspectState(config));
return;
}
if (options.command === 'smoke') {
showActiveInfo(config);
console.log('[SMOKE]');
console.log(smokeRoutes(config));
return;
}
if (options.command === 'verify') {
showActiveInfo(config);
console.log(inspectState(config));
console.log('');
console.log('[SMOKE]');
console.log(smokeRoutes(config));
return;
}
if (options.command === 'cleanup-backups') {
console.log(cleanupBackups(config, options.args));
return;
}
throw new Error(`不支持的命令: ${options.command}`);
}
try {
main();
} catch (err) {
console.error(`[ERROR] ${err && err.message ? err.message : err}`);
process.exit(1);
}