From 4cb2a4b24831a2be6c458db2b5ddfd1cf0c4f0c9 Mon Sep 17 00:00:00 2001 From: Dieter Schmidt Date: Fri, 23 Sep 2022 00:38:50 +0200 Subject: [PATCH] update docker build - BREAKING CHANGE: make printer.cfg the main config file, instead of klipper.cfg. existing configs need to be migrated: - copy all include statements from klipper.cfg to printer.cfg - remove klipper.cfg - add run_in_venv script to activate venv correctly, then start klipper/moonraker - add rpi_mcu config file and include in printer.cfg - add cap_add: SYS_NICE to docker-compose to run klipper_mcu - build & run klipper with python3 - precompile klipper C-Code in build stage, remove gcc from run image (https://www.klipper3d.org/Packaging.html) - remove python3 debian packages, globally install with pip instead --- config/host_mcu.cfg | 12 ++++++ config/klipper.cfg | 4 -- config/printer.cfg | 7 +++- docker-compose.yml | 9 ++-- klipper/{.config_linux => .config_linux_mcu} | 0 klipper/Dockerfile | 43 +++++++++++++------- klipper/builder.Dockerfile | 27 ++++++++---- klipper/klipper.ini | 4 +- klipper/klipper_mcu.ini | 3 +- klipper/moonraker.ini | 2 +- klipper/run_in_venv | 11 +++++ 11 files changed, 89 insertions(+), 33 deletions(-) create mode 100644 config/host_mcu.cfg delete mode 100644 config/klipper.cfg rename klipper/{.config_linux => .config_linux_mcu} (100%) create mode 100755 klipper/run_in_venv diff --git a/config/host_mcu.cfg b/config/host_mcu.cfg new file mode 100644 index 0000000..5ce0a7b --- /dev/null +++ b/config/host_mcu.cfg @@ -0,0 +1,12 @@ +# https://github.com/Klipper3d/klipper/blob/master/config/sample-raspberry-pi.cfg +# This file contains an example configuration with RPi as secondary mcu + +# See docs/Config_Reference.md for a description of parameters. + +# The rpi microcontroller is used as secondary. +# Typically, both the X and Y axes +# are connected to the main micro-controller. The rpi microcontroller is used +# on non time-critical functions such as enclosure sensors, additional fan or +# light sources +[mcu host] +serial: /tmp/klipper_host_mcu diff --git a/config/klipper.cfg b/config/klipper.cfg deleted file mode 100644 index f50c06f..0000000 --- a/config/klipper.cfg +++ /dev/null @@ -1,4 +0,0 @@ -[include printer.cfg] -[include client.cfg] -[include client_macros.cfg] -[include calibration_macros.cfg] diff --git a/config/printer.cfg b/config/printer.cfg index e611430..c9e7242 100644 --- a/config/printer.cfg +++ b/config/printer.cfg @@ -1,2 +1,7 @@ # This is a dummy config. -# Either mount your config from host like :/home/klippy/.config/printer.cfg or change the contents of this file in UI to match your printer! \ No newline at end of file +# Either mount your config from host like :/home/klippy/.config/printer.cfg or change the contents of this file in UI to match your printer! + +[include host_mcu.cfg] +[include client.cfg] +[include client_macros.cfg] +[include calibration_macros.cfg] diff --git a/docker-compose.yml b/docker-compose.yml index 7e1219b..963dcf9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,9 +6,12 @@ services: build: dockerfile: ./klipper/Dockerfile context: . - # args: - # DEVICE_GROUP: device - # DEVICE_GID: 987 + # if klipper cannot connect to the printer check permissions on rpi then add the group here + # group_add: + # - "996" + # - "998" + cap_add: + - SYS_NICE container_name: klipper ports: - 7125:7125 diff --git a/klipper/.config_linux b/klipper/.config_linux_mcu similarity index 100% rename from klipper/.config_linux rename to klipper/.config_linux_mcu diff --git a/klipper/Dockerfile b/klipper/Dockerfile index 1801c17..00f1e31 100644 --- a/klipper/Dockerfile +++ b/klipper/Dockerfile @@ -11,10 +11,26 @@ ARG MOONRAKER_VENV_DIR=${HOME}/moonraker-env RUN useradd -d ${HOME} -ms /bin/bash ${USER} RUN apt-get update && \ - apt-get install -y locales git sudo wget curl gzip tar python2 virtualenv python-dev libffi-dev build-essential libncurses-dev libusb-dev gpiod python3-virtualenv python3-dev libopenjp2-7 python3-libgpiod liblmdb-dev libsodium-dev + apt-get install -y \ + locales \ + git \ + sudo \ + wget \ + curl \ + gzip \ + tar \ + libffi-dev \ + build-essential \ + libncurses-dev \ + libusb-dev \ + gpiod \ + libopenjp2-7 \ + liblmdb-dev \ + libsodium-dev RUN sed -i -e 's/# en_GB.UTF-8 UTF-8/en_GB.UTF-8 UTF-8/' /etc/locale.gen RUN locale-gen +RUN python -m pip install pip -U ENV LC_ALL en_GB.UTF-8 ENV LANG en_GB.UTF-8 @@ -25,22 +41,25 @@ WORKDIR ${HOME} ### Klipper setup ### RUN git clone --single-branch --branch ${KLIPPER_BRANCH} https://github.com/Klipper3d/klipper.git klipper -RUN [ ! -d ${KLIPPER_VENV_DIR} ] && virtualenv -p python2 ${KLIPPER_VENV_DIR} -RUN ${KLIPPER_VENV_DIR}/bin/python -m pip install pip -U +RUN [ ! -d ${KLIPPER_VENV_DIR} ] && python3 -m venv ${KLIPPER_VENV_DIR} RUN ${KLIPPER_VENV_DIR}/bin/pip install wheel numpy -RUN ${KLIPPER_VENV_DIR}/bin/pip install -r klipper/scripts/klippy-requirements.txt -COPY klipper/.config_linux /home/klippy/klipper/.config +WORKDIR ${HOME}/klipper +RUN ${KLIPPER_VENV_DIR}/bin/pip install -r scripts/klippy-requirements.txt +RUN ${KLIPPER_VENV_DIR}/bin/python klippy/chelper/__init__.py +RUN ${KLIPPER_VENV_DIR}/bin/python -m compileall klippy -RUN cd ${HOME}/klipper && make +COPY klipper/.config_linux_mcu ./.config +RUN make +WORKDIR ${HOME} # Install moonraker RUN git clone --single-branch --branch ${MOONRAKER_BRANCH} https://github.com/Arksine/moonraker.git moonraker RUN [ ! -d ${MOONRAKER_VENV_DIR} ] && python3 -m venv ${MOONRAKER_VENV_DIR} -RUN ${MOONRAKER_VENV_DIR}/bin/python -m pip install pip -U RUN ${MOONRAKER_VENV_DIR}/bin/pip install wheel gpiod -RUN ${MOONRAKER_VENV_DIR}/bin/pip install -r moonraker/scripts/moonraker-requirements.txt +WORKDIR ${HOME}/moonraker +RUN ${MOONRAKER_VENV_DIR}/bin/pip install -r scripts/moonraker-requirements.txt FROM python:3.10-slim-bullseye as image @@ -49,7 +68,6 @@ ARG DEBIAN_FRONTEND=noninteractive RUN apt-get update && \ apt-get install -y \ curl \ - gcc \ git \ gpiod \ iproute2 \ @@ -61,10 +79,6 @@ RUN apt-get update && \ libssl-dev \ libtiff5 \ locales \ - python2 \ - python3-libgpiod \ - python3-numpy \ - python3-matplotlib \ rsync \ supervisor \ zlib1g-dev && \ @@ -95,7 +109,7 @@ RUN useradd --user-group --no-log-init --shell /bin/false -m -d ${HOME} ${USER} usermod -a -G dialout ${USER} && \ mkdir -p /var/log/supervisor && \ mkdir -p /var/log/klipper && chown ${USER}:${USER} /var/log/klipper ${HOME} && \ - pip install --no-cache supervisord-dependent-startup gpiod && \ + pip install --no-cache supervisord-dependent-startup gpiod numpy matplotlib && \ mkdir -p /usr/lib/python3 && \ ln -s /usr/local/lib/python3.10/site-packages /usr/lib/python3/dist-packages @@ -117,6 +131,7 @@ COPY --chown=${USER}:${USER} --from=builder ${KLIPPER_VENV_DIR} ${KLIPPER_VENV_D COPY --chown=${USER}:${USER} --from=builder ${HOME}/moonraker ${HOME}/moonraker COPY --chown=${USER}:${USER} --from=builder ${MOONRAKER_VENV_DIR} ${MOONRAKER_VENV_DIR} COPY --chown=${USER}:${USER} config ${CONFIG_DIR} +COPY --chown=${USER}:${USER} klipper/run_in_venv /usr/local/bin/run_in_venv COPY klipper/supervisord.conf /etc/supervisord/conf.d/supervisord.conf COPY klipper/*.ini /etc/supervisord/ diff --git a/klipper/builder.Dockerfile b/klipper/builder.Dockerfile index 71414cc..30b7d08 100644 --- a/klipper/builder.Dockerfile +++ b/klipper/builder.Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.10 +FROM python:3.10-bullseye ARG DEBIAN_FRONTEND=noninteractive ARG KLIPPER_BRANCH="master" @@ -7,18 +7,28 @@ ARG USER=klippy ARG HOME=/home/${USER} ARG KLIPPER_VENV_DIR=${HOME}/klippy-env -ENV PKGLIST="virtualenv python-dev libffi-dev build-essential" +ENV PKGLIST="libffi-dev build-essential" ENV PKGLIST="${PKGLIST} libncurses-dev" ENV PKGLIST="${PKGLIST} libusb-dev" ENV PKGLIST="${PKGLIST} avrdude gcc-avr binutils-avr avr-libc" ENV PKGLIST="${PKGLIST} stm32flash libnewlib-arm-none-eabi" -ENV PKGLIST="${PKGLIST} gcc-arm-none-eabi binutils-arm-none-eabi libusb-1.0" +ENV PKGLIST="${PKGLIST} gcc-arm-none-eabi binutils-arm-none-eabi libusb-1.0 pkg-config" RUN useradd -d ${HOME} -ms /bin/bash ${USER} RUN apt-get update && \ - apt-get install -y locales git sudo wget curl gzip tar ${PKGLIST} + apt-get install -y \ + locales \ + git \ + sudo \ + wget \ + curl \ + gzip \ + tar \ + ${PKGLIST} + RUN sed -i -e 's/# en_GB.UTF-8 UTF-8/en_GB.UTF-8 UTF-8/' /etc/locale.gen RUN locale-gen +RUN python -m pip install pip -U ENV LC_ALL en_GB.UTF-8 ENV LANG en_GB.UTF-8 @@ -29,10 +39,13 @@ WORKDIR ${HOME} ### Klipper setup ### RUN git clone --single-branch --branch ${KLIPPER_BRANCH} https://github.com/Klipper3d/klipper.git klipper -RUN [ ! -d ${KLIPPER_VENV_DIR} ] && virtualenv -p 2 ${KLIPPER_VENV_DIR} -RUN ${KLIPPER_VENV_DIR}/bin/python -m pip install pip -U +RUN [ ! -d ${KLIPPER_VENV_DIR} ] && python3 -m venv ${KLIPPER_VENV_DIR} RUN ${KLIPPER_VENV_DIR}/bin/pip install wheel -RUN ${KLIPPER_VENV_DIR}/bin/pip install -r klipper/scripts/klippy-requirements.txt + +WORKDIR ${HOME}/klipper +RUN ${KLIPPER_VENV_DIR}/bin/pip install -r scripts/klippy-requirements.txt +RUN ${KLIPPER_VENV_DIR}/bin/python klippy/chelper/__init__.py +RUN ${KLIPPER_VENV_DIR}/bin/python -m compileall klippy WORKDIR ${HOME}/klipper diff --git a/klipper/klipper.ini b/klipper/klipper.ini index 6611224..ddfcb49 100644 --- a/klipper/klipper.ini +++ b/klipper/klipper.ini @@ -1,8 +1,8 @@ [program:klipper] user=klippy -command=/home/klippy/klippy-env/bin/python /home/klippy/klipper/klippy/klippy.py -a /tmp/klippy_uds -l /var/log/klipper/klipper.log /home/klippy/.config/klipper.cfg -environment=USER=klippy,HOME=/home/klippy,PYTHONHOME=/home/klippy/klippy-env +command=run_in_venv /home/klippy/klippy-env python /home/klippy/klipper/klippy/klippy.py -a /tmp/klippy_uds -l /var/log/klipper/klipper.log /home/klippy/.config/printer.cfg +environment=USER=klippy,HOME=/home/klippy stdout_logfile=/dev/fd/1 stdout_logfile_maxbytes = 0 stderr_logfile=/dev/fd/2 diff --git a/klipper/klipper_mcu.ini b/klipper/klipper_mcu.ini index cd5a744..eefb5cd 100644 --- a/klipper/klipper_mcu.ini +++ b/klipper/klipper_mcu.ini @@ -6,4 +6,5 @@ stdout_logfile_maxbytes = 0 stderr_logfile=/dev/fd/2 stderr_logfile_maxbytes = 0 autorestart=true -autostart=true +autostart=false +dependent_startup=true diff --git a/klipper/moonraker.ini b/klipper/moonraker.ini index d28eaca..2a0f3f0 100644 --- a/klipper/moonraker.ini +++ b/klipper/moonraker.ini @@ -1,6 +1,6 @@ [program:moonraker] user=klippy -command=/home/klippy/moonraker-env/bin/python /home/klippy/moonraker/moonraker/moonraker.py -l /var/log/klipper/moonraker.log -c /home/klippy/.config/moonraker.conf +command=run_in_venv /home/klippy/moonraker-env python /home/klippy/moonraker/moonraker/moonraker.py -l /var/log/klipper/moonraker.log -c /home/klippy/.config/moonraker.conf environment=USER=klippy,HOME=/home/klippy stdout_logfile=/dev/fd/1 stdout_logfile_maxbytes = 0 diff --git a/klipper/run_in_venv b/klipper/run_in_venv new file mode 100755 index 0000000..c321440 --- /dev/null +++ b/klipper/run_in_venv @@ -0,0 +1,11 @@ +#!/bin/bash +VENV=$1 +if [ -z $VENV ]; then + echo "usage: run_in_venv [virtualenv_path] CMDS" + exit 1 +fi +. ${VENV}/bin/activate +shift 1 +echo "Executing $@ in ${VENV}" +exec "$@" +deactivate