Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion git_pull/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Git pull",
"version": "2.3",
"version": "2.4",
"slug": "git_pull",
"description": "Simple git pull to update the local configuration",
"url": "https://home-assistant.io/addons/git_pull/",
Expand Down
102 changes: 72 additions & 30 deletions git_pull/run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
set -e

#### config ####

CONFIG_PATH=/data/options.json

Expand All @@ -9,53 +10,83 @@ 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(
  echo "xy"
  echo "z"
) > ~/.ssh/config


echo "[Info] setup deployment_key on id_${DEPLOYMENT_KEY_PROTOCOL}"
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 {
# back to main folder
cd ..

# init config repositorie
if [ ! -d /config/.git ]; then
echo "[Info] cleanup config folder and clone from repositorie"
rm -rf /config/.[!.]* /config/* 2&> /dev/null
# create backup
BACKUP_LOCATION="./tmp/config-$(date +%Y-%m-%d_%H-%M-%S)"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/tmp/...

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; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/config/*


# remove config folder content
rm -rf config/{,.[!.],..?}* || { echo "[Error] Clearing /config failed"; exit 1; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolut path


if ! git clone "$REPOSITORY" /config 2&> /dev/null; then
echo "[Error] can't clone $REPOSITORY into /config"
exit 1
# git clone
echo "Start git clone"
git clone "$REPOSITORY" ./config || { echo "[Error] Git clone failed"; 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

cd config
}

function check-ssh-key {
if [ -n DEPLOYMENT_KEY ]; then
echo "Check SSH connection"
IFS=':' read -ra GIT_URL_PARTS <<< "$REPOSITORY"
ssh -T -o "BatchMode=yes" "${GIT_URL_PARTS[0]}"
if [ $? -eq 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

# 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)
function git-synchronize {
inside_git_repo="$(git rev-parse --is-inside-git-dir 2>/dev/null)"
if [ $? -eq 0 ]; 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
}

# 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
Expand All @@ -73,10 +104,21 @@ while true; do
echo "[Info] Nothing has changed."
fi
fi
}

###################

# do we repeat?
#### Main program ####
cd /config
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

###################