Files
update-install-newt/install_newt.sh

128 lines
3.2 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
REPO="fosrl/newt"
INSTALL_DIR="/opt/newt"
SERVICE_NAME="PVE-MGNT"
SYMLINK="${INSTALL_DIR}/newt_latest"
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
FORCE=0
for arg in "$@"; do
if [[ "$arg" == "--force" ]]; then
FORCE=1
fi
done
get_latest_version() {
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | grep -Po '"tag_name": *"\K[^"]+'
}
download_newt() {
version="$1"
arch=$(uname -m)
case "$arch" in
x86_64) file="newt_linux_amd64" ;;
aarch64) file="newt_linux_arm64" ;;
armv7l) file="newt_linux_arm32" ;;
*) error "Nicht unterstützte Architektur: $arch" && exit 1 ;;
esac
url="https://github.com/${REPO}/releases/download/${version}/${file}"
target="${INSTALL_DIR}/newt_${version}"
mkdir -p "$INSTALL_DIR"
info "⬇️ Lade $url herunter …"
curl -fsSL "$url" -o "$target" || { error "Download fehlgeschlagen."; exit 1; }
chmod +x "$target"
ln -sf "$target" "$SYMLINK"
info "✅ newt ${version} installiert als ${target}"
}
cleanup_old_versions() {
cd "$INSTALL_DIR"
keep=$(readlink -f newt_latest)
versions=($(ls -1 newt_* | grep -v latest | sort -Vr))
count=0
for ver in "${versions[@]}"; do
if [[ "$(readlink -f "$ver")" != "$keep" ]]; then
((count++))
if [ "$count" -gt 1 ]; then
info "🧹 Entferne alte Version: $ver"
rm -f "$ver"
fi
fi
done
}
setup_systemd_service() {
svc="/etc/systemd/system/${SERVICE_NAME}.service"
if [ -f "$svc" ]; then
info "systemd-Dienst ${SERVICE_NAME} existiert bereits. Überspringe Neuanlage."
sudo systemctl restart "$SERVICE_NAME"
info "Dienst ${SERVICE_NAME} neu gestartet."
return
fi
read -rp "🆔 Bitte gib die Pangolin-ID ein: " PANGOLIN_ID
read -rp "🔑 Bitte gib das Secret ein: " PANGOLIN_SECRET
read -rp "🌐 Bitte gib den Endpoint (z.B. https://pangolin.domain.de) ein: " PANGOLIN_ENDPOINT
cat <<EOF | sudo tee "$svc" > /dev/null
[Unit]
Description=Newt Client - ${SERVICE_NAME}
After=network.target
[Service]
ExecStart=${SYMLINK} --id ${PANGOLIN_ID} --secret ${PANGOLIN_SECRET} --endpoint ${PANGOLIN_ENDPOINT}
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable "$SERVICE_NAME"
sudo systemctl start "$SERVICE_NAME"
info "🛠️ systemd-Dienst ${SERVICE_NAME} eingerichtet und gestartet."
}
main() {
info "🔧 Starte Installation/Update von newt ..."
version=$(get_latest_version)
version=${version#v} # führendes "v" entfernen, falls vorhanden
info "📦 Neueste Version: ${version}"
if [ -f "${INSTALL_DIR}/newt_${version}" ] && [ $FORCE -eq 0 ]; then
info "✅ Version ${version} ist bereits installiert. Kein Update nötig."
else
if [ $FORCE -eq 1 ]; then
info "--force erkannt: Erzwinge Neuinstallation."
sudo systemctl stop "$SERVICE_NAME" || true
fi
download_newt "$version"
fi
cleanup_old_versions
setup_systemd_service
info "🚀 Fertig! newt läuft als systemd-Dienst (${SERVICE_NAME})"
}
main "$@"