百度智能云

All Product Document

          Intelligent Edge

          How to upload and call third-party dependency source codes in a custom storage volume

          When functions are written, third-party dependencies are often referenced to. This article introduces how to upload third-party dependencies to the cloud storage volume and distribute them to the device accompanying the storage volume.

          Before the operations described herein are started, create a core according to Getting Started - Build a Core.

          In this article, it takes Python 3.6 as an example for explanation.

          Create a Custom Storage Volume

          Develop a Code

          Code has to be developed and tested on the development machine before uploaded to the custom storage volume.

          The example is to get the headers information of https://baetyl.io/. Create a folder, and name the code file ‘index.py’. The code is as follows:

          #!/usr/bin/env python3 
          # -*- coding: utf-8 -*- 
          
          import requests 
          
          def handler(event, context): 
            """
            data: {"action": "A"} 
            """
            if 'action' in event: 
              if event['action'] == 'A': 
                r = requests.get('https://baetyl.io/') 
                if str(r.status_code) == '200': 
                  event['info'] = dict(r.headers) 
                else: 
                  event['info'] = 'exception found' 
              else:      event['info'] = 'action error' 
            else: 
              event['error'] = 'action not found' 
          
            return event 

          You can see that the requests database is used. Next, download the source code of requests to this folder. Enter the following in the command line:

          pip3 download requests 

          download-requests01.png

          After downloading, you can find there are multiple compression packages ending in .whl in the folder.

          download-requests02.png

          Decompress the .whl compression packages to get the source code.

          unzip -d . "*.whl" 

          download-requests03.png

          In this way, the source code is obtained, but the compression packages and .dist-info are not what you need. To minimize the file size, you can delete these files and enter the following in the command line:

          rm -rf *.whl *.dist-info 

          download-requests04.png

          Compress the index.py and 5 source code files into a ZIP package, and later upload it to the custom storage volume.

          download-request06.png

          Lastly, perform a simple test to see if index.py can correctly output what you want.

          Create a test.py file in this folder, and the code content is as follows:

          import index 
          
          event = {} 
          event['action'] = "A" 
          print(index.handler(event, "")) 

          You can see that it correctly outputs the handler content that you want. After the test, you can delete this file.

          download-requests05.png

          Create a Custom Storage Volume

          Enter the Baidu IntelliEdge storage volume page, and two custom storage volumes are required - bie-upload-code and bie-upload-code-conf, which are used to save code and configuration respectively.

          bie-upload-code Storage Volume

          First, click “Create a storage volume” on the storage volume page, select the template custom storage volume, name it bie-upload-code, and click OK.

          image.png

          Enter the custom storage volume page, and select “Upload a storage volume ZIP package”.

          image.png

          Select the compressed ZIP package in the previous step.

          image.png

          After it is uploaded, confirm whether there are any omissions in the code-related files.

          image.png

          After confirming it is OK, click “Release a new version”.

          image.png

          bie-upload-code-conf Storage Volume

          On the storage volume page, click “Create a storage volume”, name it bie-upload-code-conf , and select baetyl template configuration for the template.

          image.png

          The storage volume generates the service.yml file for us by default. Click “View”, enter the following configuration and save it.

          functions: 
            - name: 'handler' 
              handler: 'index.handler' 
              codedir: 'var/db/baetyl/request' 

          image.png

          Lastly, you have to release the new version.

          image.png

          baetyl-hub Create a Storage Volume for Services

          There are three storage volumes, namely:

          • bie-demo-hub-conf
          • bie-demo-hub-log
          • bie-demo-hub-data

          For details, see Release and Distribute a Configuration. You only need to create these three storage volumes.

          baetyl-function-manager Create a Storage Volume for Services

          On the storage volume page, click “Create a storage volume”, name the storage volume bie-upload-code-conf, and select baetyl template configuration for the template.

          image.png

          Enter the bie-upload-func-conf storage volume, modify the service.yml, enter the following configuration and save it.

          hub: 
            address: tcp://local-hub:1883 
            username: test 
            password: hahaha 
          rules: 
            - clientid: localfunc-1 
              subscribe: 
                topic: t 
              function: 
                name: handler 
              publish: 
                topic: t/handler 
          functions: 
            - name: handler 
              service: function-request 
          logger: 
            path: var/log/baetyl/service.log 
            level: "debug" 

          image.png

          Similarly, you have to release the new version.

          image.png

          Add/Create a Service

          All the 6 storage volumes to be used are created.

          Then, enter the Baidu IntelliEdge core page, select the core created in our first step, and configure it.

          baetyl-hub Service Configuration

          Click “Add a service” button.

          image.png

          Name it local-hub, select baetyl-hub as the module, enter 1883:1883 for port mapping, and then click storage volume mounting to mount the three corresponding storage volumes.

          It is not mandatory for the mapping port to be 1883, but the specific mapping port should be consistent with the configuration file of each service. For example, for tcp connection, the local-hub service monitors the port 8883, and when other services establish connection with the local-hub service, set the connection port to be 8883.

          image.png

          image.png

          Mount three storage volumes - bie-demo-hub-conf, bie-demo-hub-log and bie-demo-hub-data.

          image.png

          image.png

          image.png

          Finally, you can see that a total of three storage volumes are mounted.

          cfc-core-hub-create08.png

          baetyl-function-manager Service Configuration

          Click “Add a service”, name it local-function-manager, and select baetyl-function-manager for the module.

          image.png

          Select to mount the bie-upload-func-conf storage volume.

          image.png

          Complete the mounting.

          image.png

          baetyl-function-python36 Service Configuration

          Click “Add a service”, name it function-request, and select baetyl-function-python36 as module.

          image.png

          Mount the bie-upload-code-conf and bie-upload-code for the storage volumes.

          image.png image.png

          Complete to mount the storage volume:

          image.png

          View the Final Configuration Result and Distribute it

          The service configuration is completed, and our core services are as follows:

          image.png

          Check that the service configuration is correct, and click “Release” button to release this version.

          image.png

          After the version is released, you can see that the latest version of the core is now the “v2” version.

          If the core has been downloaded and started, click “Distribute” button directly, and wait for restart to upgrade.

          If the core has not been downloaded, you can click “Download” button. Note that the master program should also be selected to download. For the startup steps, please see Getting Started.

          image.png

          Lastly, use mqttbox to send messages for testing. For the use of mqttbox, please see [Use MQTTBox to Subscribe Data Check Results](BIE/Operation Guide/Core Management/Typical Configuration.md#Use MQTTBox to Subscribe Data Check Results). Our input example is {"action":“A”}, and the result is shown in the figure. The output result is exactly the header content of the request https://baetyl.io/.

          66ED6AB5-531E-412F-B8DA-1FBD23E6E445.jpg

          Previous
          Storage Volume Predefined Macro
          Next
          How to add and call python codes in a custom storage volume