Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f2ea26e8e | ||
|
|
e295ad8b96 | ||
|
|
99b8126b59 | ||
|
|
b472581622 | ||
|
|
8fe558f6c1 | ||
|
|
5e6df4b323 | ||
|
|
c9d5d678dc | ||
|
|
e21978c25c | ||
|
|
f9545a9d0f | ||
|
|
4d6a9307e5 | ||
|
|
b792f3f21b | ||
|
|
76a0f1ffbd | ||
|
|
70dd8e1d6b | ||
|
|
6e22b24996 |
@@ -25,6 +25,12 @@ jobs:
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Java 17
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: "17"
|
||||
|
||||
- name: Build, sign, and publish release
|
||||
shell: bash
|
||||
run: |
|
||||
@@ -58,6 +64,11 @@ jobs:
|
||||
trap cleanup EXIT
|
||||
printf '%s' "$RELEASE_PRIVATE_KEY_B64" | base64 --decode > "$key_file"
|
||||
export ERP_RELEASE_PRIVATE_KEY_FILE="$key_file"
|
||||
bash tests/release-scripts.test.sh
|
||||
(
|
||||
cd oa-backend
|
||||
./gradlew test installerTest
|
||||
)
|
||||
bash scripts/package-release.sh "$version"
|
||||
|
||||
owner="${repository%%/*}"
|
||||
@@ -97,6 +108,9 @@ jobs:
|
||||
fi
|
||||
|
||||
release_id="$(python3 -c 'import json; print(json.load(open("release.json"))["id"])')"
|
||||
curl "${curl_protocols[@]}" --fail-with-body --silent --show-error --location --retry 3 \
|
||||
--header "$auth_header" --header 'Accept: application/json' \
|
||||
--output release.json "$release_api/$release_id"
|
||||
for asset in "dist/kaidi-erp-$version.tar.gz" "dist/kaidi-erp-installer-$version.jar" dist/SHA256SUMS dist/SHA256SUMS.sig; do
|
||||
name="$(basename "$asset")"
|
||||
encoded_name="$(python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1], safe=""))' "$name")"
|
||||
|
||||
@@ -49,6 +49,7 @@ Administrator -> System Update UI -> erp-update helper
|
||||
|- README.md
|
||||
|- run.command # macOS 本地预览与 ngrok 启动器
|
||||
|- install.sh # 非 Docker 一键安装器
|
||||
|- uninstall.sh # Linux 完整卸载与数据库 schema 重置
|
||||
|- distribution/
|
||||
| |- bin/erp-run # 正式环境应用启动器
|
||||
| `- bin/erp-update # 下载、校验、切换和回滚助手
|
||||
@@ -178,6 +179,8 @@ curl -fsSL https://git.example.com/awaioi/ERP/raw/branch/main/install.sh \
|
||||
|
||||
命令行只做环境准备:优先使用已有的 Java 17+,缺少时通过当前系统的 `apt-get`、`dnf`、`yum`、`zypper` 或 Homebrew 安装 Java、curl、tar、Python 3 和 OpenSSL 3,然后下载并启动独立安装器。数据库信息不在命令行输入。
|
||||
|
||||
Linux 生产服务要求主机使用 systemd;没有 systemd 的容器、WSL 或精简系统只能显式使用 `--no-service` 做开发验收,在线更新也会保持关闭。
|
||||
|
||||
安装器启动后会输出带一次性令牌的局域网地址,例如:
|
||||
|
||||
```text
|
||||
@@ -186,6 +189,8 @@ Setup URL: http://192.168.1.20:8091/?token=<one-time-token>
|
||||
|
||||
首次打开该地址进入网页向导,依次完成环境检查、PostgreSQL 地址/端口/库名/账号/密码/SSL 测试、管理员账号/姓名/密码设置、数据库迁移和初始化。项目当前没有 Redis 依赖,因此向导不会显示 Redis 配置项。正式服务真实健康检查通过后,启动器才会原子写入安装锁并物理删除 `installer/` 和 `install.pending`。
|
||||
|
||||
PostgreSQL 必须使用专用空数据库,网页中填写的账号必须是该数据库的所有者。该约束保证账号拥有 `public` schema 建表权限,并能持有安装器创建的 `pg_trgm` 扩展;只授予 `CONNECT` 权限不足以完成迁移。
|
||||
|
||||
### macOS
|
||||
|
||||
macOS 需要 Homebrew,并使用已有 PostgreSQL:
|
||||
@@ -202,14 +207,37 @@ curl -fsSL https://git.example.com/awaioi/ERP/raw/branch/main/install.sh \
|
||||
当前 Gitea 地址 `http://38.76.196.225:10099` 只允许用于开发验收:
|
||||
|
||||
```bash
|
||||
curl -fsSL http://38.76.196.225:10099/awaioi/ERP/raw/branch/main/install.sh \
|
||||
| sudo -E bash -s -- \
|
||||
(
|
||||
set -e
|
||||
tmp="$(mktemp)"
|
||||
trap 'rm -f -- "$tmp"' EXIT
|
||||
curl -fsSL http://38.76.196.225:10099/awaioi/ERP/raw/tag/v0.3.1/install.sh -o "$tmp"
|
||||
printf '%s %s\n' '89a3c45e76f500c9475cb596ea29e3518bfafdc59f36dd3c7b316ed3cdd0448c' "$tmp" | sha256sum -c -
|
||||
sudo -E bash "$tmp" \
|
||||
--gitea-url http://38.76.196.225:10099 \
|
||||
--repository awaioi/ERP \
|
||||
--allow-insecure
|
||||
)
|
||||
```
|
||||
|
||||
只有首个 Release 发布后这条命令才可下载安装包。HTTP 会暴露请求、Release 元数据和可能使用的访问令牌,不得作为生产方案。
|
||||
只有 `v0.3.1` Release 发布后这条命令才可下载安装包。固定 tag 和 SHA-256 只用于保护当前 HTTP 引导脚本不被传输途中篡改;Release 资产仍会继续执行 Ed25519 和 SHA-256 双重校验。HTTP 会暴露请求、Release 元数据和可能使用的访问令牌,不得作为生产方案。
|
||||
|
||||
### 完整卸载后重装
|
||||
|
||||
以下命令具有破坏性:它会先停止服务,使用现有配置中的 ERP 数据库账号删除并重建目标数据库的 `public` schema,然后删除 systemd unit、程序、配置、状态和日志。脚本只允许数据库所有者执行 schema 清理,并拒绝 `postgres`、`template0`、`template1` 和危险文件路径。
|
||||
|
||||
```bash
|
||||
(
|
||||
set -e
|
||||
tmp="$(mktemp)"
|
||||
trap 'rm -f -- "$tmp"' EXIT
|
||||
curl -fsSL http://38.76.196.225:10099/awaioi/ERP/raw/tag/v0.3.1/uninstall.sh -o "$tmp"
|
||||
printf '%s %s\n' '98c56fed2fd4d01874e4ab5a1a4f3ec42ec3e29b315ffd87385a95488d587546' "$tmp" | sha256sum -c -
|
||||
sudo -E bash "$tmp" --purge-database --yes
|
||||
)
|
||||
```
|
||||
|
||||
卸载器会先核对安装配置、路径和 systemd 停止状态,再清理数据库和文件。卸载成功后,再执行上面的 Linux 一键安装命令。不要对包含其他系统数据的共享数据库运行此命令。
|
||||
|
||||
### 正式安装目录
|
||||
|
||||
@@ -226,6 +254,25 @@ Linux 默认路径:
|
||||
|
||||
使用 `--no-service` 只用于开发验收:启动器会在当前用户下运行,但默认设置 `OA_UPDATE_ENABLED=false`。生产环境应使用 systemd/launchd,让在线更新可以在 Java 进程退出后自动拉起新版本。
|
||||
|
||||
### HTTPS 反向代理
|
||||
|
||||
正式服务监听 `8090`。Nginx、宝塔、Caddy 或 CDN 终止 HTTPS 后,必须把公网协议和主机转发给 Spring Boot;否则浏览器的同源 API 请求会被误判为跨域,并收到纯文本 `403 Invalid CORS request`,前端表现为“响应非 JSON (HTTP 403)”。Nginx 的代理位置至少包含:
|
||||
|
||||
```nginx
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8090;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-Host $host;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-Port $server_port;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
}
|
||||
```
|
||||
|
||||
如果 Nginx 前面还有 Cloudflare 等上游代理,`X-Forwarded-Proto` 必须保留浏览器实际使用的 `https`,不能被内层 HTTP 链路覆盖。前端与 `/api/oa/*` 推荐使用同一个公网域名;不要在 Nginx 中附加 `Access-Control-Allow-Origin *`。
|
||||
|
||||
## 在线更新与回滚
|
||||
|
||||
管理员登录后进入:
|
||||
@@ -261,7 +308,7 @@ ERP_UPDATE_BACKUP_MODE=pg_dump
|
||||
|
||||
## Gitea Release 发布
|
||||
|
||||
推送 `v*` tag 会触发 `.gitea/workflows/release.yml`。流水线会构建前端、生成 PostgreSQL-only JAR、打包、签名,并创建或更新对应 Gitea Release。
|
||||
推送 `v*` tag 会触发 `.gitea/workflows/release.yml`。流水线会先执行 shell、后端和独立安装器测试,再构建前端、生成 PostgreSQL-only JAR、打包、签名,并创建或更新对应 Gitea Release。
|
||||
|
||||
### Actions 前置配置
|
||||
|
||||
@@ -389,6 +436,10 @@ Gitea 仓库尚未发布首个可安装版本,或 Release 缺少四个必需
|
||||
|
||||
运行 `ngrok config check` 确认配置有效,并检查固定域名是否属于当前 ngrok 账号。可以通过 `ERP_RUN_NGROK_BIN`、`ERP_RUN_NGROK_CONFIG` 和 `ERP_RUN_NGROK_API_PORT` 覆盖路径与端口。
|
||||
|
||||
### 反向代理后提示“响应非 JSON (HTTP 403)”
|
||||
|
||||
应用自身的 401/403 权限错误始终是 JSON。该提示表示 Nginx、WAF 或 Spring CORS 层提前返回了纯文本/HTML。先确认代理目标为 `http://127.0.0.1:8090`,再按“HTTPS 反向代理”一节补齐 `Host` 和 `X-Forwarded-*` 请求头;响应正文为 `Invalid CORS request` 时即可确认是协议/主机转发不完整。
|
||||
|
||||
### 签名验证失败
|
||||
|
||||
不要跳过验证。确认 Release 的四个资产来自同一次构建、`SHA256SUMS` 未被改写、签名私钥与 `distribution/release-public-key.pem` 匹配。
|
||||
|
||||
@@ -21,6 +21,7 @@ load_configuration() {
|
||||
INSTALLER_JAR="${ERP_INSTALLER_JAR:-$INSTALL_ROOT/installer/kaidi-erp-installer.jar}"
|
||||
PENDING_FILE="${ERP_INSTALL_PENDING_FILE:-$INSTALL_ROOT/state/install.pending}"
|
||||
INSTALL_LOCK_FILE="${ERP_INSTALL_LOCK_FILE:-$INSTALL_ROOT/state/install.lock}"
|
||||
INSTALL_LOG_FILE="${ERP_INSTALL_LOG_FILE:-$(dirname "$PENDING_FILE")/install-formal.log}"
|
||||
HEALTH_URL="${ERP_HEALTH_URL:-http://127.0.0.1:${SERVER_PORT:-8091}/api/oa/health}"
|
||||
HEALTH_TIMEOUT_SECONDS="${ERP_INSTALL_HEALTH_TIMEOUT_SECONDS:-240}"
|
||||
HEALTH_POLL_SECONDS="${ERP_UPDATE_HEALTH_POLL_SECONDS:-2}"
|
||||
@@ -98,10 +99,14 @@ run_pending_formal_application() {
|
||||
[[ "$HEALTH_POLL_SECONDS" =~ ^[1-9][0-9]*$ ]] || fail 'invalid health poll interval'
|
||||
|
||||
say 'Starting the formal PostgreSQL application'
|
||||
mkdir -p "$(dirname "$INSTALL_LOG_FILE")"
|
||||
: > "$INSTALL_LOG_FILE"
|
||||
chmod 600 "$INSTALL_LOG_FILE"
|
||||
"$JAVA_BIN" "${JAVA_OPTS[@]}" \
|
||||
-jar "$JAR_PATH" \
|
||||
--spring.profiles.active=postgres \
|
||||
--server.port="${SERVER_PORT:-8091}" &
|
||||
--server.port="${SERVER_PORT:-8091}" \
|
||||
> >(tee -a "$INSTALL_LOG_FILE") 2>&1 &
|
||||
APP_PID=$!
|
||||
write_pid "$APP_PID"
|
||||
|
||||
@@ -118,6 +123,7 @@ run_pending_formal_application() {
|
||||
local status=$?
|
||||
set -e
|
||||
clear_pid "$APP_PID"
|
||||
say "Formal application diagnostics: $INSTALL_LOG_FILE"
|
||||
fail "formal application exited before becoming healthy (status $status)"
|
||||
fi
|
||||
if curl -fsS --connect-timeout 2 --max-time 5 "$HEALTH_URL" >/dev/null 2>&1; then
|
||||
@@ -137,6 +143,7 @@ run_pending_formal_application() {
|
||||
wait "$APP_PID"
|
||||
set -e
|
||||
clear_pid "$APP_PID"
|
||||
say "Formal application diagnostics: $INSTALL_LOG_FILE"
|
||||
fail "formal application did not become healthy within ${HEALTH_TIMEOUT_SECONDS} seconds"
|
||||
}
|
||||
|
||||
|
||||
@@ -42,9 +42,42 @@ curl -fsSL https://git.example.com/awaioi/ERP/raw/branch/main/install.sh \
|
||||
--repository awaioi/ERP
|
||||
```
|
||||
|
||||
命令行只检查并安装 Java 17+、curl、tar、Python 3 和 OpenSSL 3,然后启动独立安装器并输出带一次性 token 的网页地址。数据库、管理员和密码全部在首次网页向导填写;安装器会真实测试 PostgreSQL 15+ 和 `pg_trgm`,迁移完成并确认正式服务健康后才写 `install.lock`,随后物理删除安装器目录。
|
||||
命令行只检查并安装 Java 17+、curl、tar、Python 3 和 OpenSSL 3,然后启动独立安装器并输出带一次性 token 的网页地址。数据库、管理员和密码全部在首次网页向导填写;安装器会真实测试 PostgreSQL 15+、数据库所有权、`public` schema 建表权限和 `pg_trgm` 所有权,迁移完成并确认正式服务健康后才写 `install.lock`,随后物理删除安装器目录。
|
||||
|
||||
当前 `http://38.76.196.225:10099` 仅用于开发测试,必须同时传入 `--allow-insecure`。HTTP 会暴露安装脚本、Release 元数据和 Gitea token,不应作为生产部署方式。
|
||||
目标 PostgreSQL 必须是专用空数据库,网页中填写的账号必须是该数据库的所有者。只拥有连接权限的账号会在网页连接测试阶段被拒绝,不再等到 Flyway 迁移后才显示笼统错误。
|
||||
|
||||
Linux 生产服务要求主机使用 systemd;没有 systemd 的容器、WSL 或精简系统只能显式使用 `--no-service` 做开发验收,在线更新也会保持关闭。
|
||||
|
||||
当前 `http://38.76.196.225:10099` 仅用于开发测试,安装器必须同时传入 `--allow-insecure`。在没有 HTTPS 的情况下,必须从固定 tag 下载引导脚本并验证本版本记录的 SHA-256,禁止把可变的 `main` 分支脚本直接管道给 root。HTTP 仍会暴露请求、Release 元数据和 Gitea token,不应作为生产部署方式。
|
||||
|
||||
当前 `v0.3.1` 安装命令:
|
||||
|
||||
```bash
|
||||
(
|
||||
set -e
|
||||
tmp="$(mktemp)"
|
||||
trap 'rm -f -- "$tmp"' EXIT
|
||||
curl -fsSL http://38.76.196.225:10099/awaioi/ERP/raw/tag/v0.3.1/install.sh -o "$tmp"
|
||||
printf '%s %s\n' '89a3c45e76f500c9475cb596ea29e3518bfafdc59f36dd3c7b316ed3cdd0448c' "$tmp" | sha256sum -c -
|
||||
sudo -E bash "$tmp" --gitea-url http://38.76.196.225:10099 \
|
||||
--repository awaioi/ERP --allow-insecure
|
||||
)
|
||||
```
|
||||
|
||||
完整卸载并清空本项目数据库 schema 后重装:
|
||||
|
||||
```bash
|
||||
(
|
||||
set -e
|
||||
tmp="$(mktemp)"
|
||||
trap 'rm -f -- "$tmp"' EXIT
|
||||
curl -fsSL http://38.76.196.225:10099/awaioi/ERP/raw/tag/v0.3.1/uninstall.sh -o "$tmp"
|
||||
printf '%s %s\n' '98c56fed2fd4d01874e4ab5a1a4f3ec42ec3e29b315ffd87385a95488d587546' "$tmp" | sha256sum -c -
|
||||
sudo -E bash "$tmp" --purge-database --yes
|
||||
)
|
||||
```
|
||||
|
||||
该命令会先确认安装路径与 `erp.env` 一致、systemd 服务已经停止,再删除目标数据库 `public` schema 中的全部对象以及 `/opt/kaidi-erp`、`/etc/kaidi-erp`、`/var/lib/kaidi-erp`、`/var/log/kaidi-erp`。只允许对 Kaidi ERP 专用数据库执行。
|
||||
|
||||
## 在线更新
|
||||
|
||||
|
||||
+21
-5
@@ -64,6 +64,14 @@ detect_platform() {
|
||||
fi
|
||||
}
|
||||
|
||||
validate_service_manager() {
|
||||
[[ "$PLATFORM" != "linux" || "$NO_SERVICE" == "1" ]] && return 0
|
||||
command -v systemctl >/dev/null 2>&1 \
|
||||
|| fail 'systemd is required for a production Linux install; use --no-service only for development'
|
||||
[[ -d /run/systemd/system ]] \
|
||||
|| fail 'systemd is not running on this Linux host; use --no-service only for development'
|
||||
}
|
||||
|
||||
java_major() {
|
||||
local java_bin="${1:-java}"
|
||||
"$java_bin" -version 2>&1 | awk -F'[".]' '/version/ {print $2; exit}'
|
||||
@@ -294,10 +302,11 @@ shell_setting() {
|
||||
prepare_layout() {
|
||||
if [[ "$PLATFORM" == "linux" ]]; then
|
||||
ERP_USER="${ERP_SERVICE_USER:-kaidi-erp}"
|
||||
ERP_GROUP="$ERP_USER"
|
||||
[[ "$ERP_USER" =~ ^[a-z_][a-z0-9_-]*[$]?$ ]] || fail 'invalid Linux service user name'
|
||||
id "$ERP_USER" >/dev/null 2>&1 \
|
||||
|| useradd --system --home-dir "$INSTALL_ROOT" --shell /usr/sbin/nologin "$ERP_USER"
|
||||
ERP_GROUP="$(id -gn "$ERP_USER" 2>/dev/null)" \
|
||||
|| fail "unable to resolve the primary group for Linux service user: $ERP_USER"
|
||||
CONFIG_ROOT="${ERP_CONFIG_ROOT:-/etc/kaidi-erp}"
|
||||
STATE_ROOT="${ERP_STATE_ROOT:-/var/lib/kaidi-erp}"
|
||||
LOG_ROOT="${ERP_LOG_ROOT:-/var/log/kaidi-erp}"
|
||||
@@ -385,6 +394,8 @@ write_bootstrap_configuration() {
|
||||
shell_setting ERP_INSTALL_PENDING_FILE "$PENDING_FILE"
|
||||
shell_setting ERP_INSTALL_LOCK_FILE "$INSTALL_LOCK_FILE"
|
||||
shell_setting ERP_INSTALL_OPERATION_LOCK_FILE "$OPERATION_LOCK_FILE"
|
||||
shell_setting ERP_INSTALL_LOG_FILE "$STATE_ROOT/install-formal.log"
|
||||
shell_setting ERP_INSTALL_HEALTH_TIMEOUT_SECONDS "${ERP_INSTALL_HEALTH_TIMEOUT_SECONDS:-240}"
|
||||
shell_setting ERP_SETUP_TOKEN "$SETUP_TOKEN"
|
||||
shell_setting ERP_INSTALLER_JAR "$INSTALL_ROOT/installer/kaidi-erp-installer.jar"
|
||||
shell_setting ERP_JAR_PATH "$INSTALL_ROOT/current/app/kaidi-erp.jar"
|
||||
@@ -433,8 +444,8 @@ User=$ERP_USER
|
||||
Group=$ERP_GROUP
|
||||
Environment="ERP_INSTALL_ROOT=$INSTALL_ROOT"
|
||||
Environment="ERP_CONFIG_FILE=$CONFIG_FILE"
|
||||
WorkingDirectory="$INSTALL_ROOT"
|
||||
ExecStart="$INSTALL_ROOT/current/bin/erp-run"
|
||||
WorkingDirectory=$INSTALL_ROOT
|
||||
ExecStart=$INSTALL_ROOT/current/bin/erp-run
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
TimeoutStopSec=90
|
||||
@@ -442,14 +453,18 @@ SuccessExitStatus=143
|
||||
UMask=0077
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectSystem=full
|
||||
ProtectHome=true
|
||||
ReadWritePaths="$INSTALL_ROOT" "$CONFIG_ROOT" "$STATE_ROOT" "$LOG_ROOT"
|
||||
ReadWritePaths=$INSTALL_ROOT $CONFIG_ROOT $STATE_ROOT $LOG_ROOT
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
systemctl daemon-reload
|
||||
if command -v systemd-analyze >/dev/null 2>&1; then
|
||||
systemd-analyze verify /etc/systemd/system/kaidi-erp.service \
|
||||
|| fail 'generated systemd unit is invalid; run systemd-analyze verify /etc/systemd/system/kaidi-erp.service'
|
||||
fi
|
||||
systemctl enable kaidi-erp.service
|
||||
systemctl restart kaidi-erp.service
|
||||
else
|
||||
@@ -506,6 +521,7 @@ detect_lan_address() {
|
||||
|
||||
main() {
|
||||
detect_platform
|
||||
validate_service_manager
|
||||
if [[ -z "$INSTALL_ROOT" ]]; then
|
||||
if [[ "$PLATFORM" == "linux" ]]; then
|
||||
INSTALL_ROOT=/opt/kaidi-erp
|
||||
|
||||
@@ -8,6 +8,8 @@ group = 'com.kaidi'
|
||||
version = providers.gradleProperty('releaseVersion')
|
||||
.orElse(System.getenv('ERP_RELEASE_VERSION') ?: '0.1.0')
|
||||
.get()
|
||||
def flywayVersion = '11.20.3'
|
||||
def postgresqlDriverVersion = '42.7.13'
|
||||
def productionBuild = providers.gradleProperty('productionBuild')
|
||||
.map { it.toBoolean() }
|
||||
.orElse(false)
|
||||
@@ -47,9 +49,9 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-websocket'
|
||||
|
||||
// Production database and versioned schema migrations.
|
||||
implementation 'org.flywaydb:flyway-core:10.22.0'
|
||||
runtimeOnly 'org.flywaydb:flyway-database-postgresql:10.22.0'
|
||||
runtimeOnly 'org.postgresql:postgresql'
|
||||
implementation "org.flywaydb:flyway-core:$flywayVersion"
|
||||
runtimeOnly "org.flywaydb:flyway-database-postgresql:$flywayVersion"
|
||||
runtimeOnly "org.postgresql:postgresql:$postgresqlDriverVersion"
|
||||
|
||||
// SQLite remains available to source-tree development and tests, but is
|
||||
// deliberately absent from PostgreSQL-only production release artifacts.
|
||||
@@ -69,9 +71,9 @@ dependencies {
|
||||
|
||||
installerImplementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
installerImplementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
installerImplementation 'org.flywaydb:flyway-core:10.22.0'
|
||||
installerRuntimeOnly 'org.flywaydb:flyway-database-postgresql:10.22.0'
|
||||
installerRuntimeOnly 'org.postgresql:postgresql'
|
||||
installerImplementation "org.flywaydb:flyway-core:$flywayVersion"
|
||||
installerRuntimeOnly "org.flywaydb:flyway-database-postgresql:$flywayVersion"
|
||||
installerRuntimeOnly "org.postgresql:postgresql:$postgresqlDriverVersion"
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.kaidi.oa.install;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.output.MigrateResult;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -36,6 +38,8 @@ import java.util.Set;
|
||||
@Service
|
||||
public class InstallerService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(InstallerService.class);
|
||||
|
||||
private static final Set<PosixFilePermission> OWNER_ONLY = EnumSet.of(
|
||||
PosixFilePermission.OWNER_READ,
|
||||
PosixFilePermission.OWNER_WRITE);
|
||||
@@ -117,6 +121,11 @@ public class InstallerService {
|
||||
.load()
|
||||
.migrate();
|
||||
} catch (RuntimeException exception) {
|
||||
log.error(
|
||||
"PostgreSQL migration failed for database {} as user {}",
|
||||
request.database().database().strip(),
|
||||
request.database().username().strip(),
|
||||
exception);
|
||||
throw new InstallApiException(
|
||||
HttpStatus.UNPROCESSABLE_ENTITY,
|
||||
42203,
|
||||
@@ -170,6 +179,7 @@ public class InstallerService {
|
||||
42201,
|
||||
"PostgreSQL 版本过低,需要 15 或更高版本");
|
||||
}
|
||||
requireDatabaseOwnership(connection);
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("SELECT 1");
|
||||
statement.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm");
|
||||
@@ -181,10 +191,16 @@ public class InstallerService {
|
||||
throw new SQLException("pg_trgm is unavailable");
|
||||
}
|
||||
}
|
||||
requirePgTrgmOwnership(connection);
|
||||
return new DatabaseCheck(major, connection.getMetaData().getDatabaseProductVersion());
|
||||
} catch (InstallApiException exception) {
|
||||
throw exception;
|
||||
} catch (SQLException | NumberFormatException exception) {
|
||||
log.warn(
|
||||
"PostgreSQL verification failed for database {} as user {}: {}",
|
||||
database.database().strip(),
|
||||
database.username().strip(),
|
||||
exception.getMessage());
|
||||
throw new InstallApiException(
|
||||
HttpStatus.UNPROCESSABLE_ENTITY,
|
||||
42202,
|
||||
@@ -192,6 +208,40 @@ public class InstallerService {
|
||||
}
|
||||
}
|
||||
|
||||
private void requireDatabaseOwnership(Connection connection) throws SQLException {
|
||||
try (Statement statement = connection.createStatement();
|
||||
ResultSet result = statement.executeQuery(
|
||||
"SELECT "
|
||||
+ "pg_get_userbyid(datdba) = current_user AS owns_database, "
|
||||
+ "has_schema_privilege(current_user, 'public', 'USAGE') AS can_use_schema, "
|
||||
+ "has_schema_privilege(current_user, 'public', 'CREATE') AS can_create_in_schema "
|
||||
+ "FROM pg_database WHERE datname = current_database()")) {
|
||||
if (!result.next()
|
||||
|| !result.getBoolean("owns_database")
|
||||
|| !result.getBoolean("can_use_schema")
|
||||
|| !result.getBoolean("can_create_in_schema")) {
|
||||
throw new InstallApiException(
|
||||
HttpStatus.UNPROCESSABLE_ENTITY,
|
||||
42206,
|
||||
"数据库账号必须是该空数据库的所有者,并拥有 public schema 建表权限");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void requirePgTrgmOwnership(Connection connection) throws SQLException {
|
||||
try (Statement statement = connection.createStatement();
|
||||
ResultSet result = statement.executeQuery(
|
||||
"SELECT pg_get_userbyid(extowner) = current_user "
|
||||
+ "FROM pg_extension WHERE extname = 'pg_trgm'")) {
|
||||
if (!result.next() || !result.getBoolean(1)) {
|
||||
throw new InstallApiException(
|
||||
HttpStatus.UNPROCESSABLE_ENTITY,
|
||||
42207,
|
||||
"数据库账号必须拥有 pg_trgm 扩展;请由管理员删除预建扩展后重新测试");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void rejectExistingInstallation(InstallRequest.Database database) {
|
||||
try (Connection connection = openConnection(database);
|
||||
PreparedStatement tableQuery = connection.prepareStatement(
|
||||
@@ -272,6 +322,11 @@ public class InstallerService {
|
||||
} catch (InstallApiException exception) {
|
||||
throw exception;
|
||||
} catch (Exception exception) {
|
||||
log.error(
|
||||
"Administrator initialization failed for database {} as user {}",
|
||||
database.database().strip(),
|
||||
database.username().strip(),
|
||||
exception);
|
||||
throw new InstallApiException(HttpStatus.UNPROCESSABLE_ENTITY, 42205, "管理员账号初始化失败");
|
||||
}
|
||||
}
|
||||
@@ -326,6 +381,8 @@ public class InstallerService {
|
||||
values.put("ERP_INSTALL_PENDING_FILE", properties.pendingFile().toAbsolutePath().normalize().toString());
|
||||
values.put("ERP_INSTALL_LOCK_FILE", properties.lockFile().toAbsolutePath().normalize().toString());
|
||||
values.put("ERP_INSTALL_OPERATION_LOCK_FILE", properties.operationLockFile().toAbsolutePath().normalize().toString());
|
||||
values.put("ERP_INSTALL_LOG_FILE", stateRoot.resolve("install-formal.log").toString());
|
||||
values.put("ERP_INSTALL_HEALTH_TIMEOUT_SECONDS", environment("ERP_INSTALL_HEALTH_TIMEOUT_SECONDS", "240"));
|
||||
values.put("ERP_SETUP_TOKEN", properties.token());
|
||||
values.put("ERP_INSTALLER_JAR", installRoot.resolve("installer/kaidi-erp-installer.jar").toString());
|
||||
values.put("ERP_JAR_PATH", installRoot.resolve("current/app/kaidi-erp.jar").toString());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
server:
|
||||
port: ${SERVER_PORT:8091}
|
||||
shutdown: graceful
|
||||
forward-headers-strategy: framework
|
||||
|
||||
spring:
|
||||
application:
|
||||
|
||||
@@ -190,6 +190,44 @@
|
||||
.progress-item.running .progress-dot { border-color: #2f6fed; background: #2f6fed; box-shadow: 0 0 0 4px #eaf1fd; }
|
||||
.progress-item.done { color: #187149; }
|
||||
.progress-item.done .progress-dot { border-color: #25805a; background: #25805a; }
|
||||
.install-progress { margin-top: 22px; padding-top: 18px; border-top: 1px solid #e8ebf0; }
|
||||
.install-progress-head, .install-progress-meta, .install-console-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
}
|
||||
.install-progress-label { color: #334057; font-size: 13px; font-weight: 650; }
|
||||
.install-progress-value { color: #1d3557; font-size: 14px; font-variant-numeric: tabular-nums; }
|
||||
.install-progress-track {
|
||||
height: 10px;
|
||||
margin-top: 10px;
|
||||
overflow: hidden;
|
||||
border-radius: 4px;
|
||||
background: #e5eaf1;
|
||||
}
|
||||
.install-progress-bar {
|
||||
width: 0;
|
||||
height: 100%;
|
||||
background: #2f6fed;
|
||||
transition: width .35s ease;
|
||||
}
|
||||
.install-progress-meta { margin-top: 8px; color: #707c90; font-size: 12px; }
|
||||
.install-console { margin-top: 14px; overflow: hidden; border: 1px solid #28364a; border-radius: 6px; background: #182233; }
|
||||
.install-console-head { min-height: 36px; padding: 0 12px; border-bottom: 1px solid #314158; background: #111a28; }
|
||||
.install-console-title { color: #dce5f2; font-size: 12px; font-weight: 650; }
|
||||
.install-console-state { color: #7dd3a8; font-size: 12px; }
|
||||
.install-log {
|
||||
height: 148px;
|
||||
margin: 0;
|
||||
overflow: auto;
|
||||
padding: 10px 12px;
|
||||
color: #c9d5e5;
|
||||
font: 12px/1.65 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
||||
letter-spacing: 0;
|
||||
overflow-wrap: anywhere;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
.complete-mark {
|
||||
width: 58px;
|
||||
height: 58px;
|
||||
@@ -220,6 +258,8 @@
|
||||
.summary-row { grid-template-columns: 1fr; gap: 5px; }
|
||||
.actions { flex-wrap: wrap-reverse; }
|
||||
.btn { flex: 1 1 120px; }
|
||||
.install-progress-meta { align-items: flex-start; flex-direction: column; gap: 4px; }
|
||||
.install-log { height: 172px; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
@@ -299,6 +339,23 @@
|
||||
<div class="progress-item" data-progress="3"><span class="progress-dot"></span><span>创建管理员账号</span></div>
|
||||
<div class="progress-item" data-progress="4"><span class="progress-dot"></span><span>启动正式服务并写入安装锁</span></div>
|
||||
</div>
|
||||
<div class="install-progress">
|
||||
<div class="install-progress-head">
|
||||
<span class="install-progress-label" id="install-phase">准备安装</span>
|
||||
<strong class="install-progress-value" id="install-percent">0%</strong>
|
||||
</div>
|
||||
<div class="install-progress-track" id="install-progress-track" role="progressbar" aria-label="安装进度" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
|
||||
<div class="install-progress-bar" id="install-progress-bar"></div>
|
||||
</div>
|
||||
<div class="install-progress-meta">
|
||||
<span id="install-elapsed">本页已等待 0 秒</span>
|
||||
<span id="install-check">等待安装任务</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="install-console">
|
||||
<div class="install-console-head"><span class="install-console-title">安装状态</span><span class="install-console-state" id="install-console-state">运行中</span></div>
|
||||
<pre class="install-log" id="install-log" aria-live="polite"></pre>
|
||||
</div>
|
||||
<div id="install-message" class="notice info">正在初始化,数据库规模较大时可能需要几分钟。</div>
|
||||
</div>
|
||||
<div id="complete-view" class="hidden">
|
||||
@@ -323,7 +380,18 @@
|
||||
history.replaceState(null, '', clean);
|
||||
}
|
||||
|
||||
const state = { step: 1, database: null, databaseFingerprint: '', administrator: null, installing: false };
|
||||
const state = {
|
||||
step: 1,
|
||||
database: null,
|
||||
databaseFingerprint: '',
|
||||
administrator: null,
|
||||
installing: false,
|
||||
installStartedAt: 0,
|
||||
installPercent: 0,
|
||||
installLogEntries: [],
|
||||
healthPolling: false,
|
||||
};
|
||||
const FORMAL_SERVICE_TIMEOUT_MS = 240000;
|
||||
const $ = (selector) => document.querySelector(selector);
|
||||
const $$ = (selector) => Array.from(document.querySelectorAll(selector));
|
||||
|
||||
@@ -421,26 +489,97 @@
|
||||
});
|
||||
}
|
||||
|
||||
function formatElapsed(milliseconds) {
|
||||
const seconds = Math.max(0, Math.floor(milliseconds / 1000));
|
||||
if (seconds < 60) return `${seconds} 秒`;
|
||||
return `${Math.floor(seconds / 60)} 分 ${seconds % 60} 秒`;
|
||||
}
|
||||
|
||||
function setInstallMeter(percent, phase, detail) {
|
||||
const value = Math.max(state.installPercent, Math.max(0, Math.min(100, Math.round(percent))));
|
||||
state.installPercent = value;
|
||||
$('#install-progress-bar').style.width = `${value}%`;
|
||||
$('#install-percent').textContent = `${value}%`;
|
||||
$('#install-phase').textContent = phase;
|
||||
$('#install-check').textContent = detail;
|
||||
$('#install-elapsed').textContent = `本页已等待 ${formatElapsed(Date.now() - state.installStartedAt)}`;
|
||||
const track = $('#install-progress-track');
|
||||
track.setAttribute('aria-valuenow', String(value));
|
||||
track.setAttribute('aria-valuetext', `${phase},${value}%`);
|
||||
}
|
||||
|
||||
function appendInstallLog(message) {
|
||||
const timestamp = new Date().toLocaleTimeString('zh-CN', { hour12: false });
|
||||
state.installLogEntries.push(`[${timestamp}] ${message}`);
|
||||
state.installLogEntries = state.installLogEntries.slice(-16);
|
||||
const output = $('#install-log');
|
||||
output.textContent = state.installLogEntries.join('\n');
|
||||
output.scrollTop = output.scrollHeight;
|
||||
}
|
||||
|
||||
function beginInstallTracking(resumed) {
|
||||
state.installStartedAt = Date.now();
|
||||
state.installLogEntries = [];
|
||||
$('#install-message').className = 'notice info';
|
||||
$('#install-console-state').textContent = '运行中';
|
||||
if (resumed) {
|
||||
setInstallMeter(70, '恢复安装任务', '准备检查正式服务');
|
||||
appendInstallLog('检测到尚未完成的安装任务,继续等待正式服务。');
|
||||
} else {
|
||||
setInstallMeter(5, '提交安装任务', '准备验证数据库');
|
||||
appendInstallLog('安装请求已提交,正在验证 PostgreSQL 连接和权限。');
|
||||
}
|
||||
}
|
||||
|
||||
async function pollFormalService() {
|
||||
if (state.healthPolling) return;
|
||||
state.healthPolling = true;
|
||||
if (!state.installStartedAt) beginInstallTracking(true);
|
||||
setProgress(4);
|
||||
$('#install-message').textContent = '初始化完成,正在等待正式服务通过健康检查。';
|
||||
const deadline = Date.now() + 240000;
|
||||
setInstallMeter(Math.max(state.installPercent, 72), '启动正式服务', '等待第 1 次健康检查');
|
||||
appendInstallLog('数据库初始化完成,安装器正在切换到正式服务。');
|
||||
$('#install-message').textContent = '初始化完成,正在等待正式服务通过健康检查。页面会持续显示检查进度。';
|
||||
const healthStartedAt = Date.now();
|
||||
const deadline = healthStartedAt + FORMAL_SERVICE_TIMEOUT_MS;
|
||||
let attempt = 0;
|
||||
while (Date.now() < deadline) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||
attempt += 1;
|
||||
const healthElapsed = Date.now() - healthStartedAt;
|
||||
const estimated = 72 + Math.floor((healthElapsed / FORMAL_SERVICE_TIMEOUT_MS) * 23);
|
||||
setInstallMeter(Math.min(95, estimated), '等待正式服务健康检查', `第 ${attempt} 次检查`);
|
||||
try {
|
||||
const response = await fetch('/api/oa/health', { cache: 'no-store' });
|
||||
const body = await response.json();
|
||||
if (response.ok && (body?.data?.status === 'UP' || body?.status === 'UP')) {
|
||||
const serviceStatus = body?.data?.status || body?.status || `HTTP ${response.status}`;
|
||||
const installLocked = body?.data?.installLocked ?? body?.installLocked;
|
||||
if (response.ok && serviceStatus === 'UP' && (installLocked === true || installLocked === 'true')) {
|
||||
setInstallMeter(100, '安装完成', '正式服务已就绪');
|
||||
appendInstallLog(`健康检查第 ${attempt} 次通过,安装锁已确认。`);
|
||||
$('#install-console-state').textContent = '已完成';
|
||||
$$('[data-progress]').forEach((item) => { item.classList.remove('running'); item.classList.add('done'); });
|
||||
$('#installing-view').classList.add('hidden');
|
||||
$('#complete-view').classList.remove('hidden');
|
||||
sessionStorage.removeItem('kaidi.setup.token');
|
||||
state.healthPolling = false;
|
||||
return;
|
||||
}
|
||||
} catch { /* Service is changing from installer to the formal application. */ }
|
||||
if (response.ok && serviceStatus === 'UP') {
|
||||
setInstallMeter(98, '正式服务已通过健康检查', '等待写入安装锁');
|
||||
appendInstallLog(`健康检查第 ${attempt} 次通过,正在等待后台写入安装锁。`);
|
||||
continue;
|
||||
}
|
||||
appendInstallLog(`健康检查第 ${attempt} 次:服务状态 ${serviceStatus}。`);
|
||||
} catch {
|
||||
appendInstallLog(`健康检查第 ${attempt} 次:服务端口正在切换,继续等待。`);
|
||||
}
|
||||
}
|
||||
state.healthPolling = false;
|
||||
setInstallMeter(95, '正式服务尚未就绪', `${Math.round(FORMAL_SERVICE_TIMEOUT_MS / 1000)} 秒检查超时`);
|
||||
appendInstallLog('健康检查超时,安装状态已保留,未写入完成锁。');
|
||||
$('#install-console-state').textContent = '需要检查';
|
||||
$('#install-message').className = 'notice error';
|
||||
$('#install-message').textContent = '正式服务尚未就绪。请在服务器查看 kaidi-erp 服务日志,安装文件和安装锁不会被误删。';
|
||||
$('#install-message').textContent = '正式服务尚未就绪。请查看 systemctl 日志或 /var/lib/kaidi-erp/install-formal.log;安装文件和待确认状态均已保留。';
|
||||
}
|
||||
|
||||
$('#start-button').addEventListener('click', () => go(2));
|
||||
@@ -506,10 +645,16 @@
|
||||
state.installing = true;
|
||||
$('#confirm-view').classList.add('hidden');
|
||||
$('#installing-view').classList.remove('hidden');
|
||||
beginInstallTracking(false);
|
||||
setProgress(1);
|
||||
let migrationTimer;
|
||||
try {
|
||||
const request = { database: state.database, administrator: state.administrator };
|
||||
setTimeout(() => setProgress(2), 700);
|
||||
migrationTimer = setTimeout(() => {
|
||||
setProgress(2);
|
||||
setInstallMeter(28, '执行数据库初始化', '迁移结构和基础数据');
|
||||
appendInstallLog('数据库连接验证通过,正在执行结构迁移和基础数据初始化。');
|
||||
}, 700);
|
||||
try {
|
||||
await api('/api/install/complete', { method: 'POST', body: JSON.stringify(request) });
|
||||
} catch (error) {
|
||||
@@ -518,12 +663,20 @@
|
||||
// is flushed; the formal health probe is the authoritative result.
|
||||
if (!(error instanceof TypeError) && error.message !== '安装服务响应无效') throw error;
|
||||
}
|
||||
clearTimeout(migrationTimer);
|
||||
setProgress(3);
|
||||
setInstallMeter(68, '创建管理员并切换服务', '数据库初始化已完成');
|
||||
appendInstallLog('数据库迁移和管理员初始化已提交,准备启动正式服务。');
|
||||
state.administrator.password = '';
|
||||
state.database.password = '';
|
||||
await pollFormalService();
|
||||
} catch (error) {
|
||||
clearTimeout(migrationTimer);
|
||||
state.installing = false;
|
||||
state.healthPolling = false;
|
||||
setInstallMeter(state.installPercent, '安装已中断', '请处理错误后重试');
|
||||
appendInstallLog(`安装中断:${error.message}`);
|
||||
$('#install-console-state').textContent = '已中断';
|
||||
$('#install-message').className = 'notice error';
|
||||
$('#install-message').textContent = error.message;
|
||||
const actions = document.createElement('div');
|
||||
@@ -548,6 +701,7 @@
|
||||
go(4);
|
||||
$('#confirm-view').classList.add('hidden');
|
||||
$('#installing-view').classList.remove('hidden');
|
||||
beginInstallTracking(true);
|
||||
return pollFormalService();
|
||||
}
|
||||
$('#java-detail').textContent = `Java ${status.javaVersion},最低要求 Java ${status.minimumJava}`;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.kaidi.oa.web;
|
||||
|
||||
import com.kaidi.oa.common.ApiResp;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -8,6 +9,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
@@ -19,9 +23,12 @@ import java.util.Map;
|
||||
public class HealthController {
|
||||
|
||||
private final DataSource dataSource;
|
||||
private final Path installLockFile;
|
||||
|
||||
public HealthController(DataSource dataSource) {
|
||||
public HealthController(DataSource dataSource,
|
||||
@Value("${ERP_INSTALL_LOCK_FILE:}") String installLockFile) {
|
||||
this.dataSource = dataSource;
|
||||
this.installLockFile = resolveInstallLockFile(installLockFile);
|
||||
}
|
||||
|
||||
@GetMapping("/health")
|
||||
@@ -30,12 +37,31 @@ public class HealthController {
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet result = statement.executeQuery("SELECT 1")) {
|
||||
if (connection.isValid(2) && result.next() && result.getInt(1) == 1) {
|
||||
return ResponseEntity.ok(ApiResp.ok(Map.of("status", "UP", "database", "UP")));
|
||||
return ResponseEntity.ok(ApiResp.ok(readiness("UP", "UP")));
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// Health responses deliberately avoid exposing connection details.
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
|
||||
.body(new ApiResp<>(50301, "database unavailable", Map.of("status", "DOWN", "database", "DOWN")));
|
||||
.body(new ApiResp<>(50301, "database unavailable", readiness("DOWN", "DOWN")));
|
||||
}
|
||||
|
||||
private Map<String, String> readiness(String status, String database) {
|
||||
return Map.of(
|
||||
"status", status,
|
||||
"database", database,
|
||||
"installLocked", Boolean.toString(installLockFile != null && Files.isRegularFile(installLockFile))
|
||||
);
|
||||
}
|
||||
|
||||
private static Path resolveInstallLockFile(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Path.of(value).toAbsolutePath().normalize();
|
||||
} catch (InvalidPathException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
server:
|
||||
port: 8090
|
||||
# Reconstruct the public scheme/host when running behind Nginx, Caddy or a
|
||||
# tunnel so same-origin HTTPS requests are not rejected as cross-origin.
|
||||
forward-headers-strategy: framework
|
||||
|
||||
spring:
|
||||
application:
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.kaidi.oa.web;
|
||||
|
||||
import com.kaidi.oa.common.ApiResp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class HealthControllerTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void reportsTheActualInstallationLockState() throws Exception {
|
||||
DataSource dataSource = mock(DataSource.class);
|
||||
Connection connection = mock(Connection.class);
|
||||
Statement statement = mock(Statement.class);
|
||||
ResultSet resultSet = mock(ResultSet.class);
|
||||
when(dataSource.getConnection()).thenReturn(connection);
|
||||
when(connection.createStatement()).thenReturn(statement);
|
||||
when(connection.isValid(2)).thenReturn(true);
|
||||
when(statement.executeQuery("SELECT 1")).thenReturn(resultSet);
|
||||
when(resultSet.next()).thenReturn(true);
|
||||
when(resultSet.getInt(1)).thenReturn(1);
|
||||
|
||||
Path lockFile = tempDir.resolve("install.lock");
|
||||
HealthController controller = new HealthController(dataSource, lockFile.toString());
|
||||
|
||||
assertThat(healthData(controller.health())).containsEntry("installLocked", "false");
|
||||
|
||||
Files.writeString(lockFile, "locked\n");
|
||||
|
||||
assertThat(healthData(controller.health()))
|
||||
.containsEntry("status", "UP")
|
||||
.containsEntry("database", "UP")
|
||||
.containsEntry("installLocked", "true");
|
||||
}
|
||||
|
||||
private static Map<String, String> healthData(
|
||||
ResponseEntity<ApiResp<Map<String, String>>> response) {
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
return response.getBody().data();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
TARGET=/root/kaidi-erp-reinstall-v031.sh
|
||||
URL=http://38.76.196.225:10099/awaioi/ERP/raw/tag/recovery-v0.3.1-1/reinstall-centos9.sh
|
||||
SHA256=0370fb17926f3b53d5ae849310fdc6cbbf019150415365a464b0972b8edf5d9a
|
||||
|
||||
curl -fsSL --connect-timeout 15 --max-time 300 "$URL" -o "$TARGET"
|
||||
printf '%s %s\n' "$SHA256" "$TARGET" | sha256sum -c -
|
||||
exec bash "$TARGET"
|
||||
Executable
+105
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
GITEA_BASE_URL="http://38.76.196.225:10099"
|
||||
REPOSITORY="awaioi/ERP"
|
||||
VERSION="0.3.4"
|
||||
TAG="v${VERSION}"
|
||||
INSTALL_SHA256="89a3c45e76f500c9475cb596ea29e3518bfafdc59f36dd3c7b316ed3cdd0448c"
|
||||
UNINSTALL_SHA256="98c56fed2fd4d01874e4ab5a1a4f3ec42ec3e29b315ffd87385a95488d587546"
|
||||
PG_ROOT="${ERP_RECOVERY_PG_ROOT:-/www/server/pgsql}"
|
||||
PG_DATA="${ERP_RECOVERY_PG_DATA:-/www/server/pgsql/data}"
|
||||
PG_INIT_SCRIPT="${ERP_RECOVERY_PG_INIT_SCRIPT:-/etc/init.d/pgsql}"
|
||||
TMP_DIR=""
|
||||
|
||||
say() { printf '[ERP Recovery] %s\n' "$*"; }
|
||||
fail() { printf '[ERP Recovery] ERROR: %s\n' "$*" >&2; exit 1; }
|
||||
|
||||
cleanup() {
|
||||
[[ -z "$TMP_DIR" || ! -d "$TMP_DIR" ]] || rm -rf -- "$TMP_DIR"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
postgres_port_open() {
|
||||
timeout 2 bash -c 'exec 3<>/dev/tcp/127.0.0.1/5432' >/dev/null 2>&1
|
||||
}
|
||||
|
||||
wait_for_postgres() {
|
||||
local _
|
||||
for _ in {1..60}; do
|
||||
postgres_port_open && return 0
|
||||
sleep 1
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
start_erp_postgres() {
|
||||
if postgres_port_open; then
|
||||
say 'PostgreSQL is already listening on 127.0.0.1:5432'
|
||||
return 0
|
||||
fi
|
||||
|
||||
[[ -f "$PG_DATA/PG_VERSION" ]] \
|
||||
|| fail "PostgreSQL data directory is missing: $PG_DATA"
|
||||
|
||||
if [[ -x "$PG_INIT_SCRIPT" ]]; then
|
||||
say "Starting PostgreSQL with $PG_INIT_SCRIPT"
|
||||
if ! "$PG_INIT_SCRIPT" start; then
|
||||
say 'The init script returned an error; checking whether PostgreSQL still started'
|
||||
fi
|
||||
else
|
||||
local pg_ctl pg_user
|
||||
pg_ctl="$(find "$PG_ROOT" -type f -name pg_ctl -print -quit 2>/dev/null || true)"
|
||||
[[ -n "$pg_ctl" && -x "$pg_ctl" ]] || fail "pg_ctl was not found below $PG_ROOT"
|
||||
pg_user="$(stat -c '%U' "$PG_DATA")" || fail 'Unable to determine the PostgreSQL owner'
|
||||
id "$pg_user" >/dev/null 2>&1 || fail "PostgreSQL owner does not exist: $pg_user"
|
||||
say "Starting PostgreSQL as $pg_user"
|
||||
runuser -u "$pg_user" -- "$pg_ctl" \
|
||||
-D "$PG_DATA" \
|
||||
-l "$PG_DATA/startup.log" \
|
||||
-w -t 60 start
|
||||
fi
|
||||
|
||||
if ! wait_for_postgres; then
|
||||
[[ ! -r "$PG_DATA/startup.log" ]] || tail -n 100 "$PG_DATA/startup.log" >&2
|
||||
fail 'PostgreSQL did not listen on 127.0.0.1:5432 within 60 seconds'
|
||||
fi
|
||||
say 'PostgreSQL is ready on 127.0.0.1:5432'
|
||||
}
|
||||
|
||||
download_verified_script() {
|
||||
local name="$1" expected="$2" destination
|
||||
destination="$TMP_DIR/$name"
|
||||
curl -fsSL --connect-timeout 15 --max-time 300 \
|
||||
"$GITEA_BASE_URL/$REPOSITORY/raw/tag/$TAG/$name" \
|
||||
-o "$destination"
|
||||
printf '%s %s\n' "$expected" "$destination" | sha256sum -c - >&2
|
||||
printf '%s' "$destination"
|
||||
}
|
||||
|
||||
main() {
|
||||
[[ "$(id -u)" == "0" ]] || fail 'Run this recovery command as root'
|
||||
for command in bash curl find runuser sha256sum stat timeout; do
|
||||
command -v "$command" >/dev/null 2>&1 || fail "Missing command: $command"
|
||||
done
|
||||
|
||||
start_erp_postgres
|
||||
TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/kaidi-erp-recovery.XXXXXX")"
|
||||
|
||||
local uninstaller installer
|
||||
uninstaller="$(download_verified_script uninstall.sh "$UNINSTALL_SHA256")"
|
||||
say 'Removing the pending installation and its PostgreSQL schema safely'
|
||||
bash "$uninstaller" --purge-database --yes
|
||||
|
||||
installer="$(download_verified_script install.sh "$INSTALL_SHA256")"
|
||||
say "Installing Kaidi ERP $VERSION"
|
||||
bash "$installer" \
|
||||
--gitea-url "$GITEA_BASE_URL" \
|
||||
--repository "$REPOSITORY" \
|
||||
--version "$VERSION" \
|
||||
--allow-insecure
|
||||
}
|
||||
|
||||
if [[ "${BASH_SOURCE[0]:-$0}" == "$0" ]]; then
|
||||
main "$@"
|
||||
fi
|
||||
Executable
+164
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -Eeuo pipefail
|
||||
|
||||
GITEA_URL="${GITEA_URL:-http://38.76.196.225:10099/}"
|
||||
GITEA_REPOSITORY="${GITEA_REPOSITORY:-awaioi/ERP}"
|
||||
RUNNER_IMAGE="${RUNNER_IMAGE:-}"
|
||||
RUNNER_CONTAINER="${RUNNER_CONTAINER:-gitea-runner}"
|
||||
RUNNER_VOLUME="${RUNNER_VOLUME:-gitea-runner-data}"
|
||||
RUNNER_LABELS="${RUNNER_LABELS:-ubuntu-latest:docker://docker.gitea.com/runner-images:ubuntu-latest}"
|
||||
|
||||
log() {
|
||||
printf '[Gitea Runner] %s\n' "$*"
|
||||
}
|
||||
|
||||
fail() {
|
||||
printf '[Gitea Runner] ERROR: %s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
command -v docker >/dev/null 2>&1 || fail "docker is not installed"
|
||||
docker info >/dev/null 2>&1 || fail "docker is not running or the current user cannot access it"
|
||||
|
||||
find_gitea_container() {
|
||||
local container_id image name
|
||||
|
||||
while read -r container_id image name; do
|
||||
case "$image" in
|
||||
gitea/gitea:* | gitea/gitea@* | */gitea/gitea:* | */gitea/gitea@* | docker.gitea.com/gitea:* | docker.gitea.com/gitea@*)
|
||||
printf '%s\n' "$container_id"
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
done < <(docker ps --format '{{.ID}} {{.Image}} {{.Names}}')
|
||||
|
||||
while read -r container_id name; do
|
||||
case "$name" in
|
||||
*runner*) ;;
|
||||
*gitea*)
|
||||
printf '%s\n' "$container_id"
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
done < <(docker ps --format '{{.ID}} {{.Names}}')
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
pull_runner_image() {
|
||||
local candidate
|
||||
local -a candidates
|
||||
|
||||
if [[ -n "$RUNNER_IMAGE" ]]; then
|
||||
candidates=("$RUNNER_IMAGE")
|
||||
else
|
||||
candidates=(
|
||||
"docker.gitea.com/runner:3.0.2"
|
||||
"docker.gitea.com/act_runner:3.0.2"
|
||||
"gitea/runner:3.0.2"
|
||||
)
|
||||
fi
|
||||
|
||||
for candidate in "${candidates[@]}"; do
|
||||
log "Pulling $candidate"
|
||||
if docker pull "$candidate"; then
|
||||
RUNNER_IMAGE="$candidate"
|
||||
return 0
|
||||
fi
|
||||
log "Image source failed; trying the next source"
|
||||
done
|
||||
|
||||
fail "all official Gitea Runner image sources failed"
|
||||
}
|
||||
|
||||
GITEA_CONTAINER="$(find_gitea_container || true)"
|
||||
[[ -n "$GITEA_CONTAINER" ]] || fail "no running Gitea container was found"
|
||||
|
||||
TMP_DIR="$(mktemp -d)"
|
||||
TOKEN_FILE="$TMP_DIR/runner-token"
|
||||
BOOTSTRAP_RUNNING=0
|
||||
|
||||
cleanup() {
|
||||
if [[ "$BOOTSTRAP_RUNNING" == "1" ]]; then
|
||||
docker rm -f "$RUNNER_CONTAINER" >/dev/null 2>&1 || true
|
||||
fi
|
||||
if [[ -f "$TOKEN_FILE" ]]; then
|
||||
: >"$TOKEN_FILE"
|
||||
fi
|
||||
rm -rf -- "$TMP_DIR"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
log "Generating a repository-scoped registration token"
|
||||
if ! docker exec -u git "$GITEA_CONTAINER" gitea actions generate-runner-token \
|
||||
--scope "$GITEA_REPOSITORY" | tr -d '\r' | tail -n 1 >"$TOKEN_FILE"; then
|
||||
fail "Gitea could not generate a runner token"
|
||||
fi
|
||||
[[ -s "$TOKEN_FILE" ]] || fail "Gitea returned an empty runner token"
|
||||
chmod 600 "$TOKEN_FILE"
|
||||
|
||||
pull_runner_image
|
||||
docker rm -f "$RUNNER_CONTAINER" >/dev/null 2>&1 || true
|
||||
docker volume create "$RUNNER_VOLUME" >/dev/null
|
||||
docker run --rm --entrypoint /bin/rm \
|
||||
-v "$RUNNER_VOLUME:/data" \
|
||||
"$RUNNER_IMAGE" -f /data/.runner
|
||||
|
||||
log "Registering the runner"
|
||||
docker run -d \
|
||||
--name "$RUNNER_CONTAINER" \
|
||||
-v "$RUNNER_VOLUME:/data" \
|
||||
-v "$TOKEN_FILE:/run/secrets/gitea_runner_token:ro,Z" \
|
||||
-v /var/run/docker.sock:/var/run/docker.sock \
|
||||
-e "GITEA_INSTANCE_URL=$GITEA_URL" \
|
||||
-e GITEA_RUNNER_REGISTRATION_TOKEN_FILE=/run/secrets/gitea_runner_token \
|
||||
-e "GITEA_RUNNER_NAME=erp-release-$(hostname -s)" \
|
||||
-e "GITEA_RUNNER_LABELS=$RUNNER_LABELS" \
|
||||
-e GITEA_MAX_REG_ATTEMPTS=24 \
|
||||
"$RUNNER_IMAGE" >/dev/null
|
||||
BOOTSTRAP_RUNNING=1
|
||||
|
||||
READY=0
|
||||
for ((attempt = 1; attempt <= 60; attempt++)); do
|
||||
if docker exec "$RUNNER_CONTAINER" test -s /data/.runner 2>/dev/null; then
|
||||
READY=1
|
||||
break
|
||||
fi
|
||||
if [[ "$(docker inspect -f '{{.State.Running}}' "$RUNNER_CONTAINER" 2>/dev/null || true)" != "true" ]]; then
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
if [[ "$READY" != "1" ]]; then
|
||||
docker logs --tail 100 "$RUNNER_CONTAINER" >&2 || true
|
||||
fail "runner registration failed"
|
||||
fi
|
||||
|
||||
docker logs --tail 30 "$RUNNER_CONTAINER" || true
|
||||
docker rm -f "$RUNNER_CONTAINER" >/dev/null
|
||||
BOOTSTRAP_RUNNING=0
|
||||
: >"$TOKEN_FILE"
|
||||
|
||||
log "Starting the persistent runner without the registration token"
|
||||
docker run -d \
|
||||
--name "$RUNNER_CONTAINER" \
|
||||
--restart unless-stopped \
|
||||
-v "$RUNNER_VOLUME:/data" \
|
||||
-v /var/run/docker.sock:/var/run/docker.sock \
|
||||
-e "GITEA_INSTANCE_URL=$GITEA_URL" \
|
||||
-e "GITEA_RUNNER_NAME=erp-release-$(hostname -s)" \
|
||||
-e "GITEA_RUNNER_LABELS=$RUNNER_LABELS" \
|
||||
"$RUNNER_IMAGE" >/dev/null
|
||||
|
||||
sleep 5
|
||||
[[ "$(docker inspect -f '{{.State.Running}}' "$RUNNER_CONTAINER")" == "true" ]] || {
|
||||
docker logs --tail 100 "$RUNNER_CONTAINER" >&2 || true
|
||||
fail "runner container stopped after registration"
|
||||
}
|
||||
|
||||
log "Runner is online; queued release jobs can now continue"
|
||||
docker ps --filter "name=^/${RUNNER_CONTAINER}$" \
|
||||
--format 'table {{.Names}}\t{{.Image}}\t{{.Status}}'
|
||||
docker logs --tail 80 "$RUNNER_CONTAINER" || true
|
||||
@@ -56,6 +56,83 @@ test_erp_run_preserves_java_option_arguments() (
|
||||
grep -Fqx -- "$tmp/current/app/kaidi-erp.jar" <<< "$output"
|
||||
)
|
||||
|
||||
test_erp_run_finalizes_healthy_pending_install() (
|
||||
local tmp
|
||||
tmp="$(mktemp -d "${TMPDIR:-/tmp}/erp-handoff-success.XXXXXX")" || return 1
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
mkdir -p "$tmp/current/app" "$tmp/run" "$tmp/state" "$tmp/installer" "$tmp/bin"
|
||||
: > "$tmp/current/app/kaidi-erp.jar"
|
||||
: > "$tmp/installer/kaidi-erp-installer.jar"
|
||||
printf 'pending\n' > "$tmp/state/install.pending"
|
||||
printf '%s\n' \
|
||||
'#!/usr/bin/env bash' \
|
||||
'if [[ "${1:-}" == "-version" ]]; then printf '\''openjdk version "17.0.12"\n'\'' >&2; exit 0; fi' \
|
||||
'printf '\''formal application output\n'\''' \
|
||||
'sleep 0.1' \
|
||||
'exit 0' > "$tmp/java"
|
||||
printf '%s\n' '#!/usr/bin/env bash' 'exit 0' > "$tmp/bin/curl"
|
||||
chmod +x "$tmp/java" "$tmp/bin/curl"
|
||||
{
|
||||
printf 'ERP_JAVA_BIN=%q\n' "$tmp/java"
|
||||
printf 'ERP_RUN_DIR=%q\n' "$tmp/run"
|
||||
printf 'ERP_JAR_PATH=%q\n' "$tmp/current/app/kaidi-erp.jar"
|
||||
printf 'ERP_INSTALLER_JAR=%q\n' "$tmp/installer/kaidi-erp-installer.jar"
|
||||
printf 'ERP_INSTALL_PENDING_FILE=%q\n' "$tmp/state/install.pending"
|
||||
printf 'ERP_INSTALL_LOCK_FILE=%q\n' "$tmp/state/install.lock"
|
||||
printf 'ERP_INSTALL_LOG_FILE=%q\n' "$tmp/state/install-formal.log"
|
||||
printf 'ERP_INSTALL_HEALTH_TIMEOUT_SECONDS=3\n'
|
||||
printf 'ERP_UPDATE_HEALTH_POLL_SECONDS=1\n'
|
||||
} > "$tmp/erp.env"
|
||||
|
||||
PATH="$tmp/bin:$PATH" ERP_INSTALL_ROOT="$tmp" ERP_CONFIG_FILE="$tmp/erp.env" \
|
||||
"$PROJECT_ROOT/distribution/bin/erp-run" > "$tmp/run.log" 2>&1 || return 1
|
||||
|
||||
[[ -f "$tmp/state/install.lock" ]] || return 1
|
||||
[[ ! -e "$tmp/state/install.pending" ]] || return 1
|
||||
[[ ! -d "$tmp/installer" ]] || return 1
|
||||
[[ ! -e "$tmp/run/app.pid" ]] || return 1
|
||||
grep -Fq 'formal application output' "$tmp/state/install-formal.log"
|
||||
)
|
||||
|
||||
test_erp_run_preserves_failed_pending_install() (
|
||||
local tmp output status=0
|
||||
tmp="$(mktemp -d "${TMPDIR:-/tmp}/erp-handoff-failure.XXXXXX")" || return 1
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
mkdir -p "$tmp/current/app" "$tmp/run" "$tmp/state" "$tmp/installer" "$tmp/bin"
|
||||
: > "$tmp/current/app/kaidi-erp.jar"
|
||||
: > "$tmp/installer/kaidi-erp-installer.jar"
|
||||
printf 'pending\n' > "$tmp/state/install.pending"
|
||||
printf '%s\n' \
|
||||
'#!/usr/bin/env bash' \
|
||||
'if [[ "${1:-}" == "-version" ]]; then printf '\''openjdk version "17.0.12"\n'\'' >&2; exit 0; fi' \
|
||||
'printf '\''formal application exploded\n'\'' >&2' \
|
||||
'sleep 0.1' \
|
||||
'exit 23' > "$tmp/java"
|
||||
printf '%s\n' '#!/usr/bin/env bash' 'exit 1' > "$tmp/bin/curl"
|
||||
chmod +x "$tmp/java" "$tmp/bin/curl"
|
||||
{
|
||||
printf 'ERP_JAVA_BIN=%q\n' "$tmp/java"
|
||||
printf 'ERP_RUN_DIR=%q\n' "$tmp/run"
|
||||
printf 'ERP_JAR_PATH=%q\n' "$tmp/current/app/kaidi-erp.jar"
|
||||
printf 'ERP_INSTALLER_JAR=%q\n' "$tmp/installer/kaidi-erp-installer.jar"
|
||||
printf 'ERP_INSTALL_PENDING_FILE=%q\n' "$tmp/state/install.pending"
|
||||
printf 'ERP_INSTALL_LOCK_FILE=%q\n' "$tmp/state/install.lock"
|
||||
printf 'ERP_INSTALL_LOG_FILE=%q\n' "$tmp/state/install-formal.log"
|
||||
printf 'ERP_INSTALL_HEALTH_TIMEOUT_SECONDS=3\n'
|
||||
printf 'ERP_UPDATE_HEALTH_POLL_SECONDS=1\n'
|
||||
} > "$tmp/erp.env"
|
||||
|
||||
output="$(PATH="$tmp/bin:$PATH" ERP_INSTALL_ROOT="$tmp" ERP_CONFIG_FILE="$tmp/erp.env" \
|
||||
"$PROJECT_ROOT/distribution/bin/erp-run" 2>&1)" || status=$?
|
||||
|
||||
[[ "$status" -ne 0 ]] || return 1
|
||||
[[ -f "$tmp/state/install.pending" ]] || return 1
|
||||
[[ ! -e "$tmp/state/install.lock" ]] || return 1
|
||||
[[ -d "$tmp/installer" ]] || return 1
|
||||
grep -Fq 'formal application exploded' "$tmp/state/install-formal.log" || return 1
|
||||
[[ "$output" == *"Formal application diagnostics: $tmp/state/install-formal.log"* ]]
|
||||
)
|
||||
|
||||
prepare_signed_archive() {
|
||||
local tmp="$1" unsafe="${2:-0}"
|
||||
mkdir -p "$tmp/package/kaidi-erp-1.2.3/app"
|
||||
@@ -187,7 +264,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
"prerelease": False,
|
||||
"assets": [
|
||||
{"name": archive, "browser_download_url": base + "/" + archive},
|
||||
{"name": f"kaidi-erp-installer-{version}.jar", "browser_download_url": base + f"/assets/kaidi-erp-installer-{version}.jar"},
|
||||
{"name": f"kaidi-erp-installer-{version}.jar", "browser_download_url": base + f"/kaidi-erp-installer-{version}.jar"},
|
||||
{"name": "SHA256SUMS", "browser_download_url": base + "/SHA256SUMS"},
|
||||
{"name": "SHA256SUMS.sig", "browser_download_url": base + "/SHA256SUMS.sig"},
|
||||
],
|
||||
@@ -235,7 +312,7 @@ PY
|
||||
NO_SERVICE=0
|
||||
PUBLIC_KEY="$(<"$tmp/public.pem")"
|
||||
|
||||
detect_platform() { PLATFORM=darwin; }
|
||||
detect_platform() { PLATFORM=darwin; ARCH=amd64; }
|
||||
check_and_install_dependencies() { JAVA_BIN="$(command -v java)"; return 0; }
|
||||
start_service() { return 0; }
|
||||
wait_for_installer() { return 0; }
|
||||
@@ -246,7 +323,7 @@ PY
|
||||
[[ -r "$INSTALL_ROOT/current/app/kaidi-erp.jar" ]] || return 1
|
||||
(
|
||||
source "$ERP_CONFIG_ROOT/erp.env"
|
||||
[[ "$SPRING_PROFILES_ACTIVE" == "postgres" ]]
|
||||
[[ -z "${SPRING_PROFILES_ACTIVE:-}" ]]
|
||||
[[ "$OA_UPDATE_ENABLED" == "true" ]]
|
||||
[[ "$OA_UPDATE_GITEA_BASE_URL" == "http://127.0.0.1:$port" ]]
|
||||
[[ "$ERP_SETUP_TOKEN" =~ ^[0-9a-f]{64}$ ]]
|
||||
@@ -451,11 +528,131 @@ test_installer_requires_explicit_gitea_url() (
|
||||
set --
|
||||
source "$root/install.sh"
|
||||
GITEA_BASE_URL=
|
||||
NO_SERVICE=1
|
||||
main
|
||||
' _ "$PROJECT_ROOT" 2>&1)" || status=$?
|
||||
[[ "$status" -ne 0 && "$output" == *'Gitea URL is required'* ]]
|
||||
)
|
||||
|
||||
test_linux_service_preflight_requires_systemd() (
|
||||
local tmp output status=0
|
||||
tmp="$(mktemp -d "${TMPDIR:-/tmp}/erp-systemd-test.XXXXXX")" || return 1
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
mkdir -p "$tmp/bin"
|
||||
source "$PROJECT_ROOT/install.sh"
|
||||
PLATFORM=linux
|
||||
NO_SERVICE=0
|
||||
output="$(PATH="$tmp/bin" validate_service_manager 2>&1)" || status=$?
|
||||
[[ "$status" -ne 0 && "$output" == *'systemd'* ]] || return 1
|
||||
NO_SERVICE=1 PATH="$tmp/bin" validate_service_manager
|
||||
)
|
||||
|
||||
test_systemd_unit_uses_compatible_protection() (
|
||||
grep -Fqx 'ProtectSystem=full' <(sed -n '/^ cat > \/etc\/systemd\/system\/kaidi-erp.service/,/^ systemctl daemon-reload/p' "$PROJECT_ROOT/install.sh")
|
||||
! grep -Fq 'ProtectSystem=strict' "$PROJECT_ROOT/install.sh"
|
||||
)
|
||||
|
||||
test_systemd_unit_uses_unquoted_legacy_paths() (
|
||||
local unit
|
||||
unit="$(sed -n '/^ cat > \/etc\/systemd\/system\/kaidi-erp.service/,/^ systemctl daemon-reload/p' "$PROJECT_ROOT/install.sh")"
|
||||
grep -Fqx 'WorkingDirectory=$INSTALL_ROOT' <<< "$unit"
|
||||
grep -Fqx 'ExecStart=$INSTALL_ROOT/current/bin/erp-run' <<< "$unit"
|
||||
grep -Fqx 'ReadWritePaths=$INSTALL_ROOT $CONFIG_ROOT $STATE_ROOT $LOG_ROOT' <<< "$unit"
|
||||
! grep -Fq 'WorkingDirectory="$INSTALL_ROOT"' <<< "$unit"
|
||||
)
|
||||
|
||||
test_uninstaller_purges_database_before_removing_files() (
|
||||
local tmp
|
||||
tmp="$(mktemp -d "${TMPDIR:-/tmp}/erp-uninstall-test.XXXXXX")" || return 1
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
mkdir -p "$tmp/bin" "$tmp/install" "$tmp/config" "$tmp/state" "$tmp/logs"
|
||||
: > "$tmp/unit.service"
|
||||
{
|
||||
printf 'ERP_PGHOST=127.0.0.1\n'
|
||||
printf 'ERP_PGPORT=5432\n'
|
||||
printf 'ERP_PGDATABASE=kaidi_test\n'
|
||||
printf 'ERP_PGSSLMODE=disable\n'
|
||||
printf 'OA_DB_USERNAME=kaidi_test\n'
|
||||
printf 'OA_DB_PASSWORD='\''test-password'\''\n'
|
||||
printf 'ERP_INSTALL_ROOT=%q\n' "$tmp/install"
|
||||
printf 'ERP_INSTALL_LOCK_FILE=%q\n' "$tmp/state/install.lock"
|
||||
} > "$tmp/config/erp.env"
|
||||
printf '%s\n' \
|
||||
'#!/usr/bin/env bash' \
|
||||
'printf '\''%s\n'\'' "$*" >> "$MOCK_SYSTEMCTL_LOG"' \
|
||||
'if [[ "${1:-}" == "is-active" ]]; then exit 3; fi' \
|
||||
'exit 0' > "$tmp/bin/systemctl"
|
||||
printf '%s\n' \
|
||||
'#!/usr/bin/env bash' \
|
||||
'printf '\''ARGS %s\n'\'' "$*" >> "$MOCK_PSQL_LOG"' \
|
||||
'if [[ " $* " == *" -c "* ]]; then printf '\''t\n'\''; exit 0; fi' \
|
||||
'cat >> "$MOCK_PSQL_LOG"' \
|
||||
'exit 0' > "$tmp/bin/psql"
|
||||
chmod +x "$tmp/bin/systemctl" "$tmp/bin/psql"
|
||||
|
||||
PATH="$tmp/bin:$PATH" \
|
||||
MOCK_SYSTEMCTL_LOG="$tmp/systemctl.log" \
|
||||
MOCK_PSQL_LOG="$tmp/psql.log" \
|
||||
ERP_UNINSTALL_TEST_MODE=1 \
|
||||
ERP_UNINSTALL_SYSTEMD_UNIT_FILE="$tmp/unit.service" \
|
||||
"$PROJECT_ROOT/uninstall.sh" --purge-database --yes \
|
||||
--config-file "$tmp/config/erp.env" \
|
||||
--install-root "$tmp/install" \
|
||||
--config-root "$tmp/config" \
|
||||
--state-root "$tmp/state" \
|
||||
--log-root "$tmp/logs" > "$tmp/uninstall.log" 2>&1 || return 1
|
||||
|
||||
[[ ! -e "$tmp/install" && ! -e "$tmp/config" && ! -e "$tmp/state" && ! -e "$tmp/logs" ]] || return 1
|
||||
[[ ! -e "$tmp/unit.service" ]] || return 1
|
||||
grep -Fq 'DROP SCHEMA IF EXISTS public CASCADE;' "$tmp/psql.log" || return 1
|
||||
grep -Fq 'stop kaidi-erp.service' "$tmp/systemctl.log" || return 1
|
||||
grep -Fq 'disable kaidi-erp.service' "$tmp/systemctl.log"
|
||||
)
|
||||
|
||||
test_uninstaller_rejects_unsafe_paths() (
|
||||
! (source "$PROJECT_ROOT/uninstall.sh"; validate_remove_path /etc) >/dev/null 2>&1 || return 1
|
||||
! (source "$PROJECT_ROOT/uninstall.sh"; validate_remove_path /etc/ssh) >/dev/null 2>&1 || return 1
|
||||
! (source "$PROJECT_ROOT/uninstall.sh"; validate_remove_path /opt/kaidi-erp/../../etc) >/dev/null 2>&1
|
||||
)
|
||||
|
||||
test_uninstaller_aborts_when_service_remains_active() (
|
||||
local tmp output status=0
|
||||
tmp="$(mktemp -d "${TMPDIR:-/tmp}/erp-uninstall-active.XXXXXX")" || return 1
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
mkdir -p "$tmp/bin" "$tmp/install" "$tmp/config" "$tmp/state" "$tmp/logs"
|
||||
{
|
||||
printf 'ERP_PGHOST=127.0.0.1\n'
|
||||
printf 'ERP_PGPORT=5432\n'
|
||||
printf 'ERP_PGDATABASE=kaidi_test\n'
|
||||
printf 'ERP_PGSSLMODE=disable\n'
|
||||
printf 'OA_DB_USERNAME=kaidi_test\n'
|
||||
printf 'OA_DB_PASSWORD=test-password\n'
|
||||
printf 'ERP_INSTALL_ROOT=%q\n' "$tmp/install"
|
||||
printf 'ERP_INSTALL_LOCK_FILE=%q\n' "$tmp/state/install.lock"
|
||||
} > "$tmp/config/erp.env"
|
||||
printf '%s\n' \
|
||||
'#!/usr/bin/env bash' \
|
||||
'if [[ "${1:-}" == "stop" ]]; then exit 1; fi' \
|
||||
'if [[ "${1:-}" == "show" ]]; then printf '\''loaded\n'\''; exit 0; fi' \
|
||||
'if [[ "${1:-}" == "is-active" ]]; then exit 0; fi' \
|
||||
'exit 0' > "$tmp/bin/systemctl"
|
||||
printf '%s\n' '#!/usr/bin/env bash' 'touch "$MOCK_PSQL_CALLED"' 'exit 0' > "$tmp/bin/psql"
|
||||
chmod +x "$tmp/bin/systemctl" "$tmp/bin/psql"
|
||||
|
||||
output="$(PATH="$tmp/bin:$PATH" MOCK_PSQL_CALLED="$tmp/psql.called" \
|
||||
ERP_UNINSTALL_TEST_MODE=1 ERP_UNINSTALL_SYSTEMD_UNIT_FILE="$tmp/unit.service" \
|
||||
"$PROJECT_ROOT/uninstall.sh" --purge-database --yes \
|
||||
--config-file "$tmp/config/erp.env" \
|
||||
--install-root "$tmp/install" \
|
||||
--config-root "$tmp/config" \
|
||||
--state-root "$tmp/state" \
|
||||
--log-root "$tmp/logs" 2>&1)" || status=$?
|
||||
|
||||
[[ "$status" -ne 0 && "$output" == *'unable to stop kaidi-erp.service'* ]] || return 1
|
||||
[[ ! -e "$tmp/psql.called" ]] || return 1
|
||||
[[ -d "$tmp/install" && -d "$tmp/config" && -d "$tmp/state" && -d "$tmp/logs" ]]
|
||||
)
|
||||
|
||||
test_no_service_install_disables_online_update() (
|
||||
local tmp
|
||||
tmp="$(mktemp -d "${TMPDIR:-/tmp}/erp-no-service-test.XXXXXX")" || return 1
|
||||
@@ -484,6 +681,8 @@ test_release_workflow_uses_scoped_job_token() (
|
||||
grep -Fqx ' releases: write' "$workflow"
|
||||
grep -Fq 'GITEA_BASE_URL: ${{ github.server_url }}' "$workflow"
|
||||
grep -Fq 'GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}' "$workflow"
|
||||
grep -Fq 'uses: actions/setup-java@v4' "$workflow"
|
||||
grep -Fq 'java-version: "17"' "$workflow"
|
||||
! grep -Fq 'RELEASE_GITEA_TOKEN' "$workflow"
|
||||
! grep -Fq 'vars.GITEA_BASE_URL' "$workflow"
|
||||
)
|
||||
@@ -496,7 +695,26 @@ test_installer_runs_when_piped_to_bash() (
|
||||
[[ "$output" != *'BASH_SOURCE'* ]]
|
||||
)
|
||||
|
||||
test_installer_shows_live_health_progress() (
|
||||
local page="$PROJECT_ROOT/oa-backend/src/installer/resources/static/index.html"
|
||||
grep -Fq 'role="progressbar"' "$page"
|
||||
grep -Fq 'id="install-log"' "$page"
|
||||
grep -Fq 'Math.min(95, estimated)' "$page"
|
||||
grep -Fq '健康检查第 ${attempt} 次' "$page"
|
||||
grep -Fq '本页已等待 ${formatElapsed' "$page"
|
||||
grep -Fq 'installLocked' "$page"
|
||||
)
|
||||
|
||||
test_app_honors_reverse_proxy_headers() (
|
||||
grep -Fqx ' forward-headers-strategy: framework' \
|
||||
"$PROJECT_ROOT/oa-backend/src/main/resources/application.yml"
|
||||
grep -Fqx ' forward-headers-strategy: framework' \
|
||||
"$PROJECT_ROOT/oa-backend/src/installer/resources/application.yml"
|
||||
)
|
||||
|
||||
run_test 'erp-run preserves Java option arguments' test_erp_run_preserves_java_option_arguments
|
||||
run_test 'erp-run finalizes a healthy pending installation' test_erp_run_finalizes_healthy_pending_install
|
||||
run_test 'erp-run preserves a failed pending installation' test_erp_run_preserves_failed_pending_install
|
||||
run_test 'installer accepts a correctly signed archive' test_installer_verifies_signed_safe_archive
|
||||
run_test 'installer rejects symlinks in release archives' test_installer_rejects_archive_symlinks
|
||||
run_test 'stable update channel rejects prerelease tags' test_stable_channel_rejects_prerelease_tag
|
||||
@@ -506,9 +724,17 @@ run_test 'update helper rolls back an unhealthy release end to end' test_update_
|
||||
run_test 'update helper rejects a concurrent process' test_update_lock_rejects_concurrent_process
|
||||
run_test 'installer moves database setup to the web wizard' test_installer_moves_database_setup_to_web_wizard
|
||||
run_test 'installer requires an explicit Gitea URL' test_installer_requires_explicit_gitea_url
|
||||
run_test 'Linux production install requires systemd' test_linux_service_preflight_requires_systemd
|
||||
run_test 'systemd unit uses compatible protection' test_systemd_unit_uses_compatible_protection
|
||||
run_test 'systemd unit uses unquoted legacy paths' test_systemd_unit_uses_unquoted_legacy_paths
|
||||
run_test 'uninstaller purges database before removing files' test_uninstaller_purges_database_before_removing_files
|
||||
run_test 'uninstaller rejects unsafe paths' test_uninstaller_rejects_unsafe_paths
|
||||
run_test 'uninstaller aborts while the service remains active' test_uninstaller_aborts_when_service_remains_active
|
||||
run_test 'no-service install disables online update' test_no_service_install_disables_online_update
|
||||
run_test 'release workflow uses the scoped Gitea job token' test_release_workflow_uses_scoped_job_token
|
||||
run_test 'installer starts correctly when piped to bash' test_installer_runs_when_piped_to_bash
|
||||
run_test 'installer shows live formal-service health progress' test_installer_shows_live_health_progress
|
||||
run_test 'application honors standard reverse-proxy headers' test_app_honors_reverse_proxy_headers
|
||||
|
||||
printf 'RESULT pass=%s fail=%s\n' "$PASS" "$FAIL"
|
||||
[[ "$FAIL" -eq 0 ]]
|
||||
|
||||
Executable
+242
@@ -0,0 +1,242 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
CONFIG_FILE="${ERP_CONFIG_FILE:-/etc/kaidi-erp/erp.env}"
|
||||
INSTALL_ROOT="${ERP_INSTALL_ROOT:-/opt/kaidi-erp}"
|
||||
CONFIG_ROOT="${ERP_CONFIG_ROOT:-/etc/kaidi-erp}"
|
||||
STATE_ROOT="${ERP_STATE_ROOT:-/var/lib/kaidi-erp}"
|
||||
LOG_ROOT="${ERP_LOG_ROOT:-/var/log/kaidi-erp}"
|
||||
PURGE_DATABASE=0
|
||||
ASSUME_YES=0
|
||||
|
||||
say() { printf '[ERP Uninstall] %s\n' "$*"; }
|
||||
fail() { printf '[ERP Uninstall] ERROR: %s\n' "$*" >&2; exit 1; }
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: uninstall.sh [options]
|
||||
--purge-database Drop and recreate the public schema using the configured ERP account
|
||||
--yes Confirm destructive removal without an interactive prompt
|
||||
--config-file PATH Installer-generated erp.env file
|
||||
--install-root PATH Installation root (default: /opt/kaidi-erp)
|
||||
--config-root PATH Configuration root (default: /etc/kaidi-erp)
|
||||
--state-root PATH State root (default: /var/lib/kaidi-erp)
|
||||
--log-root PATH Log root (default: /var/log/kaidi-erp)
|
||||
EOF
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--purge-database) PURGE_DATABASE=1; shift ;;
|
||||
--yes) ASSUME_YES=1; shift ;;
|
||||
--config-file) [[ $# -ge 2 ]] || fail '--config-file requires a value'; CONFIG_FILE="$2"; shift 2 ;;
|
||||
--install-root) [[ $# -ge 2 ]] || fail '--install-root requires a value'; INSTALL_ROOT="$2"; shift 2 ;;
|
||||
--config-root) [[ $# -ge 2 ]] || fail '--config-root requires a value'; CONFIG_ROOT="$2"; shift 2 ;;
|
||||
--state-root) [[ $# -ge 2 ]] || fail '--state-root requires a value'; STATE_ROOT="$2"; shift 2 ;;
|
||||
--log-root) [[ $# -ge 2 ]] || fail '--log-root requires a value'; LOG_ROOT="$2"; shift 2 ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) fail "unknown option: $1" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
require_root() {
|
||||
if [[ "${ERP_UNINSTALL_TEST_MODE:-0}" != "1" && "$(id -u)" != "0" ]]; then
|
||||
fail 'Linux uninstall requires root'
|
||||
fi
|
||||
}
|
||||
|
||||
require_confirmation() {
|
||||
[[ "$PURGE_DATABASE" == "1" ]] || fail '--purge-database is required for a clean reinstall'
|
||||
[[ "$ASSUME_YES" == "1" ]] || fail '--yes is required to confirm database and file removal'
|
||||
}
|
||||
|
||||
validate_remove_path() {
|
||||
local path="$1"
|
||||
[[ "$path" == /* && "$path" != "/" && ${#path} -ge 8 ]] \
|
||||
|| fail "refusing unsafe removal path: $path"
|
||||
[[ "$path" =~ ^/[A-Za-z0-9._/@:+-]+$ ]] \
|
||||
|| fail "refusing removal path with unsafe characters: $path"
|
||||
|
||||
local resolved
|
||||
resolved="$(python3 - "$path" <<'PY'
|
||||
import os
|
||||
import sys
|
||||
print(os.path.realpath(sys.argv[1]), end="")
|
||||
PY
|
||||
)" || fail "unable to resolve removal path: $path"
|
||||
if [[ "${ERP_UNINSTALL_TEST_MODE:-0}" != "1" && "$resolved" != "$path" ]]; then
|
||||
fail "removal path must be canonical and cannot traverse links: $path"
|
||||
fi
|
||||
|
||||
case "$resolved" in
|
||||
/bin|/boot|/dev|/etc|/home|/lib|/lib64|/opt|/proc|/root|/run|/sbin|/srv|/sys|/tmp|/usr|/var)
|
||||
fail "refusing unsafe removal path: $path"
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ "${ERP_UNINSTALL_TEST_MODE:-0}" != "1" ]]; then
|
||||
case "$resolved" in
|
||||
/etc/kaidi-erp|/var/lib/kaidi-erp|/var/log/kaidi-erp) ;;
|
||||
/bin/*|/boot/*|/dev/*|/etc/*|/lib/*|/lib64/*|/proc/*|/root/*|/run/*|/sbin/*|/sys/*|/tmp/*|/usr/*|/var/*)
|
||||
fail "refusing removal below a protected system directory: $path"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
printf '%s' "$resolved"
|
||||
}
|
||||
|
||||
prepare_paths() {
|
||||
command -v python3 >/dev/null 2>&1 || fail 'python3 is required'
|
||||
INSTALL_ROOT="$(validate_remove_path "$INSTALL_ROOT")"
|
||||
CONFIG_ROOT="$(validate_remove_path "$CONFIG_ROOT")"
|
||||
STATE_ROOT="$(validate_remove_path "$STATE_ROOT")"
|
||||
LOG_ROOT="$(validate_remove_path "$LOG_ROOT")"
|
||||
|
||||
local config_resolved
|
||||
config_resolved="$(python3 - "$CONFIG_FILE" <<'PY'
|
||||
import os
|
||||
import sys
|
||||
print(os.path.realpath(sys.argv[1]), end="")
|
||||
PY
|
||||
)" || fail "unable to resolve configuration path: $CONFIG_FILE"
|
||||
[[ "$config_resolved" == "$CONFIG_ROOT/erp.env" ]] \
|
||||
|| fail "configuration file must be $CONFIG_ROOT/erp.env"
|
||||
CONFIG_FILE="$config_resolved"
|
||||
}
|
||||
|
||||
read_config_value() {
|
||||
local key="$1"
|
||||
python3 - "$CONFIG_FILE" "$key" <<'PY'
|
||||
import re
|
||||
import shlex
|
||||
import sys
|
||||
|
||||
path, wanted = sys.argv[1:]
|
||||
pattern = re.compile(r"[A-Z][A-Z0-9_]*")
|
||||
seen = set()
|
||||
matches = []
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
for raw in handle:
|
||||
line = raw.rstrip("\r\n")
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, encoded = line.split("=", 1)
|
||||
if not pattern.fullmatch(key):
|
||||
raise SystemExit("invalid configuration key")
|
||||
if key in seen:
|
||||
raise SystemExit(f"duplicate configuration key: {key}")
|
||||
seen.add(key)
|
||||
if key != wanted:
|
||||
continue
|
||||
values = shlex.split(encoded, posix=True)
|
||||
if len(values) != 1:
|
||||
raise SystemExit(f"invalid value for {wanted}")
|
||||
matches.append(values[0])
|
||||
if len(matches) != 1:
|
||||
raise SystemExit(f"missing configuration value: {wanted}")
|
||||
print(matches[0], end="")
|
||||
PY
|
||||
}
|
||||
|
||||
verify_installation_identity() {
|
||||
[[ -f "$CONFIG_FILE" && ! -L "$CONFIG_FILE" ]] \
|
||||
|| fail "configuration file is missing or unsafe: $CONFIG_FILE"
|
||||
|
||||
local configured_root configured_lock configured_state
|
||||
configured_root="$(read_config_value ERP_INSTALL_ROOT)" \
|
||||
|| fail 'unable to verify the configured installation root'
|
||||
configured_lock="$(read_config_value ERP_INSTALL_LOCK_FILE)" \
|
||||
|| fail 'unable to verify the configured installation state'
|
||||
configured_root="$(validate_remove_path "$configured_root")"
|
||||
configured_state="$(validate_remove_path "$(dirname "$configured_lock")")"
|
||||
[[ "$configured_root" == "$INSTALL_ROOT" ]] \
|
||||
|| fail 'requested installation root does not match erp.env'
|
||||
[[ "$configured_state" == "$STATE_ROOT" ]] \
|
||||
|| fail 'requested state root does not match erp.env'
|
||||
}
|
||||
|
||||
purge_database() {
|
||||
command -v psql >/dev/null 2>&1 || fail 'PostgreSQL client psql is required'
|
||||
|
||||
local host port database sslmode username password owner_check
|
||||
host="$(read_config_value ERP_PGHOST)"
|
||||
port="$(read_config_value ERP_PGPORT)"
|
||||
database="$(read_config_value ERP_PGDATABASE)"
|
||||
sslmode="$(read_config_value ERP_PGSSLMODE)"
|
||||
username="$(read_config_value OA_DB_USERNAME)"
|
||||
password="$(read_config_value OA_DB_PASSWORD)"
|
||||
|
||||
[[ "$host" != *[[:space:]]* && "$port" =~ ^[0-9]+$ && "$port" -ge 1 && "$port" -le 65535 ]] \
|
||||
|| fail 'invalid database endpoint in configuration'
|
||||
[[ "$database" =~ ^[A-Za-z_][A-Za-z0-9_-]{0,62}$ ]] || fail 'invalid database name in configuration'
|
||||
[[ "$username" =~ ^[A-Za-z_][A-Za-z0-9_.-]{0,127}$ ]] || fail 'invalid database user in configuration'
|
||||
case "$database" in postgres|template0|template1) fail "refusing to purge protected database: $database" ;; esac
|
||||
case "$sslmode" in disable|allow|prefer|require|verify-ca|verify-full) ;; *) fail 'invalid database SSL mode' ;; esac
|
||||
|
||||
owner_check="$(PGPASSWORD="$password" PGSSLMODE="$sslmode" psql -XAt \
|
||||
-h "$host" -p "$port" -U "$username" -d "$database" -v ON_ERROR_STOP=1 \
|
||||
-c "SELECT pg_get_userbyid(datdba) = current_user FROM pg_database WHERE datname = current_database()")" \
|
||||
|| fail 'unable to verify database ownership'
|
||||
[[ "$owner_check" == "t" ]] \
|
||||
|| fail 'configured ERP account is not the database owner; database was not changed'
|
||||
|
||||
say "Purging PostgreSQL schema $database/public"
|
||||
PGPASSWORD="$password" PGSSLMODE="$sslmode" psql -X \
|
||||
-h "$host" -p "$port" -U "$username" -d "$database" -v ON_ERROR_STOP=1 <<'SQL'
|
||||
DROP SCHEMA IF EXISTS public CASCADE;
|
||||
SELECT format('CREATE SCHEMA public AUTHORIZATION %I', current_user) \gexec
|
||||
SQL
|
||||
}
|
||||
|
||||
stop_service() {
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
local load_state
|
||||
if ! systemctl stop kaidi-erp.service >/dev/null 2>&1; then
|
||||
load_state="$(systemctl show kaidi-erp.service --property=LoadState --value 2>/dev/null || true)"
|
||||
[[ "$load_state" == "not-found" ]] \
|
||||
|| fail 'unable to stop kaidi-erp.service; database and files were not changed'
|
||||
fi
|
||||
if systemctl is-active --quiet kaidi-erp.service; then
|
||||
fail 'kaidi-erp.service is still active; database and files were not changed'
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
remove_service() {
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
systemctl disable kaidi-erp.service >/dev/null 2>&1 || true
|
||||
fi
|
||||
local unit_file="${ERP_UNINSTALL_SYSTEMD_UNIT_FILE:-/etc/systemd/system/kaidi-erp.service}"
|
||||
if [[ "${ERP_UNINSTALL_TEST_MODE:-0}" != "1" && "$unit_file" != "/etc/systemd/system/kaidi-erp.service" ]]; then
|
||||
fail 'custom systemd unit path is only available in test mode'
|
||||
fi
|
||||
rm -f -- "$unit_file"
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
systemctl daemon-reload >/dev/null 2>&1 || true
|
||||
systemctl reset-failed kaidi-erp.service >/dev/null 2>&1 || true
|
||||
fi
|
||||
}
|
||||
|
||||
remove_files() {
|
||||
local path
|
||||
for path in "$INSTALL_ROOT" "$CONFIG_ROOT" "$STATE_ROOT" "$LOG_ROOT"; do
|
||||
[[ ! -e "$path" && ! -L "$path" ]] || rm -rf -- "$path"
|
||||
done
|
||||
}
|
||||
|
||||
main() {
|
||||
require_root
|
||||
require_confirmation
|
||||
prepare_paths
|
||||
verify_installation_identity
|
||||
stop_service
|
||||
purge_database
|
||||
remove_service
|
||||
remove_files
|
||||
say 'Kaidi ERP service, files, state, logs, and public database schema were removed'
|
||||
}
|
||||
|
||||
if [[ "${BASH_SOURCE[0]:-$0}" == "$0" ]]; then
|
||||
main "$@"
|
||||
fi
|
||||
Reference in New Issue
Block a user