返回 CodeWhale
install-services.sh
根目录 / scripts / tencent-lighthouse / install-services.sh
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 if [[ "${EUID}" -ne 0 ]]; then
5 echo "Run as root: sudo bash scripts/tencent-lighthouse/install-services.sh" >&2
6 exit 1
7 fi
8
9 REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
10 CODEWHALE_USER="${CODEWHALE_USER:-${DEEPSEEK_USER:-codewhale}}"
11 CODEWHALE_ROOT="${CODEWHALE_ROOT:-${DEEPSEEK_ROOT:-/opt/codewhale}}"
12 BRIDGE_KIND="${CODEWHALE_BRIDGE:-${DEEPSEEK_BRIDGE:-feishu}}"
13
14 case "${BRIDGE_KIND}" in
15 feishu|lark)
16 BRIDGE_SRC="integrations/feishu-bridge"
17 BRIDGE_DST="${CODEWHALE_ROOT}/bridge"
18 BRIDGE_UNIT="codewhale-feishu-bridge.service"
19 BRIDGE_ENV="/etc/codewhale/feishu-bridge.env"
20 BRIDGE_ENV_EXAMPLE="deploy/tencent-lighthouse/examples/feishu-bridge.env.example"
21 BRIDGE_STATE_DIR="/var/lib/codewhale-feishu-bridge"
22 VALIDATOR="integrations/feishu-bridge/scripts/validate-config.mjs"
23 ;;
24 telegram)
25 BRIDGE_SRC="integrations/telegram-bridge"
26 BRIDGE_DST="${CODEWHALE_ROOT}/telegram-bridge"
27 BRIDGE_UNIT="codewhale-telegram-bridge.service"
28 BRIDGE_ENV="/etc/codewhale/telegram-bridge.env"
29 BRIDGE_ENV_EXAMPLE="deploy/tencent-lighthouse/examples/telegram-bridge.env.example"
30 BRIDGE_STATE_DIR="/var/lib/codewhale-telegram-bridge"
31 VALIDATOR="integrations/telegram-bridge/scripts/validate-config.mjs"
32 ;;
33 *)
34 echo "Unknown bridge '${BRIDGE_KIND}'. Use CODEWHALE_BRIDGE=feishu or CODEWHALE_BRIDGE=telegram." >&2
35 exit 1
36 ;;
37 esac
38
39 install -d -m 0750 -o root -g "${CODEWHALE_USER}" /etc/codewhale
40 install -d -m 0700 -o "${CODEWHALE_USER}" -g "${CODEWHALE_USER}" "${BRIDGE_STATE_DIR}"
41 mkdir -p "$(dirname "${BRIDGE_DST}")"
42
43 if [[ ! -f /etc/codewhale/runtime.env && -f "${REPO_ROOT}/deploy/tencent-lighthouse/examples/runtime.env.example" ]]; then
44 install -m 0640 -o root -g "${CODEWHALE_USER}" \
45 "${REPO_ROOT}/deploy/tencent-lighthouse/examples/runtime.env.example" \
46 /etc/codewhale/runtime.env
47 fi
48
49 if [[ ! -f "${BRIDGE_ENV}" && -f "${REPO_ROOT}/${BRIDGE_ENV_EXAMPLE}" ]]; then
50 install -m 0640 -o root -g "${CODEWHALE_USER}" \
51 "${REPO_ROOT}/${BRIDGE_ENV_EXAMPLE}" \
52 "${BRIDGE_ENV}"
53 fi
54 # Build the new bridge tree beside the live one and swap it in only once its
55 # dependencies installed. A failed copy or `npm ci` (which empties
56 # node_modules first) leaves the running bridge's tree exactly as it was.
57 stage="$(mktemp -d "${BRIDGE_DST}.stage.XXXXXX")"
58 previous=""
59 trap 'if [[ -n "${stage}" ]]; then rm -rf "${stage}"; fi' EXIT
60 chmod 0755 "${stage}"
61 package="${BRIDGE_SRC##*/}"
62 rsync -a \
63 --exclude node_modules \
64 "${REPO_ROOT}/${BRIDGE_SRC}" \
65 "${REPO_ROOT}/integrations/bridge-core" \
66 "${stage}/"
67 chown -R "${CODEWHALE_USER}:${CODEWHALE_USER}" "${stage}"
68
69 if [[ -f "${stage}/${package}/package-lock.json" ]]; then
70 sudo -u "${CODEWHALE_USER}" npm --prefix "${stage}/${package}" ci --omit=dev
71 else
72 sudo -u "${CODEWHALE_USER}" npm --prefix "${stage}/${package}" install --omit=dev
73 fi
74 (
75 cd "${stage}"
76 shopt -s dotglob
77 for entry in "${package}"/*; do ln -s "${entry}" .; done
78 )
79 sudo -u "${CODEWHALE_USER}" node "${stage}/src/lib.mjs"
80
81 if [[ -e "${BRIDGE_DST}" ]]; then
82 previous="${BRIDGE_DST}.previous.$$"
83 mv "${BRIDGE_DST}" "${previous}"
84 fi
85 if ! mv "${stage}" "${BRIDGE_DST}"; then
86 # Put the old tree back rather than leave the service without one.
87 if [[ -n "${previous}" ]]; then mv "${previous}" "${BRIDGE_DST}"; fi
88 echo "Could not move the new bridge into ${BRIDGE_DST}; the previous tree was restored." >&2
89 exit 1
90 fi
91 stage=""
92 if [[ -n "${previous}" ]]; then rm -rf "${previous}"; fi
93
94 install -m 0644 "${REPO_ROOT}/deploy/tencent-lighthouse/systemd/codewhale-runtime.service" /etc/systemd/system/codewhale-runtime.service
95 install -m 0644 "${REPO_ROOT}/deploy/tencent-lighthouse/systemd/${BRIDGE_UNIT}" "/etc/systemd/system/${BRIDGE_UNIT}"
96
97 systemctl daemon-reload
98 systemctl enable codewhale-runtime "${BRIDGE_UNIT}"
99
100 cat <<'EOF'
101 Services installed but not started.
102
103 Before starting, verify:
104 /etc/codewhale/runtime.env
105 EOF
106 cat <<EOF
107 ${BRIDGE_ENV}
108 sudo -u ${CODEWHALE_USER} node ${REPO_ROOT}/${VALIDATOR} --env ${BRIDGE_ENV} --runtime-env /etc/codewhale/runtime.env --workspace-root /opt/whalebro --check-filesystem
109 Then run:
110 sudo systemctl start codewhale-runtime
111 sudo systemctl start ${BRIDGE_UNIT}
112 sudo CODEWHALE_BRIDGE=${BRIDGE_KIND} bash /opt/whalebro/codewhale/scripts/tencent-lighthouse/doctor.sh
113 sudo journalctl -u ${BRIDGE_UNIT} -f
114 EOF
115
115 lines BASH