From b1bfed4a2d0a0199f53d672b0ab0f592a2e6f170 Mon Sep 17 00:00:00 2001 From: nyyu Date: Tue, 30 Apr 2024 08:31:10 +0200 Subject: [PATCH] feat: init --- Dockerfile | 48 +++++++++++++++ rootfs/defaults/.npmrc | 7 +++ rootfs/defaults/startup.sh | 13 ++++ .../s6-rc.d/homebridge-log/dependencies | 2 + .../etc/s6-overlay/s6-rc.d/homebridge-log/run | 5 ++ .../s6-overlay/s6-rc.d/homebridge-log/type | 1 + .../s6-rc.d/homebridge/dependencies | 2 + rootfs/etc/s6-overlay/s6-rc.d/homebridge/run | 11 ++++ rootfs/etc/s6-overlay/s6-rc.d/homebridge/type | 1 + rootfs/etc/s6-overlay/s6-rc.d/setup/type | 1 + rootfs/etc/s6-overlay/s6-rc.d/setup/up | 1 + .../s6-rc.d/user/contents.d/homebridge | 0 .../s6-rc.d/user/contents.d/homebridge-log | 0 .../s6-overlay/s6-rc.d/userdata/dependencies | 1 + rootfs/etc/s6-overlay/s6-rc.d/userdata/type | 1 + rootfs/etc/s6-overlay/s6-rc.d/userdata/up | 1 + rootfs/etc/s6-overlay/scripts/setup.sh | 61 +++++++++++++++++++ rootfs/etc/s6-overlay/scripts/userdata.sh | 15 +++++ .../system/homebridge.service.d/override.conf | 5 ++ 19 files changed, 176 insertions(+) create mode 100644 Dockerfile create mode 100644 rootfs/defaults/.npmrc create mode 100755 rootfs/defaults/startup.sh create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/dependencies create mode 100755 rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/run create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/type create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/homebridge/dependencies create mode 100755 rootfs/etc/s6-overlay/s6-rc.d/homebridge/run create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/homebridge/type create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/setup/type create mode 100755 rootfs/etc/s6-overlay/s6-rc.d/setup/up create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/homebridge create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/homebridge-log create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/userdata/dependencies create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/userdata/type create mode 100755 rootfs/etc/s6-overlay/s6-rc.d/userdata/up create mode 100755 rootfs/etc/s6-overlay/scripts/setup.sh create mode 100755 rootfs/etc/s6-overlay/scripts/userdata.sh create mode 100644 rootfs/etc/systemd/system/homebridge.service.d/override.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..23652a9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,48 @@ +FROM debian:stable-slim + +ENV S6_OVERLAY_VERSION=3.1.6.2 \ + S6_CMD_WAIT_FOR_SERVICES_MAXTIME=0 \ + S6_KEEP_ENV=1 \ + USER=root \ + HOMEBRIDGE_PKG_VERSION=1.2.0 \ + HOMEBRIDGE_APT_PACKAGE=1 \ + UIX_CUSTOM_PLUGIN_PATH="/var/lib/homebridge/node_modules" \ + PATH="/opt/homebridge/bin:/var/lib/homebridge/node_modules/.bin:$PATH" \ + HOME="/home/homebridge" \ + npm_config_prefix=/opt/homebridge + +RUN apt-get update && apt-get upgrade \ + && apt-get install --no-install-recommends -y curl psmisc procps iputils-ping \ + ca-certificates jq net-tools xz-utils \ + && chmod 4755 /bin/ping \ + && apt-get clean \ + && rm -rf /tmp/* /var/lib/apt/lists/* /var/tmp/* \ + && case "$(uname -m)" in \ + x86_64) S6_ARCH='x86_64';; \ + armv7l) S6_ARCH='armhf';; \ + aarch64) S6_ARCH='aarch64';; \ + *) echo "unsupported architecture"; exit 1 ;; \ + esac \ + && curl -SLf https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-noarch.tar.xz | tar -C / -Jxpf - \ + && curl -SLf https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-${S6_ARCH}.tar.xz | tar -C / -Jxpf - \ + && case "$(uname -m)" in \ + x86_64) DEB_ARCH='amd64';; \ + armv7l) DEB_ARCH='armhf';; \ + aarch64) DEB_ARCH='arm64';; \ + *) echo "unsupported architecture"; exit 1 ;; \ + esac \ + && curl -sSLf -o /homebridge_${HOMEBRIDGE_PKG_VERSION}.deb https://github.com/homebridge/homebridge-apt-pkg/releases/download/${HOMEBRIDGE_PKG_VERSION}/homebridge_${HOMEBRIDGE_PKG_VERSION}_${DEB_ARCH}.deb \ + && dpkg --force-all -i /homebridge_${HOMEBRIDGE_PKG_VERSION}.deb \ + && rm -rf /homebridge_${HOMEBRIDGE_PKG_VERSION}.deb \ + && chown -R root:root /opt/homebridge \ + && rm -rf /var/lib/homebridge \ + && rm -rf /opt/homebridge/include + # && find /opt/homebridge -regextype posix-egrep -regex ".*/node_modules/.*/(src|source|build|\.github|.*\.map)" -exec rm -rf {} +; + +COPY rootfs / + +EXPOSE 8581/tcp +VOLUME /homebridge +WORKDIR /homebridge + +ENTRYPOINT [ "/init" ] diff --git a/rootfs/defaults/.npmrc b/rootfs/defaults/.npmrc new file mode 100644 index 0000000..a5a6626 --- /dev/null +++ b/rootfs/defaults/.npmrc @@ -0,0 +1,7 @@ +audit = false +auto-install-peers = true +fund = false +global-style = true +loglevel = "error" +update-notifier = false +package-lock = false \ No newline at end of file diff --git a/rootfs/defaults/startup.sh b/rootfs/defaults/startup.sh new file mode 100755 index 0000000..5bd9a91 --- /dev/null +++ b/rootfs/defaults/startup.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# +# Docker Homebridge Custom Startup Script - oznu/homebridge +# +# This script can be used to customise the environment and will be executed as +# the root user each time the container starts. +# +# Example installing packages: +# +# apt-get update +# apt-get install -y python3 +# diff --git a/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/dependencies b/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/dependencies new file mode 100644 index 0000000..1208f18 --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/dependencies @@ -0,0 +1,2 @@ +setup +homebridge \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/run b/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/run new file mode 100755 index 0000000..d5b2d4a --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/run @@ -0,0 +1,5 @@ +#!/command/with-contenv sh + +[ -e /homebridge/homebridge.log ] || touch /homebridge/homebridge.log + +exec tail -f --follow=name /homebridge/homebridge.log diff --git a/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/type b/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/type new file mode 100644 index 0000000..1780f9f --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/homebridge-log/type @@ -0,0 +1 @@ +longrun \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/homebridge/dependencies b/rootfs/etc/s6-overlay/s6-rc.d/homebridge/dependencies new file mode 100644 index 0000000..b6a78b2 --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/homebridge/dependencies @@ -0,0 +1,2 @@ +userdata +setup diff --git a/rootfs/etc/s6-overlay/s6-rc.d/homebridge/run b/rootfs/etc/s6-overlay/s6-rc.d/homebridge/run new file mode 100755 index 0000000..dbc9562 --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/homebridge/run @@ -0,0 +1,11 @@ +#!/command/with-contenv sh + +export HOME=/home/homebridge +export USER=root +export HOMEBRIDGE_CONFIG_UI=1 + +# this is not necessarily the ui version, it's now used as a feature compatibility indicator +export CONFIG_UI_VERSION=4.44.2 + +# start homebridge +exec /opt/homebridge/start.sh --allow-root diff --git a/rootfs/etc/s6-overlay/s6-rc.d/homebridge/type b/rootfs/etc/s6-overlay/s6-rc.d/homebridge/type new file mode 100644 index 0000000..1780f9f --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/homebridge/type @@ -0,0 +1 @@ +longrun \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/setup/type b/rootfs/etc/s6-overlay/s6-rc.d/setup/type new file mode 100644 index 0000000..3d92b15 --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/setup/type @@ -0,0 +1 @@ +oneshot \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/setup/up b/rootfs/etc/s6-overlay/s6-rc.d/setup/up new file mode 100755 index 0000000..f9874ab --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/setup/up @@ -0,0 +1 @@ +/etc/s6-overlay/scripts/setup.sh \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/homebridge b/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/homebridge new file mode 100644 index 0000000..e69de29 diff --git a/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/homebridge-log b/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/homebridge-log new file mode 100644 index 0000000..e69de29 diff --git a/rootfs/etc/s6-overlay/s6-rc.d/userdata/dependencies b/rootfs/etc/s6-overlay/s6-rc.d/userdata/dependencies new file mode 100644 index 0000000..45eae7f --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/userdata/dependencies @@ -0,0 +1 @@ +setup \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/userdata/type b/rootfs/etc/s6-overlay/s6-rc.d/userdata/type new file mode 100644 index 0000000..3d92b15 --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/userdata/type @@ -0,0 +1 @@ +oneshot \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/userdata/up b/rootfs/etc/s6-overlay/s6-rc.d/userdata/up new file mode 100755 index 0000000..331805a --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/userdata/up @@ -0,0 +1 @@ +/etc/s6-overlay/scripts/userdata.sh \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/scripts/setup.sh b/rootfs/etc/s6-overlay/scripts/setup.sh new file mode 100755 index 0000000..5a1d252 --- /dev/null +++ b/rootfs/etc/s6-overlay/scripts/setup.sh @@ -0,0 +1,61 @@ +#!/command/with-contenv sh + +# user defaults +[ -e /homebridge/startup.sh ] || cp /defaults/startup.sh /homebridge/startup.sh + +# setup homebridge +mkdir -p /homebridge +if [ "$(realpath /var/lib/homebridge)" != "/homebridge" ]; then + rm -rf /var/lib/homebridge + ln -sf /homebridge /var/lib/homebridge +fi + +# fix a mistake where we were creating a symlink loop +if [ -h "/homebridge/homebridge" ] && [ "$(realpath /homebridge/homebridge)" = "/homebridge" ]; then + rm /homebridge/homebridge +fi + +cd /homebridge + +# set the .npmrc file +cp /defaults/.npmrc /homebridge/.npmrc + +# remove the package-lock.json +if [ -e /homebridge/package-lock.json ]; then + rm -rf /homebridge/package-lock.json +fi + +# if coming from an old pnpm based install, delete plugins so they are freshly installed +if [ -e /homebridge/pnpm-lock.yaml ]; then + rm -rf /homebridge/node_modules + rm -rf /homebridge/pnpm-lock.yaml + rm -rf /homebridge/package-lock.json +fi + +# setup initial package.json with homebridge +if [ ! -e /homebridge/package.json ]; then + HOMEBRIDGE_VERSION="$(curl -sf https://registry.npmjs.org/homebridge/latest | jq -r '.version')" + echo "{ \"dependencies\": { \"homebridge\": \"$HOMEBRIDGE_VERSION\" }}" | jq . > /homebridge/package.json +fi + +# remove homebridge-config-ui-x from the package.json +if [ -e /homebridge/package.json ]; then + if [ "$(cat /homebridge/package.json | jq -r '.dependencies."homebridge-config-ui-x"')" != "null" ]; then + packageJson="$(cat /homebridge/package.json | jq -rM 'del(."dependencies"."homebridge-config-ui-x")')" + if [ "$?" = "0" ]; then + printf "$packageJson" > /homebridge/package.json + echo "Removed homebridge-config-ui-x from package.json" + fi + fi +fi + +# source the setup script +if [ -f /opt/homebridge/source.sh ]; then + . "/opt/homebridge/source.sh" +fi + +# install plugins +echo "Installing Homebridge and user plugins, please wait..." +npm --prefix /homebridge install + +exit 0 diff --git a/rootfs/etc/s6-overlay/scripts/userdata.sh b/rootfs/etc/s6-overlay/scripts/userdata.sh new file mode 100755 index 0000000..9afa6dc --- /dev/null +++ b/rootfs/etc/s6-overlay/scripts/userdata.sh @@ -0,0 +1,15 @@ +#!/command/with-contenv sh + +if [ -f /opt/homebridge/source.sh ]; then + . "/opt/homebridge/source.sh" +fi + +# run user defined custom startup script +if [ -f /homebridge/startup.sh ]; then + echo "Executing user startup script /homebridge/startup.sh" + chmod +x /homebridge/startup.sh + cd /homebridge + /homebridge/startup.sh +fi + +exit 0 diff --git a/rootfs/etc/systemd/system/homebridge.service.d/override.conf b/rootfs/etc/systemd/system/homebridge.service.d/override.conf new file mode 100644 index 0000000..472288e --- /dev/null +++ b/rootfs/etc/systemd/system/homebridge.service.d/override.conf @@ -0,0 +1,5 @@ +# this docker container does not use systemd +# the homebridge apt package just checks this file to see if the user has been changed from the default + +[Service] +User=root