2 # Thierry Parmentelat - INRIA Sophia Antipolis
6 # allows to pick an available IP among a pool
8 # input is expressed as a list of tuples ('hostname_or_ip',user_data)
9 # can be searched iteratively
11 # pool = [ (hostname1,ip1,user_data1), (hostname2,ip2,user_data2),
12 # (hostname3,ip3,user_data2), (hostname4,ip4,user_data4) ]
13 # assuming that ip1 and ip3 are taken (pingable), then we'd get
15 # pool.next_free() -> entry2
16 # pool.next_free() -> entry4
17 # pool.next_free() -> None
18 # that is, even if ip2 is not busy/pingable when the second next_free() is issued
25 def __init__ (self, pool, options):
31 def locate (self, hostname_or_ip, busy=False):
32 for (h,i,u) in self.pool:
33 if h.find(hostname_or_ip)>=0 or i.find(hostname_or_ip)>=0 :
40 # if preferred is provided, let's re-order
41 for (host,ip,user_data) in self.pool:
44 utils.header('TestPool : checking %s'%host)
45 if not TestPool.check_ping (host):
46 utils.header('%s is available'%host)
47 self.busy.append(host)
48 return (host,ip,user_data)
50 self.busy.append(host)
53 # OS-dependent ping option (support for macos, for convenience)
54 ping_timeout_option = None
55 # checks whether a given hostname/ip responds to ping
57 def check_ping (hostname):
58 if not TestPool.ping_timeout_option:
59 (status,osname) = commands.getstatusoutput("uname -s")
61 raise Exception, "TestPool: Cannot figure your OS name"
63 TestPool.ping_timeout_option="-w"
64 elif osname == "Darwin":
65 TestPool.ping_timeout_option="-t"
67 command="ping -c 1 %s 1 %s"%(TestPool.ping_timeout_option,hostname)
68 (status,output) = commands.getstatusoutput(command)