143 lines
5.7 KiB
Bash
143 lines
5.7 KiB
Bash
<#
|
|
.SYNOPSIS
|
|
Windows-Installer für den Newt-Client mit NSSM.
|
|
Nutzt einen stabilen Download-Mirror für NSSM.
|
|
#>
|
|
param([string]$mode = "install")
|
|
|
|
# 1. Stabilität & Encoding
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
|
|
# 2. Konfiguration
|
|
$Repo = "fosrl/newt"
|
|
$InstallDir = "C:\Program Files\me-msp-newt"
|
|
$ServiceName = "MAIEREDV-Managed-Site-Client"
|
|
$Symlink = "$InstallDir\newt_latest.exe"
|
|
$NssmPath = "$InstallDir\nssm.exe"
|
|
$UpdaterTaskName = "MAIEREDV-Newt-Updater"
|
|
$GiteaUrl = "https://gitea.vmd55888.de/manuel.maier/update-install-newt/raw/branch/main/install_newt-msp-site_v2.ps1"
|
|
|
|
function Write-Log($msg, $color = "White") { Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color }
|
|
|
|
# 3. NSSM bereitstellen (Mirror-Download)
|
|
function Get-NSSM {
|
|
if (Test-Path $NssmPath) { return }
|
|
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
|
|
|
|
Write-Log "NSSM wird über Mirror heruntergeladen..." "Cyan"
|
|
$nssmZip = "$env:TEMP\nssm.zip"
|
|
|
|
# Mirror von einem stabilen Repository (GitHub Mirror von NSSM)
|
|
$nssmDownloadUrl = "https://github.com/kirill-grouchnikov/nssm/raw/master/release/nssm-2.24.zip"
|
|
|
|
try {
|
|
# BITS ist auf Servern zuverlässiger als Invoke-WebRequest
|
|
Start-BitsTransfer -Source $nssmDownloadUrl -Destination $nssmZip -ErrorAction Stop
|
|
Expand-Archive -Path $nssmZip -DestinationPath "$env:TEMP\nssm_temp" -Force
|
|
|
|
$archDir = if ([Environment]::Is64BitOperatingSystem) { "win64" } else { "win32" }
|
|
Copy-Item "$env:TEMP\nssm_temp\nssm-2.24\$archDir\nssm.exe" -Destination $NssmPath -Force
|
|
|
|
Write-Log "NSSM erfolgreich bereitgestellt." "Green"
|
|
} catch {
|
|
Write-Log "FEHLER: NSSM Download fehlgeschlagen: $_" "Red"
|
|
exit 1
|
|
} finally {
|
|
Remove-Item $nssmZip -ErrorAction SilentlyContinue
|
|
Remove-Item "$env:TEMP\nssm_temp" -Recurse -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
|
|
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: Konnte Newt-Version nicht abrufen." "Red"; exit 1
|
|
}
|
|
}
|
|
|
|
function Download-Newt {
|
|
param($FullVersion)
|
|
$ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
|
|
$VersionOnly = $FullVersion.TrimStart('v')
|
|
$Url = "https://github.com/$Repo/releases/download/$VersionOnly/newt_$ArchSuffix"
|
|
$Target = "$InstallDir\newt_$VersionOnly.exe"
|
|
|
|
Get-NSSM
|
|
Write-Log "Download Newt $VersionOnly..." "Cyan"
|
|
try {
|
|
Start-BitsTransfer -Source $Url -Destination $Target -ErrorAction Stop
|
|
Copy-Item -Path $Target -Destination $Symlink -Force
|
|
} catch {
|
|
Write-Log "FEHLER beim Newt-Download: $_" "Red"; exit 1
|
|
}
|
|
}
|
|
|
|
function Setup-Service {
|
|
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
|
|
Write-Log "--- Konfiguration ---" "Yellow"
|
|
$PangolinID = Read-Host "Bitte Pangolin ID eingeben"
|
|
$PangolinSecret = Read-Host "Bitte Secret eingeben"
|
|
$PangolinEndpoint = Read-Host "Bitte Endpoint eingeben"
|
|
|
|
$ArgList = "--id $PangolinID --secret $PangolinSecret --endpoint $PangolinEndpoint"
|
|
|
|
Write-Log "Erstelle Dienst mit NSSM..." "Cyan"
|
|
& $NssmPath install $ServiceName "$Symlink" $ArgList
|
|
& $NssmPath set $ServiceName Description "MAIEREDV Managed Site Client"
|
|
& $NssmPath set $ServiceName AppExit Default Restart
|
|
& $NssmPath set $ServiceName AppRestartDelay 5000
|
|
|
|
# Output Logs aktivieren (Sehr nützlich!)
|
|
& $NssmPath set $ServiceName AppStdout "$InstallDir\newt_service.log"
|
|
& $NssmPath set $ServiceName AppStderr "$InstallDir\newt_service.log"
|
|
|
|
Start-Service $ServiceName
|
|
Write-Log "Dienst erfolgreich gestartet." "Green"
|
|
} else {
|
|
Write-Log "Dienst wird aktualisiert und neu gestartet..." "Yellow"
|
|
Restart-Service $ServiceName
|
|
}
|
|
}
|
|
|
|
function Setup-UpdaterTask {
|
|
$ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('$GiteaUrl')) -mode update`""
|
|
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $ActionCommand
|
|
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am
|
|
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName $UpdaterTaskName -User "SYSTEM" -Force | Out-Null
|
|
Write-Log "Update-Task erstellt." "Green"
|
|
}
|
|
|
|
# --- Main Logic ---
|
|
switch ($mode) {
|
|
"install" {
|
|
$v = Get-LatestVersion
|
|
Download-Newt $v
|
|
Setup-Service
|
|
Setup-UpdaterTask
|
|
Write-Log "Installation fertig!" "Green"
|
|
}
|
|
"update" {
|
|
$v = Get-LatestVersion
|
|
$vOnly = $v.TrimStart('v')
|
|
if (Test-Path "$InstallDir\newt_$vOnly.exe") {
|
|
Write-Log "Schon aktuell." "Cyan"
|
|
} else {
|
|
Download-Newt $v
|
|
Restart-Service $ServiceName
|
|
Write-Log "Update auf $v durchgefuehrt." "Green"
|
|
}
|
|
}
|
|
"uninstall" {
|
|
Write-Log "Deinstallation..." "Yellow"
|
|
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
|
Stop-Service $ServiceName -Force -ErrorAction SilentlyContinue
|
|
& $NssmPath remove $ServiceName confirm
|
|
}
|
|
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
|
Write-Log "Dienst und Task entfernt." "Green"
|
|
}
|
|
} |