diff --git a/almond/CHANGELOG.md b/almond/CHANGELOG.md index db41525..f7ab8fd 100644 --- a/almond/CHANGELOG.md +++ b/almond/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## 1.0.0 + +- Update Almond to 1.8.0 +- Rewrite onto S6 overlay +- Reduce add-on image size +- Updates Supervisor API token and endpoint + +## 0.9 + +- Update Almond to 1.7.3 + ## 0.8 - Update Almond to 1.7.2 diff --git a/almond/Dockerfile b/almond/Dockerfile index 991924b..f986e92 100644 --- a/almond/Dockerfile +++ b/almond/Dockerfile @@ -1,12 +1,23 @@ ARG BUILD_FROM FROM ${BUILD_FROM} -# Install Alomond requirements -RUN apt-get update \ +# Base env settings +ENV \ + LANG="en_US.utf8" \ + THINGENGINE_HOME="/data/almond-server" \ + THINGENGINE_HOST_BASED_AUTHENTICATION="local-ip" + +WORKDIR /opt/almond +ARG ALMOND_VERSION +RUN \ + apt-get update \ && apt-get install -y --no-install-recommends \ - software-properties-common \ + build-essential \ + git \ gnupg \ nginx \ + python-dev \ + software-properties-common \ unzip \ \ && curl -sS https://deb.nodesource.com/gpgkey/nodesource.gpg.key \ @@ -18,40 +29,43 @@ RUN apt-get update \ && echo "deb https://dl.yarnpkg.com/debian/ stable main" \ > /etc/apt/sources.list.d/yarn.list \ \ - && apt-get update && apt-get install -y --no-install-recommends \ + && apt-get update \ + && apt-get install -y --no-install-recommends \ nodejs \ yarn \ - && rm -rf /var/lib/apt/lists/* - - -# Install Almond -ARG ALMOND_VERSION -WORKDIR /opt/almond -RUN apt-get update \ - && apt-get install -y --no-install-recommends \ - git \ - python-dev \ - build-essential \ \ && git clone -b "${ALMOND_VERSION}" --depth 1 \ "https://github.com/stanford-oval/almond-server" . \ && rm -fr .git \ && yarn \ \ + && yarn global add modclean \ + && modclean \ + --path /opt/almond \ + --no-progress \ + --keep-empty \ + --run \ + && yarn global remove modclean \ + \ + && yarn cache clean \ && apt-get purge -y --auto-remove \ - git \ - python-dev \ build-essential \ + git \ + gnupg \ + python-dev \ + software-properties-common \ + unzip \ + yarn \ && rm -rf \ + /opt/almond/.[!.]* \ + /root/.cache \ + /root/.config \ + /tmp/.[!.]* \ + /tmp/* \ + /usr/lib/nginx \ + /usr/local/share/.cache \ + /usr/local/share/.config \ /var/lib/apt/lists/* \ - /root/.cache + /var/www -# Base env settings -ENV LANG=en_US.utf8 -ENV THINGENGINE_HOME=/data/almond-server -ENV THINGENGINE_HOST_BASED_AUTHENTICATION=local-ip - -COPY data/run.sh / -COPY data/nginx.conf /etc/nginx/ - -CMD ["/run.sh"] \ No newline at end of file +COPY rootfs / diff --git a/almond/build.json b/almond/build.json index 5bc9558..4eaa2c0 100644 --- a/almond/build.json +++ b/almond/build.json @@ -5,6 +5,6 @@ "armv7": "homeassistant/armv7-base-debian:buster" }, "args": { - "ALMOND_VERSION": "v1.7.3" + "ALMOND_VERSION": "v1.8.0" } } diff --git a/almond/config.json b/almond/config.json index 81a700f..d9e72b3 100644 --- a/almond/config.json +++ b/almond/config.json @@ -1,11 +1,12 @@ { "name": "Almond", - "version": "0.9", + "version": "1.0.0", "slug": "almond", "description": "The home server version of Almond", "url": "https://github.com/home-assistant/hassio-addons/blob/master/almond", "arch": ["armv7", "aarch64", "amd64"], "startup": "application", + "init": false, "boot": "auto", "discovery": ["almond"], "ingress": true, diff --git a/almond/data/run.sh b/almond/data/run.sh deleted file mode 100755 index aee1b8d..0000000 --- a/almond/data/run.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env bashio -WAIT_PIDS=() -TOKEN_VALID="$(python3 -c "import time; print((time.time() + 60 * 60 * 24 * 365 * 5) * 1000)")" - -ha_config=$(\ - bashio::var.json \ - host "$(hostname)" \ - port "3001" \ -) - -almond_config=$(\ - bashio::var.json \ - kind "io.home-assistant" \ - hassUrl "http://hassio/homeassistant" \ - accessToken "${HASSIO_TOKEN}" \ - refreshToken "" \ - accessTokenExpires "^${TOKEN_VALID}" \ - isHassio "^true" \ -) - -# HA Discovery -if bashio::discovery "almond" "${ha_config}" > /dev/null; then - bashio::log.info "Successfully send discovery information to Home Assistant." -else - bashio::log.error "Discovery message to Home Assistant failed!" -fi - -# Ingress handling -# shellcheck disable=SC2155 -export THINGENGINE_BASE_URL=$(bashio::addon.ingress_entry) - -# Setup nginx -nginx -c /etc/nginx/nginx.conf & -WAIT_PIDS+=($!) - -# Skip Auth handling -if ! bashio::fs.file_exists "${THINGENGINE_HOME}/prefs.db"; then - mkdir -p "${THINGENGINE_HOME}" - echo '{"server-login":{"password":"x","salt":"x","sqliteKeySalt":"x"}}' > "${THINGENGINE_HOME}/prefs.db" -fi - -# Start Almond -yarn start & -WAIT_PIDS+=($!) - -# Insert HA connection settings -bashio::net.wait_for 3000 -if curl -f -s -X POST -H "Content-Type: application/json" -d "${almond_config}" http://127.0.0.1:3000/api/devices/create; then - bashio::log.info "Successfully register local Home Assistant on Almond" -else - bashio::log.error "Almond registration of local Home Assistant failed!" -fi - -# Register stop -function stop_addon() { - echo "Kill Processes..." - kill -15 "${WAIT_PIDS[@]}" - wait "${WAIT_PIDS[@]}" - echo "Done." -} -trap "stop_addon" SIGTERM SIGHUP - -# Wait until all is done -wait "${WAIT_PIDS[@]}" diff --git a/almond/rootfs/etc/cont-init.d/almond.sh b/almond/rootfs/etc/cont-init.d/almond.sh new file mode 100644 index 0000000..640897f --- /dev/null +++ b/almond/rootfs/etc/cont-init.d/almond.sh @@ -0,0 +1,14 @@ +#!/usr/bin/with-contenv bashio +# ============================================================================== +# Prepare the Almond service for running +# ============================================================================== +readonly PREFS_DB="${THINGENGINE_HOME}/prefs.db" + +if ! bashio::fs.file_exists "${PREFS_DB}"; then + # Ensure Thing Engine home directory exists + mkdir -p "${THINGENGINE_HOME}" + + # Skip authentication handling + echo '{"server-login":{"password":"x","salt":"x","sqliteKeySalt":"x"}}' \ + > "${PREFS_DB}" +fi diff --git a/almond/data/nginx.conf b/almond/rootfs/etc/nginx/nginx.conf similarity index 100% rename from almond/data/nginx.conf rename to almond/rootfs/etc/nginx/nginx.conf diff --git a/almond/rootfs/etc/services.d/almond/discovery b/almond/rootfs/etc/services.d/almond/discovery new file mode 100755 index 0000000..dcf36fa --- /dev/null +++ b/almond/rootfs/etc/services.d/almond/discovery @@ -0,0 +1,21 @@ +#!/usr/bin/with-contenv bashio +# ============================================================================== +# Send Almond discovery information to Home Assistant +# ============================================================================== +declare ha_config + +# Wait for the Almond service to be available +bashio::net.wait_for 3000 + +# Prepare discovery payload +ha_config=$(\ + bashio::var.json \ + host "$(hostname)" \ + port "3001" \ +) + +if bashio::discovery "almond" "${ha_config}" > /dev/null; then + bashio::log.info "Successfully send discovery information to Home Assistant." +else + bashio::log.error "Discovery message to Home Assistant failed!" +fi diff --git a/almond/rootfs/etc/services.d/almond/finish b/almond/rootfs/etc/services.d/almond/finish new file mode 100644 index 0000000..a29ced9 --- /dev/null +++ b/almond/rootfs/etc/services.d/almond/finish @@ -0,0 +1,8 @@ +#!/usr/bin/execlineb -S1 +# ============================================================================== +# Take down the S6 supervision tree based on service exit code +# ============================================================================== +if { s6-test ${1} -ne 0 } +if { s6-test ${1} -ne 256 } + +s6-svscanctl -t /var/run/s6/services diff --git a/almond/rootfs/etc/services.d/almond/register b/almond/rootfs/etc/services.d/almond/register new file mode 100755 index 0000000..d72d25b --- /dev/null +++ b/almond/rootfs/etc/services.d/almond/register @@ -0,0 +1,38 @@ +#!/usr/bin/with-contenv bashio +# ============================================================================== +# Register Home Assistant skill with Almond +# ============================================================================== +declare access_token_expires +declare almond_config + +# Wait for Almond service to be available +bashio::net.wait_for 3000 + +# Calulcate token expire time value +access_token_expires="$(( + $(( + $(date +%s) + 60 * 60 * 24 * 365 * 5 + )) * 1000 +)).$(date +%N)" + +# Prepare Almond configuration payload +almond_config=$(\ + bashio::var.json \ + kind "io.home-assistant" \ + hassUrl "http://supervisor/homeassistant" \ + accessToken "${SUPERVISOR_TOKEN}" \ + refreshToken "" \ + accessTokenExpires "^${access_token_expires}" \ + isHassio "^true" \ +) + +# Create Home Assistant entry in Almond service +if curl \ + -f -s -X POST -H "Content-Type: application/json" \ + -d "${almond_config}" \ + "http://127.0.0.1:3000/api/devices/create" +then + bashio::log.info "Successfully register local Home Assistant on Almond" +else + bashio::log.error "Almond registration of local Home Assistant failed!" +fi diff --git a/almond/rootfs/etc/services.d/almond/run b/almond/rootfs/etc/services.d/almond/run new file mode 100644 index 0000000..ac4906c --- /dev/null +++ b/almond/rootfs/etc/services.d/almond/run @@ -0,0 +1,18 @@ +#!/usr/bin/with-contenv bashio +# ============================================================================== +# Start Almond service +# ============================================================================== +export THINGENGINE_BASE_URL + +# Set the Ingress URL as Almond base URL for correct handling +THINGENGINE_BASE_URL=$(bashio::addon.ingress_entry) + +# Send out discovery information to Home Assistant +./discovery & + +# Register Home Assistant with Almond +./register & + +# Start Almond +cd /opt/almond || bashio::exit.nok "Failed to change directory to Almond" +exec node main.js diff --git a/almond/rootfs/etc/services.d/nginx/finish b/almond/rootfs/etc/services.d/nginx/finish new file mode 100644 index 0000000..a29ced9 --- /dev/null +++ b/almond/rootfs/etc/services.d/nginx/finish @@ -0,0 +1,8 @@ +#!/usr/bin/execlineb -S1 +# ============================================================================== +# Take down the S6 supervision tree based on service exit code +# ============================================================================== +if { s6-test ${1} -ne 0 } +if { s6-test ${1} -ne 256 } + +s6-svscanctl -t /var/run/s6/services diff --git a/almond/rootfs/etc/services.d/nginx/run b/almond/rootfs/etc/services.d/nginx/run new file mode 100644 index 0000000..bd6f1e8 --- /dev/null +++ b/almond/rootfs/etc/services.d/nginx/run @@ -0,0 +1,5 @@ +#!/usr/bin/env bashio +# ============================================================================== +# Start NGINX service +# ============================================================================== +exec nginx