Files
addons/git_pull/run.sh
Julian Kaffke 2bc8d29eb4 git_pull: Prevent plugin from always restarting hass.io even if nothing has changed (#205)
* Prevent git module from always restarting hass.io

`git diff-tree -r --name-only --no-commit-id 'HEAD@{1}' HEAD)` always shows a difference. Getting the commit id prior to pulling is easy and comparing it to the one after pulling makes sure, that checking and restart is only done if ids have changed.

* Forgot to get the OLD_COMMIT id

* Better sorting and further comments

* Pleasing the linter

* Update config.json
2017-12-04 12:44:51 +01:00

79 lines
2.3 KiB
Bash

#!/bin/bash
set -e
CONFIG_PATH=/data/options.json
DEPLOYMENT_KEY=$(jq --raw-output ".deployment_key[]" $CONFIG_PATH)
DEPLOYMENT_KEY_PROTOCOL=$(jq --raw-output ".deployment_key_protocol" $CONFIG_PATH)
REPOSITORY=$(jq --raw-output '.repository' $CONFIG_PATH)
AUTO_RESTART=$(jq --raw-output '.auto_restart' $CONFIG_PATH)
REPEAT_ACTIVE=$(jq --raw-output '.repeat.active' $CONFIG_PATH)
REPEAT_INTERVAL=$(jq --raw-output '.repeat.interval' $CONFIG_PATH)
# prepare the private key, if provided
if [ ! -z "$DEPLOYMENT_KEY" ]; then
echo "[Info] setup deployment_key on id_${DEPLOYMENT_KEY_PROTOCOL}"
mkdir -p ~/.ssh
while read -r line; do
echo "$line" >> "${HOME}/.ssh/id_${DEPLOYMENT_KEY_PROTOCOL}"
done <<< "$DEPLOYMENT_KEY"
chmod 600 "${HOME}/.ssh/id_${DEPLOYMENT_KEY_PROTOCOL}"
fi
# init config repositorie
if [ ! -d /config/.git ]; then
echo "[Info] cleanup config folder and clone from repositorie"
rm -rf /config/.[!.]* /config/* 2&> /dev/null
if ! git clone "$REPOSITORY" /config 2&> /dev/null; then
echo "[Error] can't clone $REPOSITORY into /config"
exit 1
fi
fi
# Main programm
cd /config
while true; do
# get actual commit id
OLD_COMMIT=$(git rev-parse HEAD)
# perform pull
echo "[Info] pull from $REPOSITORY"
git pull 2&> /dev/null || true
# get actual (new) commit id
NEW_COMMIT=$(git rev-parse HEAD)
# autorestart of homeassistant if enabled
if [ "$AUTO_RESTART" == "true" ]; then
# Compare commit ids & check config
if [ "$NEW_COMMIT" != "$OLD_COMMIT" ]; then
echo "[Info] check Home-Assistant config"
if api_ret="$(curl -s -X POST http://hassio/homeassistant/check)"; then
result="$(echo "$api_ret" | jq --raw-output ".result")"
# Config is valid
if [ "$result" != "error" ]; then
echo "[Info] restart Home-Assistant"
curl -s -X POST http://hassio/homeassistant/restart 2&> /dev/null || true
else
echo "[Error] invalid config!"
fi
fi
else
echo "[Info] Nothing has changed."
fi
fi
# do we repeat?
if [ -z "$REPEAT_ACTIVE" ]; then
exit 0
fi
sleep "$REPEAT_INTERVAL"
done