Skip to content

Instantly share code, notes, and snippets.

@mikehale
Created July 22, 2008 19:55
Show Gist options
  • Save mikehale/1241 to your computer and use it in GitHub Desktop.
Save mikehale/1241 to your computer and use it in GitHub Desktop.
capistrano recipe for working with slicehost dns api
load 'config/dns'
set :ip, '127.0.0.1'
set :domains, %w(example.net. example.com. example.org.)
require 'activeresource'
# make sure to add `set :slicehost_api_password, 'yourkey'` to your ~/.caprc
set :slicehost_api_password, nil unless exists?(:slicehost_api_password)
SLICEHOST_API_PASSWORD = slicehost_api_password
class Zone < ActiveResource::Base
self.site = "https://api.slicehost.com/"
self.user = SLICEHOST_API_PASSWORD
def self.find_or_create(origin)
self.find_by_origin(origin)
rescue ActiveResource::Redirection
Zone.create(:origin => origin, :ttl => 86400)
end
def self.find_by_origin(origin)
Zone.find(:conditions => {:origin => origin})
end
end
class Record < ActiveResource::Base
self.site = "https://api.slicehost.com/"
self.user = SLICEHOST_API_PASSWORD
def self.safe_create(*args)
self.create(*args)
rescue ActiveResource::ForbiddenAccess
false
end
end
namespace :dns do
task :list_zones do
Zone.find(:all).each {|zone| puts zone.origin }
end
desc "Setup DNS on Slicehost"
task :setup do
domains.each {|domain|
puts "Setting up #{domain}..."
zone = Zone.find_or_create(domain)
Record.safe_create(:record_type => 'A', :zone_id => zone.id, :name => '*', :data => ip)
Record.safe_create(:record_type => 'A', :zone_id => zone.id, :name => domain, :data => ip)
(1..3).each{|e| Record.safe_create(:record_type => 'NS',
:zone_id => zone.id,
:name => domain,
:data => "ns#{e}.slicehost.net.")
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment