|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +GRHooks Windows Installation Script |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +Installs GRHooks webhook server on Windows systems with optional service configuration |
| 7 | +#> |
| 8 | + |
| 9 | +# Set Error Action |
| 10 | +$ErrorActionPreference = "Stop" |
| 11 | + |
| 12 | +# Colors |
| 13 | +$Host.UI.RawUI.ForegroundColor = "White" |
| 14 | + |
| 15 | +# Configuration - Can be overridden by environment variables |
| 16 | +$REPO = "RustLangES/grhooks" |
| 17 | +$VERSION = if ($env:VERSION) { $env:VERSION } else { "latest" } |
| 18 | +$INSTALL_DIR = if ($env:INSTALL_DIR) { $env:INSTALL_DIR } else { "$env:ProgramFiles\GRHooks" } |
| 19 | +$CONFIG_DIR = if ($env:CONFIG_DIR) { $env:CONFIG_DIR } else { "$env:ProgramData\GRHooks\config" } |
| 20 | +$SERVICE_NAME = if ($env:SERVICE_NAME) { $env:SERVICE_NAME } else { "GRHooks" } |
| 21 | +$LOG_LEVEL = if ($env:LOG_LEVEL) { $env:LOG_LEVEL } else { "info" } |
| 22 | + |
| 23 | +# Display configuration |
| 24 | +Write-Host "Install Configuration:" -ForegroundColor Yellow |
| 25 | +Write-Host "Version: " -NoNewline; Write-Host $VERSION -ForegroundColor Green |
| 26 | +Write-Host "Install Dir: " -NoNewline; Write-Host $INSTALL_DIR -ForegroundColor Green |
| 27 | +Write-Host "Configuration: " -NoNewline; Write-Host $CONFIG_DIR -ForegroundColor Green |
| 28 | +Write-Host "Service: " -NoNewline; Write-Host $SERVICE_NAME -ForegroundColor Green |
| 29 | +Write-Host "Log Level: " -NoNewline; Write-Host $LOG_LEVEL -ForegroundColor Green |
| 30 | +Write-Host "" |
| 31 | + |
| 32 | +# Check if running on Windows |
| 33 | +if ($PSVersionTable.PSVersion.Major -lt 5 -or -not $IsWindows) { |
| 34 | + Write-Host "Error: This script requires Windows PowerShell 5.1 or later" -ForegroundColor Red |
| 35 | + exit 1 |
| 36 | +} |
| 37 | + |
| 38 | +# Determine architecture |
| 39 | +$ARCH = if ([Environment]::Is64BitOperatingSystem) { "x86_64" } else { "i686" } |
| 40 | + |
| 41 | +# Determine package type (Windows always uses zip) |
| 42 | +$PKG_TYPE = "zip" |
| 43 | + |
| 44 | +function Prompt-YesNo { |
| 45 | + param( |
| 46 | + [string]$Question |
| 47 | + ) |
| 48 | + |
| 49 | + do { |
| 50 | + $response = Read-Host "$Question (Y/N)" |
| 51 | + } while ($response -notmatch '^[yYnN]$') |
| 52 | + |
| 53 | + return $response -match '^[yY]$' |
| 54 | +} |
| 55 | + |
| 56 | +if (-not (Prompt-YesNo "Do you want to continue with the installation?")) { |
| 57 | + Write-Host "Installation aborted." -ForegroundColor Red |
| 58 | + exit 1 |
| 59 | +} |
| 60 | + |
| 61 | +# Step 1: Download |
| 62 | +Write-Host "[1/4] Downloading..." -ForegroundColor Yellow |
| 63 | + |
| 64 | +if ($VERSION -eq "latest") { |
| 65 | + $releaseInfo = Invoke-RestMethod -Uri "https://api.github.com/repos/$REPO/releases/latest" |
| 66 | + $DOWNLOAD_URL = $releaseInfo.assets | Where-Object { |
| 67 | + $_.name -match "windows" -and $_.name -match $ARCH -and $_.name.EndsWith(".zip") |
| 68 | + } | Select-Object -First 1 -ExpandProperty browser_download_url |
| 69 | +} else { |
| 70 | + $DOWNLOAD_URL = "https://github.com/$REPO/releases/download/$VERSION/grhooks_${VERSION}_${ARCH}_windows.zip" |
| 71 | +} |
| 72 | + |
| 73 | +if (-not $DOWNLOAD_URL) { |
| 74 | + Write-Host "Cannot find package for your system (Arch: $ARCH)" -ForegroundColor Red |
| 75 | + exit 1 |
| 76 | +} |
| 77 | + |
| 78 | +Write-Host "Downloading: $DOWNLOAD_URL" |
| 79 | +$TEMP_DIR = Join-Path $env:TEMP "grhooks-install" |
| 80 | +New-Item -ItemType Directory -Path $TEMP_DIR -Force | Out-Null |
| 81 | +$zipFile = Join-Path $TEMP_DIR "grhooks.zip" |
| 82 | + |
| 83 | +try { |
| 84 | + Invoke-WebRequest -Uri $DOWNLOAD_URL -OutFile $zipFile |
| 85 | +} catch { |
| 86 | + Write-Host "Failed to download package: $_" -ForegroundColor Red |
| 87 | + exit 1 |
| 88 | +} |
| 89 | + |
| 90 | +# Step 2: Install |
| 91 | +Write-Host "[2/4] Installing..." -ForegroundColor Yellow |
| 92 | + |
| 93 | +try { |
| 94 | + # Create installation directory |
| 95 | + New-Item -ItemType Directory -Path $INSTALL_DIR -Force | Out-Null |
| 96 | + |
| 97 | + # Extract zip file |
| 98 | + Expand-Archive -Path $zipFile -DestinationPath $TEMP_DIR -Force |
| 99 | + |
| 100 | + # Copy files to installation directory |
| 101 | + Copy-Item -Path "$TEMP_DIR\grhooks.exe" -Destination $INSTALL_DIR -Force |
| 102 | + |
| 103 | + # Add to PATH if not already present |
| 104 | + $path = [Environment]::GetEnvironmentVariable("PATH", "Machine") |
| 105 | + if ($path -notlike "*$INSTALL_DIR*") { |
| 106 | + [Environment]::SetEnvironmentVariable("PATH", "$path;$INSTALL_DIR", "Machine") |
| 107 | + $env:PATH += ";$INSTALL_DIR" |
| 108 | + } |
| 109 | +} catch { |
| 110 | + Write-Host "Installation failed: $_" -ForegroundColor Red |
| 111 | + exit 1 |
| 112 | +} |
| 113 | + |
| 114 | +# Step 3: Configuration |
| 115 | +Write-Host "[3/4] Creating Config Directory..." -ForegroundColor Yellow |
| 116 | + |
| 117 | +try { |
| 118 | + New-Item -ItemType Directory -Path $CONFIG_DIR -Force | Out-Null |
| 119 | + |
| 120 | +# Step 4: Service Configuration |
| 121 | +if (Prompt-YesNo "Do you want to configure GRHooks as a Windows service?") { |
| 122 | + Write-Host "[4/4] Configuring Windows service..." -ForegroundColor Yellow |
| 123 | + |
| 124 | + # Check if service already exists |
| 125 | + $service = Get-Service -Name $SERVICE_NAME -ErrorAction SilentlyContinue |
| 126 | + |
| 127 | + if ($service) { |
| 128 | + Write-Host "Service $SERVICE_NAME already exists" -ForegroundColor Yellow |
| 129 | + if (Prompt-YesNo "Do you want to reconfigure it?") { |
| 130 | + Stop-Service -Name $SERVICE_NAME -Force -ErrorAction SilentlyContinue |
| 131 | + & sc.exe delete $SERVICE_NAME | Out-Null |
| 132 | + } else { |
| 133 | + exit 0 |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + # Create service |
| 138 | + try { |
| 139 | + $serviceArgs = "`"$CONFIG_DIR`"" |
| 140 | + |
| 141 | + New-Service -Name $SERVICE_NAME ` |
| 142 | + -BinaryPathName "`"$INSTALL_DIR\grhooks.exe`" $serviceArgs" ` |
| 143 | + -DisplayName "GRHooks Webhook Server" ` |
| 144 | + -Description "GRHooks Webhook Server Service" ` |
| 145 | + -StartupType Automatic ` |
| 146 | + -ErrorAction Stop | Out-Null |
| 147 | + |
| 148 | + # Configure service recovery |
| 149 | + & sc.exe failure $SERVICE_NAME reset= 60 actions= restart/5000 | Out-Null |
| 150 | + |
| 151 | + # Set environment variable |
| 152 | + [Environment]::SetEnvironmentVariable("LOG", $LOG_LEVEL, "Machine") |
| 153 | + |
| 154 | + Write-Host "Service created successfully" -ForegroundColor Green |
| 155 | + |
| 156 | + if (Prompt-YesNo "Do you want to start the service now?")) { |
| 157 | + Start-Service -Name $SERVICE_NAME |
| 158 | + Write-Host "Service started. You can view the logs in Event Viewer." -ForegroundColor Green |
| 159 | + } |
| 160 | + } catch { |
| 161 | + Write-Host "Service configuration failed: $_" -ForegroundColor Red |
| 162 | + } |
| 163 | +} else { |
| 164 | + Write-Host "[4/4] Skipping service configuration." -ForegroundColor Yellow |
| 165 | +} |
| 166 | + |
| 167 | +# Cleanup |
| 168 | +Remove-Item -Path $TEMP_DIR -Recurse -Force -ErrorAction SilentlyContinue |
| 169 | + |
| 170 | +Write-Host "Installation completed!" -ForegroundColor Green |
| 171 | +Write-Host "" |
| 172 | +Write-Host "Post your manifests here: " -NoNewline; Write-Host $CONFIG_DIR -ForegroundColor Yellow |
| 173 | +Write-Host "Binary: " -NoNewline; Write-Host "$INSTALL_DIR\grhooks.exe" -ForegroundColor Yellow |
| 174 | +if (Get-Service -Name $SERVICE_NAME -ErrorAction SilentlyContinue) { |
| 175 | + Write-Host "Service: " -NoNewline; Write-Host "Get-Service $SERVICE_NAME" -ForegroundColor Yellow |
| 176 | +} |
0 commit comments