Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Node-RED Global environment variables #64

Open
sumitshinde-84 opened this issue Feb 5, 2024 · 10 comments
Open

Support Node-RED Global environment variables #64

sumitshinde-84 opened this issue Feb 5, 2024 · 10 comments

Comments

@sumitshinde-84
Copy link

The Node currently has an option to configure the node using environment variables, but that doesn't work as expected.

@Alkarex
Copy link
Member

Alkarex commented Feb 5, 2024

Can you provide a bit more information about what the outcome is?

@sumitshinde-84
Copy link
Author

Can you provide a bit more information about what the outcome is?

I have tested both options for selecting the global context and environment variable. Unfortunately, neither of them seems to work as expected. When I choose one option among them, the provided input value for that field isn't being set. Instead, the default value, for example for host input default 127.0.0.1 is assigned. Additionally, I've tested using string type inputs, which appear to be the only ones functioning correctly.

@Alkarex
Copy link
Member

Alkarex commented Feb 6, 2024

I have just made a quick test, which seems to work fine for me.
Could you share a minimal flow illustrating the problem? (watch out not to share your credentials)

Here is my test for global context, changing only the name of the table:

Flow...
[
	{
		"id": "433b5368aae90a2b",
		"type": "debug",
		"z": "d9a661f4.ef966",
		"name": "",
		"active": true,
		"tosidebar": true,
		"console": false,
		"tostatus": false,
		"complete": "payload",
		"targetType": "msg",
		"statusVal": "",
		"statusType": "auto",
		"x": 1410,
		"y": 400,
		"wires": []
	},
	{
		"id": "d174581733ef232d",
		"type": "postgresql",
		"z": "d9a661f4.ef966",
		"name": "",
		"query": "SELECT * FROM test\nWHERE device_id = '2'\nLIMIT 10;",
		"postgreSQLConfig": "1d61b6d2fc877349",
		"split": false,
		"rowsPerMsg": 1,
		"listen": false,
		"outputs": 1,
		"x": 1230,
		"y": 400,
		"wires": [
			[
				"433b5368aae90a2b"
			]
		]
	},
	{
		"id": "96318ea597018065",
		"type": "inject",
		"z": "d9a661f4.ef966",
		"name": "",
		"props": [
			{
				"p": "payload"
			},
			{
				"p": "topic",
				"vt": "str"
			}
		],
		"repeat": "",
		"crontab": "",
		"once": false,
		"onceDelay": 0.1,
		"topic": "",
		"payload": "",
		"payloadType": "date",
		"x": 1060,
		"y": 360,
		"wires": [
			[
				"1dd60ae4d6f64665"
			]
		]
	},
	{
		"id": "1dd60ae4d6f64665",
		"type": "change",
		"z": "d9a661f4.ef966",
		"name": "",
		"rules": [
			{
				"t": "set",
				"p": "postgresdb",
				"pt": "global",
				"to": "iot",
				"tot": "str"
			}
		],
		"action": "",
		"property": "",
		"from": "",
		"to": "",
		"reg": false,
		"x": 1260,
		"y": 360,
		"wires": [
			[]
		]
	},
	{
		"id": "48f62bdaf022041f",
		"type": "inject",
		"z": "d9a661f4.ef966",
		"name": "",
		"props": [
			{
				"p": "payload"
			},
			{
				"p": "topic",
				"vt": "str"
			}
		],
		"repeat": "",
		"crontab": "",
		"once": false,
		"onceDelay": 0.1,
		"topic": "",
		"payload": "",
		"payloadType": "date",
		"x": 1060,
		"y": 400,
		"wires": [
			[
				"d174581733ef232d"
			]
		]
	},
	{
		"id": "1d61b6d2fc877349",
		"type": "postgreSQLConfig",
		"name": "admin@timescale:5432/global-variable",
		"host": "timescale",
		"hostFieldType": "str",
		"port": "5432",
		"portFieldType": "num",
		"database": "postgresdb",
		"databaseFieldType": "global",
		"ssl": "false",
		"sslFieldType": "bool",
		"applicationName": "",
		"applicationNameType": "str",
		"max": "10",
		"maxFieldType": "num",
		"idle": "1000",
		"idleFieldType": "num",
		"connectionTimeout": "10000",
		"connectionTimeoutFieldType": "num",
		"user": "demo",
		"userFieldType": "str",
		"password": "example",
		"passwordFieldType": "str"
	}
]

Regarding global variables, could you please double-check that the Node-RED flow can access your environment variables?

Example with PWD, but you should try with whatever environment variable you are trying to read:

Flow...
[
    {
        "id": "4f0274e4dd629542",
        "type": "debug",
        "z": "d9a661f4.ef966",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1810,
        "y": 920,
        "wires": []
    },
    {
        "id": "7f99f90663124bde",
        "type": "inject",
        "z": "d9a661f4.ef966",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1480,
        "y": 920,
        "wires": [
            [
                "3b4f008de378cc96"
            ]
        ]
    },
    {
        "id": "3b4f008de378cc96",
        "type": "change",
        "z": "d9a661f4.ef966",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "PWD",
                "tot": "env"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1640,
        "y": 920,
        "wires": [
            [
                "4f0274e4dd629542"
            ]
        ]
    }
]

@sumitshinde-84
Copy link
Author

sumitshinde-84 commented Feb 6, 2024

i have tested again configuring postgres using environment variable doesn't seems to work

hear is my flow: -

[
    {
        "id": "a352c8d5e5762386",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "ade9482578e6aaaf",
        "type": "group",
        "z": "a352c8d5e5762386",
        "name": "Table",
        "style": {
            "label": true
        },
        "nodes": [
            "7e4ebd722824573c",
            "2aabab04f4ce5936",
            "2d090d386835921c",
            "93963d712825cebd"
        ],
        "x": 254,
        "y": 79,
        "w": 692,
        "h": 142
    },
    {
        "id": "2aabab04f4ce5936",
        "type": "inject",
        "z": "a352c8d5e5762386",
        "g": "ade9482578e6aaaf",
        "name": "Set",
        "props": [
            {
                "p": "password",
                "v": "PASSWORD",
                "vt": "env"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 370,
        "y": 120,
        "wires": [
            [
                "7e4ebd722824573c"
            ]
        ]
    },
    {
        "id": "93963d712825cebd",
        "type": "inject",
        "z": "a352c8d5e5762386",
        "g": "ade9482578e6aaaf",
        "name": "Remove",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 360,
        "y": 180,
        "wires": [
            [
                "2d090d386835921c"
            ]
        ]
    },
    {
        "id": "7e4ebd722824573c",
        "type": "postgresql",
        "z": "a352c8d5e5762386",
        "g": "ade9482578e6aaaf",
        "name": "Create table",
        "query": "CREATE TABLE exam_data (\n  roll_no bigint PRIMARY KEY,\n  firstname varchar(255) NOT NULL,  \n  lastname varchar(255) NOT NULL, \n  status varchar(50) NOT NULL\n);\n\n",
        "postgreSQLConfig": "501b346caeb59359",
        "split": false,
        "rowsPerMsg": 1,
        "outputs": 1,
        "x": 850,
        "y": 120,
        "wires": [
            []
        ]
    },
    {
        "id": "2d090d386835921c",
        "type": "postgresql",
        "z": "a352c8d5e5762386",
        "g": "ade9482578e6aaaf",
        "name": "Drop table",
        "query": "DROP TABLE exam_data;",
        "postgreSQLConfig": "501b346caeb59359",
        "split": false,
        "rowsPerMsg": 1,
        "outputs": 1,
        "x": 850,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "501b346caeb59359",
        "type": "postgreSQLConfig",
        "name": "Test DB",
        "host": "HOST",
        "hostFieldType": "env",
        "port": "5432",
        "portFieldType": "num",
        "database": "test",
        "databaseFieldType": "str",
        "ssl": "false",
        "sslFieldType": "bool",
        "applicationName": "",
        "applicationNameType": "str",
        "max": "10",
        "maxFieldType": "num",
        "idle": "1000",
        "idleFieldType": "num",
        "connectionTimeout": "10000",
        "connectionTimeoutFieldType": "num",
        "user": "demo",
        "userFieldType": "str",
        "password": "dfdfdfd",
        "passwordFieldType": "str"
    }
]

@Alkarex
Copy link
Member

Alkarex commented Feb 6, 2024

Have you tested that you can access your environment variable from a Change node? (cf. my previous message)

image

@sumitshinde-84
Copy link
Author

I have tested printing environment variables using the change node. Additionally, I attempted to set a default type for the PostgreSQL configuration inputs, hoping that the getField function would return the value of the corresponding environment variable. However, the issue persists, and the default value of '127.0.0.1' continues to be set.

@sumitshinde-84
Copy link
Author

node-red.7.mp4

@Alkarex
Copy link
Member

Alkarex commented Feb 6, 2024

Ah, there seems to be a confusion on the terminology. I was not even aware that Node-RED has recently (in version 3.1) added support for Global environment variables node-red/designs#76
https://nodered.org/blog/2023/09/06/version-3-1-released#global-environment-variables

Those are not environment variables, though. Environment variables, as understood in standard Linux or Windows, are defined before calling Node-RED, at OS or process level. Here is an example in Docker Compose:

  nodered:
    image: nodered/node-red
    environment:
      - PG_DB=iot

I have just tested and it seems to work as expected.

It should be possible to change the syntax to support both types, though:
https://nodered.org/docs/user-guide/environment-variables

@Alkarex Alkarex changed the title Add support for configuring node by env variables Support Node-RED Global environment variables Feb 6, 2024
@Steve-Mcl
Copy link

@Alkarex if you use the utility function RED.util.evaluateNodeProperty instead of accessing process.env[prop] I believe all eventualities are covered.

            let host
            RED.util.evaluateNodeProperty('host', 'env', node, {}, function (err, value) {
                if (err) {
                    return; // do something
                } else {
                    host = value;
                }
            });

Admittingly, that ^ is generally intended for evaluating inside of on('input', ...) of a node, but it works perfectly well for evaluating in a config.

alternatively, you may simply be able to call evaluateEnvProperty (not 100% certain this takes the new global env into account)

@Alkarex
Copy link
Member

Alkarex commented Feb 12, 2024

@Steve-Mcl Thanks! Would you feel like sending a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants