routeros-config-export/app.sh

94 lines
2.2 KiB
Bash
Raw Normal View History

2023-07-21 20:40:39 +02:00
#!/bin/bash
while true ; do
# Checks
if [ -z "$INTERVAL" ] ; then
echo "INTERVAL is not set."
exit 3
fi
if [ -z "$GIT_REPO_BRANCH" ] ; then
echo "GIT_REPO_BRANCH is not set."
exit 3
fi
if [ -z "$GIT_USERNAME" ] ; then
echo "GIT_USERNAME is not set."
exit 3
fi
if [ -z "$GIT_USER_MAIL" ] ; then
echo "GIT_USER_MAIL is not set."
exit 3
fi
if [ -z "$GIT_REPO_REMOTE_NAME" ] ; then
echo "GIT_REPO_REMOTE_NAME is not set."
exit 3
fi
if [ -z "$DEVICES" ] ; then
echo "DEVICES is not set."
exit 3
fi
2023-07-21 20:51:06 +02:00
if [ -z "$GIT_REPO_DEPLOY_KEY" ] ; then
echo "GIT_REPO_DEPLOY_KEY is not set."
exit 3
fi
if [ -z "$GIT_REPO_URL" ] ; then
echo "GIT_REPO_URL is not set."
exit 3
fi
2023-07-21 20:53:51 +02:00
GIT_REPO_PATH=$(mktemp -d)
2023-07-21 20:40:39 +02:00
git config --global user.email "$GIT_USER_MAIL"
git config --global user.name "$GIT_USERNAME"
if [ -d "$GIT_REPO_PATH" ] ; then
cd "$GIT_REPO_PATH"
if [ -d ".git" ] ; then
git pull "$GIT_REPO_REMOTE_NAME" "$GIT_REPO_BRANCH" --quiet
else
echo "Error: The directory exists but is not a Git repository"
exit 1
fi
else
git clone "$GIT_REPO_URL" "$GIT_REPO_PATH" --quiet
fi
# Save the current value of IFS to restore later
OLD_IFS=$IFS
# Set the IFS to a comma to split the values
IFS=','
while read -r FQDN SSH_KEY_PATH; do
# bereinige FQDN
FQDN=$(echo "$FQDN" | tr -d "[:space:]")
# prüfe ob Key existiert
if [ ! -e "$SSH_KEY_PATH" ] ; then
echo "Error: File $SSH_KEY_PATH does not exist"
echo "Error: can not export $FQDN config"
exit 2
fi
echo "export $FQDN config"
2023-07-21 20:43:47 +02:00
ssh -n -o StrictHostKeyChecking=no -i "$SSH_KEY_PATH" "$FQDN" "/export show-sensitive" > "config_${FQDN}.rsc"
2023-07-21 20:40:39 +02:00
# 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"
git commit -m "update config_${FQDN}" --quiet
done <<< "$DEVICES"
git push
# Restore the original IFS value
IFS=$OLD_IFS
# loop
sleep "$INTERVAL"
done
# test mitvolume
# ohne volume wg git clone
# healtcheck mit letzue exit code
# beschrieben wie keys abgelegt werden müssen
# hadolint