Last active
August 29, 2015 14:15
-
-
Save gimmi/a30ae1b5737ee86523cc to your computer and use it in GitHub Desktop.
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
# Run with: | |
# env GODADDY_PASSWORD=secret LIBERO_PASSWORD=secret LIBERO_EMAIL=secret FATAL_EMAIL=secret ./bin/python sync.py | |
import sys | |
import os | |
import logging | |
import logging.handlers | |
import pif | |
import pygodaddy | |
LIBERO_EMAIL = os.environ['LIBERO_EMAIL'] | |
LIBERO_PASSWORD = os.environ['LIBERO_PASSWORD'] | |
FATAL_EMAIL = os.environ['FATAL_EMAIL'] | |
GODADDY_PASSWORD = os.environ['GODADDY_PASSWORD'] | |
def init_logging(): | |
formatter = logging.Formatter('%(asctime)s %(levelname)s:%(name)s:%(message)s') | |
stream_handler = logging.StreamHandler() | |
stream_handler.setFormatter(formatter) | |
stream_handler.setLevel(logging.DEBUG) | |
logging.root.addHandler(stream_handler) | |
log_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'log.txt') | |
file_handler = logging.handlers.RotatingFileHandler(log_path, maxBytes=1024*1024, backupCount=1) | |
file_handler.setFormatter(formatter) | |
file_handler.setLevel(logging.INFO) | |
logging.root.addHandler(file_handler) | |
smtp_handler = logging.handlers.SMTPHandler('smtp.libero.it', LIBERO_EMAIL, FATAL_EMAIL, 'home.gherardi.me sync error', credentials=(LIBERO_EMAIL, LIBERO_PASSWORD)) | |
smtp_handler.setFormatter(formatter) | |
smtp_handler.setLevel(logging.CRITICAL) | |
logging.root.addHandler(smtp_handler) | |
# syslog_handler = logging.handlers.SysLogHandler(address='/dev/log') | |
# syslog_handler.setFormatter(formatter) | |
# syslog_handler.setLevel(logging.INFO) | |
# logging.root.addHandler(syslog_handler) | |
logging.root.setLevel(logging.DEBUG) | |
logging.getLogger('requests').setLevel(logging.WARNING) | |
def update_godaddy_dns(): | |
username = 'gimmi' | |
domain = 'gherardi.me' | |
subdomain = 'home' | |
logging.info('Starting DNS update') | |
client = pygodaddy.GoDaddyClient() | |
if not client.login(username, GODADDY_PASSWORD): | |
raise Exception('Unable to login in godaddy with user %s' % username) | |
current_public_ip = pif.get_public_ip() | |
dns_ip = client.find_dns_records(domain) | |
dns_ip = filter(lambda x: x.hostname == subdomain, dns_ip) | |
dns_ip = dns_ip[0].value | |
logging.info('%s.%s: %s => %s', subdomain, domain, dns_ip, current_public_ip) | |
if dns_ip != current_public_ip: | |
if not client.update_dns_record(subdomain + '.' + domain, current_public_ip): | |
raise Exception('Unable update IP for %s' % domain) | |
logging.info('Succesfully completed') | |
if __name__ == '__main__': | |
try: | |
init_logging() | |
update_godaddy_dns() | |
except Exception as e: | |
logging.fatal('Unable to complete', exc_info=e) | |
sys.exit(1) |
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
argparse==1.2.1 | |
distribute==0.6.24 | |
pif==0.7.1 | |
pygodaddy==0.1.7 | |
requests==2.5.1 | |
wsgiref==0.1.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment