Created
November 25, 2014 05:34
-
-
Save zachfi/e0dee773b657b41f3fe9 to your computer and use it in GitHub Desktop.
Gandi DNS Update Script
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/local/bin/python | |
| import xmlrpclib | |
| import ConfigParser | |
| import os | |
| from sys import exit | |
| class GandiDomain(): | |
| def __init__(self, apikey, domain): | |
| self.apikey = apikey | |
| self.domain = domain | |
| self.gandi = xmlrpclib.ServerProxy('https://rpc.gandi.net/xmlrpc/') | |
| self.info = self.gandi.domain.info(self.apikey, self.domain) | |
| def updateRecord(self, record, rtype, address): | |
| """ Create a new zone version """ | |
| zoneVersion = self.gandi.domain.zone.version.new( | |
| self.apikey, | |
| self.info['zone_id']) | |
| recordList = self.gandi.domain.zone.record.list( | |
| self.apikey, | |
| self.info['zone_id'], | |
| zoneVersion, | |
| ) | |
| if not self.recordSynced(record, address): | |
| """ Delete the record """ | |
| recordListOptions = { | |
| "name": record, | |
| "type": rtype} | |
| self.gandi.domain.zone.record.delete( | |
| self.apikey, | |
| self.info['zone_id'], | |
| zoneVersion, | |
| recordListOptions) | |
| """ Insert the new record """ | |
| zoneRecord = { | |
| "name": record, | |
| "ttl": 3600, | |
| "type": rtype, | |
| "value": address} | |
| self.gandi.domain.zone.record.add( | |
| self.apikey, | |
| self.info['zone_id'], | |
| zoneVersion, | |
| zoneRecord) | |
| """ Commit the new version """ | |
| results = self.gandi.domain.zone.version.set( | |
| self.apikey, | |
| self.info['zone_id'], | |
| zoneVersion) | |
| if not results: | |
| exit(1) | |
| def recordSynced(self, record, value): | |
| for r in recordList: | |
| if r['name'] is record: | |
| if r['value'] is address: | |
| return True | |
| return False | |
| def main(): | |
| configFile = os.path.join( | |
| os.path.dirname( | |
| os.path.dirname(__file__)), | |
| 'etc/dns.conf') | |
| cparse = ConfigParser.ConfigParser() | |
| cparse.read(configFile) | |
| apikey = cparse.get('dns', 'apikey') | |
| domain = cparse.get('dns', 'domain') | |
| record = 'vpn' | |
| rtype = 'A' | |
| import subprocess | |
| address = subprocess.check_output(["/usr/local/bin/facter", "ipaddress"]) | |
| g = GandiDomain(apikey, domain) | |
| g.updateRecord(record, rtype, address) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment