Update scripts/openclaw-entrypoint.sh
Browse files1236 unmodified lines1011121314151 unmodified line1718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122#!/usr/bin/env bashset -euo pipefail6 unmodified linesHF_TOKEN="${HF_TOKEN:-${HUGGINGFACE_HUB_TOKEN:-}}"BACKUP_DATASET_REPO="${BACKUP_DATASET_REPO:-}"mkdir -p /var/log/openclawmkdir -p "${OPENCLAW_STATE_DIR}"/{workspace,agents/main/agent,agents/main/sessions,logs,identity}1 unmodified lineecho "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] [ENTRYPOINT] $1" | tee -a /var/log/openclaw/gateway.stdout.log}restore_if_exists() {if [[ -z "${HF_TOKEN}" ]] || [[ -z "${BACKUP_DATASET_REPO}" ]]; thenlog_info "未配置备份,跳过恢复步骤"return 0filog_info "检查备份数据..."RESTORE_TEMP=$(mktemp -d)if huggingface-cli download "${BACKUP_DATASET_REPO}" --repo-type dataset --token "${HF_TOKEN}" --local-dir "${RESTORE_TEMP}" 2>/dev/null; thenlog_info "找到备份,开始恢复数据..."[[ -d "${RESTORE_TEMP}/workspace" ]] && cp -r "${RESTORE_TEMP}/workspace/"* "${OPENCLAW_WORKSPACE_DIR}/" 2>/dev/null || true[[ -d "${RESTORE_TEMP}/agents" ]] && cp -r "${RESTORE_TEMP}/agents/"* "${OPENCLAW_STATE_DIR}/agents/" 2>/dev/null || true[[ -d "${RESTORE_TEMP}/identity" ]] && cp -r "${RESTORE_TEMP}/identity/"* "${OPENCLAW_STATE_DIR}/identity/" 2>/dev/null || truelog_info "数据恢复完成"elselog_info "未找到备份或下载失败,跳过恢复"firm -rf "${RESTORE_TEMP}"}backup_on_start() {if [[ "${BACKUP_ENABLED}" != "true" ]]; thenreturn 0fiif [[ -z "${HF_TOKEN}" ]] || [[ -z "${BACKUP_DATASET_REPO}" ]]; thenlog_info "未配置备份,跳过启动备份"return 0filog_info "执行启动备份..."BACKUP_TEMP=$(mktemp -d)cp -r "${OPENCLAW_WORKSPACE_DIR}" "${BACKUP_TEMP}/" 2>/dev/null || truecp -r "${OPENCLAW_STATE_DIR}/agents" "${BACKUP_TEMP}/" 2>/dev/null || truecp -r "${OPENCLAW_STATE_DIR}/identity" "${BACKUP_TEMP}/" 2>/dev/null || trueecho "{\"backup_time\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\", \"trigger\": \"startup\"}" > "${BACKUP_TEMP}/backup_info.json"if huggingface-cli upload "${BACKUP_DATASET_REPO}" "${BACKUP_TEMP}/" --repo-type dataset --token "${HF_TOKEN}" --commit-message "Auto-backup $(date -u +%Y-%m-%dT%H:%M:%SZ)" 2>/dev/null; thenlog_info "启动备份完成"elselog_info "启动备份失败(继续启动)"firm -rf "${BACKUP_TEMP}"}init_config() {local config_file="${OPENCLAW_STATE_DIR}/openclaw.json"log_info "初始化 OpenClaw 配置..."cat > "${config_file}" <<'EOFCONFIG'{"meta": {"lastTouchedVersion": "2026.5.6"},"gateway": {"mode": "local","bind": "lan","auth": {"mode": "token","token": "CUSTOM_GATEWAY_TOKEN_PLACEHOLDER"},"controlUi": {"allowedOrigins": ["*"],"dangerouslyDisableDeviceAuth": true,"allowInsecureAuth": true},"devicePairing": {"required": false}},"agents": {"defaults": {"model": {"primary": "PLACEHOLDER_MODEL"}}},"channels": {"weixin": {"enabled": true,"corpId": "wwdf06e4da02f37a0b","agentId": "1000003","secret": "2eVuCjsM83lXJ12nxoJG5uch677ZkOLHxew4zTPGGc8"}}}EOFCONFIGsed -i "s/CUSTOM_GATEWAY_TOKEN_PLACEHOLDER/${OPENCLAW_GATEWAY_TOKEN:-}/" "${config_file}"sed -i "s/PLACEHOLDER_MODEL/${RAW_MODEL:-z-ai\/glm4.7}/" "${config_file}"log_info "配置初始化完成"}main() {log_info "===== OpenClaw 启动 ====="log_info "状态目录: ${OPENCLAW_STATE_DIR}"log_info "工作区目录: ${OPENCLAW_WORKSPACE_DIR}"init_configrestore_if_existsbackup_on_startlog_info "启动 OpenClaw 网关..."exec openclaw gateway --bind lan --port "${OPENCLAW_GATEWAY_PORT}" --token "${OPENCLAW_GATEWAY_TOKEN}"}main "$@"123456 unmodified lines12131415161718191 unmodified line21222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179#!/usr/bin/env bashset -euo pipefail6 unmodified linesHF_TOKEN="${HF_TOKEN:-${HUGGINGFACE_HUB_TOKEN:-}}"BACKUP_DATASET_REPO="${BACKUP_DATASET_REPO:-}"WEIXIN_QR_FILE="/tmp/weixin_qr_url.txt"mkdir -p /var/log/openclawmkdir -p "${OPENCLAW_STATE_DIR}"/{workspace,agents/main/agent,agents/main/sessions,logs,identity}1 unmodified lineecho "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] [ENTRYPOINT] $1" | tee -a /var/log/openclaw/gateway.stdout.log}# ── 恢复微信登录 session ────────────────────────restore_weixin_session() {if [[ -z "${HF_TOKEN}" ]] || [[ -z "${BACKUP_DATASET_REPO}" ]]; thenlog_info "HF_TOKEN 或 BACKUP_DATASET_REPO 未设置,跳过微信 session 恢复"return 0filog_info "恢复微信登录 session..."huggingface-cli download "${BACKUP_DATASET_REPO}" --repo-type dataset --token "${HF_TOKEN}" --local-dir "${OPENCLAW_STATE_DIR}" 2>/dev/null && log_info "微信 session 恢复完成" || log_info "无微信 session 可恢复"}# ── 备份微信登录 session ────────────────────────backup_weixin_session() {if [[ -z "${HF_TOKEN}" ]] || [[ -z "${BACKUP_DATASET_REPO}" ]]; thenreturn 0filog_info "备份微信登录 session..."huggingface-cli upload "${BACKUP_DATASET_REPO}" "${OPENCLAW_STATE_DIR}" --repo-type dataset --token "${HF_TOKEN}" 2>/dev/null && log_info "微信 session 备份完成" || true}# ── 生成微信二维码 URL ──────────────────────────generate_weixin_qr() {log_info "正在获取微信二维码 URL..."openclaw channels login --channel openclaw-weixin > "${WEIXIN_QR_FILE}" 2>&1 &local LOGIN_PID=$!for i in $(seq 1 30); doif grep -q "ilinkai.weixin.qq.com" "${WEIXIN_QR_FILE}" 2>/dev/null; thenlocal QR_URLQR_URL=$(grep "ilinkai.weixin.qq.com" "${WEIXIN_QR_FILE}" | head -1 | tr -d '[:space:]')log_info "=========================================="log_info "微信登录二维码 URL:"log_info "${QR_URL}"log_info "请在浏览器中打开此链接,用微信扫码登录"log_info "=========================================="return 0fisleep 1donelog_info "未能自动获取二维码 URL,请查看日志获取 ilinkai.weixin.qq.com 链接"}restore_if_exists() {if [[ -z "${HF_TOKEN}" ]] || [[ -z "${BACKUP_DATASET_REPO}" ]]; thenlog_info "未配置备份,跳过恢复步骤"return 0filog_info "检查备份数据..."RESTORE_TEMP=$(mktemp -d)if huggingface-cli download "${BACKUP_DATASET_REPO}" --repo-type dataset --token "${HF_TOKEN}" --local-dir "${RESTORE_TEMP}" 2>/dev/null; thenlog_info "找到备份,开始恢复数据..."[[ -d "${RESTORE_TEMP}/workspace" ]] && cp -r "${RESTORE_TEMP}/workspace/"* "${OPENCLAW_WORKSPACE_DIR}/" 2>/dev/null || true[[ -d "${RESTORE_TEMP}/agents" ]] && cp -r "${RESTORE_TEMP}/agents/"* "${OPENCLAW_STATE_DIR}/agents/" 2>/dev/null || true[[ -d "${RESTORE_TEMP}/identity" ]] && cp -r "${RESTORE_TEMP}/identity/"* "${OPENCLAW_STATE_DIR}/identity/" 2>/dev/null || truelog_info "数据恢复完成"elselog_info "未找到备份或下载失败,跳过恢复"firm -rf "${RESTORE_TEMP}"}backup_on_start() {if [[ "${BACKUP_ENABLED}" != "true" ]]; thenreturn 0fiif [[ -z "${HF_TOKEN}" ]] || [[ -z "${BACKUP_DATASET_REPO}" ]]; thenlog_info "未配置备份,跳过启动备份"return 0filog_info "执行启动备份..."BACKUP_TEMP=$(mktemp -d)cp -r "${OPENCLAW_WORKSPACE_DIR}" "${BACKUP_TEMP}/" 2>/dev/null || truecp -r "${OPENCLAW_STATE_DIR}/agents" "${BACKUP_TEMP}/" 2>/dev/null || truecp -r "${OPENCLAW_STATE_DIR}/identity" "${BACKUP_TEMP}/" 2>/dev/null || trueecho "{\"backup_time\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\", \"trigger\": \"startup\"}" > "${BACKUP_TEMP}/backup_info.json"if huggingface-cli upload "${BACKUP_DATASET_REPO}" "${BACKUP_TEMP}/" --repo-type dataset --token "${HF_TOKEN}" --commit-message "Auto-backup $(date -u +%Y-%m-%dT%H:%M:%SZ)" 2>/dev/null; thenlog_info "启动备份完成"elselog_info "启动备份失败(继续启动)"firm -rf "${BACKUP_TEMP}"}init_config() {local config_file="${OPENCLAW_STATE_DIR}/openclaw.json"log_info "初始化 OpenClaw 配置..."cat > "${config_file}" <<'EOFCONFIG'{"meta": {"lastTouchedVersion": "2026.5.6"},"gateway": {"mode": "local","bind": "lan","auth": {"mode": "token","token": "CUSTOM_GATEWAY_TOKEN_PLACEHOLDER"},"controlUi": {"allowedOrigins": ["*"],"dangerouslyDisableDeviceAuth": true,"allowInsecureAuth": true},"devicePairing": {"required": false}},"plugins": {"entries": {"openclaw-weixin": {"enabled": true}}},"agents": {"defaults": {"model": {"primary": "PLACEHOLDER_MODEL"}}}}EOFCONFIGsed -i "s/CUSTOM_GATEWAY_TOKEN_PLACEHOLDER/${OPENCLAW_GATEWAY_TOKEN:-}/" "${config_file}"sed -i "s/PLACEHOLDER_MODEL/${RAW_MODEL:-z-ai\/glm4.7}/" "${config_file}"log_info "配置初始化完成"}main() {log_info "===== OpenClaw 启动 ====="log_info "状态目录: ${OPENCLAW_STATE_DIR}"log_info "工作区目录: ${OPENCLAW_WORKSPACE_DIR}"init_configrestore_weixin_sessionrestore_if_existsbackup_on_start# 检查微信是否已登录if openclaw channels status --channel openclaw-weixin 2>/dev/null | grep -q "connected"; thenlog_info "微信已登录,跳过扫码"elselog_info "未检测到微信登录,需要扫码..."generate_weixin_qrfi# 定期备份微信 session(每 5 分钟)if [[ "${BACKUP_ENABLED}" == "true" ]]; then(while true; do sleep 300; backup_weixin_session; done) &filog_info "启动 OpenClaw 网关..."exec openclaw gateway --bind lan --port "${OPENCLAW_GATEWAY_PORT}" --token "${OPENCLAW_GATEWAY_TOKEN}"}main "$@"
scripts/openclaw-entrypoint.sh+141-84