Class: Zone
Instance Attribute Summary collapse
-
#zone_file ⇒ Object
readonly
Returns the value of attribute zone_file.
Attributes inherited from Command
Instance Method Summary collapse
- #alnum_not_command(value) ⇒ Object
- #audit ⇒ Object
- #create_args ⇒ Object
- #create_flows ⇒ Object
- #delete ⇒ Object
- #delete_local_zone_files(id) ⇒ Object
- #drill_down(struct, args, level) ⇒ Object
- #execute(args, rest) ⇒ Object
- #force_online ⇒ Object
- #get_zone(forceRest = false) ⇒ Object
- #group_sanity_check(zone, groupname) ⇒ Object
- #id_and_name ⇒ Object
- #local_filename(name, id) ⇒ Object
- #pull(target_file) ⇒ Object
- #push ⇒ Object
- #remove_audit ⇒ Object
- #sanity_check(zone) ⇒ Object
- #set_format ⇒ Object
- #set_zone_file(zone = nil) ⇒ Object
- #show_audit ⇒ Object
- #validate_records(group) ⇒ Object
- #validate_zone(zone) ⇒ Object
- #write_new_zone(zone) ⇒ Object
- #write_zone_file(zone, save_backup_ok) ⇒ Object
- #write_zone_to_single_file(zone, filename) ⇒ Object
Methods included from ConfigReader
#config_dir, #config_file, #mkdir, #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?, #flow_passes_parse, #flow_passes_preconditions, #get_args_used, #init, #initialize, #option_help_string, #run, #show_help, #show_help_option
Constructor Details
This class inherits a constructor from Command
Instance Attribute Details
#zone_file ⇒ Object (readonly)
Returns the value of attribute zone_file.
153 154 155 |
# File 'lib/zone.rb', line 153 def zone_file @zone_file end |
Instance Method Details
#alnum_not_command(value) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/zone.rb', line 186 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 |
#audit ⇒ Object
390 391 392 393 394 395 396 |
# File 'lib/zone.rb', line 390 def audit audit_file = config_file( "audit", @zone_file) File.open(audit_file, 'a') { | file | file.write($USER_ENTERED_COMMAND) file.write("\n") } end |
#create_args ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/zone.rb', line 155 def create_args add_cmd(ZoneList, "list", "list all owned zones. ! outdated, * edited, - pulled") add_cmd(ZonePull, "pull", "retrieve local copy of zone data") add_cmd(ZonePush, "push", "push local changes to server") add_cmd(ZoneAudit, "audit", "show the edits done on the zone locally") add_cmd(ZonePrint, "print", "print zone data. ~~~~ online, ---- local copy") add_cmd(ZoneAdd, "add", "add record/group/healthcheck") add_cmd(ZoneUpdate, "update", "update field") add_cmd(ZoneCreate, "create", "create new zone") add_cmd(ZoneDelete, "delete", "delete zone") add_cmd(ZoneSetHealthCheck, "sethc", "set health check for group member") add_cmd(ZoneDiff, "diff", "show differences between (local, pulled, operational) versions of a 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
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/zone.rb', line 171 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> audit") add_flow_from_usage("<zname> diff") add_flow_from_usage("<zname> [<drill-down-param>...] add") add_flow_from_usage("<zname> [<drill-down-param>...] update") add_flow_from_usage("<zname> <drill-down-param>... delete") add_flow_from_usage("<zname> [<drill-down-param>...] [print]") add_flow_from_usage("<zname> <drill-down-param>... sethc") end |
#delete ⇒ Object
433 434 435 436 437 |
# File 'lib/zone.rb', line 433 def delete puts "deleting zone with id #{@zone_id}" delete_local_zone_files(@zone_id) Route.do.delete_zone(@zone_id) end |
#delete_local_zone_files(id) ⇒ Object
426 427 428 429 430 431 |
# File 'lib/zone.rb', line 426 def delete_local_zone_files(id) files = Dir.glob(config_file(ZONEDIR, "*.#{id}")) files.each { |file| File.delete(file) } files = Dir.glob(user_file(ZONEDIR, "*.#{id}")) files.each { |file| File.delete(file) } end |
#drill_down(struct, args, level) ⇒ Object
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/zone.rb', line 473 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 struct.length == 0 raise "cannot drill down empty array" 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) # raise "argument #{args[0]} not understood" end drill_down(struct[index - 1], args, level + "[#{index}]") elsif (struct.class == Hash) args = args.clone key = args.shift fullkey = Util.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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/zone.rb', line 256 def execute(args, rest) @zname = args["zname"] set_zone_file 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) command.add_input("rest", rest) end if command command.add_input("zone", self) command.run(rest) else raise "error" end end |
#force_online ⇒ Object
521 522 523 |
# File 'lib/zone.rb', line 521 def force_online @force_online = true end |
#get_zone(forceRest = false) ⇒ Object
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/zone.rb', line 450 def get_zone(forceRest = false) zone = nil if forceRest || (not File.exists?(@zone_file)) @sep_char = "~" if @zone_id.nil? raise "unable to calculate zone id" end zone = Route.do.get_zone(@zone_id, nil) if $debug > 1 File.open("raw.zone","w").write(zone) end Route.do.translate_zone(zone) else @sep_char = "-" zone = JSON.parse(File.open(@zone_file).read) end if zone.nil? raise "zone doesn't exist or is empty" end zone end |
#group_sanity_check(zone, groupname) ⇒ Object
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 |
# File 'lib/zone.rb', line 278 def group_sanity_check(zone, groupname) zone[groupname].each_with_index { | group, index | numRecords = 0 primaries = 0 grouppath = "#{groupname}[#{index+1}]" group['Group'].each { |rtype, array| if array.length > 0 if array.length > 0 numRecords += array.length if array.length < 2 @errors << "need 2 '#{rtype}' records in #{grouppath}" end end array.each { | element | if element['IsPrimary'] primaries += 1 end } end } if numRecords == 0 @errors << "empty group: #{grouppath}" end if groupname == 'FailoverGroups' if numRecords != 2 @errors << "failover group #{grouppath} must have exactly 2 records" end if primaries != 1 @errors << "failover group #{grouppath} must have exactly 1 primary" end end } end |
#id_and_name ⇒ Object
439 440 441 442 443 444 445 446 447 448 |
# File 'lib/zone.rb', line 439 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(name, id) ⇒ Object
337 338 339 340 |
# File 'lib/zone.rb', line 337 def local_filename(name, id) name.sub!(/\.\Z/, '') user_file(ZONEDIR, "#{name}.#{id}") end |
#pull(target_file) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/zone.rb', line 412 def pull(target_file) zone = get_zone(true) if target_file @zone_file = target_file # user supplied file with -f option end dputs "in zone.pull, @zname=#{@zname}, zone_file=#{@zone_file}" if @zone_file == @zname raise "zone filename can't be same as zone name" end write_zone_file(zone, true) remove_audit puts "'#{@zone_file}' saved locally" end |
#push ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/zone.rb', line 321 def push zone = get_zone sanity_check(zone) puts "pushing zone to server" Route.do.untranslate_zone(zone) if $debug > 1 File.open("untranslated.zone","w").write(JSON.pretty_generate zone) end dputs "in push, version before update = #{zone["Version"]}" zone = Route.do.update_zone(zone) Route.do.translate_zone(zone) dputs "in push, version after update = #{zone["Version"]}" write_zone_file(zone, true) remove_audit end |
#remove_audit ⇒ Object
398 399 400 401 |
# File 'lib/zone.rb', line 398 def remove_audit audit_file = config_file( "audit", @zone_file) File.delete(audit_file) end |
#sanity_check(zone) ⇒ Object
312 313 314 315 316 317 318 319 |
# File 'lib/zone.rb', line 312 def sanity_check(zone) @errors = [ ] group_sanity_check(zone, 'LoadBalancingGroups') group_sanity_check(zone, 'FailoverGroups') if @errors.length > 0 raise @errors.join("\n") end end |
#set_format ⇒ Object
517 518 519 |
# File 'lib/zone.rb', line 517 def set_format @format = :json end |
#set_zone_file(zone = nil) ⇒ Object
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 |
# File 'lib/zone.rb', line 198 def set_zone_file(zone = nil) if zone @zone_id = zone["ZoneId"] @zone_file = local_filename(zone["DomainName"], @zone_id) return end return if @zname.nil? if File.exists?(@zname) @zone_file = @zname @standard_zone_file = false return end @standard_zone_file = true id, name = id_and_name if id.nil? r = name + '\.(\d+)' else r = '.*\.(' + id + ')' end regex = Regexp.new(r) Dir.new(user_file(ZONEDIR)).each { |file| if (m = file.full_match(regex)) @zone_file = user_file(ZONEDIR, file) @zone_id = m[1] break end } unless @zone_file zones = Route.do.get_all_zones zones.each { |zonehash| zonehash_name = zonehash['DomainName'].sub!(/\.\Z/, '') if name && zonehash_name == name id = zonehash['ZoneId'] break elsif id && zonehash['ZoneId'].to_s == id name = zonehash_name dputs "set_zone_file, name = #{name}, id = #{id}" break end } if name && id @zone_file = local_filename(name, id) @zone_id = id else raise "Bad zname or id (#{@zname})" end end end |
#show_audit ⇒ Object
403 404 405 406 407 408 409 410 |
# File 'lib/zone.rb', line 403 def show_audit audit_file = config_file( "audit", @zone_file) if File.exists? audit_file puts File.read(audit_file) else puts "no edits on the zone" end end |
#validate_records(group) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/zone.rb', line 360 def validate_records(group) group.each { |rtype, value| re = case rtype.to_s when "A" /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ when "AAAA" /^(((?=.*(::))(?!.*\3.+\3))\3?|[\dA-F]{1,4}:)([\dA-F]{1,4}(\3|:\b)|\2){5}(([\dA-F]{1,4}(\3|:\b|$)|\2){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\z/i when "CNAME" /[a-zA-Z]/ else /.*/ end value.each { |record| if not record["Rdata"] =~ re raise "Rdata '#{record["Rdata"]}' in wrong format for type #{rtype}" #load orignal zone zone = get_zone() end } } end |
#validate_zone(zone) ⇒ Object
350 351 352 353 354 355 356 357 358 |
# File 'lib/zone.rb', line 350 def validate_zone(zone) validate_records(zone["Records"]) zone["LoadBalancingGroups"].each { |group| validate_records(group["Group"]) } zone["FailoverGroups"].each { |group| validate_records(group["Group"]) } end |
#write_new_zone(zone) ⇒ Object
247 248 249 250 251 252 253 254 |
# File 'lib/zone.rb', line 247 def write_new_zone(zone) set_zone_file(zone) @standard_zone_file = true write_zone_file(zone, true) zonelist = ZoneList.new(nil, nil) zonelist.add_input("zone", self) zonelist.execute(nil, nil) end |
#write_zone_file(zone, save_backup_ok) ⇒ Object
382 383 384 385 386 387 388 |
# File 'lib/zone.rb', line 382 def write_zone_file(zone, save_backup_ok) write_zone_to_single_file(zone, @zone_file) audit if save_backup_ok && @standard_zone_file write_zone_to_single_file(zone, config_file(@zone_file)) end end |
#write_zone_to_single_file(zone, filename) ⇒ Object
342 343 344 345 346 347 348 |
# File 'lib/zone.rb', line 342 def write_zone_to_single_file(zone, filename) validate_zone(zone) File.delete(filename) if File.exists?(filename) File.open(filename, "w") { |file| file.write(JSON.pretty_generate zone) } end |