mirror of
https://github.com/aljazceru/addons.git
synced 2026-01-31 18:55:32 +01:00
new implementation for git pull (#218)
* new implementation for git pull * implemented review comments: use absolute paths * fixed linting errors and warnings * fixed spellcheck hints, and ignored one of them * Update config.json * Create CHANGELOG.md * Update Dockerfile * Create build.json * add new cli
This commit is contained in:
committed by
Pascal Vizeli
parent
3b7b657f95
commit
bf67692f9d
10
git_pull/CHANGELOG.md
Normal file
10
git_pull/CHANGELOG.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Changelog
|
||||
|
||||
## 3.0
|
||||
- New CLI
|
||||
- Update base image
|
||||
- Backup of files before clearing the /config folder
|
||||
- Copy back all non YAML files and the secrets.yaml after the git clone
|
||||
- More verbose error handling. Also logging the GIT exceptions.
|
||||
- Splitted code out into functions
|
||||
- Check SSH connection before setting the key
|
||||
@@ -7,6 +7,13 @@ ENV LANG C.UTF-8
|
||||
# Setup base
|
||||
RUN apk add --no-cache jq curl git openssh-client
|
||||
|
||||
# Hass.io CLI
|
||||
ARG BUILD_ARCH
|
||||
ARG CLI_VERSION
|
||||
RUN apk add --no-cache curl \
|
||||
&& curl -Lso /usr/bin/hassio https://github.com/home-assistant/hassio-cli/releases/download/${CLI_VERSION}/hassio_${BUILD_ARCH} \
|
||||
&& chmod a+x /usr/bin/hassio
|
||||
|
||||
# Copy data
|
||||
COPY run.sh /
|
||||
RUN chmod a+x /run.sh
|
||||
|
||||
5
git_pull/build.json
Normal file
5
git_pull/build.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"args": {
|
||||
"CLI_VERSION": "1.0.1"
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Git pull",
|
||||
"version": "2.3",
|
||||
"version": "3.0",
|
||||
"slug": "git_pull",
|
||||
"description": "Simple git pull to update the local configuration",
|
||||
"url": "https://home-assistant.io/addons/git_pull/",
|
||||
|
||||
119
git_pull/run.sh
119
git_pull/run.sh
@@ -1,5 +1,6 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
#### config ####
|
||||
|
||||
CONFIG_PATH=/data/options.json
|
||||
|
||||
@@ -9,74 +10,108 @@ 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 ssh access, if the deployment key has been provided
|
||||
if [ ! -z "$DEPLOYMENT_KEY" ]; then
|
||||
|
||||
#### functions ####
|
||||
function add-ssh-key {
|
||||
echo "Start adding SSH key"
|
||||
mkdir -p ~/.ssh
|
||||
echo "[Info] disable StrictHostKeyChecking for ssh"
|
||||
echo "Host *" > ~/.ssh/config
|
||||
echo " StrictHostKeyChecking no" >> ~/.ssh/config
|
||||
|
||||
echo "[Info] setup deployment_key on id_${DEPLOYMENT_KEY_PROTOCOL}"
|
||||
(
|
||||
echo "Host *"
|
||||
echo " StrictHostKeyChecking no"
|
||||
) > ~/.ssh/config
|
||||
|
||||
echo "Setup deployment_key on id_${DEPLOYMENT_KEY_PROTOCOL}"
|
||||
while read -r line; do
|
||||
echo "$line" >> "${HOME}/.ssh/id_${DEPLOYMENT_KEY_PROTOCOL}"
|
||||
done <<< "$DEPLOYMENT_KEY"
|
||||
|
||||
chmod 600 "${HOME}/.ssh/config"
|
||||
chmod 600 "${HOME}/.ssh/id_${DEPLOYMENT_KEY_PROTOCOL}"
|
||||
fi
|
||||
}
|
||||
|
||||
function git-clone {
|
||||
# create backup
|
||||
BACKUP_LOCATION="/tmp/config-$(date +%Y-%m-%d_%H-%M-%S)"
|
||||
echo "Backup configuration to $BACKUP_LOCATION"
|
||||
|
||||
mkdir "${BACKUP_LOCATION}" || { echo "[Error] Creation of backup directory failed"; exit 1; }
|
||||
cp -rf /config/* "${BACKUP_LOCATION}" || { echo "[Error] Copy files to backup directory failed"; exit 1; }
|
||||
|
||||
# remove config folder content
|
||||
rm -rf /config/{,.[!.],..?}* || { echo "[Error] Clearing /config failed"; exit 1; }
|
||||
|
||||
# init config repositorie
|
||||
if [ ! -d /config/.git ]; then
|
||||
echo "[Info] cleanup config folder and clone from repositorie"
|
||||
rm -rf /config/.[!.]* /config/* 2&> /dev/null
|
||||
# git clone
|
||||
echo "Start git clone"
|
||||
git clone "$REPOSITORY" /config || { echo "[Error] Git clone failed"; exit 1; }
|
||||
|
||||
if ! git clone "$REPOSITORY" /config 2&> /dev/null; then
|
||||
echo "[Error] can't clone $REPOSITORY into /config"
|
||||
exit 1
|
||||
# try to copy non yml files back
|
||||
cp "${BACKUP_LOCATION}" "!(*.yaml)" /config 2>/dev/null
|
||||
|
||||
# try to copy secrets file back
|
||||
cp "${BACKUP_LOCATION}/secrets.yaml" /config 2>/dev/null
|
||||
}
|
||||
|
||||
function check-ssh-key {
|
||||
if [ -n "$DEPLOYMENT_KEY" ]; then
|
||||
echo "Check SSH connection"
|
||||
IFS=':' read -ra GIT_URL_PARTS <<< "$REPOSITORY"
|
||||
# shellcheck disable=SC2029
|
||||
if ! ssh -T -o "BatchMode=yes" "${GIT_URL_PARTS[0]}"
|
||||
then
|
||||
echo "Valid SSH connection for ${GIT_URL_PARTS[0]}"
|
||||
else
|
||||
echo "No valid SSH connection for ${GIT_URL_PARTS[0]}"
|
||||
add-ssh-key
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Main programm
|
||||
cd /config
|
||||
while true; do
|
||||
function git-synchronize {
|
||||
if git rev-parse --is-inside-git-dir &>/dev/null
|
||||
then
|
||||
echo "git repository exists, start pulling"
|
||||
OLD_COMMIT=$(git rev-parse HEAD)
|
||||
git pull || { echo "[Error] Git pull failed"; exit 1; }
|
||||
else
|
||||
echo "git repostory doesn't exist"
|
||||
git-clone
|
||||
fi
|
||||
}
|
||||
|
||||
# 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
|
||||
function validate-config {
|
||||
echo "[Info] Check if something is changed"
|
||||
if [ "$AUTO_RESTART" == "true" ]; then
|
||||
|
||||
# Compare commit ids & check config
|
||||
NEW_COMMIT=$(git rev-parse HEAD)
|
||||
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
|
||||
if hassio homeassistant check; then
|
||||
echo "[Info] restart Home-Assistant"
|
||||
hassio homeassistant restart 2&> /dev/null
|
||||
else
|
||||
echo "[Error] invalid config!"
|
||||
fi
|
||||
else
|
||||
echo "[Info] Nothing has changed."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# do we repeat?
|
||||
###################
|
||||
|
||||
#### Main program ####
|
||||
cd /config || { echo "[Error] Failed to cd into /config"; exit 1; }
|
||||
while true; do
|
||||
check-ssh-key
|
||||
git-synchronize
|
||||
validate-config
|
||||
# do we repeat?
|
||||
if [ -z "$REPEAT_ACTIVE" ]; then
|
||||
exit 0
|
||||
fi
|
||||
sleep "$REPEAT_INTERVAL"
|
||||
done
|
||||
|
||||
###################
|
||||
|
||||
Reference in New Issue
Block a user