Simplified the script by adding Runitor

This commit is contained in:
2024-10-11 22:05:11 +02:00
parent 56037ebe0f
commit 163ae5a8bf
2 changed files with 66 additions and 55 deletions

View File

@@ -1,8 +1,20 @@
FROM alpine:latest AS builder
ARG TARGETPLATFORM
RUN DOWNLOAD_ARCH=$(echo ${TARGETPLATFORM} | cut -d"/" -f 2) \
&& apk add --no-cache curl \
&& DOWNLOAD_URL=$(curl -s https://api.github.com/repos/bdd/runitor/releases/latest | grep "browser_download_url" | grep "linux-"${DOWNLOAD_ARCH} | cut -d\" -f4) \
&& curl --retry 3 -L -s -o runitor ${DOWNLOAD_URL} \
&& chmod +x runitor
FROM alpine:latest FROM alpine:latest
ADD dnsupdater.sh /usr/bin/dnsupdater.sh COPY --from=builder runitor /usr/bin/
ADD dnsupdater.sh docker-command.sh /usr/bin/
RUN apk add --no-cache bash curl jq bind-tools \ RUN apk add --no-cache bash curl jq bind-tools \
&& chmod +x /usr/bin/dnsupdater.sh && chmod +x /usr/bin/dnsupdater.sh
CMD /usr/bin/dnsupdater.sh CMD ["/usr/bin/docker-command.sh"]

View File

@@ -1,59 +1,58 @@
#!/bin/bash #!/bin/bash
while true ; do # Get my IP
# Get my current IP my_ip=$(curl -s https://api.ipify.org)
my_ip=$(curl -s https://api.ipify.org) if [[ ! ("$my_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$) ]] ; then
echo "Got incorrect IP: $my_ip"
exit 1
fi
# Get my currently registered IP # Get my registered IP
current_ip= current_ip=
if [[ "$GANDI_API_KEY" != "" ]] ; then if [[ "$GANDI_API_KEY" != "" ]] ; then
current_ip=$(curl -s -H"X-Api-Key: $GANDI_API_KEY" https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records | jq -r '.[] | select(.rrset_name == "'$DNS_HOST'") | select(.rrset_type == "A") | .rrset_values[0]') current_ip=$(curl -s -H"X-Api-Key: $GANDI_API_KEY" https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records | jq -r '.[] | select(.rrset_name == "'$DNS_HOST'") | select(.rrset_type == "A") | .rrset_values[0]')
elif [[ "$OVH_USERNAME" != "" ]] ; then elif [[ "$OVH_USERNAME" != "" ]] ; then
master_server=$(dig +short -t NS $DNS_DOMAIN | head -n 1) master_server=$(dig +short -t NS $DNS_DOMAIN | head -n 1)
current_ip=$(dig +short @$master_server $DNS_HOST.$DNS_DOMAIN) current_ip=$(dig +short @$master_server $DNS_HOST.$DNS_DOMAIN)
else
echo "No DNS provider configured"
exit 2
fi
if [[ ! ("$current_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$) ]] ; then
echo "Incorrect registered IP: $current_ip"
exit 3
fi
# If they match, do nothing
if [[ "$my_ip" == "$current_ip" ]]; then
echo "$DNS_HOST.$DNS_DOMAIN record already up-to-date with IP $my_ip"
exit 0
fi
# Update the DNS record
echo "Updating $DNS_HOST.$DNS_DOMAIN record with IP $my_ip"
if [[ "$GANDI_API_KEY" != "" ]] ; then
current_record=$(curl -s -H"X-Api-Key: $GANDI_API_KEY" https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records | jq -c '.[] | select(.rrset_name == "'$DNS_HOST'") | select(.rrset_type == "A")')
current_ttl=$(echo $current_record | jq -r '.rrset_ttl')
curl -s -X PUT -H "Content-Type: application/json" -H "X-Api-Key: $GANDI_API_KEY" -d '{"rrset_ttl": '$current_ttl', "rrset_values":["'$my_ip'"]}' https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records/$DNS_HOST/A
if [[ $? != 0 ]] ; then
echo "Unable to update GANDI record"
exit 4
fi fi
elif [[ "$OVH_USERNAME" != "" ]] ; then
# Check if both IP addresses are correct curl -s --user "$OVH_USERNAME:$OVH_PASSWORD" "http://www.ovh.com/nic/update?system=dyndns&hostname=$DNS_HOST.$DNS_DOMAIN&myip=$my_ip"
if [[ "$my_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ && "$current_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] ; then if [[ $? != 0 ]] ; then
# If they do not match, change it (and keep the TTL and TYPE) echo "Unable to update OVH record"
if [[ "$my_ip" != "$current_ip" ]]; then exit 5
result=1
echo "Updating $DNS_HOST.$DNS_DOMAIN record with IP $my_ip"
if [[ "$GANDI_API_KEY" != "" ]] ; then
current_record=$(curl -s -H"X-Api-Key: $GANDI_API_KEY" https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records | jq -c '.[] | select(.rrset_name == "'$DNS_HOST'") | select(.rrset_type == "A")')
current_ttl=$(echo $current_record | jq -r '.rrset_ttl')
curl -s -X PUT -H "Content-Type: application/json" -H "X-Api-Key: $GANDI_API_KEY" -d '{"rrset_ttl": '$current_ttl', "rrset_values":["'$my_ip'"]}' https://dns.api.gandi.net/api/v5/domains/$DNS_DOMAIN/records/$DNS_HOST/A
result=$?
elif [[ "$OVH_USERNAME" != "" ]] ; then
curl -s --user "$OVH_USERNAME:$OVH_PASSWORD" "http://www.ovh.com/nic/update?system=dyndns&hostname=$DNS_HOST.$DNS_DOMAIN&myip=$my_ip"
result=$?
fi
# If the update was OK
if [[ $result == 0 ]] ; then
# Send a notification to Slack
if [[ "$SLACK_URL" != "" ]] ; then
curl -o /dev/null -s -m 10 --retry 5 -X POST -d "payload={\"username\": \"gandi\", \"icon_emoji\": \":dart:\", \"text\": \"New IP $my_ip for host $DNS_HOST.$DNS_DOMAIN\"}" $SLACK_URL
fi
# Send a notification to Gotify
if [[ "$GOTIFY_URL" != "" ]] ; then
curl -o /dev/null -s -m 10 --retry 5 -X POST -H "accept: application/json" -H "Content-Type: application/json" -d "{\"priority\": 5, \"title\": \"New IP for host $DNS_HOST.$DNS_DOMAIN\", \"message\": \"New IP $my_ip for host $DNS_HOST.$DNS_DOMAIN, the old IP was $current_ip\"}" $GOTIFY_URL
fi
# Send a notification to Healthchecks
if [[ "$HEALTHCHECKS_URL" != "" ]] ; then
curl -o /dev/null -s -m 10 --retry 5 $HEALTHCHECKS_URL
fi
fi
else
# Send a notification to Healthchecks
if [[ "$HEALTHCHECKS_URL" != "" ]] ; then
curl -o /dev/null -s -m 10 --retry 5 $HEALTHCHECKS_URL
fi
fi
fi fi
fi
# Wait 5 minutes # Send a notification to Slack
sleep 300 if [[ "$SLACK_URL" != "" ]] ; then
done curl -o /dev/null -s -m 10 --retry 5 -X POST -d "payload={\"username\": \"gandi\", \"icon_emoji\": \":dart:\", \"text\": \"New IP $my_ip for host $DNS_HOST.$DNS_DOMAIN\"}" $SLACK_URL
fi
# Send a notification to Gotify
if [[ "$GOTIFY_URL" != "" ]] ; then
curl -o /dev/null -s -m 10 --retry 5 -X POST -H "accept: application/json" -H "Content-Type: application/json" -d "{\"priority\": 5, \"title\": \"New IP for host $DNS_HOST.$DNS_DOMAIN\", \"message\": \"New IP $my_ip for host $DNS_HOST.$DNS_DOMAIN, the old IP was $current_ip\"}" $GOTIFY_URL
fi