Skip to content

Commit c0bbbc8

Browse files
committed
fix(install): 重复安装时处理运行中进程
1 parent 9d30f8c commit c0bbbc8

3 files changed

Lines changed: 71 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ curl -fsSL https://gh-proxy.com/https://github.com/https-cert/dep
4242

4343
安装脚本会把发布包中的 `config.yaml` 模板复制到 `/opt/anssl/config.yaml`,如果配置文件已存在则不会覆盖。首次安装后直接编辑该文件,修改其中的 `accessKey` 和需要启用的部署目标。
4444

45+
重复执行安装脚本时,会保留已有 `config.yaml`。如果已安装但未运行,会直接更新程序;如果检测到 anssl 正在运行,会先停止旧版本,再安装新版本。
46+
4547
后续更新时只替换 `anssl` 可执行文件即可,避免手动解压覆盖已有的 `config.yaml`
4648

4749
`config.yaml` 示例:

README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ curl -fsSL https://gh-proxy.com/https://github.com/https-cert/dep
4242

4343
The install script copies the packaged `config.yaml` template to `/opt/anssl/config.yaml` and never overwrites an existing config file. On first install, edit that file and set its `accessKey` and any deployment targets you want to enable.
4444

45+
Running the install script again keeps the existing `config.yaml`. If anssl is installed but not running, the script updates the binary directly; if anssl is running, the script stops the old daemon before installing the new version.
46+
4547
For later updates, replace only the `anssl` executable to avoid overwriting an existing `config.yaml`.
4648

4749
`config.yaml` example:

scripts/install.sh

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
1313
CONFIG_DIR="${CONFIG_DIR:-$APP_DIR}"
1414
ACTION="install"
1515
PURGE="false"
16+
INSTALL_STATE="首次安装完成"
17+
RUNNING_DETECTED_WITH=""
1618

1719
# log_info 输出普通安装日志。
1820
log_info() {
@@ -94,6 +96,16 @@ run_privileged() {
9496
log_error "当前用户没有写入权限且未安装 sudo,请使用 root 执行"
9597
}
9698

99+
# run_with_detected_privilege 使用检测运行状态时相同的权限执行命令。
100+
run_with_detected_privilege() {
101+
if [ "$RUNNING_DETECTED_WITH" = "sudo" ]; then
102+
sudo "$@"
103+
return
104+
fi
105+
106+
"$@"
107+
}
108+
97109
# detect_platform 识别 release 包使用的系统和架构名。
98110
detect_platform() {
99111
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
@@ -172,13 +184,62 @@ verify_checksum() {
172184
log_warn "未找到 sha256sum 或 shasum,跳过校验"
173185
}
174186

187+
# is_anssl_running 检查已安装的 anssl daemon 是否正在运行。
188+
is_anssl_running() {
189+
anssl_bin="${APP_DIR}/${BIN_NAME}"
190+
RUNNING_DETECTED_WITH=""
191+
192+
if [ ! -x "$anssl_bin" ]; then
193+
return 1
194+
fi
195+
196+
if "$anssl_bin" status 2>/dev/null | grep -q "正在运行"; then
197+
RUNNING_DETECTED_WITH="direct"
198+
return 0
199+
fi
200+
201+
if [ "$(id -u)" -ne 0 ] && command -v sudo >/dev/null 2>&1; then
202+
if sudo "$anssl_bin" status 2>/dev/null | grep -q "正在运行"; then
203+
RUNNING_DETECTED_WITH="sudo"
204+
return 0
205+
fi
206+
fi
207+
208+
return 1
209+
}
210+
211+
# stop_running_anssl 如果旧版本正在运行,先停止 daemon 再继续安装。
212+
stop_running_anssl() {
213+
if [ -f "${APP_DIR}/${BIN_NAME}" ] || [ -f "${CONFIG_DIR}/config.yaml" ]; then
214+
INSTALL_STATE="已更新程序,配置已保留"
215+
fi
216+
217+
if ! is_anssl_running; then
218+
return
219+
fi
220+
221+
log_info "检测到 anssl 正在运行,正在停止旧版本"
222+
if ! run_with_detected_privilege "${APP_DIR}/${BIN_NAME}" stop; then
223+
log_error "停止正在运行的 anssl 失败,请手动停止后重试"
224+
fi
225+
226+
sleep 1
227+
if is_anssl_running; then
228+
log_error "anssl 仍在运行,请手动停止后重试"
229+
fi
230+
231+
INSTALL_STATE="已停止运行中的旧版本并完成安装"
232+
}
233+
175234
# install_binary 安装 anssl 可执行文件。
176235
install_binary() {
177236
[ -f "$BIN_NAME" ] || log_error "发布包中未找到 ${BIN_NAME}"
178237

179238
run_privileged mkdir -p "$APP_DIR"
180239
run_privileged mkdir -p "$INSTALL_DIR"
181-
run_privileged install -m 0755 "$BIN_NAME" "${APP_DIR}/${BIN_NAME}"
240+
tmp_bin="${APP_DIR}/${BIN_NAME}.new.$$"
241+
run_privileged install -m 0755 "$BIN_NAME" "$tmp_bin"
242+
run_privileged mv -f "$tmp_bin" "${APP_DIR}/${BIN_NAME}"
182243

183244
if [ -e "${INSTALL_DIR}/${BIN_NAME}" ] && [ ! -L "${INSTALL_DIR}/${BIN_NAME}" ]; then
184245
log_warn "${INSTALL_DIR}/${BIN_NAME} 已存在且不是软链接,已跳过命令链接"
@@ -259,8 +320,9 @@ print_success_banner() {
259320
anssl 安装成功
260321
261322
版本: ${version_output}
323+
状态: ${INSTALL_STATE}
262324
程序: ${APP_DIR}/${BIN_NAME}
263-
命令: ${INSTALL_DIR}/${BIN_NAME}
325+
命令: ${BIN_NAME} (${INSTALL_DIR}/${BIN_NAME})
264326
配置: ${CONFIG_DIR}/config.yaml
265327
266328
卸载:
@@ -310,6 +372,8 @@ main() {
310372
log_info "解压 ${asset_name}"
311373
tar -xzf "$asset_name"
312374

375+
stop_running_anssl
376+
313377
log_info "安装 ${BIN_NAME}${APP_DIR}"
314378
install_binary
315379

@@ -318,7 +382,7 @@ main() {
318382

319383
print_success_banner
320384
printf '%s\n' "下一步:编辑 ${CONFIG_DIR}/config.yaml,填写 server.accessKey 后运行:"
321-
printf '%s\n' "${INSTALL_DIR}/${BIN_NAME} daemon -c ${CONFIG_DIR}/config.yaml"
385+
printf '%s\n' "${BIN_NAME} daemon -c ${CONFIG_DIR}/config.yaml"
322386
}
323387

324388
main "$@"

0 commit comments

Comments
 (0)