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