Setting device specific config for nodes using file deploy


Introduction

In a lot of projects, device specific configuration like AWS-Credentials or device name are saved in file and needs to be used in flows. But since it's not possible for all nodes to access these configuration parameters through flow, node-red provides a concept called environment variables that can be accessed from a flow. Current version of Node-red used in enebular does not support the environment variables. This is a work around process that lets the user read environment variables till enebular Node-red version is upgraded. The steps below will modify enebular-runtime-agent (compiled code) to read a env.json file which can be deployed through enebular's file deploy.

Steps to be followed

Step 1

  • Use a gateway device that has enebular agent installed
  • On the gateway device, go to terminal and type the following command
  • sudo nano /home/enebular/enebular-runtime-agent/agent/lib/node-red-controller.js

Step 2:

  • Find the place to update the code for environment variables
  • Press 'ctrl + w' and type 'env' and Press 'enter'
  • You'll find code like below: let env = Object.assign(process.env, { ENEBULAR_ASSETS_DATA_PATH: this._assetsDataPath });
  • Update the code to below:
let env = Object.assign(process.env, JSON.parse(_fs.default.readFileSync(this._assetsDataPath+'/env.json'))); 
env['ENEBULAR_ASSETS_DATA_PATH'] = this._assetsDataPath;
  • Type (ctrl + x) and press enter and on prompt, type Y and enter.
  • The above code will let enebular flow read an env.json stored at the 'file assets location', parse it and add the configuration parameters into environment variables to be passed to enebular-node-red.

Step 3: (Optional: File deploy can be used)

  • Add a sample file at env.json at '/home/enebular/enebular-runtime-agent/ports/awsiot/assets/' using the command sudo nano /home/enebular/enebular-runtime-agent/ports/awsiot/assets/env.json
  • Add the following { "DeviceName":"test1" }
  • Type (ctrl + x) and press enter. On prompt, type Y and enter.

Step 4:

  • Restart the enebular agent using the command to let the changes take effect: sudo service enbular-agent-enebular restart

This will now let your new flow use the environment variables to access configuration parameters stored in json file on each device. This is a powerful tool since you can now use same flow on various devices that are working with device specific settings/configuration parameters.

Step 5: Updating the list.

  • Create a json file. Below is an example of the same.
{ 
"name":"satya", 
"email": "[email protected]", 
"AWS-accessKey": "AKIAYO*******77CRC3", 
"AWS-secretKey": "zcwOoCro0c***********Muew13W9756HzAg", 
"AWS-region": "ap-northeast-1", 
"AWS-bucket": "eric-farming-project" 
}

Step 6: Using these configuration in a flow. We'll use AWS node as an example

  • Any node can access the above variables using $(ENV_NAME). For example, in case of aws-config node. Even the secret access key is $(AWS-secretKey).

  • Install the following AWS-S3 package: node-red-node-aws

  • Adding the configuration in AWS-Node

  • Completing the flow:

  • You can also get the flow from enebular-discover: https://enebular.com/discover/flow/848265e9-7a30-485d-9526-d8e8dd8c2cd9

  • Deploy the flow.

Result:

  • The above flow sends env.json to S3 bucket named eric-farming-project.