Skip to content

Commit

Permalink
Added compelete automation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Alec Clews committed Aug 19, 2022
1 parent ac2fb4b commit 9313ae2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 67 deletions.
88 changes: 55 additions & 33 deletions mf-automation/do-we-need-to-upgrade.ps1
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
75 changes: 41 additions & 34 deletions mf-automation/do-we-need-to-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,25 @@

# This example uses xmllint, jq and the glab (the GitLab command line client). They will need to be installled

API_TOKEN="$(cat ~/.PAPERCUT_API_TOKEN)" # Dont' hard code API tokens
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
PRODUCT_UPCASE=$(echo $PRODUCT | tr [a-z] [A-Z]) # Sometimes we need upper case version
GH_REPO="PaperCutSoftware/PaperCutExamples"

if [ -e ~/.PAPERCUT_LAST_VERSION_CHECKED ] ; then
LAST_VERSION_CHECKED=$(cat ~/.PAPERCUT_LAST_VERSION_CHECKED)
fi

echo last version checked is $LAST_VERSION_CHECKED
# ID of system admin to review releases
MINION=alecthegeek

# Discover the latest PaperCut MF release from the papercut.com atom feed
# Note: Using an older version of xmllint so sed needs to add a new line to each version line
CURRENT_RELEASE=$(curl -sL http://www.papercut.com/products/$PRODUCT/release-history.atom |
xmllint --xpath "//*[local-name()='feed']/*[local-name()='entry']/*[local-name()='id']/text()" - |
sed -Ene 's/tag:papercut.com,[0-9]+-[0-9]+-[0-9]+:'$PRODUCT'\/releases\/v([0-9]+)-([0-9]+)-([0-9]+)/\1.\2.\3\
/gp' |
sort -rV | head -1)

echo $CURRENT_RELEASE > ~/.PAPERCUT_LAST_VERSION_CHECKED
# We need the config health.api.key value
# Assume we are on the PaperCut MF server and use server-command (NB Needs to run under the correct user)
if ! HEALTH_API_KEY=$(~papercut/server/linux-x64/bin/server-command get-config-value health.api.key) ; then

if [ "$LAST_VERSION_CHECKED" = "$CURRENT_RELEASE" ] ; then
echo No new release
# exit 0
fi
# Can't use server-command. Get via it the web services API.

echo Found a new release.
# Need a web services API token.Get this from your local PaperCut admin
API_TOKEN="$(cat ~/.PAPERCUT_API_TOKEN)" # Dont' hard code API tokens

# We need the health API key. Get via the web services API. Config health.api.key
HEALTH_API_KEY=$(curl -s -H "content-type:text/xml" "http://${MF_HOST}:9191/rpc/api/xmlrpc" --data @- <<EOF | xmllint --xpath '//*/value/text()' -
HEALTH_API_KEY=$(curl -s -H "content-type:text/xml" "http://${MF_HOST}:9191/rpc/api/xmlrpc" --data @- <<EOF | xmllint --xpath '//*/value/text()' -
<?xml version="1.0"?>
<methodCall>
<methodName>api.getConfigValue</methodName>
Expand All @@ -52,9 +41,7 @@ HEALTH_API_KEY=$(curl -s -H "content-type:text/xml" "http://${MF_HOST}:9191/rpc
</methodCall>
EOF
)

# If running on the PaperCut MF server this approach is much easier
# HEALTH_API_KEY=$(~papercut/server/linux-x64/bin/server-command get-config-value health.api.key)
fi

if [ -z "$HEALTH_API_KEY" ] ; then
echo HEALTH_API_KEY not found
Expand All @@ -63,21 +50,41 @@ fi

INSTALLED_RELEASE=$(curl -s -H "Authorization:$HEALTH_API_KEY" "http://${MF_HOST}:9191/api/health" | jq '.applicationServer.systemInfo.version' |sed -Ee 's/^"([0-9]+\.[0-9]+\.[0-9]+).+$/\1/')

# Discover the latest PaperCut MF release from the papercut.com atom feed
CURRENT_RELEASE=$(curl -sL http://www.papercut.com/products/$PRODUCT/release-history.atom |
xmllint --xpath "//*[local-name()='feed']/*[local-name()='entry'][1]/*[local-name()='id']/text()" - |
sed -Ene 's/tag:papercut.com,[0-9]{4}-[0-9]{2}-[0-9]{2}:'$PRODUCT'\/releases\/v([0-9]+)-([0-9]+)-([0-9]+)/\1.\2.\3/gp')

echo Latest PaperCut release is $CURRENT_RELEASE. Installed Release is $INSTALLED_RELEASE

#Default
LAST_VERSION_CHECKED=$INSTALLED_RELEASE

if [ -e ~/LAST_VERSION_CHECKED ] ;then
LAST_VERSION_CHECKED=$(cat ~/LAST_VERSION_CHECKED) # Override default
fi

if [ "$LAST_VERSION_CHECKED" = "$CURRENT_RELEASE" ] ; then
echo PaperCut $PRODUCT_UPCASE $CURRENT_RELEASE already checked. Nothing to do here
exit 0
fi

if [ "$INSTALLED_RELEASE" = "$CURRENT_RELEASE" ] ; then
echo No new release, $INSTALLED_RELEASE is up to date
# exit 0
echo Installed release $INSTALLED_RELEASE is already up to date. No new update available
exit 0
fi

echo Installed Release is $INSTALLED_RELEASE, $CURRENT_RELEASE is now avaliable. Upgrade possible
echo PaperCut $PRODUCT_UPCASE $INSTALLED_RELEASE can be upgraded to $CURRENT_RELEASE

MAJOR="$(echo $CURRENT_RELEASE | cut -d . -f 1)"
MINOR="$(echo $CURRENT_RELEASE | cut -d . -f 2)"
PATCH="$(echo $CURRENT_RELEASE | cut -d . -f 3)"
FIX="$(echo $CURRENT_RELEASE | cut -d . -f 3)"

RELEASE_NOTES="https://www.papercut.com/products/$PRODUCT/release-history/${MAJOR}-${MINOR}/#v${MAJOR}-${MINOR}-${PATCH}"
RELEASE_NOTES="https://www.papercut.com/products/$PRODUCT/release-history/${MAJOR}-${MINOR}/#v${MAJOR}-${MINOR}-${FIX}"

MINION=alecthegeek
# Use appropriate API for your ticket system
GH_TOKEN=$(cat ~/.GITHUB_ACCESS_TOKEN) gh issue -R $GH_REPO create -a $MINION \
-t "Review PaperCut $PRODUCT_UPCASE $INSTALLED_RELEASE upgrade to version $CURRENT_RELEASE" \
-b "Review release notes at $RELEASE_NOTES"

# Need to install a repo client to create job ticket, or use appropriate API on your ticket system
glab issue create -a $MINION -t "Investigate Possible PaperCUt $(echo $PRODUCT | tr [a-z] [A-Z]) upgrade to version $CURRENT_RELEASE" -d "Review release notes at $RELEASE_NOTES.
Note installed release is $INSTALLED_RELEASE"
echo -n $CURRENT_RELEASE > ~/LAST_VERSION_CHECKED

0 comments on commit 9313ae2

Please sign in to comment.