-
-
Save shapi78/36df9c70bc9eb312b7727568bc308380 to your computer and use it in GitHub Desktop.
Python script to add/update an A record at amazon area53 DNS service, using current IP. (ie, dyndns replacement)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| SCRIPT_FOLDER="/opt/scripts" | |
| masterIP=$(docker info --format '{{json .}}' | jq -r .Swarm.RemoteManagers[].Addr | awk -F: 'NR==1{print $1}') | |
| instanceID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) | |
| region=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}') | |
| echo "swarm masetr IP: $masterIP" | |
| env=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${instanceID}" --region $region| jq -r '(.Tags[]|select(.Key=="Environment"))|.Value') | |
| ${SCRIPT_FOLDER}/dyndns53.py -n swarm -s "${env}" -i "${masterIP}" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| from area53 import route53 | |
| from boto.route53.exception import DNSServerError | |
| import requests | |
| import sys | |
| from datetime import datetime | |
| import argparse, os | |
| base_domain = 'mobileodt.com' | |
| parser = argparse.ArgumentParser(description="Updating host to subdomain on Route53 DNS") | |
| parser.add_argument("--hostname","-n", type=str, help="Host name record") | |
| parser.add_argument("--subdomain","-s", type=str, help="Subdomain name.") | |
| parser.add_argument("--ip","-i", type=str, help="Host IP.") | |
| args = parser.parse_args() | |
| domain = '%s.%s' % (args.subdomain, base_domain) | |
| print ("host ",args.hostname, "ip: ", args.ip) | |
| fqdn = '%s.%s' % (args.hostname, domain) | |
| zone = route53.get_zone(domain) | |
| arec = zone.get_a(fqdn) | |
| new_value = args.ip | |
| datestr = '"Last update %s."' % datetime.utcnow().strftime('%Y-%m-%d %H:%M') | |
| if arec: | |
| old_value = arec.resource_records[0] | |
| print ("Old Value for ",fqdn, "is: ", old_value) | |
| if old_value == new_value: | |
| print (fqdn,' is current ', new_value) | |
| sys.exit(0) | |
| print ("Updating " ,fqdn,": ",old_value," -> ",new_value) | |
| try: | |
| zone.update_a(fqdn, new_value, 300) | |
| except DNSServerError: | |
| # This can happen if the record did not already exist. Let's | |
| # try to add_a in case that's the case here. | |
| zone.add_a(fqdn, new_value, 900) | |
| try: | |
| zone.update_txt(fqdn, datestr, 900) | |
| except DNSServerError: | |
| zone.add_txt(fqdn, datestr, 900) | |
| else: | |
| zone.add_a(fqdn, new_value, 900) | |
| zone.add_txt(fqdn, datestr, 900) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment