Skip to content

Instantly share code, notes, and snippets.

@jmk
Last active August 29, 2015 13:57
Show Gist options
  • Save jmk/9710558 to your computer and use it in GitHub Desktop.
Save jmk/9710558 to your computer and use it in GitHub Desktop.
# This script sets Google Talk (Jabber/XMPP) SRV records for DigitalOcean
# domains.
#
# To verify that your records are correct, you can query your domain's records
# like so:
#
# dig +short srv _xmpp-server._tcp.yourdomain.com
#
# (If your WHOIS information does not yet point to DigitalOcean's DNS servers,
# you need to add @ns1.digitalocean.com to the arguments to dig.)
#
# Note that you may need to wait several minutes for the DNS servers to update
# with your changes (including in the admin control panel).
#
# I've successfully used this script on my own domains, but use this at your
# own risk.
client_id = None
api_key = None
api_base = "https://api.digitalocean.com/domains/"
# If true, don't actually make any changes to domain records.
debug = False
import json, sys, urllib, urllib2
def request(id="", path="", **kwargs):
kwargs["client_id"] = client_id
kwargs["api_key"] = api_key
if (id and path):
path = str(id) + "/" + path
url = api_base + path + "?" + urllib.urlencode(kwargs)
return json.load(urllib2.urlopen(url))
def p(s):
print json.dumps(s, indent=2)
def getDomainId(domain):
r = request()
for d in r["domains"]:
if d["name"] == domain:
return d["id"]
# Not found.
print "Domain not found:", domain
sys.exit(2)
def addSrv(domain_id, hostname, priority, weight, port, value):
# New DNS record API:
#
# domain_id Required, Integer or Domain Name (e.g. domain.com),
# specifies the domain for which to create a record.
# record_type Required, String, the type of record you would like to
# create. 'A', 'CNAME', 'NS', 'TXT', 'MX' or 'SRV'
# data Required, String, this is the value of the record
# name Optional, String, required for 'A', 'CNAME', 'TXT' and 'SRV'
# records
# priority Optional, Integer, required for 'SRV' and 'MX' records
# port Optional, Integer, required for 'SRV' records
# weight Optional, Integer, required for 'SRV' records
args = {
"domain_id": domain_id,
"record_type": "SRV",
"data": value,
"name": hostname,
"priority": priority,
"port": port,
"weight": weight
}
if (debug):
print args
return
r = request(id=domain_id, path="records/new", **args)
if (not r["status"] == "OK"):
print "Failed to set record:", args, r
sys.exit(3)
def main(_client_id, _api_key, domain):
global client_id, api_key
client_id = _client_id
api_key = _api_key
# SRV records for Google Apps:
#
# _xmpp-server._tcp.domain.com. IN SRV 5 0 5269 xmpp-server.l.google.com.
# _xmpp-server._tcp.domain.com. IN SRV 20 0 5269 alt1.xmpp-server.l.google.com.
# _xmpp-server._tcp.domain.com. IN SRV 20 0 5269 alt2.xmpp-server.l.google.com.
# _xmpp-server._tcp.domain.com. IN SRV 20 0 5269 alt3.xmpp-server.l.google.com.
# _xmpp-server._tcp.domain.com. IN SRV 20 0 5269 alt4.xmpp-server.l.google.com.
#
# _jabber._tcp.domain.com. IN SRV 5 0 5269 xmpp-server.l.google.com.
# _jabber._tcp.domain.com. IN SRV 20 0 5269 alt1.xmpp-server.l.google.com.
# _jabber._tcp.domain.com. IN SRV 20 0 5269 alt2.xmpp-server.l.google.com.
# _jabber._tcp.domain.com. IN SRV 20 0 5269 alt3.xmpp-server.l.google.com.
# _jabber._tcp.domain.com. IN SRV 20 0 5269 alt4.xmpp-server.l.google.com.
#
# _xmpp-client._tcp.domain.com. IN SRV 5 0 5222 xmpp.l.google.com.
# _xmpp-client._tcp.domain.com. IN SRV 20 0 5222 alt1.xmpp.l.google.com.
# _xmpp-client._tcp.domain.com. IN SRV 20 0 5222 alt2.xmpp.l.google.com.
# _xmpp-client._tcp.domain.com. IN SRV 20 0 5222 alt3.xmpp.l.google.com.
# _xmpp-client._tcp.domain.com. IN SRV 20 0 5222 alt4.xmpp.l.google.com.
id = getDomainId(domain)
records = {
"_xmpp-server._tcp" : [
(5, 5269, "xmpp-server.l.google.com."),
(20, 5269, "alt1.xmpp-server.l.google.com."),
(20, 5269, "alt2.xmpp-server.l.google.com."),
(20, 5269, "alt3.xmpp-server.l.google.com."),
(20, 5269, "alt4.xmpp-server.l.google.com."),
],
"_jabber._tcp" : [
(5, 5269, "xmpp-server.l.google.com."),
(20, 5269, "alt1.xmpp-server.l.google.com."),
(20, 5269, "alt2.xmpp-server.l.google.com."),
(20, 5269, "alt3.xmpp-server.l.google.com."),
(20, 5269, "alt4.xmpp-server.l.google.com."),
],
"_xmpp-client._tcp" : [
(5, 5222, "xmpp.l.google.com."),
(20, 5222, "alt1.xmpp.l.google.com."),
(20, 5222, "alt2.xmpp.l.google.com."),
(20, 5222, "alt3.xmpp.l.google.com."),
(20, 5222, "alt4.xmpp.l.google.com."),
],
}
for name, recordlist in records.iteritems():
hostname = name + "." + domain + "."
for priority, port, value in recordlist:
# Note: apparently, weight is always 0 for Google Talk
addSrv(id,
hostname=hostname,
priority=priority,
weight=0,
port=port,
value=value)
print "All records created successfully."
if __name__ == "__main__":
if (len(sys.argv) != 4):
print "usage: %s CLIENT_ID API_KEY DOMAIN_NAME" % sys.argv[0]
sys.exit(1)
main(*sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment