feat: Dataset 改为有改动即同步(inotify);支持 XDG_DATA_HOME 直接接外部存储
Browse filesFiles changed (3)
- Dockerfile+2-1
- README.md+11-1
- scripts/entrypoint.sh+24-12
5 unmodified lines678910111213141516175 unmodified lines# 避免交互式提示ENV DEBIAN_FRONTEND=noninteractive# 安装依赖:curl/tar/git 用于 opencode,python3/huggingface_hub 用于 Dataset 持久化RUN apt-get update && apt-get install -y --no-install-recommends \ca-certificates \curl \tar \git \python3 \python3-pip \&& pip3 install --break-system-packages huggingface_hub \5 unmodified lines67891011121314151617185 unmodified lines# 避免交互式提示ENV DEBIAN_FRONTEND=noninteractive# 安装依赖:inotify-tools 用于「有改动即同步」DatasetRUN apt-get update && apt-get install -y --no-install-recommends \ca-certificates \curl \tar \git \inotify-tools \python3 \python3-pip \&& pip3 install --break-system-packages huggingface_hub \
39 unmodified lines40414243444546474839 unmodified lines3. **行为**- 启动时:从该 Dataset 拉取已有数据到 `~/.local/share/opencode`(若有)。- 运行中:每 5 分钟把当前数据上传回该 Dataset。- 未设置上述两个变量时,与之前一致:不恢复、不保存,重启后数据丢失。## 参考- [Spaces 配置说明](https://huggingface.co/docs/hub/spaces-config-reference)39 unmodified lines4041424344454647484950515253545556575839 unmodified lines3. **行为**- 启动时:从该 Dataset 拉取已有数据到 `~/.local/share/opencode`(若有)。- 运行中:**有改动即同步**(inotify 监听,约 20 秒防抖后上传),不再按固定间隔。- 未设置上述两个变量时,与之前一致:不恢复、不保存,重启后数据丢失。## 直接使用外部存储(自建/本地跑容器时)若你在**自己的机器或 VPS** 上跑本镜像,希望 OpenCode **直接读写**某块盘或网络存储(不经过 Dataset 同步):1. 把外部存储挂载到容器内某路径,例如 `/mnt/opencode-data`(NFS、云盘、本地卷均可)。2. 设置环境变量:**`XDG_DATA_HOME=/mnt/opencode-data`**(可选再加 `XDG_CONFIG_HOME=/mnt/opencode-data/.config`)。3. 启动容器。OpenCode 会直接读写该路径,无需 HF_TOKEN / Dataset,也不会跑任何同步脚本。在 **Hugging Face Spaces** 里无法挂载自定义卷,因此「直接外部存储」仅适用于自建/本地部署。## 参考- [Spaces 配置说明](https://huggingface.co/docs/hub/spaces-config-reference)
12345678910111213141516171819202122232425262728293031#!/bin/sh# 若设置了 HF_TOKEN 与 OPENCODE_DATASET_REPO,则从 Dataset 恢复并定期保存到 Dataset;# 否则直接启动 opencode serve(与原先行为一致)。set -eDATA_DIR="${HOME}/.local/share/opencode"OPENCODE_BIN="/home/user/.opencode/bin/opencode"SCRIPT_DIR="/home/user/app/scripts"SAVE_INTERVAL=300if [ -n "$HF_TOKEN" ] && [ -n "$OPENCODE_DATASET_REPO" ]; then# 从 Dataset 恢复python3 "$SCRIPT_DIR/restore_from_dataset.py" || true# 后台启动 opencode,并定期保存到 Datasettrap 'kill -TERM $OPENCODE_PID 2>/dev/null' TERM INT$OPENCODE_BIN serve --port 7860 --hostname 0.0.0.0 \--cors https://tacits-candy-shop.vercel.app \--cors https://opencode-web-pearl.vercel.app &OPENCODE_PID=$!while kill -0 $OPENCODE_PID 2>/dev/null; dosleep $SAVE_INTERVALpython3 "$SCRIPT_DIR/save_to_dataset.py" || truedonewait $OPENCODE_PIDexit $?fi# 未配置 Dataset 时直接启动(与原先 CMD 一致)exec $OPENCODE_BIN serve --port 7860 --hostname 0.0.0.0 \--cors https://tacits-candy-shop.vercel.app \--cors https://opencode-web-pearl.vercel.app12345678910111213141516171819202122232425262728293031323334353637383940414243#!/bin/sh# 1) 若设置了 XDG_DATA_HOME:视为直接使用外部存储(挂载点),只启动 opencode,不做任何同步。# 2) 若设置了 HF_TOKEN + OPENCODE_DATASET_REPO:从 Dataset 恢复,并用 inotify 在「有改动时」同步到 Dataset。# 3) 否则:直接启动 opencode serve(与原先行为一致)。set -eDATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share/opencode}"OPENCODE_BIN="/home/user/.opencode/bin/opencode"SCRIPT_DIR="/home/user/app/scripts"DEBOUNCE=20# 1) 直接使用外部存储:不恢复、不同步,只启动if [ -n "$XDG_DATA_HOME" ]; thenexec $OPENCODE_BIN serve --port 7860 --hostname 0.0.0.0 \--cors https://tacits-candy-shop.vercel.app \--cors https://opencode-web-pearl.vercel.appfi# 2) Dataset 模式:恢复 + 有改动就同步(inotify + 防抖)if [ -n "$HF_TOKEN" ] && [ -n "$OPENCODE_DATASET_REPO" ]; thenpython3 "$SCRIPT_DIR/restore_from_dataset.py" || truemkdir -p "$DATA_DIR"trap 'kill -TERM $OPENCODE_PID 2>/dev/null; kill $INOTIFY_PID 2>/dev/null' TERM INT$OPENCODE_BIN serve --port 7860 --hostname 0.0.0.0 \--cors https://tacits-candy-shop.vercel.app \--cors https://opencode-web-pearl.vercel.app &OPENCODE_PID=$!(while kill -0 $OPENCODE_PID 2>/dev/null; doinotifywait -r -q -e modify,create,delete,move "$DATA_DIR" 2>/dev/null || sleep 60sleep $DEBOUNCEpython3 "$SCRIPT_DIR/save_to_dataset.py" || truedone) &INOTIFY_PID=$!wait $OPENCODE_PIDexit $?fi# 3) 未配置:直接启动exec $OPENCODE_BIN serve --port 7860 --hostname 0.0.0.0 \--cors https://tacits-candy-shop.vercel.app \--cors https://opencode-web-pearl.vercel.app
Dockerfile+2-1
README.md+11-1
scripts/entrypoint.sh+24-12