Support optional Ubuntu system bootstrap

This commit is contained in:
武阳
2026-05-07 12:05:41 +08:00
parent fd70af4d56
commit d42e024ccd
3 changed files with 69 additions and 3 deletions
+31
View File
@@ -31,6 +31,27 @@ bash scripts/deploy-ubuntu.sh "$BRANCH" --skip-git
'
```
如果是全新 Ubuntu 机器,也可以在同一个入口中打开系统依赖初始化:
```bash
BOOTSTRAP_SYSTEM=1 APP_DIR="$HOME/zk-data-agent" BRANCH="main" REPO="git@git.n.xiaomi.com:wuyang6/zk-data-agent.git" bash -lc '
set -euo pipefail
if [ -d "$APP_DIR/.git" ]; then
cd "$APP_DIR"
git fetch origin
git checkout "$BRANCH"
git pull --ff-only origin "$BRANCH"
else
rm -rf "$APP_DIR"
git clone --branch "$BRANCH" "$REPO" "$APP_DIR"
cd "$APP_DIR"
fi
bash scripts/deploy-ubuntu.sh "$BRANCH" --skip-git --bootstrap-system
'
```
修改部署目录或分支时,只需要调整命令开头的变量:
```bash
@@ -53,6 +74,14 @@ APP_DIR="$HOME/zk-data-agent-test" BRANCH="main" REPO="git@git.n.xiaomi.com:wuya
bash scripts/deploy-ubuntu.sh
```
如果是全新 Ubuntu 机器,首次安装可以让脚本顺手安装系统依赖:
```bash
bash scripts/deploy-ubuntu.sh --bootstrap-system
```
`--bootstrap-system` 会使用 `sudo apt-get` 安装 Python 编译依赖、`git``curl` 等系统包;应用代码、`.venv`、前端依赖、运行数据和 systemd 用户服务仍然都在当前用户目录下。
部署指定分支:
```bash
@@ -158,6 +187,8 @@ Ubuntu 部署建议准备:
- Node.js `20``22`
- `npm`
如果机器缺少 Python 编译依赖,首次执行时可加 `--bootstrap-system`,脚本会请求 sudo 安装系统包。日常部署和更新不需要 sudo。
Python 后端依赖由部署脚本安装到项目根目录 `.venv`
前端依赖由部署脚本在 `frontend/app` 下执行 `npm ci` 安装,并执行 `npm run build`
+27 -1
View File
@@ -14,15 +14,18 @@ USER_SYSTEMD_DIR="${HOME}/.config/systemd/user"
BRANCH=""
FORCE=0
SKIP_GIT=0
BOOTSTRAP_SYSTEM=0
usage() {
cat <<EOF
Usage: bash scripts/deploy-ubuntu.sh [branch] [--force] [--skip-git]
Usage: bash scripts/deploy-ubuntu.sh [branch] [--force] [--skip-git] [--bootstrap-system]
Options:
branch 要部署的 git 分支;不传则使用当前分支
--force 使用 git reset --hard origin/<branch> 覆盖本地改动
--skip-git 跳过 git 拉取,只部署当前工作区
--bootstrap-system
首次安装时使用 sudo 安装 Ubuntu 系统依赖,应用仍部署在用户目录
EOF
}
@@ -36,6 +39,10 @@ while [[ $# -gt 0 ]]; do
SKIP_GIT=1
shift
;;
--bootstrap-system)
BOOTSTRAP_SYSTEM=1
shift
;;
-h|--help)
usage
exit 0
@@ -73,6 +80,24 @@ require_command() {
fi
}
bootstrap_system_packages() {
if [[ "${BOOTSTRAP_SYSTEM}" != "1" ]]; then
return
fi
require_command sudo "首次安装系统依赖需要 sudo。"
if ! command -v apt-get >/dev/null 2>&1; then
warn "未发现 apt-get,跳过系统依赖安装。请手工安装 git、curl、pyenv、Node.js、npm 和 Python 编译依赖。"
return
fi
log "首次初始化 Ubuntu 系统依赖"
sudo apt-get update
sudo apt-get install -y \
git curl ca-certificates build-essential \
libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev \
libffi-dev liblzma-dev libncursesw5-dev xz-utils tk-dev \
systemd
}
prompt_value() {
local var_name="$1"
local prompt="$2"
@@ -260,6 +285,7 @@ health_check() {
main() {
log "ZK Data Agent Ubuntu 部署"
bootstrap_system_packages
update_git
ensure_env_file
ensure_python
+11 -2
View File
@@ -7,10 +7,11 @@ REPO="${REPO:-git@git.n.xiaomi.com:wuyang6/zk-data-agent.git}"
BRANCH="${BRANCH:-main}"
APP_DIR="${APP_DIR:-${HOME}/zk-data-agent}"
FORCE=0
BOOTSTRAP_SYSTEM="${BOOTSTRAP_SYSTEM:-0}"
usage() {
cat <<EOF
Usage: bash install-from-git.sh [--repo REPO] [--branch BRANCH] [--dir APP_DIR] [--force]
Usage: bash install-from-git.sh [--repo REPO] [--branch BRANCH] [--dir APP_DIR] [--force] [--bootstrap-system]
EOF
}
@@ -32,6 +33,10 @@ while [[ $# -gt 0 ]]; do
FORCE=1
shift
;;
--bootstrap-system)
BOOTSTRAP_SYSTEM=1
shift
;;
-h|--help)
usage
exit 0
@@ -63,4 +68,8 @@ else
cd "${APP_DIR}"
fi
bash scripts/deploy-ubuntu.sh "${BRANCH}" --skip-git
deploy_args=("${BRANCH}" "--skip-git")
if [[ "${BOOTSTRAP_SYSTEM}" == "1" ]]; then
deploy_args+=("--bootstrap-system")
fi
bash scripts/deploy-ubuntu.sh "${deploy_args[@]}"