almond: Rewrite/optimize Almond add-on (#1227)

* almond: Rewrite/optimize Almond add-on

* almond: Make it 1.0.0
This commit is contained in:
Franck Nijhof
2020-04-16 16:57:58 +02:00
committed by GitHub
parent 70ee4c469b
commit ac4582b639
13 changed files with 167 additions and 93 deletions

View File

@@ -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

View File

@@ -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"]
COPY rootfs /

View File

@@ -5,6 +5,6 @@
"armv7": "homeassistant/armv7-base-debian:buster"
},
"args": {
"ALMOND_VERSION": "v1.7.3"
"ALMOND_VERSION": "v1.8.0"
}
}

View File

@@ -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,

View File

@@ -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[@]}"

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bashio
# ==============================================================================
# Start NGINX service
# ==============================================================================
exec nginx