230 lines
8.1 KiB
Bash
Executable File
230 lines
8.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
# DevBox 一键安装 / 更新 / 卸载
|
||
#
|
||
# 仓库在自己搭的 Gitea 上(公开库,一条命令就行):
|
||
# curl -fsSL http://110.42.217.133:3000/gitea/devbox/raw/branch/main/install.sh | sudo bash
|
||
# 换端口 / 更新 / 卸载:在后面加 --port 8080、--update、--uninstall
|
||
# 私有库才需要带上令牌:| sudo bash -s -- --token "$GITEA_TOKEN"
|
||
set -euo pipefail
|
||
|
||
REPO_URL="${DEVBOX_REPO:-http://110.42.217.133:3000/gitea/devbox.git}"
|
||
RAW_URL="${REPO_URL%.git}/raw/branch/main"
|
||
APP_DIR="${DEVBOX_DIR:-/opt/devbox}"
|
||
PORT="${DEVBOX_PORT:-8090}"
|
||
SERVICE="devbox"
|
||
NGINX_DOMAIN=""
|
||
MODE="install"
|
||
NO_SERVICE=0
|
||
GIT_USER="${GITEA_USER:-gitea}"
|
||
TOKEN="${GITEA_TOKEN:-${GIT_TOKEN:-}}"
|
||
REPO_HOST="$(printf '%s' "$REPO_URL" | sed -E 's#^https?://##; s#/.*$##')"
|
||
|
||
# 私有库拉代码:把用户名/令牌临时拼进 URL 用一次,用完把 origin 改回干净地址,
|
||
# 令牌不会留在 .git/config 里,也不会进 ps(只在这一次参数里)。
|
||
cred_url() {
|
||
if [ -z "$TOKEN" ]; then printf '%s' "$REPO_URL"; return; fi
|
||
printf '%s' "$REPO_URL" | sed -E "s#^(https?://)#\1${GIT_USER}:${TOKEN}@#"
|
||
}
|
||
|
||
c_reset=$'\033[0m'; c_dim=$'\033[2m'; c_cyan=$'\033[36m'; c_green=$'\033[32m'; c_red=$'\033[31m'; c_bold=$'\033[1m'
|
||
log() { printf '%s▸%s %s\n' "$c_cyan" "$c_reset" "$*"; }
|
||
ok() { printf '%s✓%s %s\n' "$c_green" "$c_reset" "$*"; }
|
||
warn() { printf '%s!%s %s\n' "$c_red" "$c_reset" "$*" >&2; }
|
||
die() { warn "$*"; exit 1; }
|
||
|
||
usage() {
|
||
cat <<'EOF'
|
||
DevBox 一键安装脚本
|
||
|
||
--dir <路径> 安装目录,默认 /opt/devbox
|
||
--port <端口> HTTP 端口,默认 8090
|
||
--nginx <域名> 额外写一份 Nginx 站点配置并 reload(域名可写 _ 表示默认站点)
|
||
--no-service 只拉代码,不装 systemd 服务(容器 / 没 systemd 时用)
|
||
--token <令牌> Gitea 访问令牌(只有私有库才需要;公开库可不填)
|
||
--user <用户名> Gitea 用户名,默认 gitea
|
||
--repo <地址> 换一个仓库地址(默认脚本里写死的那个)
|
||
--update 更新到最新代码并重启服务
|
||
--uninstall 停服务、删 unit(不删代码目录)
|
||
-h, --help 显示这份帮助
|
||
EOF
|
||
}
|
||
|
||
while [ $# -gt 0 ]; do
|
||
case "$1" in
|
||
--dir) APP_DIR="${2:?缺少目录}"; shift 2 ;;
|
||
--port) PORT="${2:?缺少端口}"; shift 2 ;;
|
||
--nginx) NGINX_DOMAIN="${2:-_}"; shift 2 ;;
|
||
--token) TOKEN="${2:?缺少令牌}"; shift 2 ;;
|
||
--user) GIT_USER="${2:?缺少用户名}"; shift 2 ;;
|
||
--repo) REPO_URL="${2:?缺少仓库地址}"; shift 2
|
||
REPO_HOST="$(printf '%s' "$REPO_URL" | sed -E 's#^https?://##; s#/.*$##')" ;;
|
||
--no-service) NO_SERVICE=1; shift ;;
|
||
--update) MODE="update"; shift ;;
|
||
--uninstall) MODE="uninstall"; shift ;;
|
||
-h|--help) usage; exit 0 ;;
|
||
*) warn "未知参数:$1"; usage; exit 1 ;;
|
||
esac
|
||
done
|
||
|
||
case "$PORT" in ''|*[!0-9]*) die "端口要填数字:$PORT" ;; esac
|
||
[ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ] || die "端口范围 1-65535:$PORT"
|
||
|
||
if [ "$(id -u)" -ne 0 ]; then
|
||
die "需要 root:把这条命令前面加上 sudo 再跑"
|
||
fi
|
||
|
||
pm_install() {
|
||
if command -v apt-get >/dev/null 2>&1; then
|
||
DEBIAN_FRONTEND=noninteractive apt-get update -qq >/dev/null 2>&1 || true
|
||
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null
|
||
elif command -v dnf >/dev/null 2>&1; then dnf install -y "$@" >/dev/null
|
||
elif command -v yum >/dev/null 2>&1; then yum install -y "$@" >/dev/null
|
||
elif command -v apk >/dev/null 2>&1; then apk add --no-cache "$@" >/dev/null
|
||
else die "没认出你的发行版,请手动装:$*"
|
||
fi
|
||
}
|
||
|
||
need() {
|
||
command -v "$1" >/dev/null 2>&1 && return 0
|
||
log "安装 $1"
|
||
pm_install "$2"
|
||
command -v "$1" >/dev/null 2>&1 || die "$1 装完还是找不到,请手动安装后重跑"
|
||
}
|
||
|
||
unit_path="/etc/systemd/system/${SERVICE}.service"
|
||
nginx_conf="/etc/nginx/conf.d/${SERVICE}.conf"
|
||
|
||
stop_service() {
|
||
if command -v systemctl >/dev/null 2>&1 && systemctl list-unit-files 2>/dev/null | grep -q "^${SERVICE}.service"; then
|
||
systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
|
||
fi
|
||
}
|
||
|
||
write_unit() {
|
||
cat >"$unit_path" <<EOF
|
||
[Unit]
|
||
Description=DevBox 开发者工具箱
|
||
After=network.target
|
||
|
||
[Service]
|
||
Type=simple
|
||
WorkingDirectory=${APP_DIR}
|
||
ExecStart=/usr/bin/env python3 -m http.server ${PORT} --bind 0.0.0.0 --directory ${APP_DIR}
|
||
Restart=always
|
||
RestartSec=3
|
||
StandardOutput=journal
|
||
StandardError=journal
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
EOF
|
||
systemctl daemon-reload
|
||
systemctl enable --now "$SERVICE" >/dev/null 2>&1 || true
|
||
systemctl restart "$SERVICE"
|
||
}
|
||
|
||
write_nginx() {
|
||
need nginx nginx
|
||
mkdir -p /etc/nginx/conf.d
|
||
cat >"$nginx_conf" <<EOF
|
||
server {
|
||
listen 80;
|
||
server_name ${NGINX_DOMAIN};
|
||
root ${APP_DIR};
|
||
index index.html;
|
||
|
||
location / {
|
||
try_files \$uri \$uri/ /index.html;
|
||
}
|
||
|
||
location ~* \\.(js|css|png|jpg|jpeg|webp|svg|woff2?)\$ {
|
||
expires 7d;
|
||
add_header Cache-Control "public";
|
||
}
|
||
}
|
||
EOF
|
||
if nginx -t >/dev/null 2>&1; then
|
||
systemctl reload nginx >/dev/null 2>&1 || service nginx reload >/dev/null 2>&1 || true
|
||
ok "Nginx 站点已写入 $nginx_conf(server_name ${NGINX_DOMAIN})"
|
||
else
|
||
warn "Nginx 配置检查没过,已保留 $nginx_conf,手动执行 nginx -t 看看"
|
||
fi
|
||
}
|
||
|
||
uninstall() {
|
||
log "卸载 $SERVICE"
|
||
stop_service
|
||
rm -f "$unit_path"
|
||
rm -f "$nginx_conf"
|
||
command -v systemctl >/dev/null 2>&1 && systemctl daemon-reload || true
|
||
command -v nginx >/dev/null 2>&1 && nginx -t >/dev/null 2>&1 && (systemctl reload nginx >/dev/null 2>&1 || true) || true
|
||
ok "服务已移除;代码目录保留在 ${APP_DIR},要彻底删就 rm -rf ${APP_DIR}"
|
||
}
|
||
|
||
pull_code() {
|
||
need git git
|
||
need curl curl
|
||
if [ -d "${APP_DIR}/.git" ]; then
|
||
log "更新 ${APP_DIR}"
|
||
GIT_TERMINAL_PROMPT=0 git -C "$APP_DIR" fetch --depth=1 "$(cred_url)" main
|
||
git -C "$APP_DIR" reset --hard FETCH_HEAD
|
||
else
|
||
log "克隆仓库到 ${APP_DIR}"
|
||
mkdir -p "$(dirname "$APP_DIR")"
|
||
rm -rf "$APP_DIR"
|
||
GIT_TERMINAL_PROMPT=0 git clone --depth=1 "$(cred_url)" "$APP_DIR" \
|
||
|| die "拉代码失败。若这个仓库是私有的,加 --token <令牌>(或先 export GITEA_TOKEN=…);公开库检查网络与地址。"
|
||
git -C "$APP_DIR" remote set-url origin "$REPO_URL"
|
||
fi
|
||
[ -f "${APP_DIR}/index.html" ] || die "拉下来的目录里没有 index.html,仓库地址对吗?"
|
||
}
|
||
|
||
health() {
|
||
command -v curl >/dev/null 2>&1 || return 0
|
||
for _ in $(seq 1 15); do
|
||
if curl -fsS "http://127.0.0.1:${PORT}/" 2>/dev/null | grep -q '<title>DevBox'; then
|
||
ok "服务已就绪"
|
||
return 0
|
||
fi
|
||
sleep 1
|
||
done
|
||
warn "15 秒内没起来,看看日志:journalctl -u ${SERVICE} -n 50 --no-pager"
|
||
return 1
|
||
}
|
||
|
||
summary() {
|
||
local ip
|
||
ip="$(hostname -I 2>/dev/null | awk '{print $1}')"
|
||
echo
|
||
printf '%sDevBox 已就位%s\n' "$c_bold" "$c_reset"
|
||
printf ' 地址 http://%s:%s/\n' "${ip:-服务器IP}" "$PORT"
|
||
printf ' 目录 %s\n' "$APP_DIR"
|
||
printf ' 命令 systemctl status %s / journalctl -u %s -f\n' "$SERVICE" "$SERVICE"
|
||
printf ' 更新 curl -fsSL %s/install.sh | sudo bash -s -- --update\n' "$RAW_URL"
|
||
printf ' 卸载 curl -fsSL %s/install.sh | sudo bash -s -- --uninstall\n' "$RAW_URL"
|
||
echo
|
||
printf '%s如果外面访问不到,记得放行端口:ufw allow %s/tcp(云服务器还要在安全组里放行)%s\n' "$c_dim" "$PORT" "$c_reset"
|
||
[ -n "$NGINX_DOMAIN" ] && printf '%sHTTPS:certbot --nginx -d %s%s\n' "$c_dim" "$NGINX_DOMAIN" "$c_reset"
|
||
}
|
||
|
||
case "$MODE" in
|
||
uninstall) uninstall; exit 0 ;;
|
||
update) pull_code; [ "$NO_SERVICE" = 1 ] || write_unit; health || true; summary; exit 0 ;;
|
||
install) pull_code ;;
|
||
esac
|
||
|
||
if [ "$NO_SERVICE" = 1 ]; then
|
||
ok "代码已就位:${APP_DIR}"
|
||
echo " 手动启动:python3 -m http.server ${PORT} --directory ${APP_DIR} --bind 0.0.0.0"
|
||
exit 0
|
||
fi
|
||
|
||
command -v systemctl >/dev/null 2>&1 || die "这台机器没有 systemd,请加 --no-service 只拉代码"
|
||
|
||
need python3 python3
|
||
log "写入 systemd 服务 /etc/systemd/system/${SERVICE}.service"
|
||
write_unit
|
||
[ -n "$NGINX_DOMAIN" ] && write_nginx
|
||
health || true
|
||
summary
|