This small application can be used to update the value of a DNS record.
at main 77 lines 2.1 kB view raw
1"""Module with CLI application.""" 2 3import argparse 4import json 5import logging 6import sys 7 8import dyns 9 10from .api import public_ip, retrieve_record, updater 11 12logger = logging.getLogger(__name__) 13 14 15def update_dns_record(args: argparse.Namespace) -> None: 16 """Parse the record and update the DNS record.""" 17 parts = args.record.split(".") 18 domain = ".".join(parts[-2:]) 19 name = ".".join(parts[:-2]) 20 21 if args.check: 22 record = retrieve_record(name=name, domain=domain) 23 if not record: 24 msg = f"Record {name}.{domain} not found" 25 logger.error(msg) 26 sys.exit(-1) 27 28 print("Record:\n", json.dumps(record, indent=2)) 29 ip = public_ip() 30 print("Public IP:", ip) 31 if ip != record["data"]: 32 print("\033[91mThe record has to be updated!\033[0m") 33 else: 34 print("\033[92mThe record is updated!\033[0m") 35 36 else: 37 logger.debug(f"Updating for name {name} in domain {domain}") 38 updater(name=name, domain=domain) 39 40 41def main(): 42 """Execute main function to handle CLI parameters.""" 43 parser = argparse.ArgumentParser( 44 description=( 45 "A simple command to update a DNS record in DigitalOcean with your public " 46 "IP." 47 ) 48 ) 49 parser.add_argument( 50 "record", 51 type=str, 52 nargs="?", 53 help="domain name (ex. 'home.example.com') to update with your public IP", 54 ) 55 parser.add_argument("--version", help="show version", action="store_true") 56 parser.add_argument( 57 "-v", help="increase verbosity of the output", action="store_true" 58 ) 59 parser.add_argument( 60 "--check", help="checks the status of the record", action="store_true" 61 ) 62 parser.set_defaults(func=update_dns_record) 63 args = parser.parse_args() 64 65 # check verbosity 66 if args.v: 67 logging.basicConfig(level=logging.INFO) 68 69 # just print version 70 if args.version: 71 print(dyns.__version__) 72 sys.exit(0) 73 elif not args.record: 74 print("You need to indicate a DNS record to update") 75 sys.exit(-1) 76 77 args.func(args)