1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
9 from TestKey import TestKey
10 from TestUser import TestUser
11 from TestNode import TestNode
12 from TestSsh import TestSsh
13 from TestUserSfa import TestUserSfa
17 def __init__ (self,test_plc,sfa_slice_spec):
18 self.test_plc=test_plc
19 self.sfa_slice_spec=sfa_slice_spec
20 self.test_ssh=TestSsh(self.test_plc.test_ssh)
22 self.sfa_spec=test_plc.plc_spec['sfa']
23 self.piuser=self.sfa_slice_spec['piuser']
24 self.regularuser=self.sfa_slice_spec['regularuser']
25 self.slicename=self.sfa_slice_spec['slicename']
26 self.login_base=self.sfa_slice_spec['login_base']
29 return self.sfa_slice_spec['slice_fields']['name']
31 def rspec_style (self): return self.sfa_slice_spec['rspec_style']
33 # the hrn for the site
35 return self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
37 # the hrn for the site
39 return "%s.%s"%(self.auth_hrn(),self.login_base)
41 # something in the site (users typically)
42 def qualified_hrn (self, name):
43 return "%s.%s"%(self.site_hrn(),name)
46 def hrn(self): return self.qualified_hrn (self.slicename)
49 def resname (self,name,ext): return "%s.%s"%(name,ext)
51 def adfile (self): return self.resname("ad","rspec")
52 def reqfile (self): return self.resname("req","rspec")
53 def nodefile (self): return self.resname("nodes","txt")
54 # xxx this needs tweaks with more recent versions of sfa that have pgv2 as the default ?
55 def discover_option(self):
56 if self.rspec_style()=='pg': return "-r protogeni"
60 return "/root/sfi/%s%s"%(self.slicename,self.rspec_style())
63 for username,keyname in self.sfa_slice_spec['usernames']:
64 key_spec=self.test_plc.locate_key(keyname)
65 test_key=TestKey(self.test_plc,key_spec)
66 publickey=test_key.publicpath()
67 privatekey=test_key.privatepath()
68 if os.path.isfile(publickey) and os.path.isfile(privatekey):
70 return (found,privatekey)
72 # dir_name is local and will be pushed later on by TestPlc
73 # by default set SFI_USER to the pi, we'll overload this
74 # on the command line when needed
75 def sfi_configure (self,dir_name):
76 plc_spec=self.test_plc.plc_spec
77 sfa_spec=self.sfa_spec
78 sfa_slice_spec=self.sfa_slice_spec
80 # fetch keys in config spec and expose to sfi
81 for (key_key,name) in [ ('pi_private_key', self.piuser+'.pkey'),
82 ('pi_public_key', self.piuser+'.pub'),
83 ('user_private_key', self.regularuser+'.pkey'),
84 ('user_public_key', self.regularuser+'.pub'),
86 file_name=os.path.join(dir_name,self.qualified_hrn(name))
87 fileconf=open(file_name,'w')
88 contents=self.sfa_slice_spec[key_key]
89 fileconf.write (contents)
91 utils.header ("(Over)wrote %s"%file_name)
93 file_name=dir_name + os.sep + 'sfi_config'
94 fileconf=open(file_name,'w')
95 SFI_AUTH="%s"%(self.site_hrn())
96 fileconf.write ("SFI_AUTH='%s'"%SFI_AUTH)
98 SFI_USER=SFI_AUTH + '.' + self.piuser
99 fileconf.write ("SFI_USER='%s'"%SFI_USER)
101 SFI_REGISTRY='http://' + sfa_spec['SFA_REGISTRY_HOST'] + ':12345/'
102 fileconf.write ("SFI_REGISTRY='%s'"%SFI_REGISTRY)
104 SFI_SM='http://' + sfa_spec['SFA_SM_HOST'] + ':12347/'
105 fileconf.write ("SFI_SM='%s'"%SFI_SM)
108 utils.header ("(Over)wrote %s"%file_name)
110 # using sfaadmin to bootstrap
111 def sfa_add_site (self, options):
112 "bootstrap a site using sfaadmin"
113 command="sfaadmin reg register -t authority -x %s"%self.site_hrn()
114 return self.test_plc.run_in_guest(command)==0
116 def sfa_add_pi (self, options):
117 "bootstrap a PI user for that site"
118 pi_hrn=self.qualified_hrn(self.piuser)
119 pi_mail=self.sfa_slice_spec['pimail']
120 # as installed by sfi_config
121 pi_key=os.path.join(self.sfi_path(),self.qualified_hrn(self.piuser+'.pub'))
122 command="sfaadmin reg register -t user -x %s --email %s --key %s"%(pi_hrn,pi_mail,pi_key)
123 if self.test_plc.run_in_guest(command)!=0: return False
124 command="sfaadmin reg update -t authority -x %s --pi %s"%(self.site_hrn(),pi_hrn)
125 return self.test_plc.run_in_guest(command)==0
128 def sfa_add_user (self, options):
129 "add a regular user using sfi add"
130 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).add_user()
131 def sfa_update_user (self, options):
132 "update a user record using sfi update"
133 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).update_user()
134 def sfa_delete_user (self, options):
136 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).delete_user()
139 def sfi_pi (self, command):
140 return "sfi -d %s -u %s %s"%(self.sfi_path(),self.qualified_hrn(self.piuser), command,)
141 # the sfi command line option to run as a regular user
142 def sfi_user (self, command):
143 return "sfi -d %s -u %s %s"%(self.sfi_path(),self.qualified_hrn(self.regularuser), command,)
145 # those are step names exposed as methods of TestPlc, hence the _sfa
147 def sfi_list (self, options):
148 "run (as regular user) sfi list (on Registry)"
150 self.test_plc.run_in_guest(self.sfi_user("list -r %s"%self.auth_hrn()))==0 and \
151 self.test_plc.run_in_guest(self.sfi_user("list %s"%(self.site_hrn())))==0
153 def sfi_show (self, options):
154 "run (as regular user) sfi show (on Registry)"
156 self.test_plc.run_in_guest(self.sfi_user("show %s"%(self.site_hrn())))==0
158 def sfi_slices (self, options):
159 "run (as regular user) sfi slices (on SM)"
161 self.test_plc.run_in_guest(self.sfi_user("slices"))==0
163 # needs to be run as pi
164 def sfa_add_slice(self,options):
165 "run sfi add (on Registry) from slice.xml"
167 for (k,v) in self.sfa_slice_spec['slice_sfi_options'].items():
168 sfi_options += " %s %s"%(k,v)
169 return self.test_plc.run_in_guest(self.sfi_pi("%s"%(sfi_options)))==0
172 def sfa_discover(self,options):
173 "discover resources into resouces_in.rspec"
174 return self.test_plc.run_in_guest(self.sfi_user(\
175 "resources %s -o %s/%s"% (self.discover_option(),self.sfi_path(),self.adfile())))==0
177 # run sfi create as a regular user
178 def sfa_create_slice(self,options):
179 "run sfi create (on SM) - 1st time"
181 "sfiListNodes.py -i %s/%s -o %s/%s"%(self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile()),
182 "sfiAddSliver.py -i %s/%s -n %s/%s -o %s/%s"%\
183 (self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile(),self.sfi_path(),self.reqfile()),
184 self.sfi_user("create %s %s"%(self.hrn(),self.reqfile())),
186 for command in commands:
187 if self.test_plc.run_in_guest(command)!=0: return False
190 # all local nodes in slice ?
191 def sfa_check_slice_plc (self,options):
192 "check sfa_create_slice at the plcs - all local nodes should be in slice"
193 slice_fields = self.sfa_slice_spec['slice_fields']
194 slice_name = slice_fields['name']
195 slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), slice_name)[0]
196 nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
199 if node['node_id'] in slice['node_ids']:
200 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
202 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
206 # actually the same for now
207 def sfa_update_slice(self,options):
208 "run sfi create (on SM) on existing object"
209 return self.sfa_create_slice(options)
212 def sfa_delete_slice(self,options):
214 self.test_plc.run_in_guest(self.sfi_pi("delete %s"%(self.hrn(),)))
215 return self.test_plc.run_in_guest(self.sfi_pi("remove -t slice %s"%(self.hrn(),)))==0
217 # check the resulting sliver
218 def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
219 "tries to ssh-enter the SFA slice"
220 timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
221 graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
223 (found,remote_privatekey)=self.locate_key()
225 utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
228 # convert nodenames to real hostnames
229 sfa_slice_spec = self.sfa_slice_spec
232 for nodename in sfa_slice_spec['nodenames']:
233 (site_spec,node_spec) = self.test_plc.locate_node(nodename)
234 tocheck.append(node_spec['node_fields']['hostname'])
236 utils.header("checking ssh access into slice %s on nodes %r"%(self.name(),tocheck))
237 utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
238 (timeout_minutes,silent_minutes,period))
240 for hostname in tocheck:
241 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
242 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.name())
243 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
244 date = utils.system (command, silent=datetime.datetime.now() < graceout)
246 utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
247 tocheck.remove(hostname)
249 # real nodes will have been checked once in case they're up - skip if not
250 if TestNode.is_real_model(node_spec['node_fields']['model']):
251 utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.name(),hostname))
252 tocheck.remove(hostname)
253 # nm restart after first failure, if requested
254 if options.forcenm and hostname not in restarted:
255 utils.header ("forcenm option : restarting nm on %s"%hostname)
256 restart_test_ssh=TestSsh(hostname,key="keys/key1.rsa")
257 access=restart_test_ssh.actual_command('service nm restart')
259 utils.header('nm restarted on %s'%hostname)
261 utils.header('Failed to restart nm on %s'%(hostname))
262 restarted.append(hostname)
266 if datetime.datetime.now() > timeout:
267 for hostname in tocheck:
268 utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
270 # wait for the period