Skip to content

Instantly share code, notes, and snippets.

@Paraphraser
Last active December 24, 2022 02:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paraphraser/c8939213faf2de8a10f2a1f67452b0c1 to your computer and use it in GitHub Desktop.
Save Paraphraser/c8939213faf2de8a10f2a1f67452b0c1 to your computer and use it in GitHub Desktop.
Hints: Running Node-Red in IOTstack

Hints: Running Node-RED in IOTstack

Much of the material that used to be in this gist has been moved to the Node-RED documentation page at SensorsIot/IOTstack.

The nodered_list_installed_nodes script is also part of the IOTstack repository.


The material below is the remainder of the original gist:

Useful script nodered_version_check

#!/usr/bin/env bash

# the name of this script is
SCRIPT=$(basename "$0")

# default image is
DEFAULTIMAGE="iotstack-nodered:latest"

# zero or one arguments supported
if [ "$#" -gt 1 ]; then
    echo "Usage: $SCRIPT {image:tag}"
    echo "   eg: $SCRIPT $DEFAULTIMAGE"
    exit -1
fi

# image can be passed as first argument, else default
IMAGE=${1:-"$DEFAULTIMAGE"}

# fetch latest version details from GitHub
LATEST=$(wget -O - -q https://raw.githubusercontent.com/node-red/node-red-docker/master/package.json | jq -r .version)

# figure out the version in the local image
INSTALLED=$(docker image inspect "$IMAGE" | jq -r .[0].Config.Labels[\"org.label-schema.version\"])

# compare versions and report result
if [ "$INSTALLED" = "$LATEST" ] ; then 

   echo "Node-Red is up-to-date (version $INSTALLED)"

else

/bin/cat <<-COLLECT_TEXT

	====================================================================
	Node-Red version number has changed on GitHub:

	    Local Version: $INSTALLED
	   GitHub Version: $LATEST
	
	This means a new version MIGHT be available on Dockerhub. Check here:

	   https://hub.docker.com/r/nodered/node-red/tags?page=1&ordering=last_updated

	When an updated version is actually avaliable, proceed like this:

	   $ REBUILD nodered
	   $ UP nodered
	   $ docker system prune
	====================================================================

COLLECT_TEXT

fi

installation

  1. Make sure ~/.local/bin exists (the following command is non-destructive):

    $ mkdir -p ~/.local/bin
    
  2. Logout and login again. A new login causes your .profile to run. That discovers the ~/.local/bin directory and adds it to your search path.

  3. Copy the nodered_version_check script to the clipboard.

  4. Paste the script into your favourite text editor.

    Note:

    • try to avoid using text editors which write line-endings as CR+LF (0x0D 0x0A). You will typically find those on Windows systems but you may also encounter the problem on other platforms if your text editor has a preference which has been set to use CR+LF line-endings. The Raspberry Pi expects LF.
  5. Save the script into ~/.local/bin with the name nodered_version_check.

  6. Make the script executable:

    $ chmod u+x nodered_version_check
    
  7. Test by running the script:

    $ nodered_version_check
    

dependencies

nodered_version_check depends on jq and wget. If those are not available on your Raspberry Pi, you can fix that by:

$ sudo apt install -y jq wget

Build once, deploy many

I have two 4GB Raspberry Pi 4Bs which I think of as "live" and "test". They are kept as close to identical as I can manage. As well as being a good testbed, my "test" Raspberry Pi is also there to guard against my "live" Raspberry Pi emitting magic smoke. If that happens, all I will have to do is:

  1. Restore the latest backup onto the "test" Raspberry Pi (the "live" Raspberry Pi takes two backups per day).
  2. Change my DHCP server to associate the MACs of the "test" Raspberry Pi with the IP addresses being assigned to the "live" Raspberry Pi.
  3. Apply power.

Because I have two Raspberry Pis, I have never seen much sense in rebuilding Node-RED on both the "live" and "test" machines. Generally, I will do the rebuild on the "test" Raspberry Pi first, make sure there are no obvious problems, and then think about the "live" Raspberry Pi.

If you're in a similar situation, here's how to avoid rebuilding Node-RED on more than one machine.

Rebuild on Raspberry Pi "A"

If a new base image has become available on DockerHub:

$ cd ~/IOTstack
$ docker-compose build --no-cache --pull nodered
$ docker-compose up -d nodered
$ docker system prune

If you have changed the Dockerfile, you only need:

$ cd ~/IOTstack
$ docker-compose up --build -d nodered
$ docker system prune

Archive images on Raspberry Pi "A"

  1. If you just rebuilt because a new base image became available on DockerHub, you should archive the new base image:

    $ docker save nodered/node-red >~/nodered_base.tar
    
  2. Archive the new local image:

    $ docker save iotstack_nodered >~/nodered_local.tar
    

Deploy images from "A" to "B"

I use scp but you can use any method that works for you to move the .tar files from "A" to "B":

$ scp ~/nodered_*.tar pi@OTHERPI:.

where "OTHERPI" is the IP address or domain name of another Raspberry Pi.

Load images on Raspberry Pi "B"

On each OTHERPI:

  1. If you archived a new base image, restore that first:

    $ docker load <~/nodered_base.tar
    
  2. Restore the new local image:

    $ docker load <~/nodered_local.tar
    
  3. Apply the changes:

    $ cd ~/IOTstack
    $ docker-compose up -d nodered
    
  4. Clean-up:

    $ docker system prune
    $ rm ~/nodered_*.tar
    

There is no need to take down the old Node-RED container. The "up" will automatically instantiate a new container based on the newer image and perform a new-for-old swap. Barely any downtime!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment