-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
new implementation for git pull #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
cc606a5
b5c0b53
c0e0381
b0029d6
da5d691
1deb221
bb34f14
c394e06
89fb8d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| #### config #### | ||
|
|
||
| CONFIG_PATH=/data/options.json | ||
|
|
||
|
|
@@ -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 | ||
|
|
||
| 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)" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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; } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| # remove config folder content | ||
| rm -rf config/{,.[!.],..?}* || { echo "[Error] Clearing /config failed"; exit 1; } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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 | ||
|
|
||
| ################### | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.