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 upgrade. 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 parameter

    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" 
    }
    
  • Now use file deploy ( htps : // / cs. 에네부 r. 코 m / 쟈 / 후 ぇ s / 인 t 로즈 c 치온 ) to deploy the same file on device. Please name it as env.json
  • You need to restart the flow running on the device to update the configuration. You can do it on enebular.com using htps : // / cs. 에네부 r. 이 m/그럼/로 ゔぃせ/FぉwS 다츠 s. HTML


  • 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: htps : // 에네부 r. 코 m/ぢs 코ゔぇr/fぉw/848265에9-7아30-485d-9526-d8에8d8c2cd9
  • Deploy the flow.

  • Result:


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

  • 좋은 웹페이지 즐겨찾기