can clean nodes
[tests.git] / system / TestPlc.py
1 # $Id$
2 import os, os.path
3 import sys
4 import xmlrpclib
5 import datetime
6 import traceback
7 import utils
8 from TestSite import TestSite
9 from TestNode import TestNode
10 from TestUser import TestUser
11 from TestKey import TestKey
12
13 # step methods must take (self, options) and return a boolean
14
15 class TestPlc:
16
17     def __init__ (self,plc_spec):
18         self.plc_spec=plc_spec
19         self.url="https://%s:443/PLCAPI/"%plc_spec['hostname']
20         self.server=xmlrpclib.Server(self.url,allow_none=True)
21         self.path=os.path.dirname(sys.argv[0])
22         try:
23             self.vserverip=plc_spec['vserverip']
24             self.vservername=plc_spec['vservername']
25             self.vserver=True
26         except:
27             self.vserver=False
28         
29     def name(self):
30         name=self.plc_spec['name']
31         if self.vserver:
32             return name+"[%s]"%self.vservername
33         else:
34             return name+"[chroot]"
35
36     # define the API methods on this object through xmlrpc
37     # would help, but not strictly necessary
38     def connect (self):
39         pass
40     
41     # build the full command so command gets run in the chroot/vserver
42     def run_command(self,command):
43         if self.vserver:
44             return "vserver %s exec %s"%(self.vservername,command)
45         else:
46             return "chroot /plc/root %s"%command
47
48     def ssh_command(self,command):
49         if self.plc_spec['hostname'] == "localhost":
50             return command
51         else:
52             return "ssh " + self.plc_spec['hostname'] + " " + command
53
54     def full_command(self,command):
55         return self.ssh_command(self.run_command(command))
56
57     def run_in_guest (self,command):
58         return utils.system(self.full_command(command))
59     def run_in_host (self,command):
60         return utils.system(self.ssh_command(command))
61
62     # xxx quick n dirty
63     def run_in_guest_piped (self,local,remote):
64         return utils.system(local+" | "+self.full_command(command))
65
66     def auth_root (self):
67         return {'Username':self.plc_spec['PLC_ROOT_USER'],
68                 'AuthMethod':'password',
69                 'AuthString':self.plc_spec['PLC_ROOT_PASSWORD'],
70                 'Role' : self.plc_spec['role']
71                 }
72     def locate_site (self,sitename):
73         for site in self.plc_spec['sites']:
74             if site['site_fields']['name'] == sitename:
75                 return site
76             if site['site_fields']['login_base'] == sitename:
77                 return site
78         raise Exception,"Cannot locate site %s"%sitename
79         
80     def locate_key (self,keyname):
81         for key in self.plc_spec['keys']:
82             if key['name'] == keyname:
83                 return key
84         raise Exception,"Cannot locate key %s"%keyname
85         
86     def kill_all_vmwares(self):
87         utils.header('Killing any running vmware or vmplayer instance')
88         utils.system('pgrep vmware | xargs -r kill')
89         utils.system('pgrep vmplayer | xargs -r kill ')
90         utils.system('pgrep vmware | xargs -r kill -9')
91         utils.system('pgrep vmplayer | xargs -r kill -9')
92         
93     #################### step methods
94
95     ### uninstall
96     def uninstall_chroot(self,options):
97         self.run_in_host('service plc safestop')
98         #####detecting the last myplc version installed and remove it
99         self.run_in_host('rpm -e myplc')
100         ##### Clean up the /plc directory
101         self.run_in_host('rm -rf  /plc/data')
102         return True
103
104     def uninstall_vserver(self,options):
105         self.run_in_host("vserver --silent %s delete"%self.vservername)
106         return True
107
108     def uninstall(self,options):
109         if self.vserver:
110             return self.uninstall_vserver(options)
111         else:
112             return self.uninstall_chroot(options)
113
114     ### install
115     def install_chroot(self,options):
116         utils.header('Installing from %s'%options.myplc_url)
117         url=options.myplc_url
118         utils.system('rpm -Uvh '+url)
119         utils.system('service plc mount')
120         return True
121
122     # xxx this would not work with hostname != localhost as mylc-init-vserver was extracted locally
123     def install_vserver_create(self,options):
124         # we need build dir for myplc-init-vserver
125         build_dir=self.path+"/build"
126         if not os.path.isdir(build_dir):
127             if utils.system("svn checkout %s %s"%(options.build_url,build_dir)) != 0:
128                 raise Exception,"Cannot checkout build dir"
129         # the repo url is taken from myplc-url 
130         # with the last two steps (i386/myplc...) removed
131         repo_url = options.myplc_url
132         repo_url = os.path.dirname(repo_url)
133         repo_url = os.path.dirname(repo_url)
134         command="%s/myplc-init-vserver.sh %s %s -- --interface eth0:%s"%\
135             (build_dir,self.vservername,repo_url,self.vserverip)
136         if utils.system(command) != 0:
137             raise Exception,"Could not create vserver for %s"%self.vservername
138         return True
139
140     def install_vserver_native(self,options):
141         self.run_in_guest("yum -y install myplc-native")
142         return True
143
144     def install(self,options):
145         if self.vserver:
146             return self.install_vserver_create(options)
147             return self.install_vserver_yum(options)
148         else:
149             return self.install_chroot(options)
150
151     ### 
152     def configure(self,options):
153         tmpname='%s/%s.plc-config-tty'%(options.path,self.name())
154         fileconf=open(tmpname,'w')
155         for var in [ 'PLC_NAME',
156                      'PLC_ROOT_PASSWORD',
157                      'PLC_ROOT_USER',
158                      'PLC_MAIL_ENABLED',
159                      'PLC_MAIL_SUPPORT_ADDRESS',
160                      'PLC_DB_HOST',
161                      'PLC_API_HOST',
162                      'PLC_WWW_HOST',
163                      'PLC_BOOT_HOST',
164                      'PLC_NET_DNS1',
165                      'PLC_NET_DNS2']:
166             fileconf.write ('e %s\n%s\n'%(var,self.plc_spec[var]))
167         fileconf.write('w\n')
168         fileconf.write('q\n')
169         fileconf.close()
170         utils.system('cat %s'%tmpname)
171         self.run_in_guest('plc-config-tty < %s'%tmpname)
172         utils.system('rm %s'%tmpname)
173         return True
174
175     # the chroot install is slightly different to this respect
176     def start(self, options):
177         if self.vserver:
178             self.run_in_guest('service plc start')
179         else:
180             self.run_in_host('service plc start')
181         return True
182         
183     def stop(self, options):
184         if self.vserver:
185             self.run_in_guest('service plc stop')
186         else:
187             self.run_in_host('service plc stop')
188         return True
189         
190     # could use a TestKey class
191     def store_keys(self, options):
192         for key_spec in self.plc_spec['keys']:
193             TestKey(self,key_spec).store_key()
194         return True
195
196     def clean_keys(self, options):
197         utils.system("rm -rf %s/keys/"%self.path)
198
199     def sites (self,options):
200         return self.do_sites(options)
201     
202     def clean_sites (self,options):
203         return self.do_sites(options,action="delete")
204     
205     def do_sites (self,options,action="add"):
206         for site_spec in self.plc_spec['sites']:
207             test_site = TestSite (self,site_spec)
208             if (action != "add"):
209                 utils.header("Deleting site %s in %s"%(test_site.name(),self.name()))
210                 test_site.delete_site()
211                 # deleted with the site
212                 #test_site.delete_users()
213                 continue
214             else:
215                 utils.header("Creating site %s & users in %s"%(test_site.name(),self.name()))
216                 test_site.create_site()
217                 test_site.create_users()
218         return True
219
220     def nodes (self, options):
221         return self.do_nodes(options)
222     def clean_nodes (self, options):
223         return self.do_nodes(options,action="delete")
224
225     def do_nodes (self, options,action="add"):
226         for site_spec in self.plc_spec['sites']:
227             test_site = TestSite (self,site_spec)
228             if action != "add":
229                 utils.header("Deleting nodes in site %s"%test_site.name())
230                 for node_spec in site_spec['nodes']:
231                     test_node=TestNode(self,test_site,node_spec)
232                     utils.header("Deleting %s"%test_node.name())
233                     test_node.delete_node()
234             else:
235                 utils.header("Creating nodes for site %s in %s"%(test_site.name(),self.name()))
236                 for node_spec in site_spec['nodes']:
237                     utils.show_spec('Creating node %s'%node_spec,node_spec)
238                     test_node = TestNode (self,test_site,node_spec)
239                     test_node.create_node ()
240         return True
241
242     def bootcd (self, options):
243         for site_spec in self.plc_spec['sites']:
244             test_site = TestSite (self,site_spec)
245             for node_spec in site_spec['nodes']:
246                 test_node=TestNode (self,test_site,node_spec)
247                 test_node.create_boot_cd(options.path)
248         return True
249             
250     def initscripts (self, options):
251         for initscript in self.plc_spec['initscripts']:
252             utils.show_spec('Adding Initscript %s in plc %s'%\
253                                 (initscript['name'],self.plc_spec['name']),
254                             initscript)
255             self.server.AddInitScript(self.auth_root(),initscript['initscript_fields'])
256         return True
257
258     def slices (self, options):
259         return self.do_slices()
260
261     def clean_slices (self, options):
262         return self.do_slices("delete")
263
264     ### would need a TestSlice class
265     def do_slices (self, add_or_delete="add"):
266         for slice in self.plc_spec['slices']:
267             site_spec = self.locate_site (slice['sitename'])
268             test_site = TestSite(self,site_spec)
269             owner_spec = test_site.locate_user(slice['owner'])
270             auth = TestUser(self,test_site,owner_spec).auth()
271             slice_fields = slice['slice_fields']
272             slice_name = slice_fields['name']
273             if (add_or_delete == "delete"):
274                 self.server.DeleteSlice(auth,slice_fields['name'])
275                 utils.header("Deleted slice %s"%slice_fields['name'])
276                 continue
277             utils.show_spec("Creating slice",slice_fields)
278             self.server.AddSlice(auth,slice_fields)
279             utils.header('Created Slice %s'%slice_fields['name'])
280             for username in slice['usernames']:
281                 user_spec=test_site.locate_user(username)
282                 test_user=TestUser(self,test_site,user_spec)
283                 self.server.AddPersonToSlice(auth, test_user.name(), slice_name)
284
285             hostnames=[]
286             for nodename in slice['nodenames']:
287                 node_spec=test_site.locate_node(nodename)
288                 test_node=TestNode(self,test_site,node_spec)
289                 hostnames += [test_node.name()]
290             utils.header("Adding %r in %s"%(hostnames,slice_name))
291             self.server.AddSliceToNodes(auth, slice_name, hostnames)
292             if slice.has_key('initscriptname'):
293                 isname=slice['initscriptname']
294                 utils.header("Adding initscript %s in %s"%(isname,slice_name))
295                 self.server.AddSliceAttribute(self.auth_root(), slice_name,
296                                               'initscript',isname)
297         return True
298         
299     def start_nodes (self, options):
300         self.kill_all_vmwares()
301         utils.header("Starting vmware nodes")
302         for site_spec in self.plc_spec['sites']:
303             TestSite(self,site_spec).start_nodes (options)
304         return True
305
306     def stop_nodes (self, options):
307         self.kill_all_vmwares ()
308         return True
309
310     # returns the filename to use for sql dump/restore, using options.dbname if set
311     def dbfile (self, database, options):
312         # uses options.dbname if it is found
313         try:
314             name=options.dbname
315             if not isinstance(name,StringTypes):
316                 raise Exception
317         except:
318             t=datetime.datetime.now()
319             d=t.date()
320             name=str(d)
321         return "/root/%s-%s.sql"%(database,name)
322
323     def db_dump(self, options):
324         
325         dump=self.dbfile("planetab4",options)
326         self.run_in_guest('pg_dump -U pgsqluser planetlab4 -f '+ dump)
327         utils.header('Dumped planetlab4 database in %s'%dump)
328         return True
329
330     def db_restore(self, options):
331         dump=self.dbfile("planetab4",options)
332         ##stop httpd service
333         self.run_in_guest('service httpd stop')
334         # xxx - need another wrapper
335         self.run_in_guest_piped('echo drop database planetlab4','psql --user=pgsqluser template1')
336         self.run_in_guest('createdb -U postgres --encoding=UNICODE --owner=pgsqluser planetlab4')
337         self.run_in_guest('psql -U pgsqluser planetlab4 -f '+dump)
338         ##starting httpd service
339         self.run_in_guest('service httpd start')
340
341         utils.header('Database restored from ' + dump)