Class: Zone
Instance Attribute Summary
Attributes inherited from Command
#desc, #name
Instance Method Summary
collapse
#config_dir, #config_file, #imkdir, #mk_config_dir, #mk_user_dir, #param, #user_file
Methods inherited from Command
#add_arg, #add_cmd, #add_flow, #add_flow_from_usage, #add_input, #add_option, #add_options, #command_name?, #get_args_used, #init, #initialize, #run, #show_help, #show_help_option
Constructor Details
This class inherits a constructor from Command
Instance Method Details
#alnum_not_command(value) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/zone.rb', line 112
def alnum_not_command(value)
test = value =~ /[[:alnum:]]+/
if test
if command_name? value
return "name of command", false
else
return nil, true
end
end
return "invalid argument", false
end
|
#create_args ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/zone.rb', line 90
def create_args
add_cmd(ZoneList, "list", "list all owned zones")
add_cmd(ZonePull, "pull", "retrieve local copy of zone data")
add_cmd(ZonePush, "push", "push local changes to server")
add_cmd(ZonePrint, "print", "print zone data. ~~~~ online, ---- local copy")
add_cmd(ZoneAdd, "add", "add record/group/healthcheck")
add_cmd(ZoneCreate, "create", "create new zone")
add_cmd(ZoneDelete, "delete", "delete zone")
add_arg("zname", "name or id of zone", :alnum_not_command)
add_arg("drill-down-param", "names of fields, or indexes of items, for drilling down", :alnum_not_command)
end
|
#create_flows ⇒ Object
102
103
104
105
106
107
108
109
110
|
# File 'lib/zone.rb', line 102
def create_flows
add_flow_from_usage("list")
add_flow_from_usage("create")
add_flow_from_usage("<zname> pull")
add_flow_from_usage("<zname> push")
add_flow_from_usage("<zname> delete")
add_flow_from_usage("<zname> [<drill-down-param>...] add")
add_flow_from_usage("<zname> [<drill-down-param>...] [print]")
end
|
#delete ⇒ Object
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/zone.rb', line 186
def delete
id, name = id_and_name
if (id.nil?)
zone = get_zone(true)
id = zone["ZoneId"]
end
puts "deleting zone with id #{id}"
delete_local_zone_files(id)
Route.do.delete_zone(id)
end
|
#delete_local_zone_files(id) ⇒ Object
179
180
181
182
183
184
|
# File 'lib/zone.rb', line 179
def delete_local_zone_files(id)
files = Dir.glob(File.join("#{config_dir}", "zones", "*.#{id}"))
files.each { |file| File.delete(file) }
files = Dir.glob(File.join("zones", "*.#{id}"))
files.each { |file| File.delete(file) }
end
|
#drill_down(struct, args, level) ⇒ Object
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
|
# File 'lib/zone.rb', line 274
def drill_down(struct, args, level)
if struct.class == Array && struct.length > 1 && level =~ /\[\]/
raise "too many arrays"
end
return struct,level if args.length == 0
if (struct.class == Array)
if args[0] == 'all'
result = [ ]
result_level = nil
args.shift
struct.each { |elt|
drilled_elt, drilled_level = drill_down(elt, args, level + "[]")
result << drilled_elt
result_level = drilled_level
}
return result, result_level
end
if args[0] =~ /\A\d+\Z/
index = args.shift.to_i
if index > struct.length || index <= 0
raise "index #{index} out of bounds"
end
elsif struct.length == 1
index = 1
else
args.unshift "all"
return drill_down(struct, args, level)
end
drill_down(struct[index - 1], args, level + "[#{index}]")
elsif (struct.class == Hash)
args = args.clone
key = args.shift
fullkey = match_key(key, struct)
raise "no (unique) key matches " + key if ! fullkey
drill_down(struct[fullkey], args, level + ".#{fullkey}")
else
raise "argument #{args[0]} not understood"
end
end
|
#execute(args, rest) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/zone.rb', line 124
def execute(args, rest)
@zname = args["zname"]
command = args["command"]
ddparams = args["drill-down-param"]
if ddparams
zone = get_zone
struct, level = drill_down(zone, ddparams, "zone")
command.add_input("struct", struct)
command.add_input("level", level)
command.add_input("sep_char", @sep_char)
command.add_input("toplevel", zone)
end
if command
command.add_input("zone", self)
command.run(rest)
else
raise "error"
end
end
|
#force_online ⇒ Object
320
321
322
|
# File 'lib/zone.rb', line 320
def force_online
@force_online = true
end
|
#get_zone(forceRest = false) ⇒ Object
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
|
# File 'lib/zone.rb', line 208
def get_zone(forceRest = false)
@sep_char = "-"
zonedir = mk_user_dir("zones")
zone = nil
id, name = id_and_name
if @zname
if ! forceRest
if id.nil?
r = name + '\.\d+'
else
r = '.*\.' + id
end
regex = Regexp.new(r)
Dir.new(zonedir).each { |file|
if (file.full_match regex) && (! @force_online)
path = File.join(zonedir, file)
zone = JSON.parse(File.open(path).read)
end
}
end
if (zone.nil?)
dputs "getting zone with id=#{id} and name=#{name}"
@sep_char = "~"
zone = Route.do.get_zone(id, name)
if zone['Records'].nil?
zone['Records'] = {
"A"=> [ ],
"AAAA"=> [ ],
"CNAME"=> [ ],
"MX"=> [ ],
"NS"=> [ ],
"SPF"=> [ ],
"SRV"=> [ ],
"TXT"=> [ ]
}
end
if zone.nil?
raise "zone doesn't exist or is empty"
end
if $debug
File.open("raw.zone","w").write(zone)
end
Route.do.translate_zone(zone)
end
end
zone
end
|
#id_and_name ⇒ Object
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/zone.rb', line 197
def id_and_name
id = name = nil
if @zname.to_s.full_match /\d+/
id = @zname
else
name = @zname
end
name = name.sub(/\.\Z/, "") if ! name.nil?
return id, name
end
|
#local_filename(zone) ⇒ Object
154
155
156
157
|
# File 'lib/zone.rb', line 154
def local_filename(zone)
file_name = zone["DomainName"].sub(/\.\Z/, '')
file_name = file_name + '.' + zone["ZoneId"].to_s
end
|
#match_key(abbrev, struct) ⇒ Object
let user lowercase and abbreviate key; abbreviation must match one key
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/zone.rb', line 257
def match_key(abbrev, struct)
abbrev = abbrev.downcase
key = nil
struct.keys.each { |fullkey|
testkey = fullkey.downcase
return fullkey if abbrev == testkey
if fullkey.downcase.start_with?(abbrev)
if (! key)
key = fullkey
else
return nil
end
end
}
key
end
|
#pull ⇒ Object
173
174
175
176
177
|
# File 'lib/zone.rb', line 173
def pull
zone = get_zone(true)
file_name = write_zone_file(zone, true)
puts "'#{file_name}' saved locally"
end
|
#push ⇒ Object
144
145
146
147
148
149
150
151
152
|
# File 'lib/zone.rb', line 144
def push
puts "pushing zone to server"
zone = get_zone
Route.do.untranslate_zone(zone)
if $debug
File.open("untranslated.zone","w").write(JSON.pretty_generate zone)
end
Route.do.update_zone(zone)
end
|
315
316
317
318
|
# File 'lib/zone.rb', line 315
def set_format
puts "in zone.set_format"
@format = :json
end
|
#write_zone_file(zone, hide_dup = false) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/zone.rb', line 159
def write_zone_file(zone, hide_dup = false)
basename = local_filename(zone)
if hide_dup
mk_config_dir("zones")
file_name = config_file("zones", basename)
File.delete(file_name) if File.exists?(file_name)
File.open(file_name, "w").write(JSON.pretty_generate zone)
end
file_name = user_file("zones", basename)
File.delete(file_name) if File.exists?(file_name)
File.open(file_name, "w").write(JSON.pretty_generate zone)
file_name
end
|