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 if self.options.quiet:
42 print 'TestPool is looking for a free IP address',
43 for (host,ip,user_data) in self.pool:
46 if not self.options.quiet:
47 utils.header('TestPool : checking %s'%host)
48 if self.options.quiet:
50 if not TestPool.check_ping (host):
51 if not self.options.quiet:
52 utils.header('%s is available'%host)
55 self.busy.append(host)
56 return (host,ip,user_data)
58 self.busy.append(host)
61 # OS-dependent ping option (support for macos, for convenience)
62 ping_timeout_option = None
63 # checks whether a given hostname/ip responds to ping
65 def check_ping (hostname):
66 if not TestPool.ping_timeout_option:
67 (status,osname) = commands.getstatusoutput("uname -s")
69 raise Exception, "TestPool: Cannot figure your OS name"
71 TestPool.ping_timeout_option="-w"
72 elif osname == "Darwin":
73 TestPool.ping_timeout_option="-t"
75 command="ping -c 1 %s 1 %s"%(TestPool.ping_timeout_option,hostname)
76 (status,output) = commands.getstatusoutput(command)