-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alec Clews
committed
Aug 19, 2022
1 parent
ac2fb4b
commit 9313ae2
Showing
2 changed files
with
96 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,92 @@ | ||
#!/usr/bin/env pwsh | ||
|
||
# Use PowerShell shell | ||
|
||
# 1. Discover the latest PaperCut MF release version fron the Atom f eed | ||
# 1. Discover the latest PaperCut MF release version fron the Atom webste feed | ||
# 2. Discover what version of PaperCut MF we are running | ||
# 3. If they are not the same then create a job ticket, but only if we have not seen this release before | ||
|
||
# PaperCut version are in semver format, which PowerShell supports via the .NET System.Version type | ||
|
||
$MF_HOST = "$(hostname).local" # Modify to suit | ||
$LAST_VERSION_CHECKED = "0.0.0" # Default to never checked | ||
$PRODUCT = "mf" # Changing to "ng" should work, but has not been tested | ||
$API_TOKEN = (Get-Content -raw ~/.PAPERCUT_API_TOKEN).trim() # Don't hard code API tokens | ||
|
||
if (Test-Path -Path ~/.PAPERCUT_LAST_VERSION_CHECKED ) { | ||
$LAST_VERSION_CHECKED = Get-Content ~/.PAPERCUT_LAST_VERSION_CHECKED | ||
} | ||
$GH_REPO = "PaperCutSoftware/PaperCutExamples" | ||
$MINION = "alecthegeek" # Assignee for review tickets | ||
|
||
$CURRENT_RELEASE = ((Invoke-RestMethod -uri http://www.papercut.com/products/mf/release-history.atom).id -replace "^tag:papercut.com,[0-9]+-[0-9]+-[0-9]+:$PRODUCT\/releases\/v(\d+)-(\d+)-(\d+)",'$1.$2.$3' | %{[System.Version]$_} | Sort-Object -Descending | Select-Object -first 1 ).toString() | ||
|
||
Set-Content -Value $CURRENT_RELEASE -Path ~/.PAPERCUT_LAST_VERSION_CHECKED | ||
|
||
if ( "$LAST_VERSION_CHECKED" -eq "$CURRENT_RELEASE" ) { | ||
Write-Host No new release | ||
# exit 0 | ||
} | ||
$HEALTH_API_KEY = "" | ||
|
||
# Assume we are on the PaperCut MF server and use server-command (NB Need to run in elevated shell for this to work) | ||
|
||
if ( Test-Path -Path "$((Get-ItemProperty -Path 'HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\PaperCut MF').InstallPath)\server\bin\win\server-command.exe" ) { | ||
$HEALTH_API_KEY = ` | ||
$HEALTH_API_KEY = ` | ||
& "$((Get-ItemProperty -Path 'HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\PaperCut MF').InstallPath)\server\bin\win\server-command.exe" ` | ||
get-config "health.api.key" | ||
|
||
} else { | ||
if ($LASTEXITCODE -ne 0 ) { | ||
|
||
# Server command did not work. Use the web services API | ||
|
||
$uri = [Uri] "http://${MF_HOST}:9191/rpc/api/xmlrpc" | ||
# Need a web services API token.Get this from your local PaperCut admin | ||
$API_TOKEN = (Get-Content -raw ~/.PAPERCUT_API_TOKEN).trim() # Don't hard code API tokens | ||
|
||
$HEALTH_API_KEY = (@" | ||
<?xml version="1.0"?> | ||
<methodCall> | ||
<methodName>api.getConfigValue</methodName> | ||
<params> | ||
<param> | ||
<value>${API_TOKEN}</value> | ||
<value>$API_TOKEN</value> | ||
</param> | ||
<param> | ||
<value>health.api.key</value> | ||
</param> | ||
</params> | ||
</methodCall> | ||
"@ | Invoke-RestMethod -Method 'Post' -Uri $uri | Select-Xml -XPath "/methodResponse/params/param/value").toString() | ||
"@ | Invoke-RestMethod -Method 'Post' -Uri "http://${MF_HOST}:9191/rpc/api/xmlrpc" | Select-Xml -XPath "/methodResponse/params/param/value").toString() | ||
|
||
|
||
} | ||
|
||
if ($HEALTH_API_KEY.Length -eq 0 ) | ||
{ | ||
Write-Host Could not retrieve health API key | ||
Exit-PSHostProcess 1 | ||
} | ||
|
||
# Set up the http header for the health API | ||
$headers = @{'Authorization' = $HEALTH_API_KEY} | ||
$URI = [Uri]"http://${MF_HOST}:9191/api/health" | ||
|
||
$uri = [Uri]"http://${MF_HOST}:9191/api/health" | ||
$rsp = Invoke-RestMethod -Uri $URI -Method Get -Headers @{ 'Authorization' = $HEALTH_API_KEY } | ||
|
||
$rsp = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers | ||
$INSTALLED_RELEASE = [System.Version]($rsp.applicationServer.systemInfo.Version -replace '^([\.\d]+).+','$1') | ||
|
||
$INSTALLED_RELEASE = $rsp.applicationServer.systemInfo.Version -replace '^([\.\d]+).+','$1' | ||
# Get the latest release from the PaperCut website (parse the XML Atom feed) | ||
$CURRENT_RELEASE = [System.Version]((Invoke-RestMethod -uri http://www.papercut.com/products/mf/release-history.atom).id[0] ` | ||
-replace "^tag:papercut.com,[0-9]+-[0-9]+-[0-9]+:$PRODUCT\/releases\/v(\d+)-(\d+)-(\d+)",'$1.$2.$3') | ||
|
||
Write-Host Installed release is $INSTALLED_RELEASE | ||
Write-Host "Latest PaperCut release is $CURRENT_RELEASE. Installed Release is $INSTALLED_RELEASE" | ||
|
||
if ( "$INSTALLED_RELEASE" -eq "$CURRENT_RELEASE" ) { | ||
Write-Host No new release, $INSTALLED_RELEASE is up to date | ||
# exit 0 | ||
try { | ||
$LAST_VERSION_CHECKED = Import-Clixml -path ~/LAST_VERSION_CHECKED | ||
} | ||
catch { | ||
$LAST_VERSION_CHECKED = $INSTALLED_RELEASE | ||
} | ||
|
||
if ( "$LAST_VERSION_CHECKED" -eq "$CURRENT_RELEASE" ) { | ||
Write-Host PaperCut $PRODUCT_UPCASE $CURRENT_RELEASE already checked. Nothing to do here | ||
Exit-PSHostProcess 0 | ||
} | ||
|
||
if ( "$CURRENT_RELEASE" -eq "$INSTALLED_RELEASE") { | ||
Write-Host Installed release $INSTALLED_RELEASE is already up to date. No new update available | ||
Exit-PSHostProcess 0 | ||
} | ||
|
||
Write-Host PaperCut $PRODUCT.ToUpper() $INSTALLED_RELEASE.ToString() can be upgraded to $CURRENT_RELEASE.ToString() | ||
|
||
$RELEASE_NOTES="https://www.papercut.com/products/$PRODUCT/release-history/$($CURRENT_RELEASE.MAJOR)-$($CURRENT_RELEASE.MINOR)/#v$($CURRENT_RELEASE.MAJOR)-$($CURRENT_RELEASE.MINOR)-$($CURRENT_RELEASE.BUILD)" | ||
|
||
# Use appropriate API for your ticket system | ||
$env:GH_TOKEN = (Get-Content -raw ~/.GITHUB_ACCESS_TOKEN).trim() | ||
|
||
gh issue -R "$GH_REPO" create -a "$MINION" ` | ||
-t "Review PaperCut $($PRODUCT.ToUpper()) $($INSTALLED_RELEASE.ToString()) upgrade to version $($CURRENT_RELEASE.ToString())" ` | ||
-b "Review release notes at $RELEASE_NOTES" | ||
|
||
Export-Clixml -path ~/LAST_VERSION_CHECKED -InputObject $CURRENT_RELEASE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters