Last active
September 21, 2017 19:26
-
-
Save JoshAshby/397fa46d8b2800bcc7c944a5099367be to your computer and use it in GitHub Desktop.
Converts chef databag dns records into DigitalOcean DNS entries
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/bin/env ruby | |
require "rubygems" | |
require "bundler/setup" | |
Bundler.require :default | |
class DODNS | |
TOKEN = ENV["DO_TOKEN"].freeze | |
DNS_FILE_LOC = Pathname.new("data_bags/dns").freeze | |
def self.run | |
new.run | |
end | |
def client | |
@client ||= DropletKit::Client.new access_token: TOKEN | |
end | |
def remote_dns_entries | |
@remote_dns_entries ||= client.domains.all.each.to_a | |
end | |
def local_dns_entries | |
@local_dns_entries ||= Dir[ DNS_FILE_LOC.join("*.json") ].each_with_object({}) do |filename, memo| | |
path = Pathname.new filename | |
entry = JSON.parse path.read, symbolize_names: true | |
key = entry[:domain].gsub(%r{\.$}, '') | |
memo[ key ] = entry | |
end | |
end | |
def remove_domain domain: | |
return unless remote_dns_entries.map(&:name).include? domain | |
client.domains.delete(name: domain) | |
end | |
def create_domain domain:, entry: | |
remove_domain domain: domain | |
root_ip = entry[:records].find { |record| record[:name] == "@" }[:value] | |
do_domain = DropletKit::Domain.new( | |
name: domain, | |
ip_address: root_ip | |
) | |
client.domains.create(do_domain) | |
end | |
def upsert_record domain:, record: | |
do_record = DropletKit::DomainRecord.new( | |
type: record[:type].upcase, | |
data: record[:value] | |
) | |
do_record.name = record.fetch(:name, "@").gsub(".#{ domain }.", '') | |
if record[:type] == "mx" | |
do_record.priority = record.fetch(:priority, 10) | |
do_record.name = "*" if record[:name] =~ %r{\*\..*$} | |
end | |
if record[:type] == "srv" | |
parts = record[:value].split " " | |
do_record.priority = parts[0] | |
do_record.weight = parts[1] | |
do_record.port = parts[2] | |
do_record.data = parts[3] | |
end | |
client.domain_records.create(do_record, for_domain: domain) | |
rescue DropletKit::Error => e | |
binding.pry | |
end | |
def run | |
local_dns_entries.each do |key, entry| | |
create_domain domain: key, entry: entry | |
entry[:records].each do |record| | |
next if record[:type] == "ns" | |
upsert_record domain: key, record: record | |
end | |
end | |
end | |
end | |
DODNS.run if __FILE__ == $0 |
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
{ | |
"domain": "isin.space.", | |
"records": [ | |
{ | |
"type": "a", | |
"name": "www", | |
"value": "IP ADDRESS" | |
}, | |
{ | |
"type": "a", | |
"name": "@", | |
"value": "IP ADDRESS" | |
}, | |
{ | |
"type": "mx", | |
"priority": 10, | |
"value": "in1-smtp.SOME EMAIL SERVER.com." | |
}, | |
{ | |
"type": "mx", | |
"priority": 20, | |
"value": "in2-smtp.SOME EMAIL SERVER.com." | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment