diff --git a/install_newt-msp-site_v2.sh b/install_newt-msp-site_v2.sh index a9c65d5..6408441 100644 --- a/install_newt-msp-site_v2.sh +++ b/install_newt-msp-site_v2.sh @@ -1,176 +1,211 @@ #!/bin/bash +set -e -param([string]$mode = "install") +REPO="fosrl/newt" +INSTALL_DIR="/opt/me-msp-newt" +SERVICE_NAME="MAIEREDV-Managed-Site-Client" +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" -# 1. Stabilität & Encoding -[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' -# 2. Konfiguration -$Repo = "fosrl/newt" -$InstallDir = "C:\Program Files\me-msp-newt" -$ServiceName = "MAIEREDV-Managed-Site-Client" -$Symlink = "$InstallDir\newt_latest.exe" -$UpdaterTaskName = "MAIEREDV-Newt-Updater" -$GiteaUrl = "https://me-gitea.maieredv.cloud/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site-win_v2.1.ps1" +info() { echo -e "${GREEN}[INFO]${NC} $1"; } +warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } +error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } -function Write-Log($msg, $color = "White") { - Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color +get_latest_version() { + curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | grep -Po '"tag_name": *"\K[^"]+' } -# 3. Umgebung vorbereiten -function Prepare-Environment { - if (!(Get-Command winget -ErrorAction SilentlyContinue)) { - Write-Log "Winget fehlt. Installiere via winget.pro..." "Cyan" - try { - Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") - } catch { - Write-Log "FEHLER: Winget Installation fehlgeschlagen." "Red"; exit 1 - } - } +download_newt() { + local version="$1" + local arch=$(uname -m) + local file="" - if (!(Get-Command nssm -ErrorAction SilentlyContinue)) { - Write-Log "NSSM wird via Winget bereitgestellt..." "Cyan" - winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") - } + 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}" } -function Get-LatestVersion { - try { - $url = "https://api.github.com/repos/${Repo}/releases/latest" - $json = Invoke-RestMethod -Uri $url -UseBasicParsing - return $json.tag_name - } catch { - Write-Log "FEHLER: GitHub API nicht erreichbar." "Red"; exit 1 - } +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" < "$UPDATER_SERVICE" < "$UPDATER_TIMER" </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 "$@"