Node-red

De Wiki Babelgate
Saltar a: navegación, buscar

Node-red Installation

This is the process to install NODE-red as root user on Ubuntu 14.04 LTS

  • First step is to update the system
   sudo aptitude update && sudo aptitude safe-upgrade
  • Install nodejs
   sudo aptitude install nodejs
  • Using npm install node-red
   sudo npm install -g --unsafe-perm node-red
  • For some legacy modules is needed to make a symbolic link of nodejs to node
   sudo ln -s /usr/bin/nodejs /usr/bin/node
  • Now test node-red executing it manually. Ctrl+c when finish
   node-red
  • While node red is executing on console, you will be able to access it with a web browser
   http://<ip>:1880/


Once installed, next step is configure a new script in /etc/init.d so NODE-red will be lauched automatically on board start.

To do so, create in /etc/init.d/ a file named node-red and coy following bash code:

   #!/bin/bash
   ### BEGIN INIT INFO
   # Provides:          node-red
   # Required-Start:    $local_fs $remote_fs $network
   # Required-Stop:     $local_fs $remote_fs $network
   # Default-Start:     2 3 4 5
   # Default-Stop:      0 1 6
   # Short-Description: Start or stop the node-red server
   ### END INIT INFO
   # Can be downloaded and installed in one go by using this command
   # sudo wget -O /tmp/download https://gist.github.com/Belphemur/cf91100f81f2b37b3e94/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/node-red && sudo update-rc.d node-red defaults
   
   # User that launches node-RED (it's advised to create a new user for Node-RED)
   # You can do : sudo useradd node-red
   # then change the USER=root by USER=node-red
   # if you change the user, don't forget to also change the ownership of the log file (and create it if it doesn't exist):
   # sudo chown NEWUSER /var/log/node-red.log
   # else the log won't be writtable
   USER=root
   
   # The location of Node-RED configuration, not mandatory, leave empty/commented to let 
   # Node-RED decides.
   
   #USER_DIR='/home/pi/node-red/'
   
   
   # DONT'T CHANGE unless you know what you're doing
   NAME=node-red
   DAEMON=/usr/local/bin/node-red
   OPTIONS="--max-old-space-size=128"
   
   if [ -n "$USER_DIR" ];  then
           OPTIONS="$OPTIONS --userDir=$USER_DIR"
   fi
   
   LOG='/var/log/node-red.log'
   
   PIDFILE=/var/run/node-red.pid
   
   . /lib/lsb/init-functions
   
   start_daemon () {
           start-stop-daemon --start --background \
           --chuid $USER --name $NAME \
                   $START_STOP_OPTIONS --make-pidfile --pidfile $PIDFILE \
           --startas /bin/bash -- -c "exec $DAEMON $OPTIONS >> $LOG 2>&1"
                   log_end_msg 0
   }
   
   case "$1" in
           start)
                           log_daemon_msg "Starting daemon" "$NAME"
                           start_daemon
   
           ;;
           stop)
                log_daemon_msg "Stopping daemon" "$NAME"
                            start-stop-daemon --stop --quiet \
               --chuid $USER \
               --pidfile $PIDFILE --retry 30 \
               --oknodo || log_end_msg $?
                           log_end_msg 0
           ;;
           restart)
                           $0 stop
                           sleep 5
                           $0 start
                   ;;
                   status)
           status_of_proc "$DAEMON" "$NAME"
           exit $?
           ;;
   
           *)
                   echo "Usage: $0 {start|stop|restart}"
                   exit 1
   esac
   exit 0


Grant the created file execution perms

   sudo chmod +x /etc/init.d/node-red

Finally register the service to be run on board init

   sudo update-rc.d node-red defaults 99

Next step is adding the log files to logrotate. Edit /etc/logrotate.conf and add the following lines to the end of the file:

   /var/log/node-red.log {
       size 500k
       daily
       copytruncate
       rotate 4
       compress
   }


With the following instruction, you may check logrotate entry is correct.

   $> logrotate -d /etc/logrotate.conf 2>&1 |grep node
       rotating pattern: /var/log/node-red.log  after 1 days (4 rotations)
       considering log /var/log/node-red.log