admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 24-Oct-2012 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4 | Copyright 2012 Open Networking Foundation (ONF) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 5 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 9 | |
| 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 13 | (at your option) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 14 | |
| 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 22 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 23 | import pexpect |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 24 | import re |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | |
| 26 | from drivers.component import Component |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class CLI( Component ): |
| 30 | |
| 31 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 32 | This will define common functions for CLI included. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 33 | """ |
| 34 | def __init__( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 35 | super( CLI, self ).__init__() |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 36 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 37 | def checkPrompt( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 38 | for key in self.options: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 39 | if key == "prompt" and self.options[ 'prompt' ] is not None: |
| 40 | self.prompt = self.options[ 'prompt' ] |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 41 | break |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 42 | |
| 43 | def connect( self, **connectargs ): |
| 44 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 45 | Connection will establish to the remote host using ssh. |
| 46 | It will take user_name ,ip_address and password as arguments<br> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 47 | and will return the handle. |
| 48 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 49 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | vars( self )[ key ] = connectargs[ key ] |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 51 | self.checkPrompt() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 53 | connect_result = super( CLI, self ).connect() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 55 | refused = "ssh: connect to host " + \ |
| 56 | self.ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | if self.port: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 58 | self.handle = pexpect.spawn( |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 59 | 'ssh -X -p ' + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 60 | self.port + |
| 61 | ' ' + |
| 62 | self.user_name + |
| 63 | '@' + |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 64 | self.ip_address + |
| 65 | ' -o ServerAliveInterval=120 -o TCPKeepAlive=yes', |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 66 | env={ "TERM": "vt100" }, |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 67 | maxread=1000000 ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 68 | else: |
| 69 | self.handle = pexpect.spawn( |
| 70 | 'ssh -X ' + |
| 71 | self.user_name + |
| 72 | '@' + |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 73 | self.ip_address + |
| 74 | ' -o ServerAliveInterval=120 -o TCPKeepAlive=yes', |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 75 | env={ "TERM": "vt100" }, |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | maxread=1000000, |
| 77 | timeout=60 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 78 | |
Jon Hall | 73057ee | 2016-08-23 09:57:26 -0700 | [diff] [blame] | 79 | # set tty window size |
| 80 | self.handle.setwinsize( 24, 250 ) |
| 81 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 82 | self.handle.logfile = self.logfile_handler |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | i = 5 |
| 84 | while i == 5: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 85 | i = self.handle.expect( [ ssh_newkey, |
| 86 | 'password:|Password:', |
| 87 | pexpect.EOF, |
| 88 | pexpect.TIMEOUT, |
| 89 | refused, |
| 90 | 'teston>', |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 91 | 'Permission denied, please try again.', |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 92 | self.prompt ], |
| 93 | 120 ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 94 | if i == 0: # Accept key, then expect either a password prompt or access |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 95 | main.log.info( "ssh key confirmation received, send yes" ) |
| 96 | self.handle.sendline( 'yes' ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 97 | i = 5 # Run the loop again |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 98 | continue |
| 99 | if i == 1: # Password required |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 100 | if self.pwd: |
| 101 | main.log.info( |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 102 | "ssh connection asked for password, gave password" ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 103 | else: |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 104 | main.log.info( "Server asked for password, but none was " |
| 105 | "given in the .topo file. Trying " |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 106 | "no password." ) |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 107 | self.pwd = "" |
| 108 | self.handle.sendline( self.pwd ) |
| 109 | j = self.handle.expect( [ |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 110 | 'password:|Password:', |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 111 | 'Permission denied, please try again.', |
| 112 | self.prompt, |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 113 | pexpect.EOF, |
| 114 | pexpect.TIMEOUT ], |
| 115 | 120 ) |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 116 | if j != 2: |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 117 | main.log.error( "Incorrect Password" ) |
| 118 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 119 | elif i == 2: |
| 120 | main.log.error( "Connection timeout" ) |
| 121 | return main.FALSE |
| 122 | elif i == 3: # timeout |
| 123 | main.log.error( |
| 124 | "No route to the Host " + |
| 125 | self.user_name + |
| 126 | "@" + |
| 127 | self.ip_address ) |
| 128 | return main.FALSE |
| 129 | elif i == 4: |
| 130 | main.log.error( |
| 131 | "ssh: connect to host " + |
| 132 | self.ip_address + |
| 133 | " port 22: Connection refused" ) |
| 134 | return main.FALSE |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 135 | elif i == 6: # Incorrect Password |
| 136 | main.log.error( "Incorrect Password" ) |
| 137 | return main.FALSE |
| 138 | elif i == 7: # Prompt |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 139 | main.log.info( "Password not required logged in" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 140 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 141 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 142 | self.handle.expect( self.prompt ) |
Jeremy Ronquillo | 0f2008a | 2017-06-23 15:32:51 -0700 | [diff] [blame] | 143 | self.handle.sendline( "cd" ) |
| 144 | self.handle.expect( self.prompt ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 145 | return self.handle |
| 146 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 147 | def disconnect( self ): |
| 148 | result = super( CLI, self ).disconnect( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 149 | result = main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 150 | # self.execute( cmd="exit",timeout=120,prompt="(.*)" ) |
| 151 | |
| 152 | def execute( self, **execparams ): |
| 153 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 154 | It facilitates the command line execution of a given command. It has arguments as : |
| 155 | cmd => represents command to be executed, |
| 156 | prompt => represents expect command prompt or output, |
| 157 | timeout => timeout for command execution, |
| 158 | more => to provide a key press if it is on. |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 159 | logCmd => log the command executed if True |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 160 | |
| 161 | It will return output of command exection. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 162 | """ |
| 163 | result = super( CLI, self ).execute( self ) |
admin | aef0055 | 2014-05-08 09:18:36 -0700 | [diff] [blame] | 164 | defaultPrompt = '.*[$>\#]' |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 165 | args = utilities.parse_args( [ "CMD", |
| 166 | "TIMEOUT", |
| 167 | "PROMPT", |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 168 | "MORE", |
| 169 | "LOGCMD" ], |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 170 | **execparams ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 171 | |
| 172 | expectPrompt = args[ "PROMPT" ] if args[ "PROMPT" ] else defaultPrompt |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 173 | self.LASTRSP = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 174 | timeoutVar = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] else 10 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 175 | cmd = '' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 176 | if args[ "CMD" ]: |
| 177 | cmd = args[ "CMD" ] |
| 178 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 179 | return 0 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | if args[ "MORE" ] is None: |
| 181 | args[ "MORE" ] = " " |
| 182 | self.handle.sendline( cmd ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 183 | self.lastCommand = cmd |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 184 | index = self.handle.expect( [ expectPrompt, |
| 185 | "--More--", |
| 186 | 'Command not found.', |
| 187 | pexpect.TIMEOUT, |
| 188 | "^:$" ], |
| 189 | timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 190 | if index == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 191 | self.LASTRSP = self.LASTRSP + \ |
| 192 | self.handle.before + self.handle.after |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 193 | if not args[ "LOGCMD" ] is False: |
| 194 | main.log.info( "Executed :" + str( cmd ) + |
| 195 | " \t\t Expected Prompt '" + str( expectPrompt ) + |
| 196 | "' Found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 197 | elif index == 1: |
| 198 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 199 | self.handle.send( args[ "MORE" ] ) |
| 200 | main.log.info( |
| 201 | "Found More screen to go , Sending a key to proceed" ) |
| 202 | indexMore = self.handle.expect( |
| 203 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 204 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 205 | main.log.info( |
| 206 | "Found anoother More screen to go , Sending a key to proceed" ) |
| 207 | self.handle.send( args[ "MORE" ] ) |
| 208 | indexMore = self.handle.expect( |
| 209 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 210 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 211 | elif index == 2: |
| 212 | main.log.error( "Command not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 213 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 214 | elif index == 3: |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 215 | main.log.error( "Expected Prompt not found, Time Out!!" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 216 | main.log.error( expectPrompt ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 217 | self.LASTRSP = self.LASTRSP + self.handle.before |
| 218 | return self.LASTRSP |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 219 | elif index == 4: |
| 220 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 221 | # self.handle.send( args[ "MORE" ] ) |
| 222 | self.handle.sendcontrol( "D" ) |
| 223 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 224 | "Found More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 225 | indexMore = self.handle.expect( |
| 226 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 227 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 229 | "Found another More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 230 | self.handle.sendcontrol( "D" ) |
| 231 | indexMore = self.handle.expect( |
| 232 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 233 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 234 | main.last_response = self.remove_contol_chars( self.LASTRSP ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 235 | return self.LASTRSP |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 236 | |
| 237 | def remove_contol_chars( self, response ): |
| 238 | # RE_XML_ILLEGAL = '([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])|([%s-%s][^%s-%s])|([^%s-%s][%s-%s])|([%s-%s]$)|(^[%s-%s])'%( unichr( 0xd800 ),unichr( 0xdbff ),unichr( 0xdc00 ),unichr( 0xdfff ),unichr( 0xd800 ),unichr( 0xdbff ),unichr( 0xdc00 ),unichr( 0xdfff ),unichr( 0xd800 ),unichr( 0xdbff ),unichr( 0xdc00 ),unichr( 0xdfff ) ) |
| 239 | # response = re.sub( RE_XML_ILLEGAL, "\n", response ) |
| 240 | response = re.sub( r"[\x01-\x1F\x7F]", "", response ) |
| 241 | # response = re.sub( r"\[\d+\;1H", "\n", response ) |
| 242 | response = re.sub( r"\[\d+\;\d+H", "", response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 243 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 244 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 245 | def runAsSudoUser( self, handle, pwd, default ): |
| 246 | |
| 247 | i = handle.expect( [ ".ssword:*", default, pexpect.EOF ] ) |
| 248 | if i == 0: |
| 249 | handle.sendline( pwd ) |
Jon Hall | 5ec6b1b | 2015-09-17 18:20:14 -0700 | [diff] [blame] | 250 | handle.sendline( "\n" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 251 | |
| 252 | if i == 1: |
| 253 | handle.expect( default ) |
| 254 | |
| 255 | if i == 2: |
| 256 | main.log.error( "Unable to run as Sudo user" ) |
| 257 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 258 | return handle |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 259 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 260 | def onfail( self ): |
| 261 | if 'onfail' in main.componentDictionary[ self.name ]: |
| 262 | commandList = main.componentDictionary[ |
| 263 | self.name ][ 'onfail' ].split( "," ) |
| 264 | for command in commandList: |
| 265 | response = self.execute( |
| 266 | cmd=command, |
| 267 | prompt="(.*)", |
| 268 | timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 269 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 270 | def secureCopy( self, userName, ipAddress, filePath, dstPath, pwd="", |
| 271 | direction="from" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 272 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 273 | Definition: |
| 274 | Execute scp command in linux to copy to/from a remote host |
| 275 | Required: |
| 276 | str userName - User name of the remote host |
| 277 | str ipAddress - IP address of the remote host |
| 278 | str filePath - File path including the file it self |
| 279 | str dstPath - Destination path |
| 280 | Optional: |
| 281 | str pwd - Password of the host |
| 282 | str direction - Direction of the scp, default to "from" which means |
| 283 | copy "from" the remote machine to local machine, |
| 284 | while "to" means copy "to" the remote machine from |
| 285 | local machine |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 286 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 287 | returnVal = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 288 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 289 | refused = "ssh: connect to host " + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 290 | ipAddress + " port 22: Connection refused" |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 291 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 292 | if direction == "from": |
| 293 | cmd = 'scp ' + str( userName ) + '@' + str( ipAddress ) + ':' + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 294 | str( filePath ) + ' ' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 295 | elif direction == "to": |
| 296 | cmd = 'scp ' + str( filePath ) + ' ' + str( userName ) + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 297 | '@' + str( ipAddress ) + ':' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 298 | else: |
| 299 | main.log.debug( "Wrong direction using secure copy command!" ) |
| 300 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 301 | |
| 302 | main.log.info( "Sending: " + cmd ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 303 | self.handle.sendline( cmd ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 304 | i = 0 |
| 305 | while i < 2: |
| 306 | i = self.handle.expect( [ |
| 307 | ssh_newkey, |
| 308 | 'password:', |
| 309 | "100%", |
| 310 | refused, |
| 311 | "No such file or directory", |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 312 | "Permission denied", |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 313 | self.prompt, |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 314 | pexpect.EOF, |
| 315 | pexpect.TIMEOUT ], |
| 316 | 120 ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 317 | if i == 0: # ask for ssh key confirmation |
| 318 | main.log.info( "ssh key confirmation received, sending yes" ) |
| 319 | self.handle.sendline( 'yes' ) |
| 320 | elif i == 1: # Asked for ssh password |
| 321 | main.log.info( "ssh connection asked for password, gave password" ) |
| 322 | self.handle.sendline( pwd ) |
| 323 | elif i == 2: # File finished transfering |
| 324 | main.log.info( "Secure copy successful" ) |
| 325 | returnVal = main.TRUE |
| 326 | elif i == 3: # Connection refused |
| 327 | main.log.error( |
| 328 | "ssh: connect to host " + |
| 329 | ipAddress + |
| 330 | " port 22: Connection refused" ) |
| 331 | returnVal = main.FALSE |
| 332 | elif i == 4: # File Not found |
| 333 | main.log.error( "No such file found" ) |
| 334 | returnVal = main.FALSE |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 335 | elif i == 5: # Permission denied |
| 336 | main.log.error( "Permission denied. Check folder permissions" ) |
| 337 | returnVal = main.FALSE |
| 338 | elif i == 6: # prompt returned |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 339 | return returnVal |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 340 | elif i == 7: # EOF |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 341 | main.log.error( "Pexpect.EOF found!!!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 342 | main.cleanAndExit() |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 343 | elif i == 8: # timeout |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 344 | main.log.error( |
| 345 | "No route to the Host " + |
| 346 | userName + |
| 347 | "@" + |
| 348 | ipAddress ) |
| 349 | returnVal = main.FALSE |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 350 | self.handle.expect( self.prompt ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 351 | return returnVal |
| 352 | |
| 353 | def scp( self, remoteHost, filePath, dstPath, direction="from" ): |
| 354 | """ |
| 355 | Definition: |
| 356 | Execute scp command in linux to copy to/from a remote host |
| 357 | Required: |
| 358 | * remoteHost - Test ON component to be parsed |
| 359 | str filePath - File path including the file it self |
| 360 | str dstPath - Destination path |
| 361 | Optional: |
| 362 | str direction - Direction of the scp, default to "from" which means |
| 363 | copy "from" the remote machine to local machine, |
| 364 | while "to" means copy "to" the remote machine from |
| 365 | local machine |
| 366 | """ |
| 367 | return self.secureCopy( remoteHost.user_name, |
| 368 | remoteHost.ip_address, |
| 369 | filePath, |
| 370 | dstPath, |
| 371 | pwd=remoteHost.pwd, |
| 372 | direction=direction ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 373 | |
| 374 | def sshToNode( self, ipAddress, uName="sdn", pwd="rocks" ): |
| 375 | ssh_newkey = 'Are you sure you want to continue connecting' |
| 376 | refused = "ssh: connect to host " + ipAddress + " port 22: Connection refused" |
| 377 | handle = pexpect.spawn( 'ssh -X ' + |
| 378 | uName + |
| 379 | '@' + |
| 380 | ipAddress, |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 381 | env={ "TERM": "vt100" }, |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 382 | maxread=1000000, |
| 383 | timeout=60 ) |
| 384 | |
| 385 | # set tty window size |
| 386 | handle.setwinsize( 24, 250 ) |
| 387 | |
| 388 | i = 5 |
| 389 | while i == 5: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 390 | i = handle.expect( [ ssh_newkey, |
| 391 | 'password:|Password:', |
| 392 | pexpect.EOF, |
| 393 | pexpect.TIMEOUT, |
| 394 | refused, |
| 395 | 'teston>', |
| 396 | self.prompt ], |
| 397 | 120 ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 398 | if i == 0: # Accept key, then expect either a password prompt or access |
| 399 | main.log.info( "ssh key confirmation received, send yes" ) |
| 400 | handle.sendline( 'yes' ) |
| 401 | i = 5 # Run the loop again |
| 402 | continue |
| 403 | if i == 1: # Password required |
| 404 | if pwd: |
| 405 | main.log.info( |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 406 | "ssh connection asked for password, gave password" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 407 | else: |
| 408 | main.log.info( "Server asked for password, but none was " |
| 409 | "given in the .topo file. Trying " |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 410 | "no password." ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 411 | pwd = "" |
| 412 | handle.sendline( pwd ) |
| 413 | j = handle.expect( [ self.prompt, |
| 414 | 'password:|Password:', |
| 415 | pexpect.EOF, |
| 416 | pexpect.TIMEOUT ], |
| 417 | 120 ) |
| 418 | if j != 0: |
| 419 | main.log.error( "Incorrect Password" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 420 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 421 | elif i == 2: |
| 422 | main.log.error( "Connection timeout" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 423 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 424 | elif i == 3: # timeout |
| 425 | main.log.error( |
| 426 | "No route to the Host " + |
| 427 | uName + |
| 428 | "@" + |
| 429 | ipAddress ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 430 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 431 | elif i == 4: |
| 432 | main.log.error( |
| 433 | "ssh: connect to host " + |
| 434 | ipAddress + |
| 435 | " port 22: Connection refused" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 436 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 437 | elif i == 6: |
| 438 | main.log.info( "Password not required logged in" ) |
| 439 | |
| 440 | handle.sendline( "" ) |
| 441 | handle.expect( self.prompt ) |
| 442 | handle.sendline( "cd" ) |
| 443 | handle.expect( self.prompt ) |
| 444 | |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 445 | main.log.info( "Successfully ssh to " + ipAddress + "." ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 446 | return handle |
| 447 | |
| 448 | def exitFromSsh( self, handle, ipAddress ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 449 | try: |
Jon Hall | 4f360bc | 2017-09-07 10:19:52 -0700 | [diff] [blame] | 450 | handle.sendline( "logout" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 451 | handle.expect( "closed." ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 452 | main.log.info( "Successfully closed ssh connection from " + ipAddress ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 453 | except pexpect.EOF: |
| 454 | main.log.error( "Failed to close the connection from " + ipAddress ) |
Jon Hall | 4f360bc | 2017-09-07 10:19:52 -0700 | [diff] [blame] | 455 | try: |
| 456 | # check that this component handle still works |
| 457 | self.handle.sendline( "" ) |
| 458 | self.handle.expect( self.prompt ) |
| 459 | except pexpect.EOF: |
| 460 | main.log.error( self.handle.before ) |
| 461 | main.log.error( "EOF after closing ssh connection" ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 462 | |
| 463 | def folderSize( self, path, size='10', unit='M', ignoreRoot=True ): |
| 464 | """ |
| 465 | Run `du -h` on the folder path and verifies the folder(s) size is |
| 466 | less than the given size. Note that if multiple subdirectories are |
| 467 | present, the result will be the OR of all the individual subdirectories. |
| 468 | |
| 469 | Arguments: |
| 470 | path - A string containing the path supplied to the du command |
| 471 | size - The number portion of the file size that the results will be compared to |
| 472 | unit - The unit portion of the file size that the results will be compared to |
| 473 | ignoreRoot - If True, will ignore the "root" of the path supplied to du. I.E. will ignore `.` |
| 474 | |
| 475 | Returns True if the folder(s) size(s) are less than SIZE UNITS, else returns False |
| 476 | """ |
| 477 | sizeRe = r'(?P<number>\d+\.*\d*)(?P<unit>\D)' |
| 478 | unitsList = [ 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' ] |
| 479 | try: |
| 480 | # make sure we convert units if size is too big |
| 481 | size = float( size ) |
| 482 | if size >= 1000: |
| 483 | size = size / 1000 |
| 484 | unit = unitsList[ unitsList.index( unit + 1 ) ] |
| 485 | cmdStr = "du -h " + path |
| 486 | self.handle.sendline( cmdStr ) |
| 487 | self.handle.expect( self.prompt ) |
| 488 | output = self.handle.before |
| 489 | assert "cannot access" not in output |
| 490 | assert "command not found" not in output |
| 491 | main.log.debug( output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 492 | lines = [ line for line in output.split( '\r\n' ) ] |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 493 | retValue = True |
| 494 | if ignoreRoot: |
| 495 | lastIndex = -2 |
| 496 | else: |
| 497 | lastIndex = -1 |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 498 | for line in lines[ 1:lastIndex ]: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 499 | parsed = line.split() |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 500 | sizeMatch = parsed[ 0 ] |
| 501 | folder = parsed[ 1 ] |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 502 | match = re.search( sizeRe, sizeMatch ) |
| 503 | num = match.group( 'number' ) |
| 504 | unitMatch = match.group( 'unit' ) |
| 505 | if unitsList.index( unitMatch ) < unitsList.index( unit ): |
| 506 | retValue &= True |
| 507 | elif unitsList.index( unitMatch ) == unitsList.index( unit ): |
| 508 | if float( num ) < float( size ): |
| 509 | retValue &= True |
| 510 | else: |
| 511 | retValue &= False |
| 512 | elif unitsList.index( unitMatch ) > unitsList.index( unit ): |
| 513 | retValue &= False |
| 514 | return retValue |
| 515 | except AssertionError: |
| 516 | main.log.error( self.name + ": Could not execute command: " + output ) |
| 517 | return False |
| 518 | except pexpect.TIMEOUT: |
| 519 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 520 | main.log.error( self.name + ": " + self.handle.before ) |
| 521 | return False |
| 522 | except pexpect.EOF: |
| 523 | main.log.error( self.name + ": EOF exception found" ) |
| 524 | main.log.error( self.name + ": " + self.handle.before ) |
| 525 | main.cleanAndExit() |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 526 | |
| 527 | def setEnv( self, variable, value=None ): |
| 528 | """ |
| 529 | Sets the environment variable to the given value for the current shell session. |
| 530 | If value is None, will unset the variable. |
| 531 | |
| 532 | Required Arguments: |
| 533 | variable - The name of the environment variable to set. |
| 534 | |
| 535 | Optional Arguments: |
| 536 | value - The value to set the variable to. ( Defaults to None, which unsets the variable ) |
| 537 | |
| 538 | Returns True if no errors are detected else returns False |
| 539 | """ |
| 540 | try: |
| 541 | if value: |
| 542 | cmd = "export {}={}".format( variable, value ) |
| 543 | else: |
| 544 | cmd = "unset {}".format( variable ) |
| 545 | self.handle.sendline( cmd ) |
| 546 | self.handle.expect( self.prompt ) |
| 547 | main.log.debug( self.handle.before ) |
| 548 | return True |
| 549 | except AssertionError: |
| 550 | main.log.error( self.name + ": Could not execute command: " + output ) |
| 551 | return False |
| 552 | except pexpect.TIMEOUT: |
| 553 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 554 | main.log.error( self.name + ": " + self.handle.before ) |
| 555 | return False |
| 556 | except pexpect.EOF: |
| 557 | main.log.error( self.name + ": EOF exception found" ) |
| 558 | main.log.error( self.name + ": " + self.handle.before ) |
| 559 | main.cleanAndExit() |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 560 | |
| 561 | def exitFromCmd( self, expect, retry=10 ): |
| 562 | """ |
| 563 | Call this function when sending ctrl+c is required to kill the current |
| 564 | command. It will retry multiple times until the running command is |
| 565 | completely killed and expected string is returned from the handle. |
| 566 | Required: |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 567 | expect: expected string or list of strings which indicates that the |
| 568 | previous command was killed successfully. |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 569 | Optional: |
| 570 | retry: maximum number of ctrl+c that will be sent. |
| 571 | """ |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 572 | expect = [ expect ] if isinstance( expect, str ) else expect |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 573 | try: |
| 574 | while retry >= 0: |
| 575 | main.log.debug( self.name + ": sending ctrl+c to kill the command" ) |
| 576 | self.handle.send( "\x03" ) |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 577 | i = self.handle.expect( expect + [ pexpect.TIMEOUT ], timeout=3 ) |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 578 | main.log.debug( self.handle.before ) |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 579 | if i < len( expect ): |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 580 | main.log.debug( self.name + ": successfully killed the command" ) |
| 581 | return main.TRUE |
| 582 | retry -= 1 |
| 583 | main.log.warn( self.name + ": failed to kill the command" ) |
| 584 | return main.FALSE |
| 585 | except pexpect.EOF: |
| 586 | main.log.error( self.name + ": EOF exception found" ) |
| 587 | main.log.error( self.name + ": " + self.handle.before ) |
| 588 | return main.FALSE |
| 589 | except Exception: |
| 590 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 591 | return main.FALSE |