-
Notifications
You must be signed in to change notification settings - Fork 4
/
ec2-set-dme-dns.rb
executable file
·144 lines (125 loc) · 4.66 KB
/
ec2-set-dme-dns.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env ruby
# Copyright (C) 2011 Vijay Brian Gupta [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# v1.0 May 25, 2011 - Created a script to be run on an ec2 that registers it's
# own fqdn with DNS Made Easy
# Make sure the dnsmeapi.properties file is available in the location from which
# you run this
require 'rubygems'
require 'time'
require 'openssl'
require 'rest_client'
require 'json'
require 'socket'
require 'open-uri'
require 'optparse'
class CNameRecord
def initialize(domainname)
prophash = load_properties
@domainname = domainname
@apiKey = prophash["apiKey"]
@secretKey = prophash["secretKey"]
@requestDate = Time.now.httpdate
@hmac = OpenSSL::HMAC.hexdigest('sha1', @secretKey, @requestDate)
@@dme_rest_url = "http://api.dnsmadeeasy.com/V1.2/domains/"
#intname = hostname + "-internal." + @domainname
end
def load_properties
propertyfile = "dnsmeapi.properties"
properties = {}
File.open(propertyfile, 'r') do |propertyfile|
propertyfile.read.each_line do |line|
line.strip!
if (line[0] != ?# and line[0] != ?=)
i = line.index('=')
if (i)
properties[line[0..i - 1].strip] = line[i + 1..-1].strip
else
properties[line] = ''
end
end
end
end
properties
end
def get_cname_record(name)
response = RestClient.get @@dme_rest_url + @domainname + "/records",
:"x-dnsme-apiKey" => @apiKey,
:"x-dnsme-hmac" => @hmac,
:"x-dnsme-requestDate" => @requestDate,
:accept =>:json
nameresults = JSON.parse(response.to_str).select {
|x| x["name"] == name and x["type"] == "CNAME"}
nameresults[0]
end
def post_cname_record(record)
response = RestClient.post @@dme_rest_url + @domainname + "/records",
record.to_json,
:"x-dnsme-apiKey" => @apiKey,
:"x-dnsme-hmac" => @hmac,
:"x-dnsme-requestDate" => @requestDate,
:"accept" =>:json,
:"content-type" =>:json
JSON.parse(response)
end
def delete_cname_record(id)
response = RestClient.delete @@dme_rest_url + @domainname + "/records/" + id.to_s,
:"x-dnsme-apiKey" => @apiKey,
:"x-dnsme-hmac" => @hmac,
:"x-dnsme-requestDate" => @requestDate,
:accept =>:json
end
end
options = {}
OptionParser.new do|opts|
opts.banner = "Usage: " + File.basename($0) + " [options] ..."
options[:verbose] = false
opts.on( "-v", "--verbose", "Output more information" ) do
options[:verbose] = true
end
opts.on("-h", "--help", "Display this screen" ) do
puts opts
exit
end
end.parse!
@@instance_data_url = "http://169.254.169.254/latest/meta-data/"
fqdn = `hostname`[0..-2] # 0..-2 lops off the trailing newline
publicname = open(@@instance_data_url + 'public-hostname').read + "."
privatename = open(@@instance_data_url + 'local-hostname').read
instance_id = open(@@instance_data_url + 'instance-id').read
hostname = fqdn.split(".")[0]
domainname = fqdn.split(".")[1..-1].join(".")
cnameobject = CNameRecord.new(domainname)
myhost = cnameobject.get_cname_record(hostname)
if myhost && myhost["data"] == publicname
puts [hostname,domainname].join(".") + " is correct in DNS" if options[:verbose]
exit
end
if myhost
puts "Record is not set correctly. Deleting the record." if options[:verbose]
cnameobject.delete_cname_record(myhost["id"])
end
if options[:verbose]
puts [hostname,domainname].join(".") + " doesn't exist in DNS. Adding." if options[:verbose]
end
dnsrecord = { "name" => hostname,
"type" => "CNAME",
"data" => publicname,
"gtdLocation" => "DEFAULT",
"ttl" => 300 }
cnameobject.post_cname_record(dnsrecord)