@@ -9,6 +9,7 @@ import os
99import json
1010import multiprocessing
1111import urllib2
12+ import socket
1213import random
1314import shutil
1415
@@ -342,6 +343,28 @@ def timed_transfer(filename, cache, destination, debug=False):
342343 return str (xrd_exit )
343344
344345
346+ def get_ips (name ):
347+ ipv4s = []
348+ ipv6s = []
349+ try :
350+ info = socket .getaddrinfo (name , 0 , 0 , socket .IPPROTO_TCP )
351+ except :
352+ logging .error ("Unable to look up %s" , name )
353+ return []
354+
355+ for tuple in info :
356+ if (tuple [0 ] == socket .AF_INET ):
357+ ipv4s .append (tuple [4 ][0 ])
358+ elif (tuple [0 ] == socket .AF_INET6 ):
359+ ipv6s .append (tuple [4 ][0 ])
360+
361+ # randomize the order of each
362+ random .shuffle (ipv4s )
363+ random .shuffle (ipv6s )
364+
365+ # always prefer IPv4
366+ return ipv4s + ipv6s
367+
345368def get_best_stashcache ():
346369
347370 # First, check for caches.json file in this file's directory:
@@ -371,12 +394,8 @@ def get_best_stashcache():
371394 # Remove the first comma
372395 caches_string = caches_string [1 :]
373396
374- # Here is a list from the output of the command:
375- # attr -qg host_list /cvmfs/oasis.opensciencegrid.org
376- geo_ip_sites = "http://cvmfs-s1fnal.opensciencegrid.org:8000/cvmfs/oasis.opensciencegrid.org;http://cvmfs-s1bnl.opensciencegrid.org:8000/cvmfs/oasis.opensciencegrid.org;http://cvmfs-egi.gridpp.rl.ac.uk:8000/cvmfs/oasis.opensciencegrid.org;http://klei.nikhef.nl:8000/cvmfs/oasis.opensciencegrid.org;http://cvmfsrep.grid.sinica.edu.tw:8000/cvmfs/oasis.opensciencegrid.org" .split (';' )
377-
378- # Add HCC's, for good measure
379- geo_ip_sites .insert (0 ,"http://hcc-cvmfs.unl.edu:8000/cvmfs/config-osg.opensciencegrid.org" )
397+ # Use the geo ip service on the WLCG Web Proxy Auto Discovery machines
398+ geo_ip_sites = ["wlcg-wpad.cern.ch" , "wlcg-wpad.fnal.gov" ]
380399
381400 # Append text before caches string
382401 append_text = "api/v1.0/geo/stashcp"
@@ -390,20 +409,24 @@ def get_best_stashcache():
390409 i = 0
391410 while found == False and i < len (geo_ip_sites ):
392411 cur_site = geo_ip_sites [i ]
393- logging .debug ("Trying geoip site of: %s" , cur_site )
394- final_url = "%s/%s/%s" % (cur_site , append_text , caches_string )
395- logging .debug ("Querying for closest cache: %s" , final_url )
396- try :
397- # Make the request
398- req = urllib2 .Request (final_url , headers = headers )
399- response = urllib2 .urlopen (req )
400- if response .getcode () == 200 :
401- logging .debug ("Got error code 200 from %s" , cur_site )
402- found = True
403- break
404- except urllib2 .URLError , e :
405- logging .debug ("URL error: %s" , str (e ))
406- i += 1
412+ headers ['Host' ] = cur_site
413+ for ip in get_ips (cur_site ):
414+ logging .debug ("Trying geoip site of: %s [%s]" , cur_site , ip )
415+ final_url = "http://%s/%s/%s" % (ip , append_text , caches_string )
416+ logging .debug ("Querying for closest cache: %s" , final_url )
417+ try :
418+ # Make the request
419+ req = urllib2 .Request (final_url , headers = headers )
420+ response = urllib2 .urlopen (req , timeout = 10 )
421+ if response .getcode () == 200 :
422+ logging .debug ("Got OK code 200 from %s" , cur_site )
423+ found = True
424+ break
425+ except urllib2 .URLError , e :
426+ logging .debug ("URL error: %s" , str (e ))
427+ except Exception , e :
428+ logging .debug ("Error: %s" , str (e ))
429+ i += 1
407430
408431 if found == False :
409432 # Unable to find a geo_ip server to use, return random choice from caches!
0 commit comments