Files
update-install-newt/install_newt_v2.sh

212 lines
5.1 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
REPO="fosrl/newt"
INSTALL_DIR="/opt/newt"
SERVICE_NAME="PVE-MGNT"
SYMLINK="${INSTALL_DIR}/newt_latest"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
UPDATER_SERVICE="/etc/systemd/system/newt-updater.service"
UPDATER_TIMER="/etc/systemd/system/newt-updater.timer"
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
get_latest_version() {
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | grep -Po '"tag_name": *"\K[^"]+'
}
download_newt() {
local version="$1"
local arch=$(uname -m)
local file=""
case "$arch" in
x86_64) file="newt_linux_amd64" ;;
aarch64) file="newt_linux_arm64" ;;
armv7l) file="newt_linux_arm32" ;;
*) error "Unsupported architecture: $arch" ;;
esac
local url="https://github.com/${REPO}/releases/download/${version}/${file}"
local target="${INSTALL_DIR}/newt_${version}"
mkdir -p "$INSTALL_DIR"
info "⬇️ Downloading $url"
curl -fsSL "$url" -o "${target}.tmp" || error "Download failed."
chmod +x "${target}.tmp"
mv "${target}.tmp" "$target"
ln -sf "$target" "$SYMLINK"
info "✅ Installed newt ${version} at ${target}"
}
setup_systemd_service() {
echo "🆔 Please enter the Pangolin ID: "
read -r PANGOLIN_ID
echo "🔑 Please enter the Secret: "
read -r PANGOLIN_SECRET
echo "🌐 Please enter the Endpoint (e.g. https://pangolin.domain.com): "
read -r PANGOLIN_ENDPOINT
cat > "$SERVICE_FILE" <<EOF
[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
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"
info "🛠️ Systemd service ${SERVICE_NAME} has been set up and started."
}
setup_update_timer() {
if [[ -f "$UPDATER_SERVICE" && -f "$UPDATER_TIMER" ]]; then
info "⏳ Update timer already exists skipping."
return
fi
info "⚙️ Creating systemd timer for daily updates …"
cat > "$UPDATER_SERVICE" <<EOF
[Unit]
Description=Automatic update for Newt installer
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'curl -fsSL https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt_v2.sh | bash -s -- --update'
EOF
cat > "$UPDATER_TIMER" <<EOF
[Unit]
Description=Run newt-updater.service daily
[Timer]
OnCalendar=03:00
Persistent=true
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now newt-updater.timer
info "✅ Update timer created (runs daily at 03:00)."
}
mode_install() {
local version
version=$(get_latest_version)
version=${version#v}
info "📦 Latest version: $version"
if [ -f "${INSTALL_DIR}/newt_${version}" ]; then
warn "⚠️ Version $version is already installed. Skipping installation."
warn "👉 Use '--reinstall' if you want to force a reinstall."
setup_update_timer
exit 0
fi
download_newt "$version"
setup_systemd_service
setup_update_timer
info "🚀 Installation completed!"
}
mode_update() {
local version
version=$(get_latest_version)
version=${version#v}
info "📦 Latest version: $version"
if [ -f "${INSTALL_DIR}/newt_${version}" ]; then
info "✅ Version $version is already installed. No update required."
setup_update_timer
exit 0
fi
download_newt "$version"
systemctl restart "$SERVICE_NAME"
setup_update_timer
info "🚀 Update completed!"
}
mode_reinstall() {
local version
version=$(get_latest_version)
version=${version#v}
info "📦 Latest version: $version"
download_newt "$version"
setup_systemd_service
setup_update_timer
info "🚀 Reinstallation completed!"
}
mode_uninstall() {
warn "⚠️ Uninstalling newt and removing all systemd entries …"
# Stop and remove service
if systemctl is-active --quiet "$SERVICE_NAME"; then
systemctl stop "$SERVICE_NAME"
fi
systemctl disable "$SERVICE_NAME" 2>/dev/null || true
rm -f "$SERVICE_FILE"
# Remove updater
if systemctl is-active --quiet newt-updater.timer; then
systemctl stop newt-updater.timer
fi
systemctl disable newt-updater.timer 2>/dev/null || true
rm -f "$UPDATER_SERVICE" "$UPDATER_TIMER"
systemctl daemon-reload
# Delete installation dir (ask first)
if [ -d "$INSTALL_DIR" ]; then
read -p "❓ Do you want to remove ${INSTALL_DIR} and all versions? (y/N): " yn
case $yn in
[Yy]*) rm -rf "$INSTALL_DIR"; info "📂 ${INSTALL_DIR} was removed." ;;
*) warn "📂 ${INSTALL_DIR} was NOT removed." ;;
esac
fi
info "🧹 Uninstallation completed!"
}
main() {
case "$1" in
--install|"")
mode_install
;;
--update)
mode_update
;;
--reinstall)
mode_reinstall
;;
--uninstall)
mode_uninstall
;;
*)
error "❌ Unknown parameter: $1 (use --install, --update, --reinstall or --uninstall)"
;;
esac
}
main "$@"