class PackerFiles::Ubuntu::Mirrors

Attributes

best[R]
hosts[R]

accessors

proxy[R]

Public Class Methods

new() click to toggle source

Constructor

# File lib/PackerFiles/OS/Ubuntu/Mirrors.rb, line 29
def initialize

   # A Single web page get more than this (in seconds) is SLOW
   limit   = 1 

   # No. of trials for a web page
   trials  = 4  
   
   # Get the name of the http proxy
   @proxy   = ENV['http_proxy'] 

   # Get the host list of mirrors for this country
   code     = Utils::AutoZone.new.country_code.upcase
   @hosts   = country_mirror(code)
   if @hosts.empty?
      @hosts   = [@@default] if @hosts.empty?
      @best    = URI::parse(@@default)
   else
      @hosts   = filter_status_unknown.compact
      f        = Utils::FastestURL.new(limit, trials)
      all      = f.best_urls(@hosts).flatten
      begin
         @best    = URI::parse(all.first)
      rescue Exception => e
         puts "Exception: #{e}, All: #{all.to_s}"
         raise e
      end
   end
end

Private Instance Methods

country_mirror(code) click to toggle source

Given the country code, get a list of mirror sites that match the country code.

# File lib/PackerFiles/OS/Ubuntu/Mirrors.rb, line 62
def country_mirror(code)
  url      = @@mirrors.gsub('@country@', code)
  contents = open(url) {|f| f.read}
  return contents.split("\n")
end
filter_status_unknown() click to toggle source

Given a list of hosts which can act as mirrors, filter out those hosts whose status is unknown, as they can cause updates and new installation packages to fail.

# File lib/PackerFiles/OS/Ubuntu/Mirrors.rb, line 72
def filter_status_unknown
  
  # Parse the HTML from the master index archive.
  page = Nokogiri::HTML(open(@@master))

  # Iterate through the list of hosts
  @hosts.map {|host|
     node = page.css('td').select {|node|
        aref = node.css('a')
        if aref.empty?
          false
        else
          aref.first['href'] == host
        end
     }
     if node.empty?
        nil
     else
        text = node.first.next_element.next_element.text
        if text.include?('Last update unknown')
          nil
        else
          host
        end
     end
  }
end