Node-RED Installer

From Open Source Controls Wiki
Revision as of 01:06, 26 December 2022 by Rhg (talk | contribs) (→‎Composer)
Jump to navigation Jump to search
Selecting Mods to install into Node-RED

The controls architecture is built around Node-RED and Docker, and the Node-RED Installer is the method used to set everything up.

The concept is as follows:

  1. A startup flow is loaded into Node-RED and deployed.
  2. This flow pulls the latest files from the GitHub repository, and creates a Docker container running Node-RED on port 5099, on which it installs a Node-RED Setup flow, with access to all credentials.
  3. The setup flow provides a menu system to select the required Application (Node-RED flows) and Mods (additional Node-RED flows to be added to the application).
  4. As items are selected, they are installed, along with any missing node types and credentials, onto the original Node-RED on port 1880.
  5. Additional Node-RED containers, on ports 5001+, are started to implement isolated services such as data management, that may need separate access rights or updating.
  6. The setup container is closed.


In essence, the Node-RED Installer gives Node-RED the ability to program itself, and create new instances of Node-RED or any other software.


This method has a number of advantages:

  • Entirely handled by Node-RED. Other systems are used by Node-RED, but using the same platform to orchestrate everything makes things more user friendly.
  • A standard simple Node-RED flow can be used as a starting point to install any system.
    https://github.com/heatweb/plumbing-controller/blob/main/flows/flows_install_installer.json
  • Node-RED provides a powerful method to manage containers, with the ability to spin up temporary containers based on logic, running any services needed to boost the systems abilities.
  • Credentials for both Node-RED elements and Docker containers can all be managed by the single setup container, that is killed once the system is up.
  • Better updating control, with the ability to build (and rebuild) custom Node-RED flows from scratch, from a large selection of flow pages that each provide a specific function.
  • Updating is performed live, without closing down existing flows. Variables are kept in memory during updates. The system can then be rebooted (if required) to clear out old variables in memory, but is generally not needed for minor updates or adding functions.
  • Easier maintenance, with a set of maintained core flows (and starting points), expanded and customised as required.
  • Manufacturers can develop their own flows adapted for different functions, place them into the GitHub repository, and make available to controllers at setup.
  • Improved reliability, with services isolated in separate containers. Redundancy can be built into the architecture.
  • Complicated setup procedures, including systems scripts, can be maintained in a Node-RED flow (stored on GitHub), deployed as needed in privileged containers, and removed.

Prerequisites:

  • Node-RED on port 1880 (with Dashboard nodes installed)
  • Docker

Composer

It is possible to build up a Node-RED installation from the various modules using the Installer.

The Composer allows one to pre-define the list of modules to install, the order to install in, and targets.

A Composer file is a JSON file that contains an array of instructions.

A Composer file allows installations to be rebuilt using the latest module versions and as such is an update function.

  • Deploy complete flow
  • Deploy individual flow - if duplicates then duplicate/overwrite/keep/fail
  • Apply changes before deploying
  • Remove a flow
  • Remove a node
  • Update source from Git
  • Copy file
  • Set Boot Config
  • Set Boot Credential
  • Backup all flows
  • Backup all flows, config & credentials
  • Run system command
[{"id":"d45724a5b9c70467","type":"tab","label":"Composer","disabled":false,"info":"","env":[]},{"id":"5c0a883d29582acd","type":"inject","z":"d45724a5b9c70467","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"[{\"dataSource\":\"https://raw.githubusercontent.com/heatweb/plumbing-controller/main/flows/flows_beta_plumbing_controller_1880.json\",\"targetHost\":\"localhost\",\"targetPort\":1880,\"action\":\"flows\"},{\"dataSource\":\"https://raw.githubusercontent.com/heatweb/plumbing-controller/main/flows/mods/flow_dhw_control.json\",\"targetHost\":\"localhost\",\"targetPort\":1880,\"action\":\"flow\"},{\"dataSource\":\"https://raw.githubusercontent.com/heatweb/plumbing-controller/main/flows/mods/flow_dhw_dashboard.json\",\"targetHost\":\"localhost\",\"targetPort\":1880,\"action\":\"flow\"}]","payloadType":"json","x":190,"y":280,"wires":[["a66678b2ea23686e"]]},{"id":"a66678b2ea23686e","type":"change","z":"d45724a5b9c70467","name":"","rules":[{"t":"set","p":"composer.default","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":280,"wires":[[]]},{"id":"c333189a999e8742","type":"inject","z":"d45724a5b9c70467","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"default","payloadType":"str","x":190,"y":400,"wires":[["72309de8a79eeadc"]]},{"id":"72309de8a79eeadc","type":"function","z":"d45724a5b9c70467","name":"Composer","func":"\nvar composition = global.get(\"composer.\" + msg.payload);\n\nif (!composition) { return null; }\n\nflow.set(\"composition\",composition);\nflow.set(\"fetched\", 0);\n\nfor (var item in composition) {\n\n    var msg1={};\n    msg1.url = composition[item].dataSource;\n    msg1.index = item;\n    node.send([msg1,null]);\n\n}\n\nreturn null;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":400,"wires":[["986cee236e456e68"],[]]},{"id":"5daabdcf26ba0523","type":"function","z":"d45724a5b9c70467","name":"flows","func":"msg.headers={};\nmsg.headers.Authorization = \"Bearer \" + flow.get(\"auth.access_token\");\nmsg.headers['Content-type'] = \"application/json\";\nmsg.headers['Node-RED-Deployment-Type'] = \"full\";\n\n\n\nvar targetPort = msg.payload.targetPort || 1880;\nvar targetHost = msg.payload.targetHost || \"localhost\";\n\ntargetHost = targetHost.replace(\"localhost\",\"host.docker.internal\")\n\n\nmsg.url = \"http://\" + targetHost + \":\" + targetPort + \"/\" + (msg.payload.action||\"flow\");\n\n//var cfrom = '\"broker\":\"localhost\"';\n//var cto = '\"broker\":\"localhost\", \"credentials\":{\"username\":\"nodereduser\", \"password\":\"' + global.get(\"noderedmqttpass\") + '\"}';\n\nvar credentials = global.get(\"credentials\")||{};\nvar config = global.get(\"config\") || {};\n\n//var ff = msg.payload;\nvar ff = msg.payload.data;\n\n\nfunction checkTab(nodeitem) {\n   return nodeitem.type == \"tab\";\n}\n\nfunction filterTab(nodeitem) {\n   return nodeitem.type != \"tab\";\n}\n\nfunction filterMqttBroker(nodeitem) {\n   return nodeitem.type != \"mqtt-broker\";\n}\n\nif (msg.payload.action == \"flow\") {   // individual flows can't contain tabs, and do not want to overwrite mqtt broker\n\n   var tabf = ff.filter(checkTab)[0];\n   tabf.nodes = ff.filter(filterTab).filter(filterMqttBroker);\n   ff = tabf;\n\n}\n\n\nfor (var part in ff) {\n\n   if (ff[part].type == \"heatwebConfig\") { \n      \n      ff[part].name = config.name ; \n      ff[part].description = config.description;\n      ff[part].nodeId = config.nodeId;\n      ff[part].networkId = config.networkId; \n\n   }\n   \n\n   if (credentials.localMqttPassword) {\n\n      if (ff[part].type == \"mqtt-broker\" && (ff[part].broker == \"mqtt\" || ff[part].broker == \"localhost\")) { ff[part].credentials = { \"user\": \"admin\", \"password\": credentials.localMqttPassword }; }\n    \n   }\n\n   if (credentials.adminPassword) {\n\n      if (ff[part].type == \"MySQLdatabase\" && ff[part].host == \"mysql\") { ff[part].credentials = { \"user\": \"root\", \"password\": credentials.adminPassword }; }\n\n   }\n\n   if (credentials.localInfluxToken) {\n\n      if (ff[part].type == \"influxdb\" && ff[part].name == \"local\") { ff[part].credentials = { \"token\": credentials.localInfluxToken }; }\n\n   }\n   if (credentials.remoteInfluxToken) {\n\n      if (ff[part].type == \"influxdb\" && ff[part].name == \"heatweb\") { ff[part].credentials = { \"token\": credentials.remoteInfluxToken }; }\n\n   }\n\n   if (credentials.emailPassword && credentials.emailUser) {\n\n      if (ff[part].type == \"e-mail\") { ff[part].credentials = { \"userid\": credentials.emailUser, \"password\": credentials.emailPassword }; }\n\n   }\n\n   \n\n\n}\n\nmsg.payload = JSON.stringify(ff);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":560,"wires":[["f4bdd8a482d1cebf"]]},{"id":"f4bdd8a482d1cebf","type":"http request","z":"d45724a5b9c70467","name":"","method":"POST","ret":"txt","url":"","tls":"","x":530,"y":560,"wires":[["df5cf78bb5e02533","7af6011b014b1971","24d72b1de9de0132"]]},{"id":"24d72b1de9de0132","type":"function","z":"d45724a5b9c70467","name":"save results","func":"var composition = flow.get(\"composition\");\n\nif (!composition) { return null; }\n\n\ncomposition[msg.index].deployed = msg.payload;\n\n\n\n\n// if (msg.filename) {\n\n//     var fn = msg.filename.replace(/\\//g, \"_\").replace(/\\./g, \"_\");\n//     global.set(\"results.\"+fn, JSON.parse(msg.payload));\n\n// }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":770,"y":560,"wires":[[]]},{"id":"986cee236e456e68","type":"http request","z":"d45724a5b9c70467","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":570,"y":400,"wires":[["14b1e8754eecdaaf"]]},{"id":"14b1e8754eecdaaf","type":"function","z":"d45724a5b9c70467","name":"compile","func":"\nvar composition = flow.get(\"composition\");\n\nif (!composition) { return null; }\n\ntry {\n\n    composition[msg.index].data = JSON.parse(msg.payload);\n\n} \ncatch (err) {\n\n    if (msg.payload[0]) { composition[msg.index].data = msg.payload; }\n\n}\n\nflow.set(\"composition\", composition);\n\nvar fetched = flow.get(\"fetched\");\nfetched++;\nflow.set(\"fetched\", fetched);\n\nif (fetched < composition.length) { return null; }\n\nflow.set(\"posted\", 0);\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":400,"wires":[["df5cf78bb5e02533","3f74b3674bd337f4"]]},{"id":"df5cf78bb5e02533","type":"function","z":"d45724a5b9c70467","name":"shift","func":"var posted = flow.get(\"posted\") || 0;\nvar composition = flow.get(\"composition\");\n\n//if (posted = composition.length) { return null; }\n\nvar msg1 = {};\nmsg1.payload = composition[posted]; //.data;\nmsg1.index = 1*posted;\n\nposted++;\nflow.set(\"posted\", posted);\n\n\nif (!msg1.payload) { return null; }\n\nreturn msg1;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":480,"wires":[["5daabdcf26ba0523","9fcd3db98350446e"]]},{"id":"9fcd3db98350446e","type":"debug","z":"d45724a5b9c70467","name":"debug 30","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":480,"wires":[]},{"id":"3f74b3674bd337f4","type":"debug","z":"d45724a5b9c70467","name":"debug 31","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":940,"y":400,"wires":[]},{"id":"7af6011b014b1971","type":"debug","z":"d45724a5b9c70467","name":"debug 32","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":620,"wires":[]},{"id":"18fbc27a546d106e","type":"function","z":"d45724a5b9c70467","name":"","func":"//var cmd = \"curl http://localhost:5002/auth/token --data 'client_id=node-red-admin&grant_type=password&scope=*&username=admin&password=Duu1cahe'\";\n\nmsg.payload={};\nmsg.payload['client_id']=\"node-red-admin\";\nmsg.payload['grant_type']=\"password\";\nmsg.payload['scope']=\"*\";\nmsg.payload['username']=\"admin\";\nmsg.payload['password']=\"admin\";\n\n\nmsg.url = \"http://\" + flow.get(\"targethost\") + \":\" + flow.get(\"targetport\") + \"/auth/token\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":840,"wires":[["916f81d06dcecf50"]]},{"id":"916f81d06dcecf50","type":"http request","z":"d45724a5b9c70467","name":"","method":"POST","ret":"txt","url":"","tls":"","x":400,"y":840,"wires":[["ca67a6abcd19c97b","f6720a2a1363cb64"]]},{"id":"953dcb2e53d3b510","type":"change","z":"d45724a5b9c70467","name":"","rules":[{"t":"set","p":"auth","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":840,"wires":[[]]},{"id":"46a3af1257eda101","type":"json","z":"d45724a5b9c70467","name":"","pretty":false,"x":740,"y":840,"wires":[["953dcb2e53d3b510"]]},{"id":"ca67a6abcd19c97b","type":"debug","z":"d45724a5b9c70467","name":"debug 33","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":780,"wires":[]},{"id":"f6720a2a1363cb64","type":"switch","z":"d45724a5b9c70467","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"access_token","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":580,"y":840,"wires":[["46a3af1257eda101"],[]]}]