8 from TestSite import TestSite
9 from TestNode import TestNode
10 from TestUser import TestUser
11 from TestKey import TestKey
13 # step methods must take (self, options) and return a boolean
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])
23 self.vserverip=plc_spec['vserverip']
24 self.vservername=plc_spec['vservername']
30 name=self.plc_spec['name']
32 return name+"[%s]"%self.vservername
34 return name+"[chroot]"
36 # define the API methods on this object through xmlrpc
37 # would help, but not strictly necessary
41 # build the full command so command gets run in the chroot/vserver
42 def run_command(self,command):
44 return "vserver %s exec %s"%(self.vservername,command)
46 return "chroot /plc/root %s"%command
48 def ssh_command(self,command):
49 if self.plc_spec['hostname'] == "localhost":
52 return "ssh " + self.plc_spec['hostname'] + " " + command
54 def full_command(self,command):
55 return self.ssh_command(self.run_command(command))
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))
63 def run_in_guest_piped (self,local,remote):
64 return utils.system(local+" | "+self.full_command(command))
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']
72 def locate_site (self,sitename):
73 for site in self.plc_spec['sites']:
74 if site['site_fields']['name'] == sitename:
76 if site['site_fields']['login_base'] == sitename:
78 raise Exception,"Cannot locate site %s"%sitename
80 def locate_key (self,keyname):
81 for key in self.plc_spec['keys']:
82 if key['name'] == keyname:
84 raise Exception,"Cannot locate key %s"%keyname
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')
93 #################### step methods
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')
104 def uninstall_vserver(self,options):
105 self.run_in_host("vserver --silent delete %s"%self.vservername)
107 def uninstall(self,options):
109 return self.uninstall_vserver(options)
111 return self.uninstall_chroot(options)
114 def install_chroot(self,options):
115 utils.header('Installing from %s'%options.myplc_url)
116 url=options.myplc_url
117 utils.system('rpm -Uvh '+url)
118 utils.system('service plc mount')
121 # xxx this would not work with hostname != localhost as mylc-init-vserver was extracted locally
122 def install_vserver(self,options):
123 # we need build dir for myplc-init-vserver
124 build_dir=self.path+"/build"
125 if not os.isdir(build_dir):
126 if utils.system("svn checkout %s %s"%(options.build_url,build_dir)) != 0:
127 raise Exception,"Cannot checkout build dir"
128 # the repo url is taken from myplc-url
129 # with the last two steps (i386/myplc...) removed
130 repo_url = options.build_url
131 repo_url = os.path(repo_url)
132 repo_url = os.path(repo_url)
133 command="%s/myplc-init-vserver.sh %s %s -- --interface eth0:%s"%\
134 (build_url,self.servername,repo_url,self.vserverip)
135 if utils.system(command) != 0:
136 raise Exception,"Could not create vserver for %s"%self.vservername
137 self.run_in_host("yum -y install myplc-native")
139 def install(self,options):
141 return self.install_vserver(options)
143 return self.install_chroot(options)
146 def configure(self,options):
147 tmpname='%s/%s.plc-config-tty'%(options.path,self.name())
148 fileconf=open(tmpname,'w')
149 for var in [ 'PLC_NAME',
153 'PLC_MAIL_SUPPORT_ADDRESS',
160 fileconf.write ('e %s\n%s\n'%(var,self.plc_spec[var]))
161 fileconf.write('w\n')
162 fileconf.write('q\n')
164 utils.system('cat %s'%tmpname)
165 self.run_in_guest('plc-config-tty < %s'%tmpname)
166 utils.system('rm %s'%tmpname)
169 def start(self, options):
170 utils.system('service plc start')
173 def stop(self, options):
174 utils.system('service plc stop')
177 # could use a TestKey class
178 def store_keys(self, options):
179 for key_spec in self.plc_spec['keys']:
180 TestKey(self,key_spec).store_key()
183 def clean_keys(self, options):
184 utils.system("rm -rf %s/keys/"%self.path)
186 def sites (self,options):
187 return self.do_sites(options)
189 def clean_sites (self,options):
190 return self.do_sites(options,"delete")
192 def do_sites (self,options,action="add"):
193 for site_spec in self.plc_spec['sites']:
194 test_site = TestSite (self,site_spec)
195 if (action == "delete"):
196 utils.header("Deleting site %s in %s"%(test_site.name(),self.name()))
197 test_site.delete_site()
198 # deleted with the site
199 #test_site.delete_users()
202 utils.header("Creating site %s & users in %s"%(test_site.name(),self.name()))
203 test_site.create_site()
204 test_site.create_users()
207 def nodes (self, options):
208 for site_spec in self.plc_spec['sites']:
209 test_site = TestSite (self,site_spec)
210 utils.header("Creating nodes for site %s in %s"%(test_site.name(),self.name()))
211 for node_spec in site_spec['nodes']:
212 utils.show_spec('Creating node %s'%node_spec,node_spec)
213 test_node = TestNode (self,test_site,node_spec)
214 test_node.create_node ()
217 def bootcd (self, options):
218 for site_spec in self.plc_spec['sites']:
219 test_site = TestSite (self,site_spec)
220 for node_spec in site_spec['nodes']:
221 test_node=TestNode (self,test_site,node_spec)
222 test_node.create_boot_cd(options.path)
225 def initscripts (self, options):
226 for initscript in self.plc_spec['initscripts']:
227 utils.show_spec('Adding Initscript %s in plc %s'%\
228 (initscript['name'],self.plc_spec['name']),
230 self.server.AddInitScript(self.auth_root(),initscript['initscript_fields'])
233 def slices (self, options):
234 return self.do_slices()
236 def clean_slices (self, options):
237 return self.do_slices("delete")
239 ### would need a TestSlice class
240 def do_slices (self, add_or_delete="add"):
241 for slice in self.plc_spec['slices']:
242 site_spec = self.locate_site (slice['sitename'])
243 test_site = TestSite(self,site_spec)
244 owner_spec = test_site.locate_user(slice['owner'])
245 auth = TestUser(self,test_site,owner_spec).auth()
246 slice_fields = slice['slice_fields']
247 slice_name = slice_fields['name']
248 if (add_or_delete == "delete"):
249 self.server.DeleteSlice(auth,slice_fields['name'])
250 utils.header("Deleted slice %s"%slice_fields['name'])
252 utils.show_spec("Creating slice",slice_fields)
253 self.server.AddSlice(auth,slice_fields)
254 utils.header('Created Slice %s'%slice_fields['name'])
255 for username in slice['usernames']:
256 user_spec=test_site.locate_user(username)
257 test_user=TestUser(self,test_site,user_spec)
258 self.server.AddPersonToSlice(auth, test_user.name(), slice_name)
261 for nodename in slice['nodenames']:
262 node_spec=test_site.locate_node(nodename)
263 test_node=TestNode(self,test_site,node_spec)
264 hostnames += [test_node.name()]
265 utils.header("Adding %r in %s"%(hostnames,slice_name))
266 self.server.AddSliceToNodes(auth, slice_name, hostnames)
267 if slice.has_key('initscriptname'):
268 isname=slice['initscriptname']
269 utils.header("Adding initscript %s in %s"%(isname,slice_name))
270 self.server.AddSliceAttribute(self.auth_root(), slice_name,
274 def start_nodes (self, options):
275 self.kill_all_vmwares()
276 utils.header("Starting vmware nodes")
277 for site_spec in self.plc_spec['sites']:
278 TestSite(self,site_spec).start_nodes (options)
281 def stop_nodes (self, options):
282 self.kill_all_vmwares ()
285 # returns the filename to use for sql dump/restore, using options.dbname if set
286 def dbfile (self, database, options):
287 # uses options.dbname if it is found
290 if not isinstance(name,StringTypes):
293 t=datetime.datetime.now()
296 return "/root/%s-%s.sql"%(database,name)
298 def db_dump(self, options):
300 dump=self.dbfile("planetab4",options)
301 self.run_in_guest('pg_dump -U pgsqluser planetlab4 -f '+ dump)
302 utils.header('Dumped planetlab4 database in %s'%dump)
305 def db_restore(self, options):
306 dump=self.dbfile("planetab4",options)
308 self.run_in_guest('service httpd stop')
309 # xxx - need another wrapper
310 self.run_in_guest_piped('echo drop database planetlab4','psql --user=pgsqluser template1')
311 self.run_in_guest('createdb -U postgres --encoding=UNICODE --owner=pgsqluser planetlab4')
312 self.run_in_guest('psql -U pgsqluser planetlab4 -f '+dump)
313 ##starting httpd service
314 self.run_in_guest('service httpd start')
316 utils.header('Database restored from ' + dump)