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 "%s.%s"%(self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH'],
38 # something in the site (users typically)
39 def qualified_hrn (self, name):
40 return "%s.%s"%(self.site_hrn(),name)
43 def hrn(self): return self.qualified_hrn (self.slicename)
46 def resname (self,name,ext): return "%s.%s"%(name,ext)
48 def addslicefile (self): return self.resname("slice_record","xml")
49 def addpersonfile (self): return self.resname("person_record","xml")
50 def adfile (self): return self.resname("ad","rspec")
51 def reqfile (self): return self.resname("req","rspec")
52 def nodefile (self): return self.resname("nodes","txt")
53 # xxx this needs tweaks with more recent versions of sfa that have pgv2 as the default ?
54 def discover_option(self):
55 if self.rspec_style()=='pg': return "-r protogeni"
59 return "/root/sfi/%s%s"%(self.slicename,self.rspec_style())
62 for username,keyname in self.sfa_slice_spec['usernames']:
63 key_spec=self.test_plc.locate_key(keyname)
64 test_key=TestKey(self.test_plc,key_spec)
65 publickey=test_key.publicpath()
66 privatekey=test_key.privatepath()
67 if os.path.isfile(publickey) and os.path.isfile(privatekey):
69 return (found,privatekey)
71 # dir_name is local and will be pushed later on by TestPlc
72 # by default set SFI_USER to the pi, we'll overload this
73 # on the command line when needed
74 def sfi_config (self,dir_name):
75 plc_spec=self.test_plc.plc_spec
76 sfa_spec=self.sfa_spec
77 sfa_slice_spec=self.sfa_slice_spec
79 for (contents,name) in [ (keys[0]['private'], self.piuser+'.pkey'),
80 (keys[0]['key_fields']['key'], self.piuser+'.pub'),
81 (keys[1]['private'], self.regularuser+'.pkey'),
82 (keys[0]['key_fields']['key'], self.regularuser+'.pub'),
84 file_name=os.path.join(dir_name,self.qualified_hrn(name))
85 fileconf=open(file_name,'w')
86 fileconf.write (contents)
88 utils.header ("(Over)wrote %s"%file_name)
90 file_name=dir_name + os.sep + self.addpersonfile()
91 fileconf=open(file_name,'w')
92 fileconf.write(sfa_slice_spec['person_record'])
95 utils.header ("(Over)wrote %s"%file_name)
97 file_name=dir_name + os.sep + 'sfi_config'
98 fileconf=open(file_name,'w')
99 SFI_AUTH="%s"%(self.site_hrn())
100 fileconf.write ("SFI_AUTH='%s'"%SFI_AUTH)
102 SFI_USER=SFI_AUTH + '.' + self.piuser
103 fileconf.write ("SFI_USER='%s'"%SFI_USER)
105 SFI_REGISTRY='http://' + sfa_spec['SFA_REGISTRY_HOST'] + ':12345/'
106 fileconf.write ("SFI_REGISTRY='%s'"%SFI_REGISTRY)
108 SFI_SM='http://' + sfa_spec['SFA_SM_HOST'] + ':12347/'
109 fileconf.write ("SFI_SM='%s'"%SFI_SM)
112 utils.header ("(Over)wrote %s"%file_name)
114 file_name=dir_name + os.sep + self.addslicefile()
115 fileconf=open(file_name,'w')
116 fileconf.write(sfa_slice_spec['slice_record'])
118 utils.header ("(Over)wrote %s"%file_name)
121 # using sfaadmin to bootstrap
122 def sfa_add_site (self, options):
123 command="sfaadmin reg register -t authority -x %s"%self.site_hrn()
124 return self.test_plc.run_in_guest(command)==0
126 def sfa_add_pi (self, options):
127 pi_hrn=self.qualified_hrn(self.piuser)
128 pi_mail=self.sfa_slice_spec['pimail']
129 # as installed by sfi_config
130 pi_key=os.path.join(self.sfi_path(),self.qualified_hrn(self.piuser+'.pub'))
131 command="sfaadmin reg register -t user -x %s --email %s --key %s"%(pi_hrn,pi_mail,pi_key)
132 if self.test_plc.run_in_guest(command)!=0: return False
133 command="sfaadmin reg update -t authority -x %s --pi %s"%(self.site_hrn(),pi_hrn)
134 return self.test_plc.run_in_guest(command)==0
137 def sfa_add_user (self, options):
138 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).add_user()
139 def sfa_update_user (self, options):
140 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).update_user()
141 def sfa_delete_user (self, options):
142 return TestUserSfa(self.test_plc, self.sfa_slice_spec, self).delete_user()
145 def sfi_pi (self, command):
146 return "sfi.py -d %s -u %s %s"%(self.sfi_path(),self.qualified_hrn(self.piuser), command,)
147 # the sfi.py command line option to run as a regular user
148 def sfi_user (self, command):
149 return "sfi.py -d %s -u %s %s"%(self.sfi_path(),self.qualified_hrn(self.regularuser), command,)
151 # those are step names exposed as methods of TestPlc, hence the _sfa
152 def sfa_view (self, options):
153 "run (as regular user) sfi list and sfi show (both on Registry) and sfi slices (on SM)"
154 root_auth=self.test_plc.plc_spec['sfa']['SFA_REGISTRY_ROOT_AUTH']
156 self.test_plc.run_in_guest(self.sfi_user("list %s"%(self.site_hrn())))==0 and \
157 self.test_plc.run_in_guest(self.sfi_user("show %s"%(self.site_hrn())))==0 and \
158 self.test_plc.run_in_guest(self.sfi_user("slices"))==0
160 # needs to be run as pi
161 def sfa_add_slice(self,options):
162 return self.test_plc.run_in_guest(self.sfi_pi("add %s"%(self.addslicefile())))==0
165 def sfa_discover(self,options):
166 return self.test_plc.run_in_guest(self.sfi_user(\
167 "resources %s -o %s/%s"% (self.discover_option(),self.sfi_path(),self.adfile())))==0
169 # run sfi create as a regular user
170 def sfa_create_slice(self,options):
172 "sfiListNodes.py -i %s/%s -o %s/%s"%(self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile()),
173 "sfiAddSliver.py -i %s/%s -n %s/%s -o %s/%s"%\
174 (self.sfi_path(),self.adfile(),self.sfi_path(),self.nodefile(),self.sfi_path(),self.reqfile()),
175 self.sfi_user("create %s %s"%(self.hrn(),self.reqfile())),
177 for command in commands:
178 if self.test_plc.run_in_guest(command)!=0: return False
181 # all local nodes in slice ?
182 def sfa_check_slice_plc (self,options):
183 slice_fields = self.sfa_slice_spec['slice_fields']
184 slice_name = slice_fields['name']
185 slice=self.test_plc.apiserver.GetSlices(self.test_plc.auth_root(), slice_name)[0]
186 nodes=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), {'peer_id':None})
189 if node['node_id'] in slice['node_ids']:
190 utils.header("local node %s found in slice %s"%(node['hostname'],slice['name']))
192 utils.header("ERROR - local node %s NOT FOUND in slice %s"%(node['hostname'],slice['name']))
196 # actually the same for now
197 def sfa_update_slice(self,options):
198 return self.sfa_create_slice(options)
201 def sfa_delete_slice(self,options):
202 self.test_plc.run_in_guest(self.sfi_pi("delete %s"%(self.hrn(),)))
203 return self.test_plc.run_in_guest(self.sfi_pi("remove -t slice %s"%(self.hrn(),)))==0
205 # check the resulting sliver
206 def ssh_slice_sfa(self,options,timeout_minutes=40,silent_minutes=30,period=15):
207 timeout = datetime.datetime.now()+datetime.timedelta(minutes=timeout_minutes)
208 graceout = datetime.datetime.now()+datetime.timedelta(minutes=silent_minutes)
210 (found,remote_privatekey)=self.locate_key()
212 utils.header("WARNING: Cannot find a valid key for slice %s"%self.name())
215 # convert nodenames to real hostnames
216 sfa_slice_spec = self.sfa_slice_spec
219 for nodename in sfa_slice_spec['nodenames']:
220 (site_spec,node_spec) = self.test_plc.locate_node(nodename)
221 tocheck.append(node_spec['node_fields']['hostname'])
223 utils.header("checking ssh access into slice %s on nodes %r"%(self.name(),tocheck))
224 utils.header("max timeout is %d minutes, silent for %d minutes (period is %s)"%\
225 (timeout_minutes,silent_minutes,period))
227 for hostname in tocheck:
228 (site_spec,node_spec) = self.test_plc.locate_hostname(hostname)
229 date_test_ssh = TestSsh (hostname,key=remote_privatekey,username=self.name())
230 command = date_test_ssh.actual_command("echo hostname ; hostname; echo id; id; echo uname -a ; uname -a")
231 date = utils.system (command, silent=datetime.datetime.now() < graceout)
233 utils.header("Successfuly entered slice %s on %s"%(self.name(),hostname))
234 tocheck.remove(hostname)
236 # real nodes will have been checked once in case they're up - skip if not
237 if TestNode.is_real_model(node_spec['node_fields']['model']):
238 utils.header("WARNING : Checking slice %s on real node %s skipped"%(self.name(),hostname))
239 tocheck.remove(hostname)
240 # nm restart after first failure, if requested
241 if options.forcenm and hostname not in restarted:
242 utils.header ("forcenm option : restarting nm on %s"%hostname)
243 restart_test_ssh=TestSsh(hostname,key="keys/key1.rsa")
244 access=restart_test_ssh.actual_command('service nm restart')
246 utils.header('nm restarted on %s'%hostname)
248 utils.header('Failed to restart nm on %s'%(hostname))
249 restarted.append(hostname)
253 if datetime.datetime.now() > timeout:
254 for hostname in tocheck:
255 utils.header("FAILURE to ssh into %s@%s"%(self.name(),hostname))
257 # wait for the period