102 lines
8.1 KiB
Bash
Executable File
102 lines
8.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
if [[ ${1:-} == --help ]]; then
|
|
echo '被控向导:bash deploy/install.sh --interactive --role node;自动化:bash deploy/install-node.sh 配对文件 凭据文件'
|
|
exit 0
|
|
fi
|
|
if [[ $# -eq 0 && -t 0 && -t 1 ]]; then exec python3 "$(dirname "${BASH_SOURCE[0]}")/install-wizard.py" --role node; fi
|
|
[[ ${EUID} -eq 0 ]] || { echo 'Run as root.' >&2; exit 1; }
|
|
[[ $# -eq 2 ]] || { echo 'Usage: install-node.sh /root/sea-node-ID.json /root/local-panel-credentials.json' >&2; exit 1; }
|
|
source_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
for binary in sea-vhost-node sea-vhost-agent sea-vhost-panel-broker sea-vhost-scheduler sea-vhost-run; do [[ -f "$source_dir/build/bin/$binary" && ! -L "$source_dir/build/bin/$binary" ]] || { echo "Missing $binary" >&2; exit 1; }; done
|
|
[[ ! -e /var/lib/sea-vhost/config.json ]] || { echo 'Controller configuration exists. Do not convert a combined installation in place.' >&2; exit 1; }
|
|
[[ ! -e /var/lib/sea-vhost-panel/resources.json ]] || { echo 'A local platform registry exists; reconcile it before installing a stateless node.' >&2; exit 1; }
|
|
[[ -d /www/server/panel && -x /www/server/nginx/sbin/nginx ]] || { echo 'Debian aaPanel/Nginx is required.' >&2; exit 1; }
|
|
[[ -x /www/server/pure-ftpd/bin/pure-pw && -d /www/server/pure-ftpd/etc ]] || { echo 'Install the free Pure-FTPd service in aaPanel first.' >&2; exit 1; }
|
|
command -v python3 >/dev/null
|
|
# Validation runs before creating accounts, changing services or copying keys.
|
|
tenant_root="$(python3 - "$1" "$2" <<'PY'
|
|
import json,os,re,stat,sys,urllib.parse
|
|
for file in sys.argv[1:]:
|
|
s=os.lstat(file)
|
|
if not stat.S_ISREG(s.st_mode) or s.st_uid!=0 or s.st_mode&0o077: raise SystemExit('Configuration files must be root-owned regular files with mode 0600.')
|
|
c=json.load(open(sys.argv[1]));p=json.load(open(sys.argv[2]));root=c['tenantRoot']
|
|
if os.path.exists('/etc/sea-vhost-node/config.json'):
|
|
old=json.load(open('/etc/sea-vhost-node/config.json'))
|
|
if old['identity']['nodeId']!=c['identity']['nodeId'] or old['tenantRoot']!=root: raise SystemExit('Existing node identity/root differs; cross-node migration is not automatic.')
|
|
if not re.fullmatch(r'/www/wwwroot/[A-Za-z0-9_/-]+',root) or os.path.normpath(root)!=root or '..' in root.split('/'): raise SystemExit('Invalid tenant root.')
|
|
if c['identity']['nodeId']<2 or c['agentSocket']!='/run/sea-vhost/agent.sock' or c['brokerSocket']!='/run/sea-vhost-panel/broker.sock': raise SystemExit('Invalid node pairing configuration.')
|
|
u=urllib.parse.urlparse(p['url'])
|
|
if u.scheme not in ('http','https') or u.hostname not in ('127.0.0.1','localhost','::1') or u.username or u.password or len(p.get('api_key',''))<16 or p.get('version')!=1: raise SystemExit('Panel credentials must use the loopback interface.')
|
|
current='/'
|
|
for part in root.strip('/').split('/'):
|
|
current=os.path.join(current,part)
|
|
if os.path.lexists(current) and (os.path.islink(current) or not os.path.isdir(current)): raise SystemExit('Tenant root contains an unsafe component.')
|
|
print(root)
|
|
PY
|
|
)"
|
|
packages=(acl build-essential ca-certificates xz-utils libssl-dev zlib1g-dev libncurses-dev libreadline-dev libsqlite3-dev libgdbm-dev libdb-dev libbz2-dev libexpat1-dev liblzma-dev tk-dev libffi-dev uuid-dev)
|
|
missing_packages=()
|
|
for package in "${packages[@]}"; do
|
|
dpkg-query -W -f='${Status}' "$package" 2>/dev/null | grep -q 'ok installed' || missing_packages+=("$package")
|
|
done
|
|
if (( ${#missing_packages[@]} > 0 )); then
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
apt-get update
|
|
apt-get install -y --no-install-recommends "${missing_packages[@]}"
|
|
fi
|
|
bash "$source_dir/deploy/prepare-runtime-roots.sh"
|
|
for group in sea-vhost-control sea-vhost-agent-control sea-vhost-panel-control; do getent group "$group" >/dev/null || groupadd --system "$group"; done
|
|
id sea-vhost >/dev/null 2>&1 || useradd --system --gid sea-vhost-control --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin sea-vhost
|
|
usermod -g sea-vhost-control -aG sea-vhost-agent-control,sea-vhost-panel-control sea-vhost
|
|
id sea-vhost-panel >/dev/null 2>&1 || useradd --system --gid sea-vhost-panel-control --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin sea-vhost-panel
|
|
for group in sea-vhost-control sea-vhost-agent-control sea-vhost-panel-control; do gpasswd -d www "$group" >/dev/null 2>&1 || true; done
|
|
gpasswd -d sea-vhost-panel sea-vhost-agent-control >/dev/null 2>&1 || true
|
|
install -d -o root -g root -m 0755 /opt/sea-vhost /opt/sea-vhost/bin
|
|
install -d -o root -g root -m 0755 /usr/local/libexec
|
|
install -o root -g root -m 0755 "$source_dir/build/bin/sea-vhost-run" /usr/local/libexec/sea-vhost-run
|
|
install -d -o root -g sea-vhost-control -m 0750 /etc/sea-vhost /etc/sea-vhost-node
|
|
install -d -o sea-vhost-panel -g sea-vhost-panel-control -m 0700 /var/lib/sea-vhost-panel
|
|
install -d -o root -g root -m 0750 /var/lib/sea-vhost
|
|
install -d -o root -g root -m 0755 /www/backup/sea-vhost /www/wwwlogs /www/server/panel/vhost/nginx/extension
|
|
# The per-site protection snippets reference these zones on fresh nodes too.
|
|
security_global=/www/server/panel/vhost/nginx/sea-vhost-security-global.conf
|
|
security_backup=''
|
|
if [[ -e "$security_global" || -L "$security_global" ]]; then
|
|
[[ -f "$security_global" && ! -L "$security_global" ]] || { echo 'Unsafe Nginx security baseline.' >&2; exit 1; }
|
|
security_backup="$(mktemp)"
|
|
cp --preserve=mode,ownership,timestamps -- "$security_global" "$security_backup"
|
|
fi
|
|
install -o root -g root -m 0644 "$source_dir/deploy/nginx/sea-vhost-security-global.conf" "$security_global"
|
|
if ! /www/server/nginx/sbin/nginx -t; then
|
|
if [[ -n "$security_backup" ]]; then cp --preserve=mode,ownership,timestamps -- "$security_backup" "$security_global"; else rm -f -- "$security_global"; fi
|
|
[[ -z "$security_backup" ]] || rm -f -- "$security_backup"
|
|
echo 'Nginx rejected the security baseline; the previous file was restored.' >&2
|
|
exit 1
|
|
fi
|
|
[[ -z "$security_backup" ]] || rm -f -- "$security_backup"
|
|
/www/server/nginx/sbin/nginx -s reload
|
|
install -d -o root -g root -m 0755 "$tenant_root"
|
|
for default_dir in /www/wwwroot/seavHost /www/wwwroot/seavHost/vHost; do
|
|
[[ ! -L "$default_dir" && ( ! -e "$default_dir" || -d "$default_dir" ) ]] || { echo 'Unsafe default managed root.' >&2; exit 1; }
|
|
[[ -d "$default_dir" ]] || install -d -o root -g root -m 0755 "$default_dir"
|
|
done
|
|
install -o root -g sea-vhost-control -m 0640 "$1" /etc/sea-vhost-node/config.json
|
|
install -o sea-vhost-panel -g sea-vhost-panel-control -m 0600 "$2" /var/lib/sea-vhost-panel/credentials.json
|
|
if [[ -e /var/lib/sea-vhost-panel/resources.json ]]; then echo 'A node-side platform registry exists; reconcile it before enabling stateless mode.' >&2; exit 1; fi
|
|
if [[ ! -e /etc/sea-vhost/sea-vhost.env ]]; then
|
|
umask 077
|
|
printf 'SEA_INTERNAL_AUTH_TOKEN=%s\nSEA_NODE_STATELESS=true\nSEA_TENANT_ROOT=%s\nSEA_AGENT_SOCKET=/run/sea-vhost/agent.sock\nSEA_PANEL_BROKER_SOCKET=/run/sea-vhost-panel/broker.sock\n' "$(openssl rand -hex 32)" "$tenant_root" > /etc/sea-vhost/sea-vhost.env
|
|
fi
|
|
grep -qx 'SEA_NODE_STATELESS=true' /etc/sea-vhost/sea-vhost.env || { echo 'Existing environment is not a stateless node.' >&2; exit 1; }
|
|
grep -qx "SEA_TENANT_ROOT=$tenant_root" /etc/sea-vhost/sea-vhost.env || { echo 'Existing node root differs from paired configuration.' >&2; exit 1; }
|
|
for binary in sea-vhost-node sea-vhost-agent sea-vhost-panel-broker sea-vhost-scheduler; do install -o root -g root -m 0755 "$source_dir/build/bin/$binary" "/opt/sea-vhost/bin/$binary"; done
|
|
for unit in sea-vhost-agent sea-vhost-panel-broker sea-vhost-node; do install -m 0644 "$source_dir/deploy/systemd/$unit.service" "/etc/systemd/system/$unit.service"; done
|
|
install -d -m 0755 /etc/systemd/system/sea-vhost-node.service.d /etc/systemd/system/sea-vhost-agent.service.d
|
|
printf '[Service]\nReadWritePaths=%s\n' "$tenant_root" > /etc/systemd/system/sea-vhost-node.service.d/paths.conf
|
|
printf '[Service]\nReadWritePaths=%s\n' "$tenant_root" > /etc/systemd/system/sea-vhost-agent.service.d/node-paths.conf
|
|
systemctl daemon-reload
|
|
systemctl enable sea-vhost-agent sea-vhost-panel-broker sea-vhost-node
|
|
systemctl restart sea-vhost-agent sea-vhost-panel-broker sea-vhost-node
|
|
echo 'Node installed. No controller database or scheduler was installed. Enable this node from the controller to probe and connect.'
|