How to extend the dependencies of code blocks
Persistence of Dependency Libraries
The following operations need to be performed while the service is running and only need to be completed once. There is no need to repeat these steps when installing additional extension libraries.
-
Create a mount directory for the dependency library (the actual directory can be customized), for example:
mkdir -p /data/hap/script/volume/command/package/python-3.6/
mkdir -p /data/hap/script/volume/command/package/nodejs-10.18.0/ -
Get the pre-installed dependency libraries
- Microservice version>=5.1.0
- Microservice version<5.1.0
docker cp $(docker ps | grep command | awk '{print $1}'):/usr/local/lib/python3.6/site-packages/ /data/hap/script/volume/command/package/python-3.6/
docker cp $(docker ps | grep command | awk '{print $1}'):/usr/local/node-10.18.0/lib/node_modules/ /data/hap/script/volume/command/package/nodejs-10.18.0/docker cp $(docker ps | grep community | awk '{print $1}'):/usr/local/lib/python3.6/site-packages/ /data/hap/script/volume/command/package/python-3.6/
docker cp $(docker ps | grep community | awk '{print $1}'):/usr/local/node-10.18.0/lib/node_modules/ /data/hap/script/volume/command/package/nodejs-10.18.0/ -
Modify the
docker-compose.yaml
file to mount the host's dependency library directory into the container- Microservice version本>=5.1.0
- Microservice version<5.1.0
services:
command:
volumes:
- ./volume/command/package/python-3.6/site-packages/:/usr/local/lib/python3.6/site-packages/
- ./volume/command/package/nodejs-10.18.0/node_modules/:/usr/local/node-10.18.0/lib/node_modules/services:
app:
volumes:
- ./volume/command/package/python-3.6/site-packages/:/usr/local/lib/python3.6/site-packages/
- ./volume/command/package/nodejs-10.18.0/node_modules/:/usr/local/node-10.18.0/lib/node_modules/ -
Execute the following command in the root directory of the manager and restart the service
bash ./service.sh restartall
Online Installation of Extension Library
- You must complete the "Persistence of Dependency Libraries" steps above first, mounting the directory of dependency libraries in the container to the host, otherwise the extension libraries will need to be reinstalled after container restart.
- Online installation of extension libraries requires ensuring that the server can access the internet.
Python
Taking the installation of the python-dateutil
extension library as an example:
- Microservice version>=5.1.0
- Microservice version<5.1.0
-
Enter the command container
docker exec -it $(docker ps | grep command | awk '{print $1}') bash
-
Install the
python-dateutil
extension library in the command containerpip3 install --target=/usr/local/lib/python3.6/site-packages/ python-dateutil
-
Enter the community container
docker exec -it $(docker ps | grep hap-community | awk '{print $1}') bash
-
Install the
python-dateutil
extension library in the community containerpip3 install --target=/usr/local/lib/python3.6/site-packages/ python-dateutil
After installation, you can find the newly installed extension library in the default path /data/hap/script/volume/command/package/python-3.6/site-packages/
on the host.
JavaScript (Based on Nodejs)
Taking the installation of the dayjs
extension library as an example:
- Microservice version>=5.1.0
- Microservice version<5.1.0
-
Enter the command container
docker exec -it $(docker ps | grep command | awk '{print $1}') bash
-
Install the
dayjs
extension library in the command container/usr/local/node-10.18.0/bin/npm -g install dayjs
-
Enter the community container
docker exec -it $(docker ps | grep hap-community | awk '{print $1}') bash
-
Install the
dayjs
extension library in the community container/usr/local/node-10.18.0/bin/npm -g install dayjs
After installation, you can find the newly installed extension library in the default path /data/hap/script/volume/command/package/nodejs-10.18.0/node_modules/
on the host.
Offline Installation of Extension Library
- You must first complete the "Persistence of Dependency Libraries" steps above to persistently mount the directory of dependency libraries in the container to the host. Otherwise, the extension library will need to be reinstalled after the container restarts.
- This process is applicable when the server cannot access the internet or when offline packages are provided for the extension library.
Python
Taking the installation of the pycryptodome
extension library as an example:
-
When internet access is available, get the offline file of the extension library
pip3 download pycryptodome -d "/package"
- An offline file for the pycryptodome dependency library will be generated in the /package directory.
-
Upload the offline file to the server where the HAP service is located
-
Copy the offline file into the container and install it
- Microservice version>=5.1.0
- Microservice version<5.1.0
-
Copy the offline file of the pycryptodome dependency library to the /tmp path in the command container
docker cp pycryptodome-3.15.0-cp35-abi3-manylinux1_x86_64.whl $(docker ps | grep command | awk '{print $1}'):/tmp
-
Enter the command container
docker exec -it $(docker ps | grep command | awk '{print $1}') bash
-
Install the offline package of the pycryptodome extension library in the command container
pip3 install --target=/usr/local/lib/python3.6/site-packages/ /tmp/pycryptodome-3.15.0-cp35-abi3-manylinux1_x86_64.whl
-
Copy the offline file of the pycryptodome dependency library to the /tmp path in the community container
docker cp pycryptodome-3.15.0-cp35-abi3-manylinux1_x86_64.whl $(docker ps | grep community | awk '{print $1}'):/tmp
-
Enter the community container
docker exec -it $(docker ps | grep community | awk '{print $1}') bash
-
Install the offline package of the pycryptodome extension library in the community container
pip3 install --target=/usr/local/lib/python3.6/site-packages/ /tmp/pycryptodome-3.15.0-cp35-abi3-manylinux1_x86_64.whl
JavaScript (based on Nodejs)
Taking the installation of the dayjs
extension library as an example:
-
When internet access is available, get the offline files for the extension library
npm install dayjs -save
- This will generate a directory
node_modules/dayjs
for the dependency library in the current directory
- This will generate a directory
-
Package the directory for the dayjs extension library
cd node_modules
tar czf dayjs.tar.gz dayjs -
Upload the packaged file
dayjs.tar.gz
of the extension library to the server where the HAP service is running. -
Unzip the offline files to the path where the dependency library is operated for persistence.
tar xf dayjs.tar.gz -C /data/hap/script/volume/command/package/nodejs-10.18.0/node_modules
- If the default data path in HAP has been modified, the unzip path should be based on the actual environment path.