Context
This solution might be interesting for you if:
- You want to use the Python Scripting Automation (Python Scripting - Automation | openHAB) to implement some rules.
- You need some packages from pip (In that case you need your own venv for graalpy initialized- as described in the documentation of the automation)
- You run OpenHAB in a container
Problem
The naive way to add the venv is to prepare it in the userdata folder from some other machine (i.e. the host running the container) as described in the documtation, in addition to that also the graalpy directoy needs to be mounted into the container with the same path (as the venv uses symlinks to it).
Initially this approach worked fine but I discovered the following issues:
- “patchelf” seems not to be “so optional” - I had multiple errors that rules couldn’t be loaded as patchelf was not found (and patchelf is not installed in the official openhab images)
- I looked for a solution that could avoid manual manipulation of the userdata folder when a new OH instance get’s deployed - i.e. if I need to setup a fresh OH instance for testing.
Solution
To solve this issues I decided to built my own OH docker image based on the official OH image. This image basically adds the following stuff to the official image:
- install patchelf
- download graalpy and add it to the container
- initialize a venv on container startup in the userdata folder (if not there already)
In addition I also added some automated tests to ensure that with that image the python automation can be used with external packages installed via pip without additional tweaks.
If you are interested in it: Dockerfile and tooling can be found here GitHub - christian-edelsbrunner/openhab-docker-graalpy-venv: OpenHab docker image that automatically create a python venv using graalpy ready to be used with https://www.openhab.org/addons/automation/pythonscripting/.