百度智能云

All Product Document

          Intelligent Edge

          Storage Volume Predefined Macro

          What is a predefined macro

          When a storage volume file is configured (only limited to modules provided by the official platform), for the configuration file content that does not have to be displayed, you can write a predefined macro to replace it with an environment variable when the configuration is loaded.

          How to use the predefined macro

          You can write a predefined macro to replace the macro with an environment variable in the container system;

          E.g.: There are environment variables BAETYL=0.1.6 Write the following in the configuration file:

          {{.BAETYL}} 

          After the official module configuration is loaded, it will automatically resolve it to

          0.1.6

          Note: The macro format must be in ‘{{.}}’ format

          Typical Case

          Take two actual cases to demonstrate how to use the predefined macro.

          Case 1: Custom Environment Variables Dynamically Fill in the Predefined Macro

          Step1: create a storage volume with a predefined macro

          Take creating a configuration storage volume for the baetyl-hub module as an example, as shown in the figure below:

          image.png

          Edit the service.yml configuration file, and use the predefined macro {{.DEVICE_PUB}} in the configuration file, as shown below:

          listen: 
            - tcp://0.0.0.0:1883
          principals: 
            - username: test 
              password: hahaha 
              permissions: 
                - action: 'pub' 
                  permit: ['{{.DEVICE_PUB}}'] 
                - action: 'sub' 
                  permit: ['#'] 
          subscriptions: 
            - source: 
                topic: 't' 
              target: 
                topic: 't/topic' 
          logger: 
            path: var/log/baetyl/service.log 
            level: "debug" 

          {{.DEVICE_PUB}} is a predefined macro. After the configuration file is loaded, it will be dynamically filled in with the corresponding value

          Step2: Create an application template

          Create an application template, add a baetyl-hub module in the application template and set the environment variable DEVICE_PUB:ABC, as shown in the figure below:

          image.png

          The KEY value ABC in the environment variable DEVICE_PUB:ABC will be automatically injected into the predefined macro {{.DEVICE_PUB}}.

          Mount the configuration storage volume, as shown in the figure below:

          image.png

          Step3: Create a core based on the application template

          When a core is selected, select the application template created in step2, as shown in the figure below:

          image.png

          After the core is created, deploy the core to the edge device. For detailed steps, see Get ting Started

          Step4: Verify whether the predefined configuration is effective

          Use MQTT.fx to connect to the local hub. The MQTT.fx configuration is shown in the following figure:

          image.png

          Try to send a message to the topic t. Since there is no privilege, it is disconnected as expected, as shown in the figure below:

          image.png

          Try to send a message to the topic ABC, and it is sent successfully as expected, as shown in the figure below:

          image.png

          image.png

          Case 2: Use a System Environment Variable BAETYL_HOST_ID

          Use BAETYL_HOST_ID as the Log File Name

          BAETYL_HOST_ID You do not have to configure the environment variable. It is an environment variable automatically injected when the Baidu OpenEdge framework (official modules after version 0.1.6) starts the container. Its value on each device is different, and the same application template and storage volume configuration can have different effects on different core devices.

          In Mac, the value of this variable is kern.uuid; for values in more systems, please see https://github.com/shirou/gopsutil for the HostID setting.

          Step1: Create a storage volume with a predefined macro:

          image.png

          Fill in the configuration file and write the predefined macro:

          listen: 
            - tcp://0.0.0.0:1883
          principals: 
            - username: test 
              password: hahaha 
              permissions: 
                - action: 'pub' 
                  permit: ['#'] 
                - action: 'sub' 
                  permit: ['#'] 
          subscriptions: 
            - source: 
                topic: 't' 
              target: 
                topic: 't/topic' 
          logger: 
            path: var/log/baetyl/{{.BAETYL_HOST_ID}}.log 
            level: "debug" 

          Please note that {{.BAETYL_HOST_ID}} is an entered predefined macro. After the configuration file is loaded, it will be replaced with a corresponding value;

          Set the log file name as the predefined macro, and you have to create a storage volume with empty directory to view logs.

          Step2: Create an application template, and add a hub service

          image.png

          Please note that the environment variable BAETYL_HOST_IDdoes not have to be entered, and the system can add it;

          Mount the storage volume and save it:

          image.png

          The final result:

          image.png

          Step3: Create a core based on the template (you can also directly configure it in the core)

          Select the application template which was just created;

          image.png

          After it is created, download the V1 version configuration and start it;

          Step4: Verify whether the predefined configuration is effective

          Observe the log file name of the hub module, and find that the configuration is effective.

          image.png

          Use the BAETYL_HOST_ID as the MQTT ClientID

          When the remote mqtt module is used, if you have many cores which need to be connected to the same remote hub, when one configuration is shared, their clientId are the same, which can cause mutual kick-out. At this time, you can use the predefined macro {{.BAETYL_HOST_ID}} as the clientId, as shown below:

          hub: 
            address: tcp://localhub:1883 
            username: test 
            password: hahaha 
          remotes: 
            - name: iothub 
              address: 'ssl://xxxxxxxxxxxxxxx' 
              clientid: {{.BAETYL_HOST_ID}} 
              username: xxxxxxx/test 
              ca: var/db/baetyl/cert/ca.pem 
              cert: var/db/baetyl/cert/client.pem 
              key: var/db/baetyl/cert/client.key 
          rules: 
            - hub: 
                subscriptions: 
                  - topic: t 
              remote: 
                name: iothub 
                subscriptions: 
                  - topic: t/remote 
                    qos: 1 
          logger: 
            path: var/log/baetyl/service.log 
            level: "debug" 

          In this way, the clientId of the core on each machine connected to the remote hub is different.

          Previous
          Storage Volume Type
          Next
          How to upload and call third-party dependency source codes in a custom storage volume