Skip to content

Instantly share code, notes, and snippets.

@mattly
Created February 2, 2010 18:52
Show Gist options
  • Select an option

  • Save mattly/292909 to your computer and use it in GitHub Desktop.

Select an option

Save mattly/292909 to your computer and use it in GitHub Desktop.
a dsl for managing domain names, concrete implementation tied to linode's api
require 'linode'
require 'json'
EMAIL = "matt@flowerpowered.com"
L = Linode.new(api_key:"...")
class Domain
def initialize(name)
@hostname = name
@id = 0
@a = {}
@cname = {}
@txt = []
@srv = []
@mx = {}
r = L.domain.create(domain: @hostname, type:'master', soa_email: EMAIL)
@id = r.domainid
end
def ip(val=nil)
if val
@ip = val
else
@ip
end
end
def a(hosts, value=nil)
hosts = [hosts].flatten
hosts.each do |host|
@a[host] = value || ip
end
end
def google
{'aspmx' => 1, 'alt1.aspmx' => 5, 'alt2.aspmx' => 5}.each do |h,v|
mx("#{h}.l.google.com", v)
end
{'aspmx2' => 10, 'aspmx3' => 10, 'aspmx4' => 10, 'aspmx5' => 10}.each do |h,v|
mx("#{h}.googlemail.com", v)
end
{'' => 5, '1' => 20, '2' => 20, '3' => 20, '4' => 20}.each do |serv, v|
srv(0, 5269, 'xmpp-sever', 'tcp', "xmpp-server#{serv}.l.google.com", v)
srv(0, 5269, 'jabber', 'tcp', "xmpp-server#{serv}.l.google.com", v)
if v.to_i < 3
srv(0, 5222, 'xmpp-client', 'tcp', "talk#{srv}.l.google.com", v)
end
end
txt("v=spf1 a mx include:aspmx.googlemail.com ~all")
end
def mx(host, value)
@mx[host] = value
end
def srv(weight, port, service, protocol, target, value)
@srv << {weight:weight, port:port, protocol:protocol, target:target, value:value, name:service}
end
def txt(data)
@txt << data
end
def cname(host, target)
@cname[host] = target
end
def to_records
@a[''] = ip if @a[''].nil? && @cname[''].nil?
@a['*'] = ip if @a['*'].nil? && @cname['*'].nil?
records = @a.map {|k,v| {type: "A", name: k, target: v} }
records += @txt.map {|r| {type: "TXT", name: @hostname, target: r}}
records += @mx.map {|k,v| {type: "MX", target:k, priority: v} }
records += @cname.map {|k,v| {type: "CNAME", name:k, target: v} }
records += @srv.map {|r| {type: "SRV"}.update(r) }
records.each {|r| r.update(domainid: @id)}
records
end
end
def domain(name, &block)
d = Domain.new(name)
d.instance_exec &block
d.to_records.each do |r|
puts "creating: #{r}"
L.domain.resource.create(r)
end
end
require "dns"
domain("example.com") do
ip "192.168.1.1"
a "*"
a "staging", "192.168.1.2"
cname "foo", "foobar.com"
mx "mail.example.com", 10
txt "fuck yeah text records!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment