entrypoint.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env bash
  2. # 容器入口脚本
  3. # dsh-web 容器入口:校验必需变量 → 生成 bcrypt → 首启初始化数据卷 → 启动 supervisord
  4. #
  5. # Container Entrypoint Script
  6. # dsh-web container entrypoint: validate required env vars → generate bcrypt → initialize the data volume on first run → start supervisord
  7. set -euo pipefail
  8. log() { echo "[entrypoint] $*"; }
  9. # ---- 必需变量校验 ----
  10. #
  11. # ---- Required variable validation ----
  12. : "${DSH_AUTH_USER:?必须设置 DSH_AUTH_USER(.env)}"
  13. : "${DSH_AUTH_PASSWORD:?必须设置 DSH_AUTH_PASSWORD(.env)}"
  14. : "${DEEPSEEK_API_KEY:?必须设置 DEEPSEEK_API_KEY(.env)}"
  15. # ---- basic auth:明文密码 → bcrypt 哈希(注入 Caddyfile 环境变量)----
  16. #
  17. # ---- Basic auth: plaintext password → bcrypt hash (injected into Caddyfile env vars) ----
  18. DSH_AUTH_HASH="$(caddy hash-password --plaintext "$DSH_AUTH_PASSWORD" | tail -n 1)"
  19. export DSH_AUTH_HASH
  20. # ---- 数据卷首启初始化(/home/node/.dsh,node 用户为 uid 1000)----
  21. #
  22. # ---- Data volume first-run initialization (/home/node/.dsh, node user with uid 1000) ----
  23. DHS_HOME=/home/node/.dsh
  24. mkdir -p "$DHS_HOME"
  25. # 1. settings.yaml:不存在时复制默认模板(之后由用户持久化维护)
  26. #
  27. # 1. settings.yaml: copy the default template when absent (later maintained by the user)
  28. if [ ! -f "$DHS_HOME/settings.yaml" ]; then
  29. cp /opt/dsh/defaults/settings.yaml "$DHS_HOME/settings.yaml"
  30. log "已初始化默认 settings.yaml"
  31. fi
  32. # 1b. AGENTS.md:容器环境指引(dsh 会话自动加载,agent 必读)
  33. #
  34. # 1b. AGENTS.md: container environment guide (auto-loaded by dsh sessions, mandatory for agents)
  35. if [ ! -f "$DHS_HOME/AGENTS.md" ]; then
  36. cp /opt/dsh/defaults/AGENTS.md "$DHS_HOME/AGENTS.md"
  37. log "已初始化 AGENTS.md(容器环境指引)"
  38. fi
  39. # 2. trustedHosts 注入 cordis.patch.yml
  40. # profile 其余文件(package.json/pnpm-workspace.yaml)由 dsh 首启自动补全,
  41. # 且 dsh 的 initProfile 对已存在文件绝不覆盖("Existing files are never touched"),
  42. # 故此处预写安全。
  43. # 注入条件:patch 不存在,或为 dsh 首启生成的空模板(顶层数组 [])。
  44. # 已有任何条目(用户维护)或不可解析时跳过——绝不覆盖用户文件。
  45. #
  46. # 2. Inject trustedHosts into cordis.patch.yml
  47. # The rest of the profile (package.json/pnpm-workspace.yaml) is auto-completed by dsh on first run,
  48. # and dsh's initProfile never touches existing files ("Existing files are never touched"),
  49. # so pre-writing here is safe.
  50. # Injection happens when the patch is absent, or when it is the empty template dsh generates
  51. # on first run (top-level array []). Any existing entries (user-maintained) or an unparseable
  52. # file cause a skip — user files are never overwritten.
  53. if [ -n "${DSH_TRUSTED_HOSTS:-}" ]; then
  54. PROFILE_DIR="$DHS_HOME/profiles/web"
  55. PATCH="$PROFILE_DIR/cordis.patch.yml"
  56. INJECT=1
  57. if [ -f "$PATCH" ]; then
  58. PATCH_LEN="$(yq '. | length' "$PATCH" 2>/dev/null || true)"
  59. if [ "$PATCH_LEN" != "0" ]; then
  60. INJECT=0
  61. log "cordis.patch.yml 已有内容,跳过 trustedHosts 注入(用户维护;若需变更请直接编辑该文件)"
  62. fi
  63. fi
  64. if [ "$INJECT" = 1 ]; then
  65. mkdir -p "$PROFILE_DIR"
  66. {
  67. echo "# 由 dsh-web entrypoint 自动注入(DSH_TRUSTED_HOSTS)"
  68. echo "# Auto-injected by the dsh-web entrypoint (DSH_TRUSTED_HOSTS)"
  69. echo "- id: connection"
  70. echo " config:"
  71. echo " trustedHosts:"
  72. # shellcheck disable=SC2086 # 有意按空白分词(逗号分隔列表)
  73. for host in ${DSH_TRUSTED_HOSTS//,/ }; do
  74. echo " - $host"
  75. done
  76. } > "$PATCH"
  77. log "已注入 trustedHosts: $DSH_TRUSTED_HOSTS"
  78. fi
  79. fi
  80. # 3. 修正数据卷属主(bind mount 挂整个 /home/node,首启属主可能是 root)
  81. # 仅当根目录属主非 1000 时全量修正;后续启动跳过——数据卷含 x-cmd 工具,
  82. # 全量递归 chown 在大卷上会拖慢启动
  83. #
  84. # 3. Fix data volume ownership (the whole /home/node is bind-mounted; owner may be root on first run)
  85. # Only fix recursively when the root owner is not 1000; later starts skip it —
  86. # the volume holds x-cmd tools and a full recursive chown slows startup on large volumes
  87. if [ "$(stat -c '%u:%g' /home/node 2>/dev/null || echo '0:0')" != "1000:1000" ]; then
  88. chown -R node:node /home/node
  89. log "已修正数据卷属主(uid:gid → 1000:1000)"
  90. else
  91. log "数据卷属主正确,跳过 chown"
  92. fi
  93. # ---- x-cmd:agent 自行配置运行环境的工具(幂等安装 + 系统 PATH 接入)----
  94. # agent 的 bash 工具环境是白名单(PATH 固定为 /usr/local/sbin:/usr/local/bin:/usr/bin,无 HOME),
  95. # 因此:1) 生成 HOME 注入的 x wrapper;2) 把已 use 包的 shim 软链进 /usr/local/bin。
  96. # agent 用法见数据卷 AGENTS.md:`x env use <pkg>` 安装,`x <pkg>` 前缀调用(始终可用),
  97. # 裸命令对本次启动前已 use 的包可用(重启容器后对新装包生效)。
  98. #
  99. # ---- x-cmd: tools agents install to configure their runtime (idempotent install + system PATH integration) ----
  100. # The agent's bash tool environment is a whitelist (PATH fixed to /usr/local/sbin:/usr/local/bin:/usr/bin, no HOME),
  101. # hence: 1) generate an x wrapper that injects HOME; 2) symlink shims of already-used packages into /usr/local/bin.
  102. # Agent usage is documented in the data-volume AGENTS.md: install with `x env use <pkg>`, invoke with the `x <pkg>` prefix (always available),
  103. # Bare commands work for packages used before this startup (newly installed packages take effect after a container restart).
  104. XCMD_HOME=/home/node/.x-cmd.root
  105. if [ ! -x "$XCMD_HOME/bin/x" ]; then
  106. log "首次启动:安装 x-cmd(agent 环境自举,约 80MB,写入数据卷;阿里云 OSS 源)"
  107. if timeout 300 su -s /bin/bash node -c 'HOME=/home/node curl -fsSL https://get.x-cmd.com | sh' >/dev/null 2>&1; then
  108. log "x-cmd 安装完成"
  109. else
  110. log "警告:x-cmd 安装失败(网络或 su 受限?),dsh 照常启动;"
  111. log " 可稍后手动安装:docker exec -it dsh-web su -s /bin/bash node -c 'curl -fsSL https://get.x-cmd.com | sh'"
  112. fi
  113. fi
  114. if [ -x "$XCMD_HOME/bin/x" ]; then
  115. # 1) x wrapper:agent bash 无 HOME,x 需要 HOME 定位安装根
  116. #
  117. # 1) x wrapper: the agent bash has no HOME; x needs HOME to locate its install root
  118. cat > /usr/local/bin/x <<EOF
  119. #!/bin/bash
  120. export HOME=/home/node
  121. exec /home/node/.x-cmd.root/bin/x "$@"
  122. EOF
  123. chmod 755 /usr/local/bin/x
  124. # 2) 已 use 包 shim 快照软链 → /usr/local/bin(裸命令直接可用)
  125. # exec/<pkg> 文件内容为 shim_bin 路径;shim_bin/<pkg> 是 `#! /bin/sh + exec 真实 bin` 包装
  126. #
  127. # 2) Symlink the shim snapshot of used packages → /usr/local/bin (bare commands become directly usable)
  128. # exec/<pkg> contains the shim_bin path; shim_bin/<pkg> is a `#! /bin/sh + exec real bin` wrapper
  129. for shim in "$XCMD_HOME/local/data/pkg/exec"/*; do
  130. [ -f "$shim" ] || continue
  131. pkg="$(basename "$shim")"
  132. target="$(head -n 1 "$shim" 2>/dev/null)" || continue
  133. if [ -n "$target" ] && [ -e "$target" ]; then
  134. ln -sf "$target" "/usr/local/bin/$pkg"
  135. fi
  136. done
  137. pkg_list="$("$XCMD_HOME/bin/x" env ls 2>/dev/null | awk '{print $1}' | tr '\n' ' ')"
  138. log "x-cmd 就绪(已启用包:$pkg_list)"
  139. fi
  140. # ---- 启动 supervisord(dsh + caddy)----
  141. #
  142. # ---- Start supervisord (dsh + caddy) ----
  143. log "启动 supervisord(DSH_AUTH_USER=$DSH_AUTH_USER,对外端口由 compose 映射)"
  144. exec supervisord -c /etc/supervisor/conf.d/dsh.conf