Node-RED on TinyCore Linux
Author: | akeil |
---|---|
Date: | 2017-01-14 |
Version: | 2 |
Node-RED [1] is a flow based tool which can be used to connect services and devices with each other and to small automation flows.
Node-RED runs as a NodeJS [2] application and is managed through the browser.
This post shows how to install it on a Raspberry Pi [3] running TinyCore Linux [4]
Installation
First, install Node.js:
$ tce-load -w- i node
Then install Node-RED through the Node Package Manager [5] (npm):
$ sudo npm install -g --unsafe-perm node-red
Note
The -g option installs it globally. The recommended procedure is to install packages which contain executable command (like Node-RED) globally and other packages locally (simply without the -g).
The Default location for global NPM packages on TinyCore Linux is /usr/local/lib/node_modules/<package>. Local packages are installed in a node_modules directory inside the current working directory.
The npm install command will also be used to install additional packages (flows or nodes) for Node-RED.
Unfortunately, having a custom package manager does not go well with the philosophy of TinyCore extensions. Packages installed through npm will not be persisted and are lost on reboot.
Two things can be done to prevent this:
- install packages into a location which is backed up and restored, e.g. one of the /home/* directories. This will be the solution for additional node packages.
- Create tce's for every package. We will do this for the Node-RED package only.
To create a tce for the node-red package:
$ mkdir -p /tmp/node-red/usr/local/lib/node_modules $ sudo mv /usr/local/lib/node_modules/node-red /tmp/node-red/usr/local/lib/node_modules $ mkdir -p /tmp/node-red/usr/local/bin $ cd /tmp/node-red/usr/local/bin $ ln -s ../lib/node_modules/node-red/red.js node-red $ ln -s ../lib/node_modules/node-red/bin/node-red-pi node-red-pi $ cd ~ $ sudo chown root:root -R /tmp/node-red/ $ tce-load -wo squashfs-tools $ tce-load -il squashfs-tools $ mksquashfs /tmp/node-red/ /tmp/node-red.tcz $ sudo rm -r /tmp/node-red/ $ mv /tmp/node-red.tcz /etc/sysconfig/tcedir/optional/
Now, after a reboot, the node-red installation should be gone. But it can now be installed with:
$ tce-load -i node-red
To make sure that Node-RED is reinstalled on boot, add it to onboot.lst:
$ echo node-red.tcz >> /etc/sysconfig/tcedir/onboot.lst
Finally, we need to secure the Node-RED configuration and data files. These are stored in $HOME/.node-red and we add it to /opt/.filetool.lst. Or rather: make sure that home directories are present (they are, by default).
Additional Node Packages
Additional node packages will be installed in the /home/nodered/.node-red, assuming the the nodered user will run the application. This is done like this:
$ cd /home/nodered/.node.red $ npm install node-red-contrib-foo
User
Next, we need to create the nodered user which we will use to run the service.
$ sudo adduser -S nodered
Start Script
To start the service at boot, edit /opt/bootlocal.sh. Since Node-RED only implements logging to the console, we redirect its output into a log file:
# start Node-RED with nodered-user in a subshell NR_LOG=/var/log/node-red.log touch "$NR_LOG" chown nodered:root "$NR_LOG" (su nodered -c /usr/local/bin/node-red -s /bin/sh >> "$NR_LOG") &
Monitoring (with monit)
This is how monit [6] can be used to monitor that Node-RED is up and running. This assumes a working installation of monit on TinyCore Linux.
Adding checks for Node-RED is done by editing a configuration file. The file is located in /usr/local/etc/monit.d. Actually, monit's configuration file is /usr/local/etc/monitrc but it is possible to split the configuration into several files by adding this line to monitrc:
include /usr/local/etc/monit.d/*
So, inside monit.d/, create a new file named node-red and add a check which looks for the Node-red process and make a HTTP request against the Node-RED Admin API [7], specifically its auth endpoint. The auth endpoint lists the currently active authentication scheme and is available even if authentication is on.
The endpoint should return either a JSON object with details on the authentication scheme or an empty JSON object. In any case, a pair of curly braces.
We will also make sure that the application is running with the intended user.
The check looks like this:
# list authentication scheme from Admin-API - see # http://nodered.org/docs/api/admin/oauth check process node-red matching node-red group node-red if failed port 1880 with protocol http request /auth/login content = "\{.*?\}" then alert if failed uid nodered then alert
[1] | https://nodered.org/ |
[2] | https://nodejs.org/ |
[3] | https://www.raspberrypi.org/ |
[4] | http://tinycorelinux.net/ |
[5] | https://www.npmjs.com/ |
[6] | https://mmonit.com/monit/ |
[7] | http://nodered.org/docs/api/admin/ |