-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinode-cli.rb
executable file
·315 lines (258 loc) · 7.08 KB
/
linode-cli.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env ruby
require 'rubygems'
require 'linode'
require 'optparse'
# Where to put this...
class Array
def rest
self[1..-1]
end
end
def l
if not ENV.has_key?('LINODE_API_KEY')
puts "You must define the environment variable LINODE_API_KEY"
exit 1
end
$l ||= Linode.new(:api_key => ENV['LINODE_API_KEY'])
end
class Env
# Populate with usage string in subclass
@usage = nil
def self.go(params)
end
def self.usage
puts "Usage: #{@usage}"
end
end
class SummaryEnv < Env
def self.go(params)
puts "linode"
end
end
class DNSRecord
RecordTypes = [:a, :aaaa, :cname, :mx, :txt]
def initialize(record)
@record = record
end
def type
@record.type.downcase.to_sym
end
def to_s
case type
when :a
unless @record.name.to_s.empty?
"A #{@record.name} #{@record.target}"
else
"A #{@record.target}"
end
when :aaaa
unless @record.name.to_s.empty?
"AAAA #{@record.name} #{@record.target}"
else
"AAAA #{@record.target}"
end
when :cname
"CNAME #{@record.name} #{@record.target}"
when :mx
"MX #{@record.target} #{@record.priority}"
when :txt
unless @record.name.empty?
"TXT #{@record.name} #{@record.target}"
else
"TXT #{@record.target}"
end
end
end
end
class DNSUtil
def self.getDomainNames
l.domain.list.map {|domain| domain.domain}
end
def self.getDomainId(domain)
(l.domain.list.detect {|res| res.domain == domain}).domainid
end
def self.getResourceId(domainid, type, name)
(l.domain.resource.list(:domainid => domainid).detect {|res|
res.type.downcase == type.to_s and res.name == name
}).resourceid
end
def self.getDomainRecords(domain_id)
records = {}
for type in DNSRecord::RecordTypes
records[type] = []
end
l.domain.resource.list(:domainid => domain_id).each do |record|
dns_record = DNSRecord.new record
records[dns_record.type] << dns_record
end
return records
end
def self.getRecords(domain)
domains = {}
unless domain == :all
domain_id = getDomainId domain
domains[domain] = getDomainRecords domain_id
else
domains_raw = l.domain.list
domains_raw.each do |domain_raw|
domains[domain_raw.domain] = getDomainRecords(domain_raw.domainid)
end
end
return domains
end
end
class DNSList < Env
def self.go(params)
puts 'All accessible domains:'
DNSUtil::getDomainNames.each do |domain|
puts ' ' + domain
end
end
end
class DNSShow < Env
@usage = 'dns show <type?> <domain?>'
def self.go(params)
if params.size == 2
type = params[0].downcase.to_sym
domain = params[1]
unless DNSRecord::RecordTypes.include? type
puts "Invalid DNS record type: #{type}"
exit 1
end
elsif params.size == 1
if DNSRecord::RecordTypes.include? params[0].downcase.to_sym
type = params[0].downcase.to_sym
domain = :all
else
type = :all
domain = params[0]
end
elsif params.size == 0
type = :all
domain = :all
else
usage
exit 1
end
DNSUtil::getRecords(domain).each do |domain, records|
if type == :all
records_to_show =
records[:a] +
records[:aaaa] +
records[:cname] +
records[:mx] +
records[:txt]
unless records_to_show.empty?
puts "Showing all records for #{domain}"
end
else
records_to_show = records[type]
unless records_to_show.empty?
puts "Showing #{type.upcase} records for #{domain}"
end
end
records_to_show.each do |r|
puts ' ' + r.to_s
end
end
end
end
class DNSAdd < Env
def self.go(params)
@usage = 'dns add <domain> <host> <ip>'
unless params.size == 3
usage
exit 1
end
domain = params[0]
host = params[1]
ip = params[2]
host = '' if host == '@'
domainid = DNSUtil::getDomainId domain
l.domain.resource.create(
:domainid => domainid,
:type => 'a',
:name => host,
:target => ip
)
end
end
class DNSUpdate < Env
def self.go(params)
@usage = 'dns update <domain> <host> <ip>'
unless params.size == 3
usage
exit 1
end
domain = params[0]
host = params[1]
ip = params[2]
host = '' if host == '@'
domainid = DNSUtil::getDomainId domain
resourceid = DNSUtil::getResourceId domainid, :a, host
l.domain.resource.update(
:domainid => domainid,
:resourceid => resourceid,
:type => 'a',
:name => host,
:target => ip
)
end
end
class DNSDel < Env
def self.go(params)
@usage = 'dns del <domain> <host>'
unless params.size == 2
usage
exit 1
end
domain = params[0]
host = params[1]
host = '' if host == '@'
domainid = DNSUtil::getDomainId domain
resourceid = DNSUtil::getResourceId domainid, :a, host
l.domain.resource.delete(
:domainid => domainid,
:resourceid => resourceid
)
end
end
class DNSEnv < Env
Commands = {
:list => DNSList,
:show => DNSShow,
:add => DNSAdd,
:update => DNSUpdate,
:del => DNSDel
}
@usage = 'linode dns <list, show, add, update, del> ...'
def self.go(params)
if params.size > 0
cmd = params[0].downcase.to_sym
unless Commands.include? cmd
puts "Invalid DNS query command: #{cmd}"
usage
exit 1
end
else
cmd = :list
end
Commands[cmd]::go params.rest
end
end
class LinodeEnv < Env
def self.go(params)
puts "whee"
end
end
ENVS = {
:summary => SummaryEnv,
:dns => DNSEnv,
:l => LinodeEnv
}
env = ARGV[0] && ARGV[0].downcase.to_sym || :summary
unless ENVS.include? env
puts "Not valid env"
exit 1
end
ENVS[env]::go ARGV.rest