svn keywords
[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 store (self):
32         out = file(self.filename,'w')
33         for track in self.tracks:
34             out.write('%s\n'%(track))
35         out.close()
36
37     def record (self,track):
38         for already in self.tracks:
39             if already==track:
40                 print '%s is already included in %s'%(already,self.filename)
41                 return
42         if self.options.dry_run:
43             print 'dry_run: Tracker.record - skipping %s'%(track)
44             return
45         self.tracks.append( track )
46         print "Recorded %s in tracker %s"%(track,self.filename)
47
48     # this actually stops the old instances to fit the number of instances 
49     def free (self):
50         # number of instances to stop
51         how_many=len(self.tracks)-self.instances
52         # nothing todo until we have more than keep_vservers in the tracker
53         if how_many <= 0:
54             print 'Tracker.free : limit %d not reached'%self.instances
55             return
56         to_stop = self.tracks[:how_many]
57         for track in to_stop:
58             command = self.stop_command (track)
59             utils.system(command)
60         if not self.options.dry_run:
61             self.tracks = self.tracks[how_many:]
62
63     # this stops ALL known instances
64     def cleanup (self):
65         for track in self.tracks:
66             command=self.stop_command(track)
67             utils.system(command)
68         if not self.options.dry_run:
69             self.tracks=[]
70
71 class TrackerPlc (Tracker):
72     
73     DEFAULT_FILENAME=os.environ['HOME']+"/tracker-plcs"
74     # how many concurrent plcs are we keeping alive - adjust with the IP pool size
75     DEFAULT_MAX_INSTANCES = 12
76
77     def __init__ (self,options,filename=None,instances=0):
78         if not filename: filename=TrackerPlc.DEFAULT_FILENAME
79         if not instances: instances=TrackerPlc.DEFAULT_MAX_INSTANCES
80         Tracker.__init__(self,options,filename,instances)
81
82     def record (self, hostname, vservername):
83         Tracker.record (self,"%s@%s"%(hostname,vservername))
84
85     def stop_command (self, track):
86         (hostname,vservername) = track.split('@')
87         return TestSsh(hostname).actual_command("vserver --silent %s stop"%vservername)
88         
89
90 class TrackerQemu (Tracker):
91
92     DEFAULT_FILENAME=os.environ['HOME']+"/tracker-emus"
93     # how many concurrent plcs are we keeping alive - adjust with the IP pool size
94     DEFAULT_MAX_INSTANCES = 2
95
96     def __init__ (self,options,filename=None,instances=0):
97         if not filename: filename=TrackerQemu.DEFAULT_FILENAME
98         if not instances: instances=TrackerQemu.DEFAULT_MAX_INSTANCES
99         Tracker.__init__(self,options,filename,instances)
100
101     def record (self, hostname, buildname, nodename):
102         Tracker.record (self,"%s@%s@%s"%(hostname,buildname,nodename))
103
104     def stop_command (self, track):
105         (hostname,buildname,nodename) = track.split('@')
106         return TestSsh(hostname).actual_command("%s/qemu-%s/qemu-kill-node this"%(buildname,nodename))