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
After downloading, you can find there are multiple compression packages ending in .whl in the folder.
Decompress the .whl compression packages to get the source code.
unzip -d . "*.whl"
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
Compress the index.py
and 5 source code files into a ZIP package, and later upload it to the custom storage volume.
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.
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.
Enter the custom storage volume page, and select “Upload a storage volume ZIP package”.
Select the compressed ZIP package in the previous step.
After it is uploaded, confirm whether there are any omissions in the code-related files.
After confirming it is OK, click “Release a new version”.
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.
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'
Lastly, you have to release the new version.
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.
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"
Similarly, you have to release the new version.
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.
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.
Mount three storage volumes - bie-demo-hub-conf
, bie-demo-hub-log
and bie-demo-hub-data
.
Finally, you can see that a total of three storage volumes are mounted.
baetyl-function-manager Service Configuration
Click “Add a service”, name it local-function-manager
, and select baetyl-function-manager
for the module.
Select to mount the bie-upload-func-conf
storage volume.
Complete the mounting.
baetyl-function-python36 Service Configuration
Click “Add a service”, name it function-request
, and select baetyl-function-python36
as module.
Mount the bie-upload-code-conf
and bie-upload-code
for the storage volumes.
Complete to mount the storage volume:
View the Final Configuration Result and Distribute it
The service configuration is completed, and our core services are as follows:
Check that the service configuration is correct, and click “Release” button to release this version.
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.
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/.