install_newt-msp-site-win_v2.sh aktualisiert

This commit is contained in:
2026-02-18 10:44:24 +01:00
parent 02bae9ae1a
commit 089ae4c26f

View File

@@ -1,70 +1,143 @@
# TLS 1.2 erzwingen (Wichtig für GitHub/Downloads) <#
.SYNOPSIS
Windows-Pendant zum Newt-Installer (Bash).
Unterstützt: --install, --update, --reinstall, --uninstall
#>
# 1. TLS 1.2 erzwingen (Wichtig für GitHub Downloads auf Windows Server)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# 2. Variablen definieren
$Repo = "fosrl/newt" $Repo = "fosrl/newt"
$InstallDir = "C:\Program Files\me-msp-newt" $InstallDir = "C:\Program Files\me-msp-newt"
$ServiceName = "MAIEREDV-Managed-Site-Client" $ServiceName = "MAIEREDV-Managed-Site-Client"
$Symlink = "$InstallDir\newt_latest.exe" $Symlink = "$InstallDir\newt_latest.exe"
$TaskName = "Newt-Updater"
# Helfer für bunte Ausgaben
function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Green }
function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow }
function Write-ErrorMsg($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red; exit 1 }
# 3. Neueste Version von GitHub holen (API)
function Get-LatestVersion { function Get-LatestVersion {
try { try {
$url = "https://api.github.com/repos/$Repo/releases/latest" $url = "https://api.github.com/repos/$Repo/releases/latest"
$json = Invoke-RestMethod -Uri $url -ErrorAction Stop $json = Invoke-RestMethod -Uri $url -UseBasicParsing
return $json.tag_name.TrimStart('v') return $json.tag_name.TrimStart('v')
} catch { } catch {
Write-Error "Fehler beim Abrufen der Version: $_" Write-ErrorMsg "Konnte Version nicht von GitHub abrufen."
exit 1
} }
} }
# 4. Download via BITS (Server-sicher)
function Download-Newt { function Download-Newt {
param($Version) param($Version)
$Arch = if ([Environment]::Is64BitOperatingSystem) { "newt_windows_amd64.exe" } else { "newt_windows_386.exe" } $Arch = if ([Environment]::Is64BitOperatingSystem) { "newt_windows_amd64.exe" } else { "newt_windows_386.exe" }
$Url = "https://github.com/$Repo/releases/download/v$Version/$Arch" $Url = "https://github.com/$Repo/releases/download/v$Version/$Arch"
$Target = "$InstallDir\newt_$Version.exe" $Target = "$InstallDir\newt_$Version.exe"
if (!(Test-Path $InstallDir)) { if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
Write-Host "⬇️ Downloading $Url ..." -ForegroundColor Cyan Write-Info "⬇️ Downloading $Url via BITS..."
try { try {
Invoke-WebRequest -Uri $Url -OutFile $Target -ErrorAction Stop Start-BitsTransfer -Source $Url -Destination $Target -ErrorAction Stop
if (Test-Path $Target) {
Copy-Item -Path $Target -Destination $Symlink -Force Copy-Item -Path $Target -Destination $Symlink -Force
Write-Host "✅ Installiert: newt $Version" -ForegroundColor Green Write-Info "✅ Installiert: newt $Version unter $Target"
}
} catch { } catch {
Write-Error "Download fehlgeschlagen: $_" Write-ErrorMsg "Download fehlgeschlagen: $_"
exit 1
} }
} }
# 5. Dienst erstellen (Nativ mit PowerShell-Wrapper)
function Setup-Service { function Setup-Service {
# Abfrage nur, wenn Dienst noch nicht existiert $PangolinID = Read-Host "🆔 Bitte Pangolin ID eingeben"
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) { $PangolinSecret = Read-Host "🔑 Bitte Secret eingeben"
$PangolinID = Read-Host "🆔 Pangolin ID" $PangolinEndpoint = Read-Host "🌐 Bitte Endpoint eingeben (z.B. https://pangolin.domain.com)"
$PangolinSecret = Read-Host "🔑 Secret"
$PangolinEndpoint = Read-Host "🌐 Endpoint (z.B. https://...)"
$ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint" $ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint"
# Nativer Windows Dienst (PowerShell Wrapper um Timeouts zu vermeiden) # Da newt.exe kein nativer Windows-Service ist, nutzen wir einen PowerShell-Wrapper
# Dieser verhindert den "Dienst antwortete nicht rechtzeitig" Fehler.
$BinaryPath = "powershell.exe -WindowStyle Hidden -Command `"$Symlink $ArgList`""
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Write-Warn "Dienst existiert bereits. Aktualisiere Konfiguration..."
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
# Set-Service kann den BinaryPath nicht direkt ändern, daher sc.exe (nativ)
& sc.exe config $ServiceName binPath= $BinaryPath
} else {
New-Service -Name $ServiceName ` New-Service -Name $ServiceName `
-BinaryPathName "powershell.exe -WindowStyle Hidden -Command & '$Symlink' $ArgList" ` -BinaryPathName $BinaryPath `
-DisplayName "MAIEREDV Managed Site Client" ` -DisplayName "MAIEREDV Managed Site Client" `
-Description "Managed Newt Client by MAIEREDV" `
-StartupType Automatic -StartupType Automatic
}
Start-Service $ServiceName Start-Service $ServiceName
Write-Host "🛠️ Dienst $ServiceName wurde erstellt und gestartet." -ForegroundColor Green Write-Info "🛠️ Dienst $ServiceName gestartet."
} else {
Restart-Service $ServiceName
Write-Host "🔄 Dienst neu gestartet." -ForegroundColor Yellow
}
} }
# Haupt-Logik (Beispiel für --install) # 6. Täglicher Update-Task (Pendant zum systemd timer)
$Latest = Get-LatestVersion function Setup-UpdateTask {
Download-Newt $Latest if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) {
Setup-Service Write-Info "⏳ Update-Task existiert bereits."
return
}
$ScriptPath = $MyInvocation.MyCommand.Path
# Falls als One-Liner ausgeführt, hier festen Pfad oder URL eintragen:
$ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('DEINE_GITEA_URL_ZUM_SCRIPT')) -mode update`""
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $ActionCommand
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $TaskName -User "SYSTEM" -Force | Out-Null
Write-Info "✅ Täglicher Update-Task (03:00 Uhr) erstellt."
}
# 7. Modi
function Mode-Install {
$v = Get-LatestVersion
if (Test-Path "$InstallDir\newt_$v.exe") {
Write-Warn "⚠️ Version $v ist bereits installiert. Nutze --reinstall."
Setup-UpdateTask
return
}
Download-Newt $v
Setup-Service
Setup-UpdateTask
Write-Info "🚀 Installation abgeschlossen!"
}
function Mode-Update {
$v = Get-LatestVersion
if (Test-Path "$InstallDir\newt_$v.exe") {
Write-Info "✅ Version $v ist aktuell. Nichts zu tun."
return
}
Download-Newt $v
Restart-Service $ServiceName
Write-Info "🚀 Update auf $v abgeschlossen!"
}
function Mode-Uninstall {
Write-Warn "⚠️ Deinstalliere Newt..."
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
Stop-Service $ServiceName -Force
$service = Get-CimInstance Win32_Service -Filter "Name='$ServiceName'"
$service | Remove-CimInstance
}
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
Write-Info "🧹 Dienst und Task entfernt. Ordner $InstallDir bitte manuell löschen falls gewünscht."
}
# 8. Main Logic
param([string]$mode = "install")
switch ($mode) {
"install" { Mode-Install }
"update" { Mode-Update }
"reinstall" { Download-Newt (Get-LatestVersion); Setup-Service; Setup-UpdateTask }
"uninstall" { Mode-Uninstall }
default { Write-ErrorMsg "Unbekannter Parameter: $mode (Nutze install, update, reinstall, uninstall)" }
}