Skip to content

Instantly share code, notes, and snippets.

@trinitronx
Created May 16, 2013 17:20
Show Gist options
  • Save trinitronx/5593422 to your computer and use it in GitHub Desktop.
Save trinitronx/5593422 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
require 'rubygems'
require 'net/http'
require 'net/ping/tcp'
require 'pp'
mirrorlist_host = 'mirrors.ubuntu.com'
timeout=0.05
uri_list = []
class EmptyListException < Exception
end
Net::HTTP.start(mirrorlist_host) do |http|
resp = http.get("/mirrors.txt")
resp.body.each_line do |line|
uri_list.push( URI(line) )
end
end
puts "Total Mirrors Found: #{uri_list.length}"
all_uris = uri_list.clone
File.open("mirrors.txt", 'wt') do |f|
try_count=0
# Remove hosts with >50ms ping
begin
uri_list.delete_if do |uri|
# puts "LENGTH: #{uri_list.length}"
# puts uri_list
if Net::Ping::TCP.new('mirror.vcu.edu',80, timeout).ping
printf "%40s\t%s\n", uri.host, "\033[1;32m✓\033[0m"
f.write(uri.to_s + "\n")
false
else
printf "%40s\t%s\n", uri.host, "\033[1;31m✗\033[0m"
# puts "Removing index: #{uri_list.find_index(uri)}"
true
end
end
try_count += 1
# puts "FINAL LIST:"
# puts uri_list
if uri_list.length.zero? || uri_list.length < 5
f.truncate(0)
puts "All mirror URIs failed to pass ping test!"
raise EmptyListException, 'All mirror URIs failed to pass ping test!'
end
rescue EmptyListException
# restart with full list
uri_list = all_uris.clone
puts "Retrying with full url list"
puts "TRIES: #{try_count}"
puts "LEN : #{uri_list.length}"
puts "ZERO?: #{uri_list.length.zero?}"
if try_count < 3
retry
else
timeout += 0.01
puts "Increasing timeout: #{timeout}"
retry
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment