Docker Image with graalpy venv setup for pythonautomation addon

Context

This solution might be interesting for you if:

  1. You want to use the Python Scripting Automation (Python Scripting - Automation | openHAB) to implement some rules.
  2. 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)
  3. 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/.

It’s been a long time since I’ve done anything with Python and OH but I’m surprised by setting the venv in the userdata folder. I would have expected it to be in the conf folder, specifically conf/automation/python. Is there a reason to put it in userdata?

An alternative approach to building a new image would be to create a shell script that does all these steps and mount it to /etc/cont-init.d. All the scripts found there get executed when the container starts up before openHAB is launched.

Hi,

yeah I also wondered about the location in the userdata but it seems to be the default one (also described in the documentation Python Scripting - Automation | openHAB)

The idea with cont-init.d is also a good one - might also be much simpler …

The user home folder is userdata so that might be it. But there is no requirement to put a venv in the home folder. :person_shrugging:

The downside of cont-init.d is depending on what the scripts do it can add a good bit of time to the starting of the container, particularly with a new container. I have one such script that installs ffmpeg which has a ton of other packages so that first start after an upgrade is excruciatingly long.

I just wanted to point out that there is an alternative for those who may not want to build a custom image or use an unofficial image for some reason.