3 This file is used to setup the running environment
4 Include Download code,setup environment variable
5 Set onos running config
7 Onos-push-keys and so on
11 # All rights reserved. This program and the accompanying materials
12 # are made available under the terms of the Apache License, Version 2.0
13 # which accompanies this distribution, and is available at
14 # http://www.apache.org/licenses/LICENSE-2.0
24 from connection import connection
27 class environment(connection):
30 connection.__init__(self)
31 self.loginfo = connection()
32 self.masterhandle = ''
35 def DownLoadCode(self, handle, codeurl):
37 Download Code use 'git clone'
39 handle: current working handle
40 codeurl: clone code url
42 print "Now loading test codes! Please wait in patient..."
43 originalfolder = sys.path[0]
46 gitclone.sendline("git clone " + codeurl)
49 while index != 1 or index != 4:
50 index = gitclone.expect(['already exists',
51 'esolving deltas: 100%',
54 'npacking objects: 100%', pexpect.EOF])
56 filefolder = self.home + '/' + codeurl.split('/')[-1].split('.')[0]
60 os.chdir(originalfolder)
61 self.loginfo.log('Download code success!')
63 elif index == 1 or index == 4:
64 self.loginfo.log('Download code success!')
65 gitclone.sendline("mkdir onos")
67 gitclone.sendline("cp -rf " + filefolder + "/tools onos/")
71 os.write(1, gitclone.before)
74 self.loginfo.log('Download code failed!')
75 self.loginfo.log('Information before' + gitclone.before)
79 def InstallDefaultSoftware(self, handle):
81 Install default software
83 handle(input): current working handle
85 print "Now Cleaning test environment"
86 handle.sendline("sudo apt-get install -y mininet")
88 handle.sendline("sudo pip install configobj")
90 handle.sendline("sudo apt-get install -y sshpass")
92 handle.sendline("OnosSystemTest/TestON/bin/cleanup.sh")
95 self.loginfo.log('Clean environment success!')
97 def OnosPushKeys(self, handle, cmd, password):
99 Using onos-push-keys to make ssh device without password
101 handle(input): working handle
102 cmd(input): onos-push-keys xxx(xxx is device)
103 password(input): login in password
105 print "Now Pushing Onos Keys:" + cmd
107 Pushkeys.sendline(cmd)
110 Result = Pushkeys.expect(["(yes/no)", "assword:", "PEXPECT]#",
111 pexpect.EOF, pexpect.TIMEOUT])
113 Pushkeys.sendline("yes")
115 Pushkeys.sendline(password)
117 self.loginfo.log("ONOS Push keys Success!")
120 self.loginfo.log("ONOS Push keys Error!")
126 def SetOnosEnvVar(self, handle, masterpass, agentpass):
128 Setup onos pushkeys to all devices(3+2)
130 handle(input): current working handle
131 masterpass: scripts running server's password
132 agentpass: onos cluster&compute node password
134 print "Now Setting test environment"
135 for host in self.hosts:
136 print "try to connect " + str(host)
137 result = self.CheckSshNoPasswd(host)
139 print ("ssh lgin failed,try to copy master publickey" +
140 "to agent " + str(host))
141 self.CopyPublicKey(host)
142 self.OnosPushKeys(handle, "onos-push-keys " + self.OCT, masterpass)
143 self.OnosPushKeys(handle, "onos-push-keys " + self.OC1, agentpass)
144 self.OnosPushKeys(handle, "onos-push-keys " + self.OC2, agentpass)
145 self.OnosPushKeys(handle, "onos-push-keys " + self.OC3, agentpass)
146 self.OnosPushKeys(handle, "onos-push-keys " + self.OCN, agentpass)
147 self.OnosPushKeys(handle, "onos-push-keys " + self.OCN2, agentpass)
149 def CheckSshNoPasswd(self, host):
151 Check master can connect agent with no password
153 login = pexpect.spawn("ssh " + str(host))
156 index = login.expect(['(yes/no)', '>|#|\$',
157 pexpect.EOF, pexpect.TIMEOUT])
159 login.sendline("yes")
162 self.loginfo.log("ssh connect to " + str(host) +
163 " success,no need to copy ssh public key")
168 def ChangeOnosName(self, user, password):
170 Change onos name in envDefault file
171 Because some command depend on this
173 user: onos&compute node user
174 password: onos&compute node password
176 print "Now Changing ONOS name&password"
177 filepath = self.home + '/onos/tools/build/envDefaults'
178 line = open(filepath, 'r').readlines()
179 lenall = len(line) - 1
180 for i in range(lenall):
181 if "ONOS_USER=" in line[i]:
182 line[i] = line[i].replace("sdn", user)
183 if "ONOS_GROUP" in line[i]:
184 line[i] = line[i].replace("sdn", user)
185 if "ONOS_PWD" in line[i]:
186 line[i] = line[i].replace("rocks", password)
187 NewFile = open(filepath, 'w')
188 NewFile.writelines(line)
192 def ChangeTestCasePara(self, testcase, user, password):
194 When running test script, there's something need \
195 to change in every test folder's *.param & *.topo files
196 user: onos&compute node user
197 password: onos&compute node password
199 print "Now Changing " + testcase + " name&password"
200 if self.masterusername == 'root':
203 filepath = '/home/' + self.masterusername + '/'
204 filepath = (filepath + "OnosSystemTest/TestON/tests/" +
205 testcase + "/" + testcase + ".topo")
206 line = open(filepath, 'r').readlines()
207 lenall = len(line) - 1
208 for i in range(lenall - 2):
209 if("localhost" in line[i]) or ("OCT" in line[i]):
210 line[i + 1] = re.sub(">\w+", ">" + user, line[i + 1])
211 line[i + 2] = re.sub(">\w+", ">" + password, line[i + 2])
212 if ("OC1" in line[i] or "OC2" in line[i] or "OC3" in line[i] or
213 "OCN" in line[i] or "OCN2" in line[i]):
214 line[i + 1] = re.sub(">\w+", ">root", line[i + 1])
215 line[i + 2] = re.sub(">\w+", ">root", line[i + 2])
216 NewFile = open(filepath, 'w')
217 NewFile.writelines(line)
220 def SSHlogin(self, ipaddr, username, password):
222 SSH login provide a connection to destination.
225 username: login user name
226 password: login password
229 login = pxssh.pxssh()
230 login.login(ipaddr, username, password, original_prompt='[$#>]')
232 login.sendline('ls -l')
235 print("SSH login " + ipaddr + " success!")
238 def SSHRelease(self, handle):
242 def CopyOnostoTestbin(self):
243 sourcefile = self.cipath + '/dependencies/onos'
244 destifile = self.home + '/onos/tools/test/bin/'
246 runcommand = 'cp ' + sourcefile + ' ' + destifile
247 os.system(runcommand)
249 def CopyPublicKey(self, host):
250 output = os.popen('cat /root/.ssh/id_rsa.pub')
251 publickey = output.read().strip('\n')
252 tmphandle = self.SSHlogin(self.installer_master,
253 self.installer_master_username,
254 self.installer_master_password)
255 tmphandle.sendline("ssh " + host + " -T \'echo " +
256 str(publickey) + ">>/root/.ssh/authorized_keys\'")
258 self.SSHRelease(tmphandle)
259 print "Add OCT PublicKey to " + host + " success"
261 def OnosEnvSetup(self, handle):
263 Onos Environment Setup function
265 self.Gensshkey(handle)
266 self.home = self.GetEnvValue(handle, 'HOME')
267 self.AddKnownHost(handle, self.OC1, "karaf", "karaf")
268 self.AddKnownHost(handle, self.OC2, "karaf", "karaf")
269 self.AddKnownHost(handle, self.OC3, "karaf", "karaf")
270 self.DownLoadCode(handle,
271 'https://github.com/sunyulin/OnosSystemTest.git')
272 # self.DownLoadCode(handle, 'https://gerrit.onosproject.org/onos')
273 if self.masterusername == 'root':
276 filepath = '/home/' + self.masterusername + '/'
277 self.OnosRootPathChange(filepath)
278 self.CopyOnostoTestbin()
279 self.ChangeOnosName(self.agentusername, self.agentpassword)
280 self.InstallDefaultSoftware(handle)
281 self.SetOnosEnvVar(handle, self.masterpassword, self.agentpassword)