2023-07-21 20:40:39 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-07-21 21:36:27 +02:00
|
|
|
GIT_REPO_PATH=$(mktemp -d)
|
|
|
|
|
|
|
|
git config --global user.email "$GIT_USER_MAIL"
|
|
|
|
git config --global user.name "$GIT_USERNAME"
|
|
|
|
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[INFO] clone repository..."
|
2023-07-21 21:44:08 +02:00
|
|
|
GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no -i $GIT_REPO_DEPLOY_KEY" git clone "$GIT_REPO_URL" "$GIT_REPO_PATH" --quiet > /dev/null
|
2023-08-02 18:56:04 +02:00
|
|
|
cd "$GIT_REPO_PATH" || exit
|
2023-07-21 21:36:27 +02:00
|
|
|
|
2023-07-21 20:40:39 +02:00
|
|
|
while true ; do
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[INFO] pull repository..."
|
2023-07-21 21:36:27 +02:00
|
|
|
git pull --quiet &> /dev/null
|
|
|
|
|
2023-08-04 10:24:17 +02:00
|
|
|
echo "[INFO] check variables..."
|
2023-07-21 20:40:39 +02:00
|
|
|
if [ -z "$INTERVAL" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] \$INTERVAL is not set."
|
2023-07-21 20:40:39 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
if [ -z "$GIT_REPO_BRANCH" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] \$GIT_REPO_BRANCH is not set."
|
2023-07-21 20:40:39 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
if [ -z "$GIT_USERNAME" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] \$GIT_USERNAME is not set."
|
2023-07-21 20:40:39 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
if [ -z "$GIT_USER_MAIL" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] \$GIT_USER_MAIL is not set."
|
2023-07-21 20:40:39 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
if [ -z "$GIT_REPO_REMOTE_NAME" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] \$GIT_REPO_REMOTE_NAME is not set."
|
2023-07-21 20:40:39 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
if [ -z "$DEVICES" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] \$DEVICES is not set."
|
2023-07-21 20:40:39 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
2023-07-21 20:51:06 +02:00
|
|
|
if [ -z "$GIT_REPO_DEPLOY_KEY" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] \$GIT_REPO_DEPLOY_KEY is not set."
|
2023-07-21 20:51:06 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
if [ -z "$GIT_REPO_URL" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] \$GIT_REPO_URL is not set."
|
2023-07-21 20:51:06 +02:00
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
2023-07-21 20:40:39 +02:00
|
|
|
# Save the current value of IFS to restore later
|
|
|
|
OLD_IFS=$IFS
|
|
|
|
|
|
|
|
# Set the IFS to a comma to split the values
|
|
|
|
IFS=','
|
|
|
|
|
2023-07-21 20:58:50 +02:00
|
|
|
while read -r FQDN USERNAME SSH_KEY_PATH; do
|
2023-07-21 20:40:39 +02:00
|
|
|
# bereinige FQDN
|
|
|
|
FQDN=$(echo "$FQDN" | tr -d "[:space:]")
|
|
|
|
# prüfe ob Key existiert
|
|
|
|
if [ ! -e "$SSH_KEY_PATH" ] ; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] File $SSH_KEY_PATH does not exist"
|
|
|
|
echo "[ERROR] can not export $FQDN config"
|
2023-07-21 20:40:39 +02:00
|
|
|
exit 2
|
|
|
|
fi
|
2023-08-04 10:16:54 +02:00
|
|
|
# check if target is reachable
|
|
|
|
if ping -c1 -W1 "${FQDN}" > /dev/null; then
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[INFO] export $FQDN config..."
|
2023-08-04 10:16:54 +02:00
|
|
|
ssh -n -o StrictHostKeyChecking=no -i "$SSH_KEY_PATH" "${USERNAME}@${FQDN}" "/export show-sensitive" > "config_${FQDN}.rsc" 2> /dev/null
|
|
|
|
# entferne Datumszeile
|
|
|
|
sed -i -r '/^# [0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}\:[0-9]{2}\:[0-9]{2}\sby\sRouterOS\s[0-9\s\.]+/d' "config_${FQDN}.rsc"
|
|
|
|
git add "config_${FQDN}.rsc"
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[INFO] commit config for ${FQDN} ..."
|
2023-08-04 10:16:54 +02:00
|
|
|
git commit -m "update config_${FQDN}" --quiet > /dev/null
|
|
|
|
else
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[ERROR] ${FQDN} not reachable!"
|
2023-08-04 10:16:54 +02:00
|
|
|
fi
|
2023-07-21 20:40:39 +02:00
|
|
|
done <<< "$DEVICES"
|
|
|
|
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[INFO] push config(s)..."
|
2023-07-21 21:44:08 +02:00
|
|
|
GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no -i $GIT_REPO_DEPLOY_KEY" git push > /dev/null
|
2023-07-21 20:40:39 +02:00
|
|
|
|
|
|
|
# Restore the original IFS value
|
|
|
|
IFS=$OLD_IFS
|
|
|
|
|
|
|
|
# loop
|
2023-08-04 10:22:34 +02:00
|
|
|
echo "[INFO] sleep..."
|
2023-07-21 20:40:39 +02:00
|
|
|
sleep "$INTERVAL"
|
|
|
|
|
|
|
|
done
|