Auto Updates for Desktop Clients - HOWTO

Discussion topics related to mobile applications and ZoneMinder Event Server (including machine learning)
Post Reply
jsylvia007
Posts: 116
Joined: Wed Mar 11, 2009 8:32 pm

Auto Updates for Desktop Clients - HOWTO

Post by jsylvia007 »

Howdy all!!

So I wanted a way to update zmNinja automatically without having to visit the web.

Nothing in the below is complicated, but sometimes it's nice to put the pieces together.

I used "download-github-release" from here: https://www.npmjs.com/package/download-github-release

This works on both Windows and Linux.

On linux (using Ubuntu as a sample here), issue:

Code: Select all

sudo apt install nodejs nodejs-legacy npm
Once that's all installed, issue:

Code: Select all

sudo npm install -g download-github-release
Once download-github-release is installed, you'd run it this way:

download-github-release -s linux64bit pliablepixels zmNinja /PATH/TO/WHERE/YOU/WANT

That's it! It will automatically download and unpackage it in a folder called "zmNinja-linux64bit" inside the above path. Just point your shortcut to the zmNinja executable in this place, and you're all set.

I then created a cron job to run the above command daily at 3am, but also put a shortcut to it on my desktop next to the app,

For Windows, you'll need to download Node.js from here: http://blog.teamtreehouse.com/install-n ... pm-windows

Once it's downloaded, run the executable to install Node.js.

Once installed, issue the same npm install command from above (this time without the sudo):

Code: Select all

npm install -g download-github-release
Once that's installed, you'd use this syntax:

Code: Select all

C:\Users\YOUR_USERNAME\AppData\Roaming\npm\download-github-release.cmd -s win64 pliablepixels zmNinja C:\Users\YOUR_USERNAME\Documents
Anyway, hope this helps someone. I've been tinkering around with trying to get the "latest" release from Github, and it looks like this does the job perfectly so I thought I'd share with the community.
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Auto Updates for Desktop Clients - HOWTO

Post by asker »

Thanks for your contribution - looks interesting. One tweak you might consider is not to download it if the version is the same. I suppose I could add some command line parameters to zmninja which lets you check that easily. I'll work on it.
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
jsylvia007
Posts: 116
Joined: Wed Mar 11, 2009 8:32 pm

Re: Auto Updates for Desktop Clients - HOWTO

Post by jsylvia007 »

asker wrote:Thanks for your contribution - looks interesting. One tweak you might consider is not to download it if the version is the same. I suppose I could add some command line parameters to zmninja which lets you check that easily. I'll work on it.
Interesting concept! I don't know much about git, but I'm sure that there's a way for me to check what the "release" version is...
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Auto Updates for Desktop Clients - HOWTO

Post by asker »

Yes, you can easily get the latest release # using

Code: Select all

https://api.github.com/repos/pliablepixels/zmNinja/releases/latest
And then read the "tag_name" parameter

You will however have to figure out which version you currently have, programatically. Probably just write a "version" file each time you download and install, till I add a command line parameter to query zmninja itself
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
jsylvia007
Posts: 116
Joined: Wed Mar 11, 2009 8:32 pm

Re: Auto Updates for Desktop Clients - HOWTO

Post by jsylvia007 »

So... Here is a linux script updated that will create a version file based on the version it downloads. This means it will download at least one time, but won't continue to re-download.

I will work on a Windows one... but I'm not so good with PowerShell, and it looks like that might be required.

We'll see!

Code: Select all

#!/bin/bash

if [ "$#" -gt 1 ]; then
    echo "This Script only takes '-q' as an argument, should you not want any output."
    exit 0
fi


##below function comes from here: http://ask.xmodulo.com/compare-two-version-numbers.html
function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
    
DEST_DIR=$HOME/Documents/
DEST_VERSION="zmNinja-linux64bit"
INSTALL_DIR=$DEST_DIR$DEST_VERSION

if [ -f $INSTALL_DIR/version.txt ]; then
	INSTALLED_VERSION=`cat $INSTALL_DIR/version.txt`
else
	INSTALLED_VERSION=0
fi

LATEST_RELEASE=$(curl -L -s -H 'Accept: application/json' https://github.com/pliablepixels/zmNinja/releases/latest)
LATEST_VERSION=$(echo $LATEST_RELEASE | sed -e 's/.*"tag_name":"\(.*\)".*/\1/')
LATEST_VERSION=$(echo $LATEST_VERSION | sed 's/["].*$//')
LATEST_VERSION=${LATEST_VERSION#?}

if [ "$1" != "-q" ]; then

	echo $DEST_DIR
	echo $DEST_VERSION
	echo $INSTALL_DIR
	echo $INSTALLED_VERSION
	echo $LATEST_VERSION
fi

if version_gt $LATEST_VERSION $INSTALLED_VERSION; then
	

	if [ "$1" != "-q" ]; then
		echo "Update Required, Updating..."
	fi
	
	download-github-release -s $DEST_VERSION pliablepixels zmNinja $DEST_DIR
	echo "$LATEST_VERSION" > $INSTALL_DIR/version.txt
	
	if [ "$1" != "-q" ]; then
		echo "Update Completed."
	fi
else
	if [ "$1" != "-q" ]; then
		echo "No Update is Required."
	fi
fi
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Auto Updates for Desktop Clients - HOWTO

Post by asker »

nice!
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
jsylvia007
Posts: 116
Joined: Wed Mar 11, 2009 8:32 pm

Re: Auto Updates for Desktop Clients - HOWTO

Post by jsylvia007 »

Finally got around to making the Windows script. Tested on Windows 10. Note that it does require node.JS from the previous post, but just in case...
jsylvia007 wrote:Howdy all!!

For Windows, you'll need to download Node.js from here: http://blog.teamtreehouse.com/install-n ... pm-windows

Once it's downloaded, run the executable to install Node.js.

Once installed, open a command prompt, and issue the same npm install command from above:

Code: Select all

npm install -g download-github-release
Once it's all installed, you can create a scheduled task with this VBS Script...

Code: Select all

' updateZMNinja.vbs
' Script to Update zmNinja
' Author Jacob D. Sylvia (Jacob.Sylvia@gmail.com) https://www.jacobsylvia.com/
' Version 0.1 - 05 MAR 2017
' ------------------------------------------------' 

'VERSION COMPARE FUNCTION FROM HERE, and heavily modified:
'http://www.ozgrid.com/forum/showthread.php?t=61514
Function VersionCompare (Version1, Version2) 
	'returns 1 if Version 1 is newer
    'returns -1 if version 1 is older
    'returns 0 if both versions are the same
	
    Dim i, k
    Dim Version1Array
    Dim Version2Array
    Version1Array = Split(Version1, ".")
    Version2Array = Split(Version2, ".")
	
    k = UBound(Version1Array)
    If UBound(Version2Array) < k Then k = UBound(Version2Array)
		
    For i = 0 To k
        'msgbox Version1Array(i)
		If CInt(Version1Array(i)) > Cint(Version2Array(i)) Then
            VersionCompare = 1
            Exit For
        ElseIf CInt(Version1Array(i)) < CInt(Version2Array(i)) Then
            VersionCompare = -1
            Exit For
        Else
            If UBound(Version1Array) = UBound(Version2Array) Then
                VersionCompare = 0
            ElseIf UBound(Version1Array) > UBound(Version2Array) Then
                VersionCompare = 1
            Else
                VersionCompare = -1
            End If
        End If
    Next
End Function

Dim DEST_DIR
Dim DEST_VERSION
Dim INSTALL_DIR
Dim INSTALLED_VERSION

Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")

DEST_DIR = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERPROFILE%") & "\Documents\Utilities\"
DEST_VERSION = "zmNinja-win64bit"
INSTALL_DIR= DEST_DIR & DEST_VERSION

'msgbox INSTALL_DIR

If FSO.FileExists(INSTALL_DIR & "\version.txt") Then
    Set file = FSO.OpenTextFile(INSTALL_DIR & "\version.txt", 1)
	content = file.ReadAll
	If (Left(content, 1) = "v") Then
		INSTALLED_VERSION = Right(content, len(content)-1)
	Else
		INSTALLED_VERSION = content
	End If
Else
	INSTALLED_VERSION=0
End If

'msgbox "Installed Version - " & INSTALLED_VERSION

set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlhttp.open "get", "https://github.com/pliablepixels/zmNinja/releases/latest", false
xmlhttp.send

'msgbox xmlhttp.responseText

strOutput = split(xmlhttp.responseText,"<title>")(1)
strOutput = split(strOutput,"</title>")(0)
strOutput = split(strOutput," ")(1)

LATEST_VERSION = Right(strOutput, len(strOutput)-1)

'msgbox "LATEST VERSION - " & LATEST_VERSION

comparison = VersionCompare (INSTALLED_VERSION, LATEST_VERSION)

'msgbox comparison

If (comparison = -1) Then
	'msgbox "There is a newer version..."
	execString = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERPROFILE%") & "\AppData\Roaming\npm\download-github-release.cmd -s win64 pliablepixels zmNinja " & DEST_DIR
	'msgbox execString
	
	Set oShell = CreateObject ("WScript.Shell")
	oShell.run execString, DontShowWindow, WaitUntilFinished
	
	Set objFSOOut=CreateObject("Scripting.FileSystemObject")
	outFile=INSTALL_DIR & "\version.txt"
	Set objFileOut = objFSOOut.CreateTextFile(outFile,True)
	objFileOut.Write LATEST_VERSION
	objFileOut.Close

End If

Wscript.Quit
Also -
asker wrote:nice!
I noticed that you create a "version" file now, which is awesome... For some reason, it's not updating though. The most recent version is 1.2.39, but the contents of "version" is still 1.2.5... Thought you'd like to know.

Hope these scripts help everyone out!
User avatar
asker
Posts: 1553
Joined: Sun Mar 01, 2015 12:12 pm

Re: Auto Updates for Desktop Clients - HOWTO

Post by asker »

Ah, sorry, I forgot I added the file and did not update my desktop script to re-generate it.fixed it.

I noticed that you create a "version" file now, which is awesome... For some reason, it's not updating though. The most recent version is 1.2.39, but the contents of "version" is still 1.2.5... Thought you'd like to know.
I no longer work on zmNinja, zmeventnotification, pyzm or mlapi. I may respond on occasion based on my available time/interest.

Please read before posting:
How to set up logging properly
How to troubleshoot and report - ES
How to troubleshoot and report - zmNinja
ES docs
zmNinja docs
Post Reply