How to add and call python codes in a custom storage volume
In addition to using the Cloud Function Compute (CFC), you can also realize it by adding codes you need in the custom storage volume and calling one of the methods.
Before the operations described herein are started, create a core according to Getting Started - Build a Core.
If you have already operated according to How to configure multiple functions in the Cloud Function Compute (CFC), you only need to create the two storage volumes of bie-person-compute-code
and bie-person-compute-conf
in [Custom Storage Volume](#Create a custom storage volume), and the previous storage volumes can be reused for others.
In this article, it takes Python 3.6 as an example for explanation.
Create a Storage Volume for Computing Services
Create a Custom Storage Volume
Enter the Baidu IntelliEdge storage volume page, and two custom storage volumes are required - bie-person-compute-code
and bie-person-compute-conf
, which are used to save code and configuration respectively.
bie-person-compute-codeStorage Volume
First, click “Create a storage volume” button on the storage volume page, select the template custom storage volume, name it bie-person-compute-code
, and click “OK”.
Enter the custom storage volume page, and select “Create an editable file”. Here, a total of 2 files have to be created.
Create the first file add2.py
, and the code content is as follows:
# -*- coding: utf-8 -*-
def add2(event, context):
result = {}
result['result'] = event['x'] + event['y'] + event['z']
return result
Save this file, and create anotheradd.py
file. The code content is as follows:
# -*- coding: utf-8 -*-
import add2
def add(event, context):
result = {}
result['result'] = event['x'] + event['y'] + event['z']
result['result2'] = add2.add2(event, context)['result']
return result
Two files -add.py
and add2.py
are created. Check that they are correct, and click “Release a new version”.
bie-person-compute-confStorage Volume
On the storage volume page, click “Create a storage volume” button, name it bie-person-compute-conf
, and select baetyl template configuration for the template.
The storage volume generates the service.yml
file for us by default. Click “Edit”, enter the following configuration and save it.
functions:
- name: 'add'
handler: 'add.add'
codedir: 'var/db/baetyl/compute'
- name: 'add2'
handler: 'add2.add2'
codedir: 'var/db/baetyl/compute'
Lastly, you have to release the new version.
baetyl-hubCreate 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-managerCreate a Storage Volume for Services
On the storage volume page, click “Create a storage volume” button, and select baetyl template configuration for the template.
Enter the bie-cfc-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: add
publish:
topic: t/add
- clientid: localfunc-2
subscribe:
topic: t
function:
name: add2
publish:
topic: t/add2
functions:
- name: add
service: function-compute
- name: add2
service: function-compute
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-hubService 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.
Mount the bie-cfc-func-conf
storage volume.
baetyl-function-python36 Service Configuration
Click “Add a service”, name it function-compute
, and select baetyl-function-python36
for the module.
Mount the bie-person-compute-conf
and bie-person-compute-code
for the storage volumes.
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.
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 {"x":1,"y":2,"z":3}
, and the result is shown in the figure. The output result is exactly the result computed in the functions add
and add2
in the custom storage volume.