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 | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 36 | self.inDocker = False |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 37 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 38 | def checkPrompt( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 39 | for key in self.options: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 40 | if key == "prompt" and self.options[ 'prompt' ] is not None: |
| 41 | self.prompt = self.options[ 'prompt' ] |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 42 | break |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 43 | |
| 44 | def connect( self, **connectargs ): |
| 45 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 46 | Connection will establish to the remote host using ssh. |
| 47 | It will take user_name ,ip_address and password as arguments<br> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 48 | and will return the handle. |
| 49 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 51 | vars( self )[ key ] = connectargs[ key ] |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 52 | self.checkPrompt() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 53 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 54 | connect_result = super( CLI, self ).connect() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 55 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 56 | refused = "ssh: connect to host " + \ |
| 57 | self.ip_address + " port 22: Connection refused" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 58 | if self.port: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 59 | self.handle = pexpect.spawn( |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 60 | 'ssh -X -p ' + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 61 | self.port + |
| 62 | ' ' + |
| 63 | self.user_name + |
| 64 | '@' + |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 65 | self.ip_address + |
| 66 | ' -o ServerAliveInterval=120 -o TCPKeepAlive=yes', |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 67 | env={ "TERM": "vt100" }, |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 68 | maxread=1000000 ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 69 | else: |
| 70 | self.handle = pexpect.spawn( |
| 71 | 'ssh -X ' + |
| 72 | self.user_name + |
| 73 | '@' + |
Jon Hall | 7a5e0a2 | 2017-12-11 10:35:50 -0800 | [diff] [blame] | 74 | self.ip_address + |
| 75 | ' -o ServerAliveInterval=120 -o TCPKeepAlive=yes', |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 76 | env={ "TERM": "vt100" }, |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 77 | maxread=1000000, |
| 78 | timeout=60 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 79 | |
Jon Hall | 73057ee | 2016-08-23 09:57:26 -0700 | [diff] [blame] | 80 | # set tty window size |
| 81 | self.handle.setwinsize( 24, 250 ) |
| 82 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 83 | self.handle.logfile = self.logfile_handler |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 84 | i = 5 |
| 85 | while i == 5: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 86 | i = self.handle.expect( [ ssh_newkey, |
| 87 | 'password:|Password:', |
| 88 | pexpect.EOF, |
| 89 | pexpect.TIMEOUT, |
| 90 | refused, |
| 91 | 'teston>', |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 92 | 'Permission denied, please try again.', |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 93 | self.prompt ], |
| 94 | 120 ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 95 | if i == 0: # Accept key, then expect either a password prompt or access |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 96 | main.log.info( self.name + ": ssh key confirmation received, send yes" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 97 | self.handle.sendline( 'yes' ) |
acsmars | 32de0bc | 2015-06-30 09:57:12 -0700 | [diff] [blame] | 98 | i = 5 # Run the loop again |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 99 | continue |
| 100 | if i == 1: # Password required |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 101 | if self.pwd: |
| 102 | main.log.info( |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 103 | "ssh connection asked for password, gave password" ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 104 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 105 | main.log.info( self.name + ": Server asked for password, but none was " |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 106 | "given in the .topo file. Trying " |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 107 | "no password." ) |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 108 | self.pwd = "" |
| 109 | self.handle.sendline( self.pwd ) |
| 110 | j = self.handle.expect( [ |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 111 | 'password:|Password:', |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 112 | 'Permission denied, please try again.', |
| 113 | self.prompt, |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 114 | pexpect.EOF, |
| 115 | pexpect.TIMEOUT ], |
| 116 | 120 ) |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 117 | if j != 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 118 | main.log.error( self.name + ": Incorrect Password" ) |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 119 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 120 | elif i == 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 121 | main.log.error( self.name + ": Connection timeout" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | return main.FALSE |
| 123 | elif i == 3: # timeout |
| 124 | main.log.error( |
| 125 | "No route to the Host " + |
| 126 | self.user_name + |
| 127 | "@" + |
| 128 | self.ip_address ) |
| 129 | return main.FALSE |
| 130 | elif i == 4: |
| 131 | main.log.error( |
| 132 | "ssh: connect to host " + |
| 133 | self.ip_address + |
| 134 | " port 22: Connection refused" ) |
| 135 | return main.FALSE |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 136 | elif i == 6: # Incorrect Password |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 137 | main.log.error( self.name + ": Incorrect Password" ) |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 138 | return main.FALSE |
| 139 | elif i == 7: # Prompt |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 140 | main.log.info( self.name + ": Password not required logged in" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 141 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 142 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 143 | self.handle.expect( self.prompt ) |
Jeremy Ronquillo | 0f2008a | 2017-06-23 15:32:51 -0700 | [diff] [blame] | 144 | self.handle.sendline( "cd" ) |
| 145 | self.handle.expect( self.prompt ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 146 | return self.handle |
| 147 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 148 | def disconnect( self ): |
| 149 | result = super( CLI, self ).disconnect( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | result = main.TRUE |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 151 | |
| 152 | def Prompt( self ): |
| 153 | """ |
| 154 | Returns the prompt to expect depending on what program we are in |
| 155 | """ |
| 156 | return self.prompt if not self.inDocker else self.dockerPrompt |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 157 | |
| 158 | def execute( self, **execparams ): |
| 159 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 160 | It facilitates the command line execution of a given command. It has arguments as : |
| 161 | cmd => represents command to be executed, |
| 162 | prompt => represents expect command prompt or output, |
| 163 | timeout => timeout for command execution, |
| 164 | more => to provide a key press if it is on. |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 165 | logCmd => log the command executed if True |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 166 | |
| 167 | It will return output of command exection. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 168 | """ |
| 169 | result = super( CLI, self ).execute( self ) |
admin | aef0055 | 2014-05-08 09:18:36 -0700 | [diff] [blame] | 170 | defaultPrompt = '.*[$>\#]' |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 171 | args = utilities.parse_args( [ "CMD", |
| 172 | "TIMEOUT", |
| 173 | "PROMPT", |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 174 | "MORE", |
| 175 | "LOGCMD" ], |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 176 | **execparams ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 177 | |
| 178 | expectPrompt = args[ "PROMPT" ] if args[ "PROMPT" ] else defaultPrompt |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 179 | self.LASTRSP = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | timeoutVar = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] else 10 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 181 | cmd = '' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 182 | if args[ "CMD" ]: |
| 183 | cmd = args[ "CMD" ] |
| 184 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 185 | return 0 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 186 | if args[ "MORE" ] is None: |
| 187 | args[ "MORE" ] = " " |
| 188 | self.handle.sendline( cmd ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | self.lastCommand = cmd |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 190 | index = self.handle.expect( [ expectPrompt, |
| 191 | "--More--", |
| 192 | 'Command not found.', |
| 193 | pexpect.TIMEOUT, |
| 194 | "^:$" ], |
| 195 | timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | if index == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 197 | self.LASTRSP = self.LASTRSP + \ |
| 198 | self.handle.before + self.handle.after |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 199 | if not args[ "LOGCMD" ] is False: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 200 | main.log.info( self.name + ": Executed :" + str( cmd ) + |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 201 | " \t\t Expected Prompt '" + str( expectPrompt ) + |
| 202 | "' Found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 203 | elif index == 1: |
| 204 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 205 | self.handle.send( args[ "MORE" ] ) |
| 206 | main.log.info( |
| 207 | "Found More screen to go , Sending a key to proceed" ) |
| 208 | indexMore = self.handle.expect( |
| 209 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 210 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 211 | main.log.info( |
| 212 | "Found anoother More screen to go , Sending a key to proceed" ) |
| 213 | self.handle.send( args[ "MORE" ] ) |
| 214 | indexMore = self.handle.expect( |
| 215 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 216 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 217 | elif index == 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 218 | main.log.error( self.name + ": Command not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 219 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 220 | elif index == 3: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 221 | main.log.error( self.name + ": Expected Prompt not found, Time Out!!" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 222 | main.log.error( expectPrompt ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 223 | self.LASTRSP = self.LASTRSP + self.handle.before |
| 224 | return self.LASTRSP |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 225 | elif index == 4: |
| 226 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 227 | # self.handle.send( args[ "MORE" ] ) |
| 228 | self.handle.sendcontrol( "D" ) |
| 229 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 230 | "Found More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 231 | indexMore = self.handle.expect( |
| 232 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 233 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 234 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 235 | "Found another More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 236 | self.handle.sendcontrol( "D" ) |
| 237 | indexMore = self.handle.expect( |
| 238 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 239 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 240 | main.last_response = self.remove_contol_chars( self.LASTRSP ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 241 | return self.LASTRSP |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 242 | |
| 243 | def remove_contol_chars( self, response ): |
| 244 | # 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 ) ) |
| 245 | # response = re.sub( RE_XML_ILLEGAL, "\n", response ) |
| 246 | response = re.sub( r"[\x01-\x1F\x7F]", "", response ) |
| 247 | # response = re.sub( r"\[\d+\;1H", "\n", response ) |
| 248 | response = re.sub( r"\[\d+\;\d+H", "", response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 249 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 250 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 251 | def runAsSudoUser( self, handle, pwd, default ): |
| 252 | |
| 253 | i = handle.expect( [ ".ssword:*", default, pexpect.EOF ] ) |
| 254 | if i == 0: |
| 255 | handle.sendline( pwd ) |
Jon Hall | 5ec6b1b | 2015-09-17 18:20:14 -0700 | [diff] [blame] | 256 | handle.sendline( "\n" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | |
| 258 | if i == 1: |
| 259 | handle.expect( default ) |
| 260 | |
| 261 | if i == 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 262 | main.log.error( self.name + ": Unable to run as Sudo user" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 263 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 264 | return handle |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 265 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 266 | def onfail( self ): |
| 267 | if 'onfail' in main.componentDictionary[ self.name ]: |
| 268 | commandList = main.componentDictionary[ |
| 269 | self.name ][ 'onfail' ].split( "," ) |
| 270 | for command in commandList: |
| 271 | response = self.execute( |
| 272 | cmd=command, |
| 273 | prompt="(.*)", |
| 274 | timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 275 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 276 | def secureCopy( self, userName, ipAddress, filePath, dstPath, pwd="", |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 277 | direction="from", options="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 279 | Definition: |
| 280 | Execute scp command in linux to copy to/from a remote host |
| 281 | Required: |
| 282 | str userName - User name of the remote host |
| 283 | str ipAddress - IP address of the remote host |
| 284 | str filePath - File path including the file it self |
| 285 | str dstPath - Destination path |
| 286 | Optional: |
| 287 | str pwd - Password of the host |
| 288 | str direction - Direction of the scp, default to "from" which means |
| 289 | copy "from" the remote machine to local machine, |
| 290 | while "to" means copy "to" the remote machine from |
| 291 | local machine |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 292 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 293 | returnVal = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 294 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 295 | refused = "ssh: connect to host " + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 296 | ipAddress + " port 22: Connection refused" |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 297 | cmd = "scp %s " % options |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 298 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 299 | if direction == "from": |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 300 | cmd = cmd + str( userName ) + '@' + str( ipAddress ) + ':' + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 301 | str( filePath ) + ' ' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 302 | elif direction == "to": |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 303 | cmd = cmd + str( filePath ) + ' ' + str( userName ) + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 304 | '@' + str( ipAddress ) + ':' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 305 | else: |
| 306 | main.log.debug( "Wrong direction using secure copy command!" ) |
| 307 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 308 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 309 | main.log.info( self.name + ": Sending: " + cmd ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 310 | self.handle.sendline( cmd ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 311 | i = 0 |
| 312 | while i < 2: |
| 313 | i = self.handle.expect( [ |
| 314 | ssh_newkey, |
| 315 | 'password:', |
| 316 | "100%", |
| 317 | refused, |
| 318 | "No such file or directory", |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 319 | "Permission denied", |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 320 | self.prompt, |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 321 | pexpect.EOF, |
| 322 | pexpect.TIMEOUT ], |
| 323 | 120 ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 324 | if i == 0: # ask for ssh key confirmation |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 325 | main.log.info( self.name + ": ssh key confirmation received, sending yes" ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 326 | self.handle.sendline( 'yes' ) |
| 327 | elif i == 1: # Asked for ssh password |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 328 | main.log.info( self.name + ": ssh connection asked for password, gave password" ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 329 | self.handle.sendline( pwd ) |
| 330 | elif i == 2: # File finished transfering |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 331 | main.log.info( self.name + ": Secure copy successful" ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 332 | returnVal = main.TRUE |
| 333 | elif i == 3: # Connection refused |
| 334 | main.log.error( |
| 335 | "ssh: connect to host " + |
| 336 | ipAddress + |
| 337 | " port 22: Connection refused" ) |
| 338 | returnVal = main.FALSE |
| 339 | elif i == 4: # File Not found |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 340 | main.log.error( self.name + ": No such file found" ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 341 | main.log.debug( self.handle.before + self.handle.after ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 342 | returnVal = main.FALSE |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 343 | elif i == 5: # Permission denied |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 344 | main.log.error( self.name + ": Permission denied. Check folder permissions" ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 345 | main.log.debug( self.handle.before + self.handle.after ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 346 | returnVal = main.FALSE |
| 347 | elif i == 6: # prompt returned |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 348 | return returnVal |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 349 | elif i == 7: # EOF |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 350 | main.log.error( self.name + ": Pexpect.EOF found!!!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 351 | main.cleanAndExit() |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 352 | elif i == 8: # timeout |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 353 | main.log.error( |
| 354 | "No route to the Host " + |
| 355 | userName + |
| 356 | "@" + |
| 357 | ipAddress ) |
| 358 | returnVal = main.FALSE |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 359 | self.handle.expect( self.prompt ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 360 | return returnVal |
| 361 | |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 362 | def scp( self, remoteHost, filePath, dstPath, direction="from", options="" ): |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 363 | """ |
| 364 | Definition: |
| 365 | Execute scp command in linux to copy to/from a remote host |
| 366 | Required: |
| 367 | * remoteHost - Test ON component to be parsed |
| 368 | str filePath - File path including the file it self |
| 369 | str dstPath - Destination path |
| 370 | Optional: |
| 371 | str direction - Direction of the scp, default to "from" which means |
| 372 | copy "from" the remote machine to local machine, |
| 373 | while "to" means copy "to" the remote machine from |
| 374 | local machine |
| 375 | """ |
| 376 | return self.secureCopy( remoteHost.user_name, |
| 377 | remoteHost.ip_address, |
| 378 | filePath, |
| 379 | dstPath, |
| 380 | pwd=remoteHost.pwd, |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 381 | direction=direction, |
| 382 | options=options ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 383 | |
| 384 | def sshToNode( self, ipAddress, uName="sdn", pwd="rocks" ): |
| 385 | ssh_newkey = 'Are you sure you want to continue connecting' |
| 386 | refused = "ssh: connect to host " + ipAddress + " port 22: Connection refused" |
| 387 | handle = pexpect.spawn( 'ssh -X ' + |
| 388 | uName + |
| 389 | '@' + |
| 390 | ipAddress, |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 391 | env={ "TERM": "vt100" }, |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 392 | maxread=1000000, |
| 393 | timeout=60 ) |
| 394 | |
| 395 | # set tty window size |
| 396 | handle.setwinsize( 24, 250 ) |
| 397 | |
| 398 | i = 5 |
| 399 | while i == 5: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 400 | i = handle.expect( [ ssh_newkey, |
| 401 | 'password:|Password:', |
| 402 | pexpect.EOF, |
| 403 | pexpect.TIMEOUT, |
| 404 | refused, |
| 405 | 'teston>', |
| 406 | self.prompt ], |
| 407 | 120 ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 408 | if i == 0: # Accept key, then expect either a password prompt or access |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 409 | main.log.info( self.name + ": ssh key confirmation received, send yes" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 410 | handle.sendline( 'yes' ) |
| 411 | i = 5 # Run the loop again |
| 412 | continue |
| 413 | if i == 1: # Password required |
| 414 | if pwd: |
| 415 | main.log.info( |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 416 | "ssh connection asked for password, gave password" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 417 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 418 | main.log.info( self.name + ": Server asked for password, but none was " |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 419 | "given in the .topo file. Trying " |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 420 | "no password." ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 421 | pwd = "" |
| 422 | handle.sendline( pwd ) |
| 423 | j = handle.expect( [ self.prompt, |
| 424 | 'password:|Password:', |
| 425 | pexpect.EOF, |
| 426 | pexpect.TIMEOUT ], |
| 427 | 120 ) |
| 428 | if j != 0: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 429 | main.log.error( self.name + ": Incorrect Password" ) |
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 == 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 432 | main.log.error( self.name + ": Connection timeout" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 433 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 434 | elif i == 3: # timeout |
| 435 | main.log.error( |
| 436 | "No route to the Host " + |
| 437 | uName + |
| 438 | "@" + |
| 439 | ipAddress ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 440 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 441 | elif i == 4: |
| 442 | main.log.error( |
| 443 | "ssh: connect to host " + |
| 444 | ipAddress + |
| 445 | " port 22: Connection refused" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 446 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 447 | elif i == 6: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 448 | main.log.info( self.name + ": Password not required logged in" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 449 | |
| 450 | handle.sendline( "" ) |
| 451 | handle.expect( self.prompt ) |
| 452 | handle.sendline( "cd" ) |
| 453 | handle.expect( self.prompt ) |
| 454 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 455 | main.log.info( self.name + ": Successfully ssh to " + ipAddress + "." ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 456 | return handle |
| 457 | |
| 458 | def exitFromSsh( self, handle, ipAddress ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 459 | try: |
Jon Hall | 4f360bc | 2017-09-07 10:19:52 -0700 | [diff] [blame] | 460 | handle.sendline( "logout" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 461 | handle.expect( "closed." ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 462 | main.log.info( self.name + ": Successfully closed ssh connection from " + ipAddress ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 463 | except pexpect.EOF: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 464 | main.log.error( self.name + ": Failed to close the connection from " + ipAddress ) |
Jon Hall | 4f360bc | 2017-09-07 10:19:52 -0700 | [diff] [blame] | 465 | try: |
| 466 | # check that this component handle still works |
| 467 | self.handle.sendline( "" ) |
| 468 | self.handle.expect( self.prompt ) |
| 469 | except pexpect.EOF: |
| 470 | main.log.error( self.handle.before ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 471 | main.log.error( self.name + ": EOF after closing ssh connection" ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 472 | |
| 473 | def folderSize( self, path, size='10', unit='M', ignoreRoot=True ): |
| 474 | """ |
| 475 | Run `du -h` on the folder path and verifies the folder(s) size is |
| 476 | less than the given size. Note that if multiple subdirectories are |
| 477 | present, the result will be the OR of all the individual subdirectories. |
| 478 | |
| 479 | Arguments: |
| 480 | path - A string containing the path supplied to the du command |
| 481 | size - The number portion of the file size that the results will be compared to |
| 482 | unit - The unit portion of the file size that the results will be compared to |
| 483 | ignoreRoot - If True, will ignore the "root" of the path supplied to du. I.E. will ignore `.` |
| 484 | |
| 485 | Returns True if the folder(s) size(s) are less than SIZE UNITS, else returns False |
| 486 | """ |
| 487 | sizeRe = r'(?P<number>\d+\.*\d*)(?P<unit>\D)' |
| 488 | unitsList = [ 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' ] |
| 489 | try: |
| 490 | # make sure we convert units if size is too big |
| 491 | size = float( size ) |
| 492 | if size >= 1000: |
| 493 | size = size / 1000 |
| 494 | unit = unitsList[ unitsList.index( unit + 1 ) ] |
| 495 | cmdStr = "du -h " + path |
| 496 | self.handle.sendline( cmdStr ) |
| 497 | self.handle.expect( self.prompt ) |
| 498 | output = self.handle.before |
| 499 | assert "cannot access" not in output |
| 500 | assert "command not found" not in output |
| 501 | main.log.debug( output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 502 | lines = [ line for line in output.split( '\r\n' ) ] |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 503 | retValue = True |
| 504 | if ignoreRoot: |
| 505 | lastIndex = -2 |
| 506 | else: |
| 507 | lastIndex = -1 |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 508 | for line in lines[ 1:lastIndex ]: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 509 | parsed = line.split() |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 510 | sizeMatch = parsed[ 0 ] |
| 511 | folder = parsed[ 1 ] |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 512 | match = re.search( sizeRe, sizeMatch ) |
| 513 | num = match.group( 'number' ) |
| 514 | unitMatch = match.group( 'unit' ) |
| 515 | if unitsList.index( unitMatch ) < unitsList.index( unit ): |
| 516 | retValue &= True |
| 517 | elif unitsList.index( unitMatch ) == unitsList.index( unit ): |
| 518 | if float( num ) < float( size ): |
| 519 | retValue &= True |
| 520 | else: |
| 521 | retValue &= False |
| 522 | elif unitsList.index( unitMatch ) > unitsList.index( unit ): |
| 523 | retValue &= False |
| 524 | return retValue |
| 525 | except AssertionError: |
| 526 | main.log.error( self.name + ": Could not execute command: " + output ) |
| 527 | return False |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 528 | except ValueError as e: |
| 529 | main.log.error( self.name + ": Error parsing output: " + output ) |
| 530 | main.log.error( e ) |
| 531 | return False |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 532 | except pexpect.TIMEOUT: |
| 533 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 534 | main.log.error( self.name + ": " + self.handle.before ) |
| 535 | return False |
| 536 | except pexpect.EOF: |
| 537 | main.log.error( self.name + ": EOF exception found" ) |
| 538 | main.log.error( self.name + ": " + self.handle.before ) |
| 539 | main.cleanAndExit() |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 540 | |
| 541 | def setEnv( self, variable, value=None ): |
| 542 | """ |
| 543 | Sets the environment variable to the given value for the current shell session. |
| 544 | If value is None, will unset the variable. |
| 545 | |
| 546 | Required Arguments: |
| 547 | variable - The name of the environment variable to set. |
| 548 | |
| 549 | Optional Arguments: |
| 550 | value - The value to set the variable to. ( Defaults to None, which unsets the variable ) |
| 551 | |
| 552 | Returns True if no errors are detected else returns False |
| 553 | """ |
| 554 | try: |
| 555 | if value: |
| 556 | cmd = "export {}={}".format( variable, value ) |
| 557 | else: |
| 558 | cmd = "unset {}".format( variable ) |
| 559 | self.handle.sendline( cmd ) |
| 560 | self.handle.expect( self.prompt ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 561 | output = self.handle.before |
| 562 | main.log.debug( output ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 563 | return True |
| 564 | except AssertionError: |
| 565 | main.log.error( self.name + ": Could not execute command: " + output ) |
| 566 | return False |
| 567 | except pexpect.TIMEOUT: |
| 568 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 569 | main.log.error( self.name + ": " + self.handle.before ) |
| 570 | return False |
| 571 | except pexpect.EOF: |
| 572 | main.log.error( self.name + ": EOF exception found" ) |
| 573 | main.log.error( self.name + ": " + self.handle.before ) |
| 574 | main.cleanAndExit() |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 575 | |
| 576 | def exitFromCmd( self, expect, retry=10 ): |
| 577 | """ |
| 578 | Call this function when sending ctrl+c is required to kill the current |
| 579 | command. It will retry multiple times until the running command is |
| 580 | completely killed and expected string is returned from the handle. |
| 581 | Required: |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 582 | expect: expected string or list of strings which indicates that the |
| 583 | previous command was killed successfully. |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 584 | Optional: |
| 585 | retry: maximum number of ctrl+c that will be sent. |
| 586 | """ |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 587 | expect = [ expect ] if isinstance( expect, str ) else expect |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 588 | try: |
| 589 | while retry >= 0: |
| 590 | main.log.debug( self.name + ": sending ctrl+c to kill the command" ) |
| 591 | self.handle.send( "\x03" ) |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 592 | i = self.handle.expect( expect + [ pexpect.TIMEOUT ], timeout=3 ) |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 593 | main.log.debug( self.handle.before ) |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 594 | if i < len( expect ): |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 595 | main.log.debug( self.name + ": successfully killed the command" ) |
| 596 | return main.TRUE |
| 597 | retry -= 1 |
| 598 | main.log.warn( self.name + ": failed to kill the command" ) |
| 599 | return main.FALSE |
| 600 | except pexpect.EOF: |
| 601 | main.log.error( self.name + ": EOF exception found" ) |
| 602 | main.log.error( self.name + ": " + self.handle.before ) |
| 603 | return main.FALSE |
| 604 | except Exception: |
| 605 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 606 | return main.FALSE |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 607 | |
| 608 | def cleanOutput( self, output, debug=False ): |
| 609 | """ |
| 610 | Clean ANSI characters from output |
| 611 | """ |
| 612 | ansiEscape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') |
| 613 | cleaned = ansiEscape.sub( '', output ) |
| 614 | if debug: |
| 615 | main.log.debug( self.name + ": cleanOutput:" ) |
| 616 | main.log.debug( self.name + ": " + repr( cleaned ) ) |
| 617 | return cleaned |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 618 | |
| 619 | def dockerPull( self, image, tag=None ): |
| 620 | """ |
| 621 | Pull a docker image from a registry |
| 622 | """ |
| 623 | try: |
| 624 | imgStr = "%s%s" % ( image, ":%s" % tag if tag else "" ) |
| 625 | cmdStr = "docker pull %s" % imgStr |
| 626 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 627 | self.handle.sendline( cmdStr) |
| 628 | i = self.handle.expect( [ self.prompt, |
| 629 | "Error response from daemon", |
| 630 | pexpect.TIMEOUT ], 120 ) |
| 631 | if i == 0: |
| 632 | return main.TRUE |
| 633 | else: |
| 634 | main.log.error( self.name + ": Error pulling docker image " + imgStr ) |
| 635 | output = self.handle.before + str( self.handle.after ) |
| 636 | if i == 1: |
| 637 | self.handle.expect( self.prompt ) |
| 638 | output += self.handle.before + str( self.handle.after ) |
| 639 | main.log.debug( self.name + ": " + output ) |
| 640 | return main.FALSE |
| 641 | except pexpect.EOF: |
| 642 | main.log.error( self.name + ": EOF exception found" ) |
| 643 | main.log.error( self.name + ": " + self.handle.before ) |
| 644 | return main.FALSE |
| 645 | except Exception: |
| 646 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 647 | return main.FALSE |
| 648 | |
| 649 | def dockerBuild( self, path, imageTag, pull=False, options="", timeout=600 ): |
| 650 | """ |
| 651 | Build a docker image |
| 652 | Required Arguments: |
| 653 | - path: Path to the dockerfile, it is recommended to avoid relative paths |
| 654 | - imageTag: Give a tag to the built docker image |
| 655 | Optional Arguments: |
| 656 | - pull: Whether to attempt to pull latest images before building |
| 657 | - options: A string containing any addition optional arguments |
| 658 | for the docker build command |
| 659 | - timeout: How many seconds to wait for the build to complete |
| 660 | """ |
| 661 | try: |
| 662 | response = main.TRUE |
| 663 | if pull: |
| 664 | options = "--pull " + options |
| 665 | cmdStr = "docker build -t %s %s %s" % ( imageTag, options, path ) |
| 666 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 667 | self.handle.sendline( cmdStr) |
| 668 | i = self.handle.expect( [ "Successfully built", |
| 669 | "Error response from daemon", |
| 670 | pexpect.TIMEOUT ], timeout=timeout ) |
| 671 | output = self.handle.before |
| 672 | if i == 0: |
| 673 | output += self.handle.after |
| 674 | self.handle.expect( self.prompt ) |
| 675 | output += self.handle.before + self.handle.after |
| 676 | return response |
| 677 | elif i == 1: |
| 678 | response = main.FALSE |
| 679 | output += self.handle.after |
| 680 | self.handle.expect( self.prompt ) |
| 681 | output += self.handle.before + self.handle.after |
| 682 | elif i == 2: |
| 683 | response = main.FALSE |
| 684 | main.log.error( self.name + ": Error building docker image" ) |
| 685 | main.log.debug( self.name + ": " + output ) |
| 686 | return response |
| 687 | except pexpect.EOF: |
| 688 | main.log.error( self.name + ": EOF exception found" ) |
| 689 | main.log.error( self.name + ": " + self.handle.before ) |
| 690 | return main.FALSE |
| 691 | except Exception: |
| 692 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 693 | return main.FALSE |
| 694 | |
| 695 | def dockerStop( self, containerName ): |
| 696 | """ |
| 697 | Stop a docker container |
| 698 | Required Arguments: |
| 699 | - containerName: Name of the container to stop |
| 700 | """ |
| 701 | try: |
| 702 | cmdStr = "docker stop %s" % ( containerName ) |
| 703 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 704 | self.handle.sendline( cmdStr) |
| 705 | i = self.handle.expect( [ self.prompt, |
| 706 | "Error response from daemon", |
| 707 | pexpect.TIMEOUT ], 120 ) |
| 708 | output = self.handle.before |
| 709 | if i == 0: |
| 710 | return main.TRUE |
| 711 | elif i == 1: |
| 712 | output += self.handle.after |
| 713 | self.handle.expect( self.prompt ) |
| 714 | output += self.handle.before |
| 715 | elif i == 2: |
| 716 | pass |
| 717 | main.log.debug( "%s: %s" % ( self.name, output ) ) |
| 718 | if "No such container" in output: |
| 719 | return main.TRUE |
| 720 | main.log.error( self.name + ": Error stopping docker image" ) |
| 721 | main.log.debug( self.name + ": " + output ) |
| 722 | return main.FALSE |
| 723 | except pexpect.EOF: |
| 724 | main.log.error( self.name + ": EOF exception found" ) |
| 725 | main.log.error( self.name + ": " + self.handle.before ) |
| 726 | return main.FALSE |
| 727 | except Exception: |
| 728 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 729 | return main.FALSE |
| 730 | |
| 731 | def dockerRun( self, image, containerName, options="", imageArgs="" ): |
| 732 | """ |
| 733 | Run a docker image |
| 734 | Required Arguments: |
| 735 | - containerName: Give a name to the container once its started |
| 736 | - image: Run the given image |
| 737 | Optional Arguments: |
| 738 | - options: A string containing any addition optional arguments |
| 739 | for the docker run command |
| 740 | - imageArgs: A string containing command line arguments for the |
| 741 | command run by docker |
| 742 | """ |
| 743 | try: |
| 744 | cmdStr = "docker run --name %s %s %s %s" % ( containerName, |
| 745 | options if options else "", |
| 746 | image, |
| 747 | imageArgs ) |
| 748 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 749 | self.handle.sendline( cmdStr) |
| 750 | i = self.handle.expect( [ self.prompt, |
| 751 | "Error response from daemon", |
| 752 | pexpect.TIMEOUT ], 120 ) |
| 753 | if i == 0: |
| 754 | return main.TRUE |
| 755 | else: |
| 756 | output = self.handle.before |
| 757 | main.log.debug( self.name + ": " + output ) |
| 758 | main.log.error( self.name + ": Error running docker image" ) |
| 759 | if i == 1: |
| 760 | output += self.handle.after |
| 761 | self.handle.expect( self.prompt ) |
| 762 | output += self.handle.before + self.handle.after |
| 763 | main.log.debug( self.name + ": " + output ) |
| 764 | return main.FALSE |
| 765 | except pexpect.EOF: |
| 766 | main.log.error( self.name + ": EOF exception found" ) |
| 767 | main.log.error( self.name + ": " + self.handle.before ) |
| 768 | return main.FALSE |
| 769 | except Exception: |
| 770 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 771 | return main.FALSE |
| 772 | |
| 773 | def dockerAttach( self, containerName, dockerPrompt="" ): |
| 774 | """ |
| 775 | Attach to a docker image |
| 776 | Required Arguments: |
| 777 | - containerName: The name of the container to attach to |
| 778 | Optional Arguments: |
| 779 | - dockerPrompt: a regex for matching the docker shell prompt |
| 780 | """ |
| 781 | try: |
| 782 | if dockerPrompt: |
| 783 | self.dockerPrompt = dockerPrompt |
| 784 | cmdStr = "docker attach %s" % containerName |
| 785 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 786 | self.handle.sendline( cmdStr) |
| 787 | i = self.handle.expect( [ self.dockerPrompt, |
| 788 | "Error response from daemon", |
| 789 | pexpect.TIMEOUT ] ) |
| 790 | if i == 0: |
| 791 | self.inDocker = True |
| 792 | return main.TRUE |
| 793 | else: |
| 794 | main.log.error( self.name + ": Error connecting to docker container" ) |
| 795 | output = self.handle.before + str( self.handle.after ) |
| 796 | if i == 1: |
| 797 | self.handle.expect( self.prompt ) |
| 798 | output += self.handle.before + str( self.handle.after ) |
| 799 | main.log.debug( self.name + ": " + output ) |
| 800 | return main.FALSE |
| 801 | except pexpect.EOF: |
| 802 | main.log.error( self.name + ": EOF exception found" ) |
| 803 | main.log.error( self.name + ": " + self.handle.before ) |
| 804 | return main.FALSE |
| 805 | except AttributeError as e: |
| 806 | main.log.exception( self.name + ": AttributeError - " + str( e ) ) |
| 807 | main.log.warn( self.name + ": Make sure dockerPrompt is set" ) |
| 808 | main.cleanup() |
| 809 | main.exit() |
| 810 | except Exception: |
| 811 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 812 | return main.FALSE |
| 813 | |
| 814 | def dockerExec( self, containerName, command="/bin/bash", options="-it", dockerPrompt="" ): |
| 815 | """ |
| 816 | Attach to a docker image |
| 817 | Required Arguments: |
| 818 | - containerName: The name of the container to attach to |
| 819 | Optional Arguments: |
| 820 | - command: Command to run in the docker container |
| 821 | - options: Docker exec options |
| 822 | - dockerPrompt: a regex for matching the docker shell prompt |
| 823 | """ |
| 824 | try: |
| 825 | if dockerPrompt: |
| 826 | self.dockerPrompt = dockerPrompt |
| 827 | cmdStr = "docker exec %s %s %s" % ( options, containerName, command ) |
| 828 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 829 | self.handle.sendline( cmdStr) |
| 830 | i = self.handle.expect( [ self.dockerPrompt, |
| 831 | "Error response from daemon", |
| 832 | pexpect.TIMEOUT ] ) |
| 833 | if i == 0: |
| 834 | self.inDocker = True |
| 835 | return main.TRUE |
| 836 | else: |
| 837 | main.log.error( self.name + ": Error connecting to docker container" ) |
| 838 | output = self.handle.before + str( self.handle.after ) |
| 839 | if i == 1: |
| 840 | self.handle.expect( self.prompt ) |
| 841 | output += self.handle.before + str( self.handle.after ) |
| 842 | main.log.debug( self.name + ": " + output ) |
| 843 | return main.FALSE |
| 844 | except pexpect.EOF: |
| 845 | main.log.error( self.name + ": EOF exception found" ) |
| 846 | main.log.error( self.name + ": " + self.handle.before ) |
| 847 | return main.FALSE |
| 848 | except AttributeError as e: |
| 849 | main.log.exception( self.name + ": AttributeError - " + str( e ) ) |
| 850 | main.log.warn( self.name + ": Make sure dockerPrompt is set" ) |
| 851 | main.cleanup() |
| 852 | main.exit() |
| 853 | except Exception: |
| 854 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 855 | return main.FALSE |
| 856 | |
| 857 | def dockerCp( self, containerName, dockerPath, hostPath, direction="from" ): |
| 858 | """ |
| 859 | Copy a file from/to a docker container to the host |
| 860 | Required Arguments: |
| 861 | - containerName: The name of the container to copy from/to |
| 862 | - dockerPath: the path in the container to copy from/to |
| 863 | - hostPath: the path on the host to copy to/from |
| 864 | Optional Arguments: |
| 865 | - direction: Choose whether to copy "from" the container or "to" the container |
| 866 | """ |
| 867 | try: |
| 868 | cmdStr = "docker cp " |
| 869 | if direction == "from": |
| 870 | cmdStr += "%s:%s %s" % ( containerName, dockerPath, hostPath ) |
| 871 | elif direction == "to": |
| 872 | cmdStr += "%s %s:%s" % ( hostPath, containerName, dockerPath ) |
| 873 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 874 | self.handle.sendline( cmdStr) |
| 875 | i = self.handle.expect( [ self.prompt, |
| 876 | "Error", |
| 877 | pexpect.TIMEOUT ] ) |
| 878 | if i == 0: |
| 879 | retValue = main.TRUE |
| 880 | else: |
| 881 | main.log.error( self.name + ": Error in docker cp" ) |
| 882 | output = self.handle.before + str( self.handle.after ) |
| 883 | if i == 1: |
| 884 | self.handle.expect( self.prompt ) |
| 885 | output += self.handle.before + str( self.handle.after ) |
| 886 | main.log.debug( self.name + ": " + output ) |
| 887 | retValue = main.FALSE |
| 888 | return retValue |
| 889 | except pexpect.EOF: |
| 890 | main.log.error( self.name + ": EOF exception found" ) |
| 891 | main.log.error( self.name + ": " + self.handle.before ) |
| 892 | return main.FALSE |
| 893 | except AttributeError as e: |
| 894 | main.log.exception( self.name + ": AttributeError - " + str( e ) ) |
| 895 | main.log.warn( self.name + ": Make sure dockerPrompt is set" ) |
| 896 | main.cleanup() |
| 897 | main.exit() |
| 898 | except Exception: |
| 899 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 900 | return main.FALSE |
| 901 | |
| 902 | def dockerDisconnect( self ): |
| 903 | """ |
| 904 | Send ctrl-c, ctrl-d to session, which should close and exit the |
| 905 | attached docker session. This will likely exit the running program |
| 906 | in the container and also stop the container. |
| 907 | """ |
| 908 | try: |
| 909 | cmdStr = "\x03" |
| 910 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 911 | self.handle.send( cmdStr) |
| 912 | cmdStr = "\x04" |
| 913 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 914 | self.handle.send( cmdStr) |
| 915 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ] ) |
| 916 | if i == 0: |
| 917 | self.inDocker = False |
| 918 | return main.TRUE |
| 919 | else: |
| 920 | main.log.error( self.name + ": Error disconnecting from docker image" ) |
| 921 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 922 | return main.FALSE |
| 923 | except pexpect.EOF: |
| 924 | main.log.error( self.name + ": EOF exception found" ) |
| 925 | main.log.error( self.name + ": " + self.handle.before ) |
| 926 | return main.FALSE |
| 927 | except Exception: |
| 928 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 929 | return main.FALSE |