less pollution in logs, longer timeouts
[tests.git] / system / Trackers.py
1 #!/usr/bin/python
2 # $Id$
3
4 import os
5
6 import utils
7 from TestSsh import TestSsh
8
9 # 2 types of trackers
10 # (*) plc trackers remembers the running myplcs
11 # (*) qemu trackers keeps track of the running qemu nodes
12 #
13 # trackers allow us to let the test run after the build has finished, 
14 # and to kill/stop the oldest instances later when we need space
15
16
17 #################### Tracker
18 class Tracker:
19     
20     def __init__ (self, options,filename, instances):
21         self.options=options
22         self.filename=filename
23         self.instances=instances
24         try:
25             tracks=file(self.filename).readlines()
26             tracks = [ track.strip() for track in tracks ]
27         except:
28             tracks=[]
29         self.tracks = [track for track in tracks if track]
30
31     def list (self):
32         try:
33             contents=file(self.filename).read()
34             print "==>",self.filename,"<=="
35             print contents
36         except:
37             print "xxxxxxxxxxxx",self.filename,"not found"
38
39     def store (self):
40         out = file(self.filename,'w')
41         for track in self.tracks:
42             out.write('%s\n'%(track))
43         out.close()
44
45     def record (self,track):
46         for already in self.tracks:
47             if already==track:
48                 print '%s is already included in %s'%(already,self.filename)
49                 return
50         if self.options.dry_run:
51             print 'dry_run: Tracker.record - skipping %s'%(track)
52             return
53         self.tracks.append( track )
54         print "Recorded %s in tracker %s"%(track,self.filename)
55
56     # this actually stops the old instances, so that the total fits in the number of instances 
57     def free (self):
58         # number of instances to stop
59         how_many=len(self.tracks)-self.instances
60         # nothing todo until we have more than keep_vservers in the tracker
61         if how_many <= 0:
62             print 'Tracker.free : limit %d not reached'%self.instances
63             return
64         to_stop = self.tracks[:how_many]
65         for track in to_stop:
66             command = self.stop_command (track)
67             utils.header("Trackers.free track : %s"%command)
68             utils.system(command)
69         if not self.options.dry_run:
70             self.tracks = self.tracks[how_many:]
71
72     # this stops ALL known instances
73     def cleanup (self):
74         for track in self.tracks:
75             command=self.stop_command(track)
76             utils.header("Trackers.cleanup track : %s"%command)
77             utils.system(command)
78         if not self.options.dry_run:
79             self.tracks=[]
80
81 class TrackerPlc (Tracker):
82     
83     DEFAULT_FILENAME=os.environ['HOME']+"/tracker-plcs"
84     # how many concurrent plcs are we keeping alive - adjust with the IP pool size
85     DEFAULT_MAX_INSTANCES = 12
86
87     def __init__ (self,options,filename=None,instances=0):
88         if not filename: filename=TrackerPlc.DEFAULT_FILENAME
89         if not instances: instances=TrackerPlc.DEFAULT_MAX_INSTANCES
90         Tracker.__init__(self,options,filename,instances)
91
92     def record (self, hostname, vservername):
93         Tracker.record (self,"%s@%s"%(hostname,vservername))
94
95     def stop_command (self, track):
96         (hostname,vservername) = track.split('@')
97         return TestSsh(hostname).actual_command("vserver --silent %s stop"%vservername)
98         
99
100 class TrackerQemu (Tracker):
101
102     DEFAULT_FILENAME=os.environ['HOME']+"/tracker-qemus"
103     # how many concurrent plcs are we keeping alive - adjust with the IP pool size
104     DEFAULT_MAX_INSTANCES = 3
105
106     def __init__ (self,options,filename=None,instances=0):
107         if not filename: filename=TrackerQemu.DEFAULT_FILENAME
108         if not instances: instances=TrackerQemu.DEFAULT_MAX_INSTANCES
109         Tracker.__init__(self,options,filename,instances)
110
111     def record (self, hostname, buildname, nodename):
112         Tracker.record (self,"%s@%s@%s"%(hostname,buildname,nodename))
113
114     def stop_command (self, track):
115         (hostname,buildname,nodename) = track.split('@')
116         return TestSsh(hostname).actual_command("%s/qemu-%s/qemu-kill-node this"%(buildname,nodename))