Skip to content

Commit

Permalink
Migrate Node-RED config out of Home Assistant config folder (#1729)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Nov 10, 2023
1 parent b5d82ef commit 0f52e90
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
7 changes: 3 additions & 4 deletions node-red/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,14 @@ on garbage collection in an effort to free unused memory.

## Configuration folder

The addon will store most of its configuration in the `config/node-red` folder,
including the `flows.json`. Please ensure this is included in your backup. It is
also important to note that this will not be removed on uninstalling the addon.
The addon will store most of its configuration in the Node-RED add-on
configuration folder, including the `flows.json`.

## Time zone configuration

The addon will use the configured time zone of the underlying operating system.
If this is incorrect (for example with the Home Assistant Operating System it
will be UTC), this can be configured in the `/config/node-red/settings.js` file.
will be UTC), this can be configured in the `settings.js` file.

To do so, open the file with a text editor and add the following above the
`module.exports = {` line.
Expand Down
3 changes: 2 additions & 1 deletion node-red/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ host_network: true
auth_api: true
uart: true
map:
- config:rw
- addon_config:rw
- homeassistant_config:rw
- media:rw
- share:rw
- ssl
Expand Down
6 changes: 3 additions & 3 deletions node-red/rootfs/etc/node-red/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const config = require("/config/node-red/settings.js");
const config = require("/config/settings.js");
const fs = require("fs");
const options = JSON.parse(fs.readFileSync("/data/options.json", "utf8"));
const bcrypt = require("bcryptjs");
Expand All @@ -12,10 +12,10 @@ if ("theme" in options) {
// Sane and required defaults for the add-on
config.debugUseColors = false;
config.flowFile = "flows.json";
config.nodesDir = "/config/node-red/nodes";
config.nodesDir = "/config/nodes";
config.uiHost = "127.0.0.1";
config.uiPort = 46836;
config.userDir = "/config/node-red/";
config.userDir = "/config/";

//Set path for HTTP_Nodes to be served from avoiding lua auth
config.httpNodeRoot = "/endpoint";
Expand Down
4 changes: 2 additions & 2 deletions node-red/rootfs/etc/node-red/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
* - debugUseColors (disabled, won't work in the add-on)
* - flowFile (fixed to flows.json)
* - credentialSecret (credentials_secret in the add-on configuration)
* - userDir (is set fixed to `/config/node-red`)
* - nodesDir (is set fixed to `/config/node-red/nodes`)
* - userDir (is set fixed to `/config`)
* - nodesDir (is set fixed to `/config/nodes`)
* - adminAuth (known as users in the add-on configuration)
* - https (ssl settings in the add-on configuration)
* - logging.console.level (log_level in the add-on configuration)
Expand Down
23 changes: 16 additions & 7 deletions node-red/rootfs/etc/s6-overlay/s6-rc.d/init-nodered/run
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ if bashio::config.is_empty 'credential_secret'; then
bashio::exit.nok
fi

# Migrate add-on data from the Home Assistant config folder,
# to the add-on configuration folder.
if ! bashio::fs.directory_exists '/config/settings.js' \
&& bashio::fs.file_exists '/homeassistant/node-red/settings.js'; then
shopt -s dotglob
mv /homeassistant/node-red/* /config/ \
|| bashio::exit.nok "Failed to migrate Node-RED configuration"
fi

# Ensure configuration exists
if ! bashio::fs.directory_exists '/config/node-red/'; then
mkdir -p /config/node-red/nodes \
if ! bashio::fs.directory_exists '/config/settings.js'; then
mkdir -p /config/nodes \
|| bashio::exit.nok "Failed to create node-red configuration directory"

# Copy in template files
cp /etc/node-red/flows.json /config/node-red/
cp /etc/node-red/settings.js /config/node-red/
cp /etc/node-red/flows.json /config/
cp /etc/node-red/settings.js /config/

# Create random flow id
id=$(node -e "console.log((1+Math.random()*4294967295).toString(16));")
sed -i "s/%%ID%%/${id}/" "/config/node-red/flows.json"
sed -i "s/%%ID%%/${id}/" "/config/flows.json"
fi

# Pass in port & SSL settings
Expand All @@ -58,8 +67,8 @@ else
fi

# Ensures conflicting Node-RED packages are absent
cd /config/node-red || bashio::exit.nok "Could not change directory to Node-RED"
if bashio::fs.file_exists "/config/node-red/package.json"; then
cd /config || bashio::exit.nok "Could not change directory to Node-RED"
if bashio::fs.file_exists "/config/package.json"; then
npm uninstall \
node-red-contrib-home-assistant \
node-red-contrib-home-assistant-llat \
Expand Down

0 comments on commit 0f52e90

Please sign in to comment.