From 4e2b713065b71ad8e04cfae39203efe94fb63fb6 Mon Sep 17 00:00:00 2001 From: Lysann Schlegel Date: Fri, 6 Jan 2023 22:59:36 +0100 Subject: [PATCH] Convert to integration/docker image so: - removed addon & bashio dependencies - added the needed S6 dependency directly instead of via hassioaddons/base:8.0.6 --- plejd/Dockerfile | 57 +++++++++++++++++++++++-- plejd/PlejdDeviceCommunication.js | 2 +- plejd/addon-base-rootfs/usr/bin/service | 33 ++++++++++++++ plejd/rootfs/etc/services.d/plejd/run | 10 ++--- plejd/rootfs/usr/bin/plejd.sh | 4 +- 5 files changed, 95 insertions(+), 11 deletions(-) create mode 100755 plejd/addon-base-rootfs/usr/bin/service diff --git a/plejd/Dockerfile b/plejd/Dockerfile index 89cef69..fd09906 100644 --- a/plejd/Dockerfile +++ b/plejd/Dockerfile @@ -1,7 +1,55 @@ -ARG BUILD_FROM=hassioaddons/base:8.0.6 -FROM $BUILD_FROM +ARG BUILD_FROM=alpine:3.12.1 +## hadolint ignore=DL3006 +FROM ${BUILD_FROM} -ENV LANG C.UTF-8 +# Environment variables +ENV \ + HOME="/root" \ + LANG="C.UTF-8" \ + PS1="$(whoami)@$(hostname):$(pwd)$ " \ + S6_BEHAVIOUR_IF_STAGE2_FAILS=2 \ + S6_CMD_WAIT_FOR_SERVICES=1 \ + TERM="xterm-256color" + +# Copy root filesystem +COPY addon-base-rootfs / + +# Set shell +SHELL ["/bin/ash", "-o", "pipefail", "-c"] + +# Install base system +ARG BUILD_ARCH=armv7 +RUN \ + set -o pipefail \ + \ + && echo '@edge http://dl-cdn.alpinelinux.org/alpine/edge/main' >> /etc/apk/repositories \ + && echo '@edge http://dl-cdn.alpinelinux.org/alpine/edge/community' >> /etc/apk/repositories \ + && echo '@edge http://dl-cdn.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories \ + \ + && apk add --no-cache \ + libcrypto1.1=1.1.1g-r0 \ + libssl1.1=1.1.1g-r0 \ + musl-utils=1.1.24-r10 \ + musl=1.1.24-r10 \ + \ + && apk add --no-cache \ + bash=5.0.17-r0 \ + curl=7.79.1-r1 \ + jq=1.6-r1 \ + tzdata=2022a-r0 \ + \ + && S6_ARCH="${BUILD_ARCH}" \ + && if [ "${BUILD_ARCH}" = "i386" ]; then S6_ARCH="x86"; fi \ + && if [ "${BUILD_ARCH}" = "armv7" ]; then S6_ARCH="arm"; fi \ + \ + && curl -L -s "https://github.com/just-containers/s6-overlay/releases/download/v2.1.0.2/s6-overlay-${S6_ARCH}.tar.gz" \ + | tar zxvf - -C / \ + \ + && mkdir -p /etc/fix-attrs.d \ + && mkdir -p /etc/services.d + +# Entrypoint & CMD +ENTRYPOINT ["/init"] # Set shell SHELL ["/bin/bash", "-o", "pipefail", "-c"] @@ -45,6 +93,9 @@ RUN npm install \ # Copy root filesystem COPY rootfs / +# Set up empty /data/options.json +RUN mkdir -p /data && echo "{}" > /data/options.json + # Build arguments ARG BUILD_DATE ARG BUILD_REF diff --git a/plejd/PlejdDeviceCommunication.js b/plejd/PlejdDeviceCommunication.js index 9b38604..7178892 100644 --- a/plejd/PlejdDeviceCommunication.js +++ b/plejd/PlejdDeviceCommunication.js @@ -71,7 +71,7 @@ class PlejdDeviceCommunication extends EventEmitter { await this.plejdBleHandler.init(); } catch (err) { - logger.error('Failed init() of BLE. Starting reconnect loop.'); + logger.error('Failed init() of BLE. Starting reconnect loop.', err); await this.plejdBleHandler.startReconnectPeriodicallyLoop(); } } diff --git a/plejd/addon-base-rootfs/usr/bin/service b/plejd/addon-base-rootfs/usr/bin/service new file mode 100755 index 0000000..ff2f8db --- /dev/null +++ b/plejd/addon-base-rootfs/usr/bin/service @@ -0,0 +1,33 @@ +#!/bin/bash +# ============================================================================== +# Home Assistant Community Add-on: Base Images +# This script patches all service commands into the appropriate s6- commands +# Why not systemd? Docker said no, so did we ;) +# ============================================================================== + +start() { + s6-svc -wU -u -T2500 "/var/run/s6/services/${service}" +} + +stop() { + s6-svc -wD -d -T2500 "/var/run/s6/services/${service}" +} + +restart() { + stop + start +} + +status() { + s6-svstat "/var/run/s6/services/${service}" +} + +service="$1" +command="$2" + +if [[ ! -d "/var/run/s6/services/${service}" ]] ; then + echo "s6 service not found for ${service}, exiting..." + exit +fi; + +${command} "${service}" diff --git a/plejd/rootfs/etc/services.d/plejd/run b/plejd/rootfs/etc/services.d/plejd/run index 14ef936..f02f857 100644 --- a/plejd/rootfs/etc/services.d/plejd/run +++ b/plejd/rootfs/etc/services.d/plejd/run @@ -1,17 +1,17 @@ -#!/usr/bin/with-contenv bashio +#!/bin/bash # ============================================================================== # Community Hass.io Add-ons: Plejd # Runs the Plejd service # ============================================================================== -bashio::log.info 'Starting the Plejd service...' +echo 'Starting the Plejd service...' # Change working directory -cd /plejd || bashio::exit.nok 'Unable to change working directory' +cd /plejd || (>&2 echo 'Unable to change working directory'; exit $ERRCODE) # Run the Plejd service -bashio::log.info 'Verified permissions on startup script' +echo 'Verifying permissions on startup script' chmod +x /usr/bin/plejd.sh -bashio::log.info 'Executing startup script' +echo 'Executing startup script' exec /usr/bin/plejd.sh diff --git a/plejd/rootfs/usr/bin/plejd.sh b/plejd/rootfs/usr/bin/plejd.sh index 627843e..9e613a4 100644 --- a/plejd/rootfs/usr/bin/plejd.sh +++ b/plejd/rootfs/usr/bin/plejd.sh @@ -1,5 +1,5 @@ -#!/usr/bin/with-contenv bashio +#!/bin/bash -bashio::log.info 'Running add-on' +echo 'Running add-on' exec node /plejd/main.js