install_newt-msp-site_v2.sh aktualisiert
This commit is contained in:
+156
-187
@@ -1,211 +1,180 @@
|
|||||||
#!/bin/bash
|
<#
|
||||||
set -e
|
.SYNOPSIS
|
||||||
|
Windows-Installer für den Newt-Client.
|
||||||
|
Finaler Fix für Taskplaner Error 0x1 & Pfad-Probleme.
|
||||||
|
#>
|
||||||
|
param([string]$mode = "install")
|
||||||
|
|
||||||
REPO="fosrl/newt"
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||||
INSTALL_DIR="/opt/me-msp-newt"
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||||
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"
|
|
||||||
|
|
||||||
GREEN='\033[0;32m'
|
# --- Konfiguration ---
|
||||||
YELLOW='\033[1;33m'
|
$Repo = "fosrl/newt"
|
||||||
RED='\033[0;31m'
|
$InstallDir = "C:\Program Files\me-msp-newt"
|
||||||
NC='\033[0m'
|
$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.ps1"
|
||||||
|
|
||||||
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
|
function Write-Log($msg, $color = "White") {
|
||||||
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
|
Write-Host "[$(Get-Date -Format 'HH:mm:ss')] $msg" -ForegroundColor $color
|
||||||
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() {
|
# --- Umgebung vorbereiten ---
|
||||||
local version="$1"
|
function Prepare-Environment {
|
||||||
local arch=$(uname -m)
|
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
|
||||||
local file=""
|
Write-Log "Winget fehlt. Installiere via winget.pro..." "Cyan"
|
||||||
|
try {
|
||||||
case "$arch" in
|
Invoke-RestMethod -Uri "https://winget.pro/install.ps1" | Invoke-Expression
|
||||||
x86_64) file="newt_linux_amd64" ;;
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
aarch64) file="newt_linux_arm64" ;;
|
} catch {
|
||||||
armv7l) file="newt_linux_arm32" ;;
|
Write-Log "FEHLER: Winget Installation fehlgeschlagen." "Red"; exit 1
|
||||||
*) 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() {
|
if (!(Get-Command nssm -ErrorAction SilentlyContinue)) {
|
||||||
echo "🆔 Please enter the Pangolin ID: "
|
Write-Log "NSSM wird via Winget bereitgestellt..." "Cyan"
|
||||||
read -r PANGOLIN_ID
|
winget install nssm --silent --accept-package-agreements --accept-source-agreements | Out-Null
|
||||||
echo "🔑 Please enter the Secret: "
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
|
||||||
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() {
|
function Get-LatestVersion {
|
||||||
if [[ -f "$UPDATER_SERVICE" && -f "$UPDATER_TIMER" ]]; then
|
try {
|
||||||
info "⏳ Update timer already exists – skipping."
|
$url = "https://api.github.com/repos/${Repo}/releases/latest"
|
||||||
return
|
$json = Invoke-RestMethod -Uri $url -UseBasicParsing
|
||||||
fi
|
return $json.tag_name
|
||||||
|
} catch {
|
||||||
info "⚙️ Creating systemd timer for daily updates …"
|
Write-Log "FEHLER: GitHub API nicht erreichbar." "Red"; exit 1
|
||||||
|
}
|
||||||
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-msp-site_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() {
|
# --- Download & Cleanup ---
|
||||||
local version
|
function Download-Newt {
|
||||||
version=$(get_latest_version)
|
param($FullVersion)
|
||||||
version=${version#v}
|
$ArchSuffix = if ([Environment]::Is64BitOperatingSystem) { "windows_amd64.exe" } else { "windows_386.exe" }
|
||||||
info "📦 Latest version: $version"
|
$VersionOnly = $FullVersion.TrimStart('v')
|
||||||
|
$Url = "https://github.com/${Repo}/releases/download/${VersionOnly}/newt_${ArchSuffix}"
|
||||||
|
$Target = "${InstallDir}\newt_${VersionOnly}.exe"
|
||||||
|
|
||||||
if [ -f "${INSTALL_DIR}/newt_${version}" ]; then
|
if (!(Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null }
|
||||||
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"
|
if (!(Test-Path $Target)) {
|
||||||
setup_systemd_service
|
Write-Log "Downloade Version $VersionOnly..." "Cyan"
|
||||||
setup_update_timer
|
try {
|
||||||
info "🚀 Installation completed!"
|
Start-BitsTransfer -Source $Url -Destination $Target -Priority Foreground -ErrorAction Stop
|
||||||
|
} catch {
|
||||||
|
Write-Log "BITS fehlgeschlagen. Versuche Web-Fallback..." "Yellow"
|
||||||
|
Invoke-WebRequest -Uri $Url -OutFile $Target -UseBasicParsing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mode_update() {
|
$Service = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
local version
|
$WasRunning = $Service -and $Service.Status -eq 'Running'
|
||||||
version=$(get_latest_version)
|
|
||||||
version=${version#v}
|
|
||||||
info "📦 Latest version: $version"
|
|
||||||
|
|
||||||
if [ -f "${INSTALL_DIR}/newt_${version}" ]; then
|
if ($WasRunning) {
|
||||||
info "✅ Version $version is already installed. No update required."
|
Write-Log "Stoppe Dienst für Datei-Update..." "Yellow"
|
||||||
setup_update_timer
|
Stop-Service $ServiceName -Force
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
download_newt "$version"
|
|
||||||
systemctl restart "$SERVICE_NAME"
|
|
||||||
setup_update_timer
|
|
||||||
info "🚀 Update completed!"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mode_reinstall() {
|
try {
|
||||||
local version
|
Copy-Item -Path $Target -Destination $Symlink -Force
|
||||||
version=$(get_latest_version)
|
Write-Log "newt_latest.exe auf Stand $VersionOnly aktualisiert." "Green"
|
||||||
version=${version#v}
|
} catch {
|
||||||
info "📦 Latest version: $version"
|
Write-Log "FEHLER: Datei blockiert." "Red"
|
||||||
|
|
||||||
download_newt "$version"
|
|
||||||
setup_systemd_service
|
|
||||||
setup_update_timer
|
|
||||||
info "🚀 Reinstallation completed!"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mode_uninstall() {
|
if ($WasRunning) {
|
||||||
warn "⚠️ Uninstalling newt and removing all systemd entries …"
|
Start-Service $ServiceName
|
||||||
|
Write-Log "Dienst wieder gestartet." "Green"
|
||||||
# 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() {
|
# Cleanup: Behalte die neuesten 2 Versionen
|
||||||
case "$1" in
|
$OldVersions = Get-ChildItem -Path $InstallDir -Filter "newt_*.exe" |
|
||||||
--install|"")
|
Where-Object { $_.Name -ne "newt_latest.exe" } |
|
||||||
mode_install
|
Sort-Object LastWriteTime -Descending |
|
||||||
;;
|
Select-Object -Skip 2
|
||||||
--update)
|
|
||||||
mode_update
|
foreach ($file in $OldVersions) {
|
||||||
;;
|
Remove-Item $file.FullName -Force
|
||||||
--reinstall)
|
}
|
||||||
mode_reinstall
|
|
||||||
;;
|
|
||||||
--uninstall)
|
|
||||||
mode_uninstall
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
error "❌ Unknown parameter: $1 (use --install, --update, --reinstall or --uninstall)"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
function Setup-Service {
|
||||||
|
if (!(Get-Service $ServiceName -ErrorAction SilentlyContinue)) {
|
||||||
|
Write-Log "--- Dienst-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}"
|
||||||
|
|
||||||
|
& nssm install $ServiceName "$Symlink" $ArgList
|
||||||
|
& nssm set $ServiceName Description "MAIEREDV Managed Site Client"
|
||||||
|
& nssm set $ServiceName AppExit Default Restart
|
||||||
|
& nssm set $ServiceName AppRestartDelay 5000
|
||||||
|
|
||||||
|
$LogFile = "${InstallDir}\newt_service.log"
|
||||||
|
& nssm set $ServiceName AppStdout "$LogFile"
|
||||||
|
& nssm set $ServiceName AppStderr "$LogFile"
|
||||||
|
& nssm set $ServiceName AppRotateFiles 1
|
||||||
|
& nssm set $ServiceName AppRotateOnline 1
|
||||||
|
& nssm set $ServiceName AppRotateBytes 10485760
|
||||||
|
|
||||||
|
Start-Service $ServiceName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Setup-UpdaterTask {
|
||||||
|
# Wir bauen den Befehl so um, dass er robuster gegenüber Gänsefüßchen ist
|
||||||
|
$ScriptBody = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; `$s = (New-Object System.Net.WebClient).DownloadString('$GiteaUrl'); `$b = [scriptblock]::Create(`$s); & `$b -mode update"
|
||||||
|
$ActionCommand = "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command `"$ScriptBody`""
|
||||||
|
|
||||||
|
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -Command `"$ScriptBody`""
|
||||||
|
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am
|
||||||
|
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
|
||||||
|
|
||||||
|
# Task registrieren
|
||||||
|
Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName $UpdaterTaskName -Force | Out-Null
|
||||||
|
Write-Log "Update-Task (03:00 Uhr) mit SYSTEM-Principal registriert." "Green"
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- Main ---
|
||||||
|
Prepare-Environment
|
||||||
|
|
||||||
|
switch ($mode) {
|
||||||
|
"install" {
|
||||||
|
$v = Get-LatestVersion
|
||||||
|
Download-Newt $v
|
||||||
|
Setup-Service
|
||||||
|
Setup-UpdaterTask
|
||||||
|
Write-Log "🚀 Installation abgeschlossen!" "Green"
|
||||||
|
}
|
||||||
|
"update" {
|
||||||
|
$v = Get-LatestVersion
|
||||||
|
$vOnly = $v.TrimStart('v')
|
||||||
|
$TargetVersionPath = "${InstallDir}\newt_${vOnly}.exe"
|
||||||
|
|
||||||
|
$NeedsUpdate = $false
|
||||||
|
if (!(Test-Path $TargetVersionPath)) {
|
||||||
|
$NeedsUpdate = $true
|
||||||
|
} elseif (Test-Path $Symlink) {
|
||||||
|
$HashLatest = (Get-FileHash $Symlink).Hash
|
||||||
|
$HashTarget = (Get-FileHash $TargetVersionPath).Hash
|
||||||
|
if ($HashLatest -ne $HashTarget) { $NeedsUpdate = $true }
|
||||||
|
} else { $NeedsUpdate = $true }
|
||||||
|
|
||||||
|
if ($NeedsUpdate) {
|
||||||
|
Download-Newt $v
|
||||||
|
} else {
|
||||||
|
if ((Get-Service $ServiceName).Status -ne 'Running') { Start-Service $ServiceName }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"uninstall" {
|
||||||
|
if (Get-Service $ServiceName -ErrorAction SilentlyContinue) {
|
||||||
|
Stop-Service $ServiceName -Force
|
||||||
|
& nssm remove $ServiceName confirm
|
||||||
|
}
|
||||||
|
Unregister-ScheduledTask -TaskName $UpdaterTaskName -Confirm:$false -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user