Files
hair-keeper/.cloud-dev/entrypoint.sh

96 lines
2.5 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ============================================
# 文件持久化:启动时恢复 + 后台定期同步到命名卷
# ============================================
PERSIST_DIR="/root/.hair-keeper-persist"
PERSIST_FILES=(
"/root/.claude.json"
"/root/.zsh_history"
"/root/.bash_history"
)
mkdir -p "$PERSIST_DIR"
# 启动时恢复:持久化目录中的文件优先覆盖到原路径
for filepath in "${PERSIST_FILES[@]}"; do
filename=$(basename "$filepath")
persist_path="$PERSIST_DIR/$filename"
if [ -f "$persist_path" ] && [ -s "$persist_path" ]; then
cp "$persist_path" "$filepath"
fi
done
# 后台定期同步将原路径文件同步回持久化目录每60秒
(
while true; do
sleep 60
for filepath in "${PERSIST_FILES[@]}"; do
filename=$(basename "$filepath")
persist_path="$PERSIST_DIR/$filename"
if [ -f "$filepath" ]; then
rsync -a "$filepath" "$persist_path"
fi
done
done
) &
# 设置默认密码
DEV_PASSWORD=${DEV_PASSWORD:-clouddev}
# 设置 root 密码
echo "root:${DEV_PASSWORD}" | chpasswd
# 确保 code-server 配置目录存在
mkdir -p /root/.local/share/code-server
# 创建 code-server 配置文件
cat > /root/.config/code-server/config.yaml << EOF
bind-addr: 0.0.0.0:8080
auth: password
password: ${DEV_PASSWORD}
cert: false
EOF
# 启动 SSH 服务
echo "Starting SSH service..."
service ssh start
# 启动 code-server
echo "Starting code-server on port 8080..."
code-server --bind-addr 0.0.0.0:8080 /workspace &
# 输出服务信息
echo ""
echo "=========================================="
echo "Cloud Development Container Started"
echo "=========================================="
echo "SSH: Port 22 (user: root, password: ${DEV_PASSWORD})"
echo "Code Server: Port 8080 (password: ${DEV_PASSWORD})"
echo "Next.js Dev: Port 3000 (run 'pnpm run dev' to start)"
echo "=========================================="
echo ""
echo "Workspace: /workspace"
echo "Default Shell: zsh with oh-my-zsh"
echo ""
echo "提示: 可通过环境变量 DEV_PASSWORD 自定义密码"
echo ""
# 容器停止时执行最终同步
cleanup() {
echo "Syncing persistent files before shutdown..."
for filepath in "${PERSIST_FILES[@]}"; do
filename=$(basename "$filepath")
persist_path="$PERSIST_DIR/$filename"
if [ -f "$filepath" ]; then
rsync -a "$filepath" "$persist_path"
fi
done
exit 0
}
trap cleanup SIGTERM SIGINT
# 保持容器运行
tail -f /dev/null &
wait $!