Files
addons/git_pull/run.sh
Delio Castillo c077c9fb8d Use reflog to calculate diff and allow 'git://' repo addresses (#198)
* use reflog instead of ORIG_HEAD to get the changes

* Allow git:// URLS too

Requiring url in repo only allows for https addresses

* trying to fix the tests and regex match

* fix lint
2017-11-14 11:36:13 +01:00

71 lines
2.1 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
# Run duckdns
cd /config
while true; do
echo "[Info] pull from $REPOSITORY"
git pull 2&> /dev/null || true
# Enable autorestart of homeassistant
if [ "$AUTO_RESTART" == "true" ]; then
changed_files="$(git diff-tree -r --name-only --no-commit-id 'HEAD@{1}' HEAD)"
# Files have changed & check config
if [ ! -z "$changed_files" ]; 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
fi
fi
# do we repeat?
if [ -z "$REPEAT_ACTIVE" ]; then
exit 0
fi
sleep "$REPEAT_INTERVAL"
done