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,ip,user_data)
9 # that can be searched iteratively for a free slot
10 # TestPoolIP : look for a free IP address
11 # TestPoolQemu : look for a test_box with no qemu running
13 # pool = [ (hostname1,ip1,user_data1),
14 # (hostname2,ip2,user_data2),
15 # (hostname3,ip3,user_data2),
16 # (hostname4,ip4,user_data4) ]
17 # assuming that ip1 and ip3 are taken (pingable), then we'd get
18 # pool=TestPoolIP(pool)
19 # pool.next_free() -> entry2
20 # pool.next_free() -> entry4
21 # pool.next_free() -> None
22 # that is, even if ip2 is not busy/pingable when the second next_free() is issued
29 def __init__ (self, pool, options,message):
36 def match (self,triple,hostname_or_ip):
38 return h.find(hostname_or_ip)>=0 or (i and i.find(hostname_or_ip)>=0)
40 def locate_entry (self, hostname_or_ip):
41 for (h,i,u) in self.pool:
42 if self.match ( (h,i,u,), hostname_or_ip):
45 utils.header('TestPool.locate_entry: Could not locate entry for %r in pool:'%hostname_or_ip)
48 # the hostnames provided (from a tracker) are considered last
49 def next_free (self, tracker_hostnames):
50 utils.header('TestPool is looking for a %s'%self.message)
51 # create 2 lists of (h,i,u) entries, the ones not in the tracker, and the ones in the tracker
54 for (h,i,u) in self.pool:
56 for hostname in tracker_hostnames:
57 if self.match ( (h,i,u,) , hostname) : in_tracker = True
58 if in_tracker: in_track_pool.append ( (h,i,u,) )
59 else: out_track_pool.append ( (h,i,u,) )
60 # consider outsiders first
61 for (hostname,ip,user_data) in out_track_pool + in_track_pool:
62 utils.header ('* candidate %s' % hostname)
63 for (hostname,ip,user_data) in out_track_pool + in_track_pool:
64 if hostname in self.busy:
66 utils.header('TestPool : checking %s'%hostname)
67 if self.free_hostname(hostname):
68 utils.header('%s is available'%hostname)
69 self.busy.append(hostname)
70 return (hostname,ip,user_data)
72 self.busy.append(hostname)
73 raise Exception, "No space left in pool (%s)"%self.message
75 class TestPoolIP (TestPool):
77 def __init__ (self,pool,options):
78 TestPool.__init__(self,pool,options,"free IP address")
80 def free_hostname (self, hostname):
81 return not self.check_ping(hostname)
83 # OS-dependent ping option (support for macos, for convenience)
84 ping_timeout_option = None
85 # checks whether a given hostname/ip responds to ping
86 def check_ping (self,hostname):
87 if not TestPoolIP.ping_timeout_option:
88 (status,osname) = commands.getstatusoutput("uname -s")
90 raise Exception, "TestPool: Cannot figure your OS name"
92 TestPoolIP.ping_timeout_option="-w"
93 elif osname == "Darwin":
94 TestPoolIP.ping_timeout_option="-t"
96 if self.options.verbose:
97 utils.header ("TestPoolIP: pinging %s"%hostname)
98 command="ping -c 1 %s 1 %s"%(TestPoolIP.ping_timeout_option,hostname)
99 (status,output) = commands.getstatusoutput(command)
102 class TestPoolQemu (TestPool):
104 def __init__ (self,pool,options):
105 TestPool.__init__(self,pool,options,"free qemu box")
107 def free_hostname (self, hostname):
108 return not self.busy_qemu(hostname)
110 # is there a qemu runing on that box already ?
111 def busy_qemu (self, hostname):
112 if self.options.verbose:
113 utils.header("TestPoolQemu: checking for running qemu instances on %s"%hostname)
114 command="ssh -o ConnectTimeout=5 root@%s ps -e -o cmd"%hostname
115 (status,output) = commands.getstatusoutput(command)
116 # if we fail to run that, let's assume we don't have ssh access, so
117 # we pretend the box is busy
120 elif output.find("qemu") >=0 :