From b55e96d78c9cedbdf4a51f9c801f6be36df95456 Mon Sep 17 00:00:00 2001 From: David Ervideira Date: Mon, 9 Dec 2019 16:07:43 +0000 Subject: [PATCH] The script it self can run forver now * Add option for the script to repeat itself after N seconds * Add a Dockerfile so that this could be run inside docker --- Dockerfile | 13 +++++++++++++ README.md | 15 +++++++++++---- src/gandi-live-dns.py | 16 ++++++++++++++-- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3f7068e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.7-buster + +LABEL maintainer=david@dme.ninja +LABEL version="0.1" + +# Copy scripts and requirements.txt +COPY src/ /gandi-live-dns +COPY requirements.txt /requirements.txt + +# Install script requirements.txt +RUN pip install -r /requirements.txt + +CMD ["python", "/gandi-live-dns/gandi-live-dns.py", "-v", "-r", "3600"] diff --git a/README.md b/README.md index 6c44376..3913b38 100644 --- a/README.md +++ b/README.md @@ -99,12 +99,14 @@ Status Code: 201 , DNS Record Created , IP updated for subdomain3 ``` root@dyndns:~/gandi-live-dns-master/src# ./gandi-live-dns.py -h -usage: gandi-live-dns.py [-h] [-f] +usage: gandi-live-dns.py [-h] [-f] [-v] [-r] optional arguments: - -h, --help show this help message and exit - -f, --force force an update/create - + -h, --help show this help message and exit + -v, --verbose increase output verbosity + -f, --force force an update/create + -r REPEAT, --repeat REPEAT + keep running and repeat every N seconds ``` The force option runs the script, even when no IP change has been detected. @@ -135,6 +137,11 @@ Run the script every five minutes. ``` */5 * * * * /root/gandi-live-dns-master/src/gandi-live-dns.py >/dev/null 2>&1 ``` + +### Run with Docker + +Use the docker file to build the image. With docker, the script will run every 3600 seconds. (This value can be changed in the Dockerfile. + ### Limitations The XML-RPC API has a limit of 30 requests per 2 seconds, so i guess it's safe to update 25 subdomains at once with the REST API. diff --git a/src/gandi-live-dns.py b/src/gandi-live-dns.py index 7153fd2..c367b31 100755 --- a/src/gandi-live-dns.py +++ b/src/gandi-live-dns.py @@ -4,10 +4,12 @@ Gandi v5 LiveDNS - DynDNS Update via REST API and CURL/requests @author: cave +@author: dvdme License GPLv3 https://www.gnu.org/licenses/gpl-3.0.html Created on 13 Aug 2017 +Forked on 08 Dec 2019 http://doc.livedns.gandi.net/ http://doc.livedns.gandi.net/#api-endpoint -> https://dns.gandi.net/api/v5/ ''' @@ -17,6 +19,7 @@ import requests import ipaddress import config import argparse +import threading as th from pprint import pprint @@ -111,7 +114,7 @@ def update_records(uuid, dynIP, subdomain, is_ipv6=False, verbose=False): -def main(force_update, verbosity): +def main(force_update, verbosity, repeat): if verbosity: print('verbosity turned on') @@ -119,6 +122,9 @@ def main(force_update, verbosity): else: verbose =False + if repeat and verbose: + print(f'repeat turned on, will repeat every {repeat} seconds') + #get zone ID from Account uuid = get_uuid() @@ -150,10 +156,16 @@ def main(force_update, verbosity): print (f'IP Address Mismatch - going to update the DNS Records for the subdomains with new IP {dynIP}') for sub in subdomains: update_records(uuid, dynIP, sub, is_ipv6, verbose) + if repeat: + if verbosity: + print(f'Repeating in {repeat} seconds') + th.Timer(repeat, main, [force_update, verbosity, repeat]).start() + if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-v', '--verbose', help='increase output verbosity', action='store_true') parser.add_argument('-f', '--force', help='force an update/create', action='store_true') + parser.add_argument('-r', '--repeat', type=int, help='keep running and repeat every N seconds') args = parser.parse_args() - main(args.force, args.verbose) + main(args.force, args.verbose, args.repeat)