Skip to content

Instantly share code, notes, and snippets.

@mikehale
Created November 10, 2008 20:56
Show Gist options
  • Save mikehale/23624 to your computer and use it in GitHub Desktop.
Save mikehale/23624 to your computer and use it in GitHub Desktop.
require 'config/recipes/dns'
Capistrano::Configuration.instance.load do
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 => e
puts e
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)
Record.safe_create(:record_type => 'TXT', :zone_id => zone.id, :name => domain, :data => 'v=spf1 a ~all')
(1..3).each{|e| Record.safe_create(:record_type => 'NS',
:zone_id => zone.id,
:name => domain,
:data => "ns#{e}.slicehost.net.")
}
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment