-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·296 lines (271 loc) · 14.8 KB
/
setup.sh
File metadata and controls
executable file
·296 lines (271 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env bash
# shellcheck shell=bash
# ──────────────────────────────────────────────────────────────────────────────
# AstrBot Cross-Platform Installer | supports: Arch / Debian / Ubuntu / RHEL / Fedora / openSUSE
#
# Usage:
# ./setup.sh Install / Update AstrBot
# ./setup.sh deps Step 1: Install dependencies only
# ./setup.sh setups Step 2: Setup users / dirs / permissions
# ./setup.sh files Step 3: Clone app + install service
# ./setup.sh help Show this help
# ──────────────────────────────────────────────────────────────────────────────
set -euo pipefail
# BASH_SOURCE[0] is empty when run via: curl ... | bash
# Use PWD as fallback so the script still works
if [[ -z "${BASH_SOURCE[0]:-}" ]]; then
_SCRIPT_DIR="$(pwd)"
else
_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
fi
REPO_ROOT="${REPO_ROOT:-$_SCRIPT_DIR}"
SKIP_ALLGREETING="${SKIP_ALLGREETING:-false}"
SKIP_ALLDEPS="${SKIP_ALLDEPS:-false}"
SKIP_ALLSETUPS="${SKIP_ALLSETUPS:-false}"
SKIP_ALLFILES="${SKIP_ALLFILES:-false}"
FORCE_REINSTALL="${FORCE_REINSTALL:-false}"
## ─── Env defaults ──────────────────────────────────────────────────────────────
: "${ASTRBOT_USER:=astrbot}"
: "${ASTRBOT_GROUP:=astrbot}"
: "${ASTRBOT_HOME_DIR:=/var/lib/astrbot}"
: "${ASTRBOT_APP_DIR:=/opt/astrbot}"
: "${ASTRBOT_DATA_DIR:=/var/lib/astrbot}"
: "${ASTRBOT_CACHE_DIR:=/var/cache/astrbot}"
: "${ASTRBOT_CONFIG_DIR:=/etc/astrbot}"
: "${ASTRBOT_UPSTREAM:=https://github.com/AstrBotDevs/AstrBot.git}"
: "${ASTRBOT_BRANCH:=dev}"
## ─── Style helpers ─────────────────────────────────────────────────────────────
if command -v tput >/dev/null 2>&1 && [[ -n "$(tput colors 2>/dev/null)" ]]; then
RST="$(tput sgr0)"; BOLD="$(tput bold)"
DIM="$(tput dim)"; RED="$(tput setaf 1)"; GRN="$(tput setaf 2)"
YEL="$(tput setaf 3)";CYN="$(tput setaf 6)"; ULN="$(tput smul)"
else
RST="\033[0m"; BOLD="\033[1m"; DIM="\033[2m"
RED="\033[31m"; GRN="\033[32m"; YEL="\033[33m"; CYN="\033[36m"; ULN="\033[4m"
fi
_log() { printf '%s[%s]%s %s\n' "${DIM}" "$(date '+%H:%M:%S')" "${RST}" "$*"; }
info() { printf '%sℹ%s %s\n' "${CYN}" "${RST}" "$*"; }
ok() { printf '%s✔%s %s\n' "${GRN}" "${RST}" "$*"; }
warn() { printf '%s⚠%s %s\n' "${YEL}" "${RST}" "$*" >&2; }
err() { printf '%s✖%s %s\n' "${RED}" "${RST}" "$*" >&2; exit 1; }
need_cmd() { command -v "$1" >/dev/null 2>&1 || err "Required command not found: $1"; }
## ─── Privilege ────────────────────────────────────────────────────────────────
prevent_root() {
if [[ "${EUID}" -eq 0 ]]; then
err "Do NOT run as root. The installer asks for sudo when needed."
fi
}
require_root() { sudo -n true 2>/dev/null || err "Root required. Run with sudo or as root."; }
sudo_keepalive() {
[[ "${EUID}" -eq 0 ]] && return 0
command -v sudo >/dev/null || err "sudo is required."
sudo -v 2>/dev/null &
sudo_pid=$!
}
sudo_release() { [[ -n "${sudo_pid:-}" ]] && kill "${sudo_pid}" 2>/dev/null || true; }
trap sudo_release EXIT INT TERM
## ─── Distro detection ─────────────────────────────────────────────────────────
detect_distro() {
if [[ -f /etc/os-release ]]; then
# shellcheck disable=SC1091
source /etc/os-release
DIST_ID="${ID:-unknown}"; DIST_ID_LIKE="${ID_LIKE:-}"; DIST_VER="${VERSION:-}"
elif [[ -f /etc/redhat-release ]]; then DIST_ID="rhel"; DIST_ID_LIKE="rhel"
elif [[ -f /etc/debian_version ]]; then DIST_ID="debian"; DIST_ID_LIKE="debian"
else DIST_ID="unknown"; DIST_ID_LIKE=""
fi
case "${DIST_ID_LIKE}" in
*debian*) DIST_FAM="debian" ;;
*rhel*|*fedora*) DIST_FAM="rhel" ;;
*arch*) DIST_FAM="arch" ;;
*suse*) DIST_FAM="suse" ;;
"") DIST_FAM="${DIST_ID}" ;;
*) DIST_FAM="${DIST_ID}" ;;
esac
_log "Detected: ${DIST_ID} (${DIST_FAM}) ${DIST_VER}"
}
## ─── Package installers ────────────────────────────────────────────────────────
_install_deps_arch() {
need_cmd pacman
_log "Installing via pacman..."
local pkgs=(python python-pip uv git certbot)
# rustup provides cargo; do not install rust/cargo from official repos if rustup exists
if ! command -v rustup >/dev/null 2>&1; then
pkgs+=(rust cargo)
fi
sudo pacman -Sy --needed --noconfirm "${pkgs[@]}"
}
_install_deps_debian() {
need_cmd apt-get
_log "Installing via apt..."
local pkgs=(python3 python3-pip python3-venv git certbot curl wget)
sudo apt-get update -qq
sudo apt-get install -y --no-install-recommends "${pkgs[@]}"
if ! command -v rustc >/dev/null 2>&1; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
fi
if ! command -v uv >/dev/null 2>&1; then
curl -LsSf https://astral.sh/uv/install.sh | sh
fi
}
_install_deps_rhel() {
_log "Installing via dnf/yum..."
if command -v dnf >/dev/null 2>&1; then
sudo dnf install -y python3 python3-pip git certbot curl
else
sudo yum install -y python3 python3-pip git certbot curl
fi
if ! command -v rustc >/dev/null 2>&1; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
fi
if ! command -v uv >/dev/null 2>&1; then
curl -LsSf https://astral.sh/uv/install.sh | sh
fi
}
_install_deps_suse() {
need_cmd zypper
_log "Installing via zypper..."
sudo zypper install -y python3 python3-pip git certbot curl rust
}
install_deps() {
detect_distro
case "${DIST_FAM}" in
arch) _install_deps_arch ;;
debian) _install_deps_debian ;;
rhel) _install_deps_rhel ;;
suse) _install_deps_suse ;;
*) warn "Unknown distro. Trying Debian approach..."; _install_deps_debian ;;
esac
need_cmd git; need_cmd python3
ok "Dependencies installed."
}
## ─── Setup: users, dirs, permissions ─────────────────────────────────────────
setup_perms() {
require_root
_log "Creating user '${ASTRBOT_USER}'..."
if ! getent passwd "${ASTRBOT_USER}" >/dev/null 2>&1; then
sudo useradd -r -M -d "${ASTRBOT_HOME_DIR}" -s /usr/bin/nologin \
-c "AstrBot Service User" "${ASTRBOT_USER}"
ok "User created."
else
info "User already exists."
fi
_log "Creating directories..."
sudo install -dm755 -o "${ASTRBOT_USER}" -g "${ASTRBOT_GROUP}" "${ASTRBOT_DATA_DIR}"
sudo install -dm755 -o "${ASTRBOT_USER}" -g "${ASTRBOT_GROUP}" "${ASTRBOT_CACHE_DIR}"
sudo install -dm755 -o "${ASTRBOT_USER}" -g "${ASTRBOT_GROUP}" "${ASTRBOT_CACHE_DIR}/python"
sudo install -dm755 -o "${ASTRBOT_USER}" -g "${ASTRBOT_GROUP}" "${ASTRBOT_CACHE_DIR}/cargo"
sudo install -dm755 -o "${ASTRBOT_USER}" -g "${ASTRBOT_GROUP}" "${ASTRBOT_CACHE_DIR}/rustup"
sudo install -dm755 -o "${ASTRBOT_USER}" -g "${ASTRBOT_GROUP}" "${ASTRBOT_CACHE_DIR}/cargo_target"
sudo install -dm755 -o root -g root "${ASTRBOT_CONFIG_DIR}"
sudo install -dm755 -o root -g root "${ASTRBOT_APP_DIR}"
ok "Directories created."
_log "Setting ownership..."
sudo chown -R "${ASTRBOT_USER}:${ASTRBOT_GROUP}" "${ASTRBOT_DATA_DIR}" "${ASTRBOT_CACHE_DIR}" 2>/dev/null || true
git config --global --add safe.directory "${ASTRBOT_APP_DIR}" 2>/dev/null || true
ok "Permissions set."
}
## ─── Files: clone app + install service ───────────────────────────────────────
install_files() {
require_root
_log "Cloning AstrBot (${ASTRBOT_BRANCH}) → ${ASTRBOT_APP_DIR}..."
if mount | grep -q " ${ASTRBOT_APP_DIR} "; then
info "Overlay mount detected — skipping clone (update in progress)."
elif [[ -d "${ASTRBOT_APP_DIR}" ]]; then
if [[ "${FORCE_REINSTALL}" == true ]]; then
warn "Force reinstall — removing existing ${ASTRBOT_APP_DIR}"
sudo rm -rf "${ASTRBOT_APP_DIR}"
else
info "${ASTRBOT_APP_DIR} already exists. Skipping clone."
info "Use FORCE_REINSTALL=true to override."
fi
fi
if [[ ! -d "${ASTRBOT_APP_DIR}" ]]; then
_log "Downloading AstrBot (${ASTRBOT_BRANCH}) from GitHub..."
local tarball="/tmp/astrbot-install-$$.tar.gz"
local extract_dir="/tmp/astrbot-install-$$"
local url="https://github.com/AstrBotDevs/AstrBot/archive/refs/heads/${ASTRBOT_BRANCH}.tar.gz"
mkdir -p "$extract_dir"
if ! curl -L --fail --silent --show-error -o "$tarball" "$url" 2>&1; then
rm -f "$tarball"; rm -rf "$extract_dir"
err "Failed to download AstrBot from GitHub."
fi
tar -xzf "$tarball" -C "$extract_dir" || \
{ rm -f "$tarball"; rm -rf "$extract_dir"; err "Failed to extract AstrBot tarball."; }
local extracted
extracted=$(find "$extract_dir" -mindepth 1 -maxdepth 1 -type d | head -1)
mv "$extracted" "${ASTRBOT_APP_DIR}"
rm -f "$tarball"; rm -rf "$extract_dir"
# Init git repo so git describe works inside /opt/astrbot
git -C "${ASTRBOT_APP_DIR}" init --quiet
git -C "${ASTRBOT_APP_DIR}" remote add origin "${ASTRBOT_UPSTREAM}"
git -C "${ASTRBOT_APP_DIR}" fetch --depth=1 origin "${ASTRBOT_BRANCH}" --quiet
git -C "${ASTRBOT_APP_DIR}" checkout "${ASTRBOT_BRANCH}" --quiet
# Write version file for AUR pkgver
local _ver
_ver=$(git -C "${ASTRBOT_APP_DIR}" describe --long --tags 2>/dev/null | sed 's/\([^-]*-g\)/r\1/;s/-/./g' | sed 's/^v//g')
[ -z "$_ver" ] && _ver="4.22.2"
echo "$_ver" > "${ASTRBOT_APP_DIR}/.version"
ok "Installed: ${ASTRBOT_APP_DIR} (${_ver})"
fi
_log "Installing systemd service..."
if [[ -f "${REPO_ROOT}/astrbot@.service" ]]; then
sudo install -Dm644 "${REPO_ROOT}/astrbot@.service" \
"/etc/systemd/system/astrbot@.service"
sudo systemctl daemon-reload
ok "systemd service installed."
fi
}
## ─── Subcommands ────────────────────────────────────────────────────────────────
show_help() {
printf '%s\n' ""
printf '%s╔═══════════════════════════════════════════════════════╗%s\n' "${CYN}" "${RST}"
printf '%s║%s ✦ AstrBot Cross-Platform Installer ✦ %s║%s\n' "${CYN}" "${RST}" "${BOLD}${CYN}" "${RST}"
printf '%s╠═══════════════════════════════════════════════════════╣%s\n' "${CYN}" "${RST}"
printf '%s║%s Supports: Arch / Debian / Ubuntu / RHEL / Fedora… %s║%s\n' "${CYN}" "${RST}" "${DIM}" "${RST}"
printf '%s╚═══════════════════════════════════════════════════════╝%s\n' "${CYN}" "${RST}"
printf '%s\n' ""
printf ' %s./setup.sh%s Install / Update AstrBot\n' "${GRN}" "${RST}"
printf ' %s./setup.sh deps%s Step 1 — Install system dependencies\n' "${GRN}" "${RST}"
printf ' %s./setup.sh setups%s Step 2 — Setup users, dirs, permissions\n' "${GRN}" "${RST}"
printf ' %s./setup.sh files%s Step 3 — Clone app + install service\n' "${GRN}" "${RST}"
printf ' %s./setup.sh help%s Show this help\n' "${GRN}" "${RST}"
printf '%s\n' ""
printf ' %sNOTE:%s Run %swithout sudo%s — the installer asks when needed.\n' "${YEL}" "${RST}" "${BOLD}" "${RST}"
printf '%s\n' ""
}
show_banner() {
printf '\n%s╔═══════════════════════════════════════════════════════════╗%s\n' "${CYN}" "${RST}"
printf '%s║%s ✦ AstrBot Cross-Platform Installer ✦ %s║%s\n' "${CYN}" "${RST}" "${BOLD}${CYN}" "${RST}"
printf '%s╠═══════════════════════════════════════════════════════════╣%s\n' "${CYN}" "${RST}"
printf '%s║%s Multi-instance AI chatbot · Discord · Telegram · … %s║%s\n' "${CYN}" "${RST}" "${DIM}" "${RST}"
printf '%s╠═══════════════════════════════════════════════════════════╣%s\n' "${CYN}" "${RST}"
printf '%s║%s GitHub: %shttps://github.com/AstrBotDevs/AstrBot%s %s║%s\n' \
"${CYN}" "${RST}" "${ULN}${CYN}" "${RST}" "${CYN}" "${RST}"
printf '%s╚═══════════════════════════════════════════════════════════╝%s\n' "${CYN}" "${RST}"
printf '\n'
}
pause() { [[ "${CI:-false}" == true ]] && return 0; printf '\n%sPress %sENTER%s to continue...%s\n' "${DIM}" "${BOLD}" "${RST}" "${DIM}"; read -r _ </dev/tty; }
## ─── Main dispatch ─────────────────────────────────────────────────────────────
prevent_root
case "${1:-install}" in
""|install)
sudo_keepalive
[[ "${SKIP_ALLGREETING}" != true ]] && show_banner
[[ "${SKIP_ALLDEPS}" != true ]] && { install_deps; pause; }
[[ "${SKIP_ALLSETUPS}" != true ]] && { setup_perms; pause; }
[[ "${SKIP_ALLFILES}" != true ]] && install_files
printf '\n'
ok "AstrBot is ready!"
printf '\n %sCreate an instance:%s %sastrbotctl init <name>%s\n' "${BOLD}" "${RST}" "${GRN}" "${RST}"
printf ' %sStart service:%s %ssudo systemctl start astrbot@<name>%s\n' "${BOLD}" "${RST}" "${GRN}" "${RST}"
printf ' %sEnable on boot:%s %ssudo systemctl enable --now astrbot@<name>%s\n' "${BOLD}" "${RST}" "${GRN}" "${RST}"
printf ' %sDocs:%s %shttps://docs.astrbot.app%s\n' "${BOLD}" "${RST}" "${CYN}" "${RST}"
printf '\n'
;;
deps) install_deps ;;
setups) sudo_keepalive; setup_perms ;;
files) sudo_keepalive; install_files ;;
help|--help|-h) show_help ;;
*) err "Unknown subcommand: $1 (try: ./setup.sh help)" ;;
esac