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 |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 25 | import os |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 26 | |
| 27 | from drivers.component import Component |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class CLI( Component ): |
| 31 | |
| 32 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 33 | This will define common functions for CLI included. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 34 | """ |
| 35 | def __init__( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 36 | super( CLI, self ).__init__() |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 37 | self.inDocker = False |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 38 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 39 | def checkPrompt( self ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 40 | for key in self.options: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 41 | if key == "prompt" and self.options[ 'prompt' ] is not None: |
| 42 | self.prompt = self.options[ 'prompt' ] |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 43 | break |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | |
| 45 | def connect( self, **connectargs ): |
| 46 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 47 | Connection will establish to the remote host using ssh. |
| 48 | It will take user_name ,ip_address and password as arguments<br> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 49 | and will return the handle. |
| 50 | """ |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 51 | self.shell = "/bin/bash -l" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 53 | vars( self )[ key ] = connectargs[ key ] |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 54 | self.checkPrompt() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 55 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 56 | connect_result = super( CLI, self ).connect() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 58 | refused = "ssh: connect to host " + \ |
| 59 | self.ip_address + " port 22: Connection refused" |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 60 | ssh_options = "-t -X -A -o ServerAliveInterval=120 -o TCPKeepAlive=yes" |
| 61 | ssh_destination = self.user_name + "@" + self.ip_address |
| 62 | envVars = { "TERM": "vt100" } |
| 63 | # TODO: Add option to specify which shell/command to use |
| 64 | jump_host = main.componentDictionary[ self.name ].get( 'jump_host' ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 65 | if self.port: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 66 | ssh_option += " -p " + self.port |
| 67 | if jump_host: |
| 68 | jump_host = main.componentDictionary.get( jump_host ) |
| 69 | ssh_options += " -J %s@%s" % ( jump_host.get( 'user' ), jump_host.get( 'host' ) ) |
| 70 | ssh_auth = os.getenv('SSH_AUTH_SOCK') |
| 71 | if ssh_auth: |
| 72 | envVars[ 'SSH_AUTH_SOCK' ] = ssh_auth |
| 73 | self.handle = pexpect.spawn( |
| 74 | "ssh %s %s %s" % ( ssh_options, ssh_destination, self.shell ), |
| 75 | env=envVars, |
| 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 |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 95 | main.log.info( self.name + ": ssh key confirmation received, send yes" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 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: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 104 | main.log.info( self.name + ": Server asked for password, but none was " |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 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: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 117 | main.log.error( self.name + ": Incorrect Password" ) |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 118 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 119 | elif i == 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 120 | main.log.error( self.name + ": Connection timeout" ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 121 | main.log.debug( self.handle.before ) |
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 ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 129 | main.log.debug( self.handle.before ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 130 | return main.FALSE |
| 131 | elif i == 4: |
| 132 | main.log.error( |
| 133 | "ssh: connect to host " + |
| 134 | self.ip_address + |
| 135 | " port 22: Connection refused" ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 136 | main.log.debug( self.handle.before ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | return main.FALSE |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 138 | elif i == 6: # Incorrect Password |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 139 | main.log.error( self.name + ": Incorrect Password" ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 140 | main.log.debug( self.handle.before ) |
Jon Hall | d906613 | 2018-03-01 14:52:53 -0800 | [diff] [blame] | 141 | return main.FALSE |
| 142 | elif i == 7: # Prompt |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 143 | main.log.info( self.name + ": Password not required logged in" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 144 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 145 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 146 | self.handle.expect( self.prompt ) |
Jeremy Ronquillo | 0f2008a | 2017-06-23 15:32:51 -0700 | [diff] [blame] | 147 | self.handle.sendline( "cd" ) |
| 148 | self.handle.expect( self.prompt ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 149 | return self.handle |
| 150 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 151 | def disconnect( self ): |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 152 | result = self.preDisconnect() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 153 | result = super( CLI, self ).disconnect( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 154 | result = main.TRUE |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 155 | |
| 156 | def Prompt( self ): |
| 157 | """ |
| 158 | Returns the prompt to expect depending on what program we are in |
| 159 | """ |
| 160 | return self.prompt if not self.inDocker else self.dockerPrompt |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 161 | |
| 162 | def execute( self, **execparams ): |
| 163 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 164 | It facilitates the command line execution of a given command. It has arguments as : |
| 165 | cmd => represents command to be executed, |
| 166 | prompt => represents expect command prompt or output, |
| 167 | timeout => timeout for command execution, |
| 168 | more => to provide a key press if it is on. |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 169 | logCmd => log the command executed if True |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 170 | |
| 171 | It will return output of command exection. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 172 | """ |
| 173 | result = super( CLI, self ).execute( self ) |
admin | aef0055 | 2014-05-08 09:18:36 -0700 | [diff] [blame] | 174 | defaultPrompt = '.*[$>\#]' |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 175 | args = utilities.parse_args( [ "CMD", |
| 176 | "TIMEOUT", |
| 177 | "PROMPT", |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 178 | "MORE", |
| 179 | "LOGCMD" ], |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 180 | **execparams ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 181 | |
| 182 | expectPrompt = args[ "PROMPT" ] if args[ "PROMPT" ] else defaultPrompt |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 183 | self.LASTRSP = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 184 | timeoutVar = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] else 10 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 185 | cmd = '' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 186 | if args[ "CMD" ]: |
| 187 | cmd = args[ "CMD" ] |
| 188 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | return 0 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 190 | if args[ "MORE" ] is None: |
| 191 | args[ "MORE" ] = " " |
| 192 | self.handle.sendline( cmd ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 193 | self.lastCommand = cmd |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 194 | index = self.handle.expect( [ expectPrompt, |
| 195 | "--More--", |
| 196 | 'Command not found.', |
| 197 | pexpect.TIMEOUT, |
| 198 | "^:$" ], |
| 199 | timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 200 | if index == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 201 | self.LASTRSP = self.LASTRSP + \ |
| 202 | self.handle.before + self.handle.after |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 203 | if not args[ "LOGCMD" ] is False: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 204 | main.log.info( self.name + ": Executed :" + str( cmd ) + |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 205 | " \t\t Expected Prompt '" + str( expectPrompt ) + |
| 206 | "' Found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 207 | elif index == 1: |
| 208 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 209 | self.handle.send( args[ "MORE" ] ) |
| 210 | main.log.info( |
| 211 | "Found More screen to go , Sending a key to proceed" ) |
| 212 | indexMore = self.handle.expect( |
| 213 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 214 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 215 | main.log.info( |
| 216 | "Found anoother More screen to go , Sending a key to proceed" ) |
| 217 | self.handle.send( args[ "MORE" ] ) |
| 218 | indexMore = self.handle.expect( |
| 219 | [ "--More--", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 220 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 221 | elif index == 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 222 | main.log.error( self.name + ": Command not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 223 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 224 | elif index == 3: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 225 | main.log.error( self.name + ": Expected Prompt not found, Time Out!!" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 226 | main.log.error( expectPrompt ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 227 | self.LASTRSP = self.LASTRSP + self.handle.before |
| 228 | return self.LASTRSP |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 229 | elif index == 4: |
| 230 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 231 | # self.handle.send( args[ "MORE" ] ) |
| 232 | self.handle.sendcontrol( "D" ) |
| 233 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 234 | "Found More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 235 | indexMore = self.handle.expect( |
| 236 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 237 | while indexMore == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 238 | main.log.info( |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 239 | "Found another More screen to go, Sending a key to proceed" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 240 | self.handle.sendcontrol( "D" ) |
| 241 | indexMore = self.handle.expect( |
| 242 | [ "^:$", expectPrompt ], timeout=timeoutVar ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 243 | self.LASTRSP = self.LASTRSP + self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 244 | main.last_response = self.remove_contol_chars( self.LASTRSP ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 245 | return self.LASTRSP |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 246 | |
| 247 | def remove_contol_chars( self, response ): |
| 248 | # 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 ) ) |
| 249 | # response = re.sub( RE_XML_ILLEGAL, "\n", response ) |
| 250 | response = re.sub( r"[\x01-\x1F\x7F]", "", response ) |
| 251 | # response = re.sub( r"\[\d+\;1H", "\n", response ) |
| 252 | response = re.sub( r"\[\d+\;\d+H", "", response ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 253 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 254 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | def runAsSudoUser( self, handle, pwd, default ): |
| 256 | |
| 257 | i = handle.expect( [ ".ssword:*", default, pexpect.EOF ] ) |
| 258 | if i == 0: |
| 259 | handle.sendline( pwd ) |
Jon Hall | 5ec6b1b | 2015-09-17 18:20:14 -0700 | [diff] [blame] | 260 | handle.sendline( "\n" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 261 | |
| 262 | if i == 1: |
| 263 | handle.expect( default ) |
| 264 | |
| 265 | if i == 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 266 | main.log.error( self.name + ": Unable to run as Sudo user" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 268 | return handle |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 269 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 270 | def onfail( self ): |
| 271 | if 'onfail' in main.componentDictionary[ self.name ]: |
| 272 | commandList = main.componentDictionary[ |
| 273 | self.name ][ 'onfail' ].split( "," ) |
| 274 | for command in commandList: |
| 275 | response = self.execute( |
| 276 | cmd=command, |
| 277 | prompt="(.*)", |
| 278 | timeout=120 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 279 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 280 | def secureCopy( self, userName, ipAddress, filePath, dstPath, pwd="", |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 281 | direction="from", options="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 282 | """ |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 283 | Definition: |
| 284 | Execute scp command in linux to copy to/from a remote host |
| 285 | Required: |
| 286 | str userName - User name of the remote host |
| 287 | str ipAddress - IP address of the remote host |
| 288 | str filePath - File path including the file it self |
| 289 | str dstPath - Destination path |
| 290 | Optional: |
| 291 | str pwd - Password of the host |
| 292 | str direction - Direction of the scp, default to "from" which means |
| 293 | copy "from" the remote machine to local machine, |
| 294 | while "to" means copy "to" the remote machine from |
| 295 | local machine |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 296 | """ |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 297 | returnVal = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 298 | ssh_newkey = 'Are you sure you want to continue connecting' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | refused = "ssh: connect to host " + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 300 | ipAddress + " port 22: Connection refused" |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 301 | cmd = "scp %s " % options |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 302 | try: |
| 303 | self.handle.sendline( "" ) |
| 304 | self.handle.expect( self.prompt, timeout=5 ) |
| 305 | except pexpect.TIMEOUT: |
| 306 | main.log.error( "%s: Component not ready for input" % self.name ) |
| 307 | main.log.debug( "%s: %s%s" % ( self.name, self.handle.before, str( self.handle.after ) ) ) |
| 308 | self.handle.send( "\x03" ) # CTRL-C |
| 309 | self.handle.expect( self.prompt, timeout=5 ) |
acsmars | 07f9d39 | 2015-07-15 10:30:58 -0700 | [diff] [blame] | 310 | |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 311 | if direction == "from": |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 312 | cmd = cmd + str( userName ) + '@' + str( ipAddress ) + ':' + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 313 | str( filePath ) + ' ' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 314 | elif direction == "to": |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 315 | cmd = cmd + str( filePath ) + ' ' + str( userName ) + \ |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 316 | '@' + str( ipAddress ) + ':' + str( dstPath ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 317 | else: |
| 318 | main.log.debug( "Wrong direction using secure copy command!" ) |
| 319 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 320 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 321 | main.log.info( self.name + ": Sending: " + cmd ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 322 | self.handle.sendline( cmd ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 323 | i = 0 |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 324 | timeout = 120 |
| 325 | while i <= 6 : |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 326 | i = self.handle.expect( [ |
| 327 | ssh_newkey, |
| 328 | 'password:', |
| 329 | "100%", |
| 330 | refused, |
| 331 | "No such file or directory", |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 332 | "Permission denied", |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 333 | self.prompt, |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 334 | pexpect.EOF, |
| 335 | pexpect.TIMEOUT ], |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 336 | timeout=timeout ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 337 | if i == 0: # ask for ssh key confirmation |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 338 | main.log.info( self.name + ": ssh key confirmation received, sending yes" ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 339 | self.handle.sendline( 'yes' ) |
| 340 | elif i == 1: # Asked for ssh password |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 341 | timeout = 120 |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 342 | main.log.info( self.name + ": ssh connection asked for password, gave password" ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 343 | self.handle.sendline( pwd ) |
| 344 | elif i == 2: # File finished transfering |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 345 | main.log.info( self.name + ": Secure copy successful" ) |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 346 | timeout = 10 |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 347 | returnVal = main.TRUE |
| 348 | elif i == 3: # Connection refused |
| 349 | main.log.error( |
| 350 | "ssh: connect to host " + |
| 351 | ipAddress + |
| 352 | " port 22: Connection refused" ) |
| 353 | returnVal = main.FALSE |
| 354 | elif i == 4: # File Not found |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 355 | main.log.error( self.name + ": No such file found" ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 356 | main.log.debug( self.handle.before + self.handle.after ) |
Jon Hall | 547e058 | 2015-09-21 17:35:40 -0700 | [diff] [blame] | 357 | returnVal = main.FALSE |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 358 | elif i == 5: # Permission denied |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 359 | main.log.error( self.name + ": Permission denied. Check folder permissions" ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 360 | main.log.debug( self.handle.before + self.handle.after ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 361 | returnVal = main.FALSE |
| 362 | elif i == 6: # prompt returned |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 363 | timeout = 10 |
| 364 | main.log.debug( "%s: %s%s" % ( self.name, repr( self.handle.before ), repr( self.handle.after ) ) ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 365 | elif i == 7: # EOF |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 366 | main.log.error( self.name + ": Pexpect.EOF found!!!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 367 | main.cleanAndExit() |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 368 | elif i == 8: # timeout |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 369 | if returnVal != main.TRUE: |
| 370 | main.log.error( |
| 371 | "No route to the Host " + |
| 372 | userName + |
| 373 | "@" + |
| 374 | ipAddress ) |
| 375 | return returnVal |
| 376 | self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout=5 ) |
| 377 | main.log.debug( "%s: %s%s" % ( self.name, repr( self.handle.before ), repr( self.handle.after ) ) ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 378 | return returnVal |
| 379 | |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 380 | def scp( self, remoteHost, filePath, dstPath, direction="from", options="" ): |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 381 | """ |
| 382 | Definition: |
| 383 | Execute scp command in linux to copy to/from a remote host |
| 384 | Required: |
| 385 | * remoteHost - Test ON component to be parsed |
| 386 | str filePath - File path including the file it self |
| 387 | str dstPath - Destination path |
| 388 | Optional: |
| 389 | str direction - Direction of the scp, default to "from" which means |
| 390 | copy "from" the remote machine to local machine, |
| 391 | while "to" means copy "to" the remote machine from |
| 392 | local machine |
| 393 | """ |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 394 | jump_host = main.componentDictionary[ remoteHost.name ].get( 'jump_host' ) |
| 395 | if jump_host: |
| 396 | jump_host = main.componentDictionary.get( jump_host ) |
Jon Hall | 669bc86 | 2021-03-09 12:24:44 -0800 | [diff] [blame] | 397 | options += " -o 'ProxyJump %s@%s' " % ( jump_host.get( 'user' ), jump_host.get( 'host' ) ) |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 398 | return self.secureCopy( remoteHost.user_name, |
| 399 | remoteHost.ip_address, |
| 400 | filePath, |
| 401 | dstPath, |
| 402 | pwd=remoteHost.pwd, |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 403 | direction=direction, |
| 404 | options=options ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 405 | |
| 406 | def sshToNode( self, ipAddress, uName="sdn", pwd="rocks" ): |
| 407 | ssh_newkey = 'Are you sure you want to continue connecting' |
| 408 | refused = "ssh: connect to host " + ipAddress + " port 22: Connection refused" |
| 409 | handle = pexpect.spawn( 'ssh -X ' + |
| 410 | uName + |
| 411 | '@' + |
| 412 | ipAddress, |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 413 | env={ "TERM": "vt100" }, |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 414 | maxread=1000000, |
| 415 | timeout=60 ) |
| 416 | |
| 417 | # set tty window size |
| 418 | handle.setwinsize( 24, 250 ) |
| 419 | |
| 420 | i = 5 |
| 421 | while i == 5: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 422 | i = handle.expect( [ ssh_newkey, |
| 423 | 'password:|Password:', |
| 424 | pexpect.EOF, |
| 425 | pexpect.TIMEOUT, |
| 426 | refused, |
| 427 | 'teston>', |
| 428 | self.prompt ], |
| 429 | 120 ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 430 | 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] | 431 | main.log.info( self.name + ": ssh key confirmation received, send yes" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 432 | handle.sendline( 'yes' ) |
| 433 | i = 5 # Run the loop again |
| 434 | continue |
| 435 | if i == 1: # Password required |
| 436 | if pwd: |
| 437 | main.log.info( |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 438 | "ssh connection asked for password, gave password" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 439 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 440 | main.log.info( self.name + ": Server asked for password, but none was " |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 441 | "given in the .topo file. Trying " |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 442 | "no password." ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 443 | pwd = "" |
| 444 | handle.sendline( pwd ) |
| 445 | j = handle.expect( [ self.prompt, |
| 446 | 'password:|Password:', |
| 447 | pexpect.EOF, |
| 448 | pexpect.TIMEOUT ], |
| 449 | 120 ) |
| 450 | if j != 0: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 451 | main.log.error( self.name + ": Incorrect Password" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 452 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 453 | elif i == 2: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 454 | main.log.error( self.name + ": Connection timeout" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 455 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 456 | elif i == 3: # timeout |
| 457 | main.log.error( |
| 458 | "No route to the Host " + |
| 459 | uName + |
| 460 | "@" + |
| 461 | ipAddress ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 462 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 463 | elif i == 4: |
| 464 | main.log.error( |
| 465 | "ssh: connect to host " + |
| 466 | ipAddress + |
| 467 | " port 22: Connection refused" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 468 | main.cleanAndExit() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 469 | elif i == 6: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 470 | main.log.info( self.name + ": Password not required logged in" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 471 | |
| 472 | handle.sendline( "" ) |
| 473 | handle.expect( self.prompt ) |
| 474 | handle.sendline( "cd" ) |
| 475 | handle.expect( self.prompt ) |
| 476 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 477 | main.log.info( self.name + ": Successfully ssh to " + ipAddress + "." ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 478 | return handle |
| 479 | |
| 480 | def exitFromSsh( self, handle, ipAddress ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 481 | try: |
Jon Hall | 4f360bc | 2017-09-07 10:19:52 -0700 | [diff] [blame] | 482 | handle.sendline( "logout" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 483 | handle.expect( "closed." ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 484 | main.log.info( self.name + ": Successfully closed ssh connection from " + ipAddress ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 485 | except pexpect.EOF: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 486 | main.log.error( self.name + ": Failed to close the connection from " + ipAddress ) |
Jon Hall | 4f360bc | 2017-09-07 10:19:52 -0700 | [diff] [blame] | 487 | try: |
| 488 | # check that this component handle still works |
| 489 | self.handle.sendline( "" ) |
| 490 | self.handle.expect( self.prompt ) |
| 491 | except pexpect.EOF: |
| 492 | main.log.error( self.handle.before ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 493 | main.log.error( self.name + ": EOF after closing ssh connection" ) |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 494 | |
| 495 | def folderSize( self, path, size='10', unit='M', ignoreRoot=True ): |
| 496 | """ |
| 497 | Run `du -h` on the folder path and verifies the folder(s) size is |
| 498 | less than the given size. Note that if multiple subdirectories are |
| 499 | present, the result will be the OR of all the individual subdirectories. |
| 500 | |
| 501 | Arguments: |
| 502 | path - A string containing the path supplied to the du command |
| 503 | size - The number portion of the file size that the results will be compared to |
| 504 | unit - The unit portion of the file size that the results will be compared to |
| 505 | ignoreRoot - If True, will ignore the "root" of the path supplied to du. I.E. will ignore `.` |
| 506 | |
| 507 | Returns True if the folder(s) size(s) are less than SIZE UNITS, else returns False |
| 508 | """ |
| 509 | sizeRe = r'(?P<number>\d+\.*\d*)(?P<unit>\D)' |
| 510 | unitsList = [ 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' ] |
| 511 | try: |
| 512 | # make sure we convert units if size is too big |
| 513 | size = float( size ) |
| 514 | if size >= 1000: |
| 515 | size = size / 1000 |
| 516 | unit = unitsList[ unitsList.index( unit + 1 ) ] |
| 517 | cmdStr = "du -h " + path |
| 518 | self.handle.sendline( cmdStr ) |
| 519 | self.handle.expect( self.prompt ) |
| 520 | output = self.handle.before |
| 521 | assert "cannot access" not in output |
| 522 | assert "command not found" not in output |
| 523 | main.log.debug( output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 524 | lines = [ line for line in output.split( '\r\n' ) ] |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 525 | retValue = True |
| 526 | if ignoreRoot: |
| 527 | lastIndex = -2 |
| 528 | else: |
| 529 | lastIndex = -1 |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 530 | for line in lines[ 1:lastIndex ]: |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 531 | parsed = line.split() |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 532 | sizeMatch = parsed[ 0 ] |
| 533 | folder = parsed[ 1 ] |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 534 | match = re.search( sizeRe, sizeMatch ) |
| 535 | num = match.group( 'number' ) |
| 536 | unitMatch = match.group( 'unit' ) |
| 537 | if unitsList.index( unitMatch ) < unitsList.index( unit ): |
| 538 | retValue &= True |
| 539 | elif unitsList.index( unitMatch ) == unitsList.index( unit ): |
| 540 | if float( num ) < float( size ): |
| 541 | retValue &= True |
| 542 | else: |
| 543 | retValue &= False |
| 544 | elif unitsList.index( unitMatch ) > unitsList.index( unit ): |
| 545 | retValue &= False |
| 546 | return retValue |
| 547 | except AssertionError: |
| 548 | main.log.error( self.name + ": Could not execute command: " + output ) |
| 549 | return False |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 550 | except ValueError as e: |
| 551 | main.log.error( self.name + ": Error parsing output: " + output ) |
| 552 | main.log.error( e ) |
| 553 | return False |
Jon Hall | 4173b24 | 2017-09-12 17:04:38 -0700 | [diff] [blame] | 554 | except pexpect.TIMEOUT: |
| 555 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 556 | main.log.error( self.name + ": " + self.handle.before ) |
| 557 | return False |
| 558 | except pexpect.EOF: |
| 559 | main.log.error( self.name + ": EOF exception found" ) |
| 560 | main.log.error( self.name + ": " + self.handle.before ) |
| 561 | main.cleanAndExit() |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 562 | |
| 563 | def setEnv( self, variable, value=None ): |
| 564 | """ |
| 565 | Sets the environment variable to the given value for the current shell session. |
| 566 | If value is None, will unset the variable. |
| 567 | |
| 568 | Required Arguments: |
| 569 | variable - The name of the environment variable to set. |
| 570 | |
| 571 | Optional Arguments: |
| 572 | value - The value to set the variable to. ( Defaults to None, which unsets the variable ) |
| 573 | |
| 574 | Returns True if no errors are detected else returns False |
| 575 | """ |
| 576 | try: |
| 577 | if value: |
| 578 | cmd = "export {}={}".format( variable, value ) |
| 579 | else: |
| 580 | cmd = "unset {}".format( variable ) |
| 581 | self.handle.sendline( cmd ) |
| 582 | self.handle.expect( self.prompt ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 583 | output = self.handle.before |
| 584 | main.log.debug( output ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 585 | return True |
| 586 | except AssertionError: |
| 587 | main.log.error( self.name + ": Could not execute command: " + output ) |
| 588 | return False |
| 589 | except pexpect.TIMEOUT: |
| 590 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 591 | main.log.error( self.name + ": " + self.handle.before ) |
| 592 | return False |
| 593 | except pexpect.EOF: |
| 594 | main.log.error( self.name + ": EOF exception found" ) |
| 595 | main.log.error( self.name + ": " + self.handle.before ) |
| 596 | main.cleanAndExit() |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 597 | |
| 598 | def exitFromCmd( self, expect, retry=10 ): |
| 599 | """ |
| 600 | Call this function when sending ctrl+c is required to kill the current |
| 601 | command. It will retry multiple times until the running command is |
| 602 | completely killed and expected string is returned from the handle. |
| 603 | Required: |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 604 | expect: expected string or list of strings which indicates that the |
| 605 | previous command was killed successfully. |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 606 | Optional: |
| 607 | retry: maximum number of ctrl+c that will be sent. |
| 608 | """ |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 609 | expect = [ expect ] if isinstance( expect, str ) else expect |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 610 | try: |
| 611 | while retry >= 0: |
| 612 | main.log.debug( self.name + ": sending ctrl+c to kill the command" ) |
| 613 | self.handle.send( "\x03" ) |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 614 | i = self.handle.expect( expect + [ pexpect.TIMEOUT ], timeout=3 ) |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 615 | main.log.debug( self.handle.before ) |
You Wang | d4fae5c | 2018-08-22 13:56:49 -0700 | [diff] [blame] | 616 | if i < len( expect ): |
You Wang | b65d237 | 2018-08-17 15:37:59 -0700 | [diff] [blame] | 617 | main.log.debug( self.name + ": successfully killed the command" ) |
| 618 | return main.TRUE |
| 619 | retry -= 1 |
| 620 | main.log.warn( self.name + ": failed to kill the command" ) |
| 621 | return main.FALSE |
| 622 | except pexpect.EOF: |
| 623 | main.log.error( self.name + ": EOF exception found" ) |
| 624 | main.log.error( self.name + ": " + self.handle.before ) |
| 625 | return main.FALSE |
| 626 | except Exception: |
| 627 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 628 | return main.FALSE |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 629 | |
| 630 | def cleanOutput( self, output, debug=False ): |
| 631 | """ |
| 632 | Clean ANSI characters from output |
| 633 | """ |
| 634 | ansiEscape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') |
| 635 | cleaned = ansiEscape.sub( '', output ) |
| 636 | if debug: |
| 637 | main.log.debug( self.name + ": cleanOutput:" ) |
| 638 | main.log.debug( self.name + ": " + repr( cleaned ) ) |
| 639 | return cleaned |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 640 | |
| 641 | def dockerPull( self, image, tag=None ): |
| 642 | """ |
| 643 | Pull a docker image from a registry |
| 644 | """ |
| 645 | try: |
| 646 | imgStr = "%s%s" % ( image, ":%s" % tag if tag else "" ) |
| 647 | cmdStr = "docker pull %s" % imgStr |
| 648 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 649 | self.handle.sendline( cmdStr) |
| 650 | i = self.handle.expect( [ self.prompt, |
| 651 | "Error response from daemon", |
| 652 | pexpect.TIMEOUT ], 120 ) |
| 653 | if i == 0: |
| 654 | return main.TRUE |
| 655 | else: |
| 656 | main.log.error( self.name + ": Error pulling docker image " + imgStr ) |
| 657 | output = self.handle.before + str( self.handle.after ) |
| 658 | if i == 1: |
| 659 | self.handle.expect( self.prompt ) |
| 660 | output += self.handle.before + str( self.handle.after ) |
| 661 | main.log.debug( self.name + ": " + output ) |
| 662 | return main.FALSE |
| 663 | except pexpect.EOF: |
| 664 | main.log.error( self.name + ": EOF exception found" ) |
| 665 | main.log.error( self.name + ": " + self.handle.before ) |
| 666 | return main.FALSE |
| 667 | except Exception: |
| 668 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 669 | return main.FALSE |
| 670 | |
| 671 | def dockerBuild( self, path, imageTag, pull=False, options="", timeout=600 ): |
| 672 | """ |
| 673 | Build a docker image |
| 674 | Required Arguments: |
| 675 | - path: Path to the dockerfile, it is recommended to avoid relative paths |
| 676 | - imageTag: Give a tag to the built docker image |
| 677 | Optional Arguments: |
| 678 | - pull: Whether to attempt to pull latest images before building |
| 679 | - options: A string containing any addition optional arguments |
| 680 | for the docker build command |
| 681 | - timeout: How many seconds to wait for the build to complete |
| 682 | """ |
| 683 | try: |
| 684 | response = main.TRUE |
| 685 | if pull: |
| 686 | options = "--pull " + options |
| 687 | cmdStr = "docker build -t %s %s %s" % ( imageTag, options, path ) |
| 688 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 689 | self.handle.sendline( cmdStr) |
| 690 | i = self.handle.expect( [ "Successfully built", |
| 691 | "Error response from daemon", |
| 692 | pexpect.TIMEOUT ], timeout=timeout ) |
| 693 | output = self.handle.before |
| 694 | if i == 0: |
| 695 | output += self.handle.after |
| 696 | self.handle.expect( self.prompt ) |
| 697 | output += self.handle.before + self.handle.after |
| 698 | return response |
| 699 | elif i == 1: |
| 700 | response = main.FALSE |
| 701 | output += self.handle.after |
| 702 | self.handle.expect( self.prompt ) |
| 703 | output += self.handle.before + self.handle.after |
| 704 | elif i == 2: |
| 705 | response = main.FALSE |
| 706 | main.log.error( self.name + ": Error building docker image" ) |
| 707 | main.log.debug( self.name + ": " + output ) |
| 708 | return response |
| 709 | except pexpect.EOF: |
| 710 | main.log.error( self.name + ": EOF exception found" ) |
| 711 | main.log.error( self.name + ": " + self.handle.before ) |
| 712 | return main.FALSE |
| 713 | except Exception: |
| 714 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 715 | return main.FALSE |
| 716 | |
| 717 | def dockerStop( self, containerName ): |
| 718 | """ |
| 719 | Stop a docker container |
| 720 | Required Arguments: |
| 721 | - containerName: Name of the container to stop |
| 722 | """ |
| 723 | try: |
| 724 | cmdStr = "docker stop %s" % ( containerName ) |
| 725 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 726 | self.handle.sendline( cmdStr) |
| 727 | i = self.handle.expect( [ self.prompt, |
| 728 | "Error response from daemon", |
| 729 | pexpect.TIMEOUT ], 120 ) |
| 730 | output = self.handle.before |
| 731 | if i == 0: |
| 732 | return main.TRUE |
| 733 | elif i == 1: |
| 734 | output += self.handle.after |
| 735 | self.handle.expect( self.prompt ) |
| 736 | output += self.handle.before |
| 737 | elif i == 2: |
| 738 | pass |
| 739 | main.log.debug( "%s: %s" % ( self.name, output ) ) |
| 740 | if "No such container" in output: |
| 741 | return main.TRUE |
| 742 | main.log.error( self.name + ": Error stopping docker image" ) |
| 743 | main.log.debug( self.name + ": " + output ) |
| 744 | return main.FALSE |
| 745 | except pexpect.EOF: |
| 746 | main.log.error( self.name + ": EOF exception found" ) |
| 747 | main.log.error( self.name + ": " + self.handle.before ) |
| 748 | return main.FALSE |
| 749 | except Exception: |
| 750 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 751 | return main.FALSE |
| 752 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 753 | def dockerRun( self, image, containerName, options="", imageArgs="", background=False ): |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 754 | """ |
| 755 | Run a docker image |
| 756 | Required Arguments: |
| 757 | - containerName: Give a name to the container once its started |
| 758 | - image: Run the given image |
| 759 | Optional Arguments: |
| 760 | - options: A string containing any addition optional arguments |
| 761 | for the docker run command |
| 762 | - imageArgs: A string containing command line arguments for the |
| 763 | command run by docker |
| 764 | """ |
| 765 | try: |
| 766 | cmdStr = "docker run --name %s %s %s %s" % ( containerName, |
| 767 | options if options else "", |
| 768 | image, |
| 769 | imageArgs ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 770 | if background: |
| 771 | cmdStr += " &" |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 772 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 773 | self.handle.sendline( cmdStr) |
| 774 | i = self.handle.expect( [ self.prompt, |
| 775 | "Error response from daemon", |
| 776 | pexpect.TIMEOUT ], 120 ) |
| 777 | if i == 0: |
| 778 | return main.TRUE |
| 779 | else: |
| 780 | output = self.handle.before |
| 781 | main.log.debug( self.name + ": " + output ) |
| 782 | main.log.error( self.name + ": Error running docker image" ) |
| 783 | if i == 1: |
| 784 | output += self.handle.after |
| 785 | self.handle.expect( self.prompt ) |
| 786 | output += self.handle.before + self.handle.after |
| 787 | main.log.debug( self.name + ": " + output ) |
| 788 | return main.FALSE |
| 789 | except pexpect.EOF: |
| 790 | main.log.error( self.name + ": EOF exception found" ) |
| 791 | main.log.error( self.name + ": " + self.handle.before ) |
| 792 | return main.FALSE |
| 793 | except Exception: |
| 794 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 795 | return main.FALSE |
| 796 | |
| 797 | def dockerAttach( self, containerName, dockerPrompt="" ): |
| 798 | """ |
| 799 | Attach to a docker image |
| 800 | Required Arguments: |
| 801 | - containerName: The name of the container to attach to |
| 802 | Optional Arguments: |
| 803 | - dockerPrompt: a regex for matching the docker shell prompt |
| 804 | """ |
| 805 | try: |
| 806 | if dockerPrompt: |
| 807 | self.dockerPrompt = dockerPrompt |
| 808 | cmdStr = "docker attach %s" % containerName |
| 809 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 810 | self.handle.sendline( cmdStr) |
| 811 | i = self.handle.expect( [ self.dockerPrompt, |
| 812 | "Error response from daemon", |
| 813 | pexpect.TIMEOUT ] ) |
| 814 | if i == 0: |
| 815 | self.inDocker = True |
| 816 | return main.TRUE |
| 817 | else: |
| 818 | main.log.error( self.name + ": Error connecting to docker container" ) |
| 819 | output = self.handle.before + str( self.handle.after ) |
| 820 | if i == 1: |
| 821 | self.handle.expect( self.prompt ) |
| 822 | output += self.handle.before + str( self.handle.after ) |
| 823 | main.log.debug( self.name + ": " + output ) |
| 824 | return main.FALSE |
| 825 | except pexpect.EOF: |
| 826 | main.log.error( self.name + ": EOF exception found" ) |
| 827 | main.log.error( self.name + ": " + self.handle.before ) |
| 828 | return main.FALSE |
| 829 | except AttributeError as e: |
| 830 | main.log.exception( self.name + ": AttributeError - " + str( e ) ) |
| 831 | main.log.warn( self.name + ": Make sure dockerPrompt is set" ) |
| 832 | main.cleanup() |
| 833 | main.exit() |
| 834 | except Exception: |
| 835 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 836 | return main.FALSE |
| 837 | |
| 838 | def dockerExec( self, containerName, command="/bin/bash", options="-it", dockerPrompt="" ): |
| 839 | """ |
| 840 | Attach to a docker image |
| 841 | Required Arguments: |
| 842 | - containerName: The name of the container to attach to |
| 843 | Optional Arguments: |
| 844 | - command: Command to run in the docker container |
| 845 | - options: Docker exec options |
| 846 | - dockerPrompt: a regex for matching the docker shell prompt |
| 847 | """ |
| 848 | try: |
| 849 | if dockerPrompt: |
| 850 | self.dockerPrompt = dockerPrompt |
| 851 | cmdStr = "docker exec %s %s %s" % ( options, containerName, command ) |
| 852 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 853 | self.handle.sendline( cmdStr) |
| 854 | i = self.handle.expect( [ self.dockerPrompt, |
| 855 | "Error response from daemon", |
| 856 | pexpect.TIMEOUT ] ) |
| 857 | if i == 0: |
| 858 | self.inDocker = True |
| 859 | return main.TRUE |
| 860 | else: |
| 861 | main.log.error( self.name + ": Error connecting to docker container" ) |
| 862 | output = self.handle.before + str( self.handle.after ) |
| 863 | if i == 1: |
| 864 | self.handle.expect( self.prompt ) |
| 865 | output += self.handle.before + str( self.handle.after ) |
| 866 | main.log.debug( self.name + ": " + output ) |
| 867 | return main.FALSE |
| 868 | except pexpect.EOF: |
| 869 | main.log.error( self.name + ": EOF exception found" ) |
| 870 | main.log.error( self.name + ": " + self.handle.before ) |
| 871 | return main.FALSE |
| 872 | except AttributeError as e: |
| 873 | main.log.exception( self.name + ": AttributeError - " + str( e ) ) |
| 874 | main.log.warn( self.name + ": Make sure dockerPrompt is set" ) |
| 875 | main.cleanup() |
| 876 | main.exit() |
| 877 | except Exception: |
| 878 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 879 | return main.FALSE |
| 880 | |
| 881 | def dockerCp( self, containerName, dockerPath, hostPath, direction="from" ): |
| 882 | """ |
| 883 | Copy a file from/to a docker container to the host |
| 884 | Required Arguments: |
| 885 | - containerName: The name of the container to copy from/to |
| 886 | - dockerPath: the path in the container to copy from/to |
| 887 | - hostPath: the path on the host to copy to/from |
| 888 | Optional Arguments: |
| 889 | - direction: Choose whether to copy "from" the container or "to" the container |
| 890 | """ |
| 891 | try: |
| 892 | cmdStr = "docker cp " |
| 893 | if direction == "from": |
| 894 | cmdStr += "%s:%s %s" % ( containerName, dockerPath, hostPath ) |
| 895 | elif direction == "to": |
| 896 | cmdStr += "%s %s:%s" % ( hostPath, containerName, dockerPath ) |
| 897 | main.log.info( self.name + ": sending: " + cmdStr ) |
| 898 | self.handle.sendline( cmdStr) |
| 899 | i = self.handle.expect( [ self.prompt, |
| 900 | "Error", |
| 901 | pexpect.TIMEOUT ] ) |
| 902 | if i == 0: |
| 903 | retValue = main.TRUE |
| 904 | else: |
| 905 | main.log.error( self.name + ": Error in docker cp" ) |
| 906 | output = self.handle.before + str( self.handle.after ) |
| 907 | if i == 1: |
| 908 | self.handle.expect( self.prompt ) |
| 909 | output += self.handle.before + str( self.handle.after ) |
| 910 | main.log.debug( self.name + ": " + output ) |
| 911 | retValue = main.FALSE |
| 912 | return retValue |
| 913 | except pexpect.EOF: |
| 914 | main.log.error( self.name + ": EOF exception found" ) |
| 915 | main.log.error( self.name + ": " + self.handle.before ) |
| 916 | return main.FALSE |
| 917 | except AttributeError as e: |
| 918 | main.log.exception( self.name + ": AttributeError - " + str( e ) ) |
| 919 | main.log.warn( self.name + ": Make sure dockerPrompt is set" ) |
| 920 | main.cleanup() |
| 921 | main.exit() |
| 922 | except Exception: |
| 923 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 924 | return main.FALSE |
| 925 | |
| 926 | def dockerDisconnect( self ): |
| 927 | """ |
| 928 | Send ctrl-c, ctrl-d to session, which should close and exit the |
| 929 | attached docker session. This will likely exit the running program |
| 930 | in the container and also stop the container. |
| 931 | """ |
| 932 | try: |
| 933 | cmdStr = "\x03" |
| 934 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 935 | self.handle.send( cmdStr) |
| 936 | cmdStr = "\x04" |
| 937 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 938 | self.handle.send( cmdStr) |
| 939 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ] ) |
| 940 | if i == 0: |
| 941 | self.inDocker = False |
| 942 | return main.TRUE |
| 943 | else: |
| 944 | main.log.error( self.name + ": Error disconnecting from docker image" ) |
| 945 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 946 | return main.FALSE |
| 947 | except pexpect.EOF: |
| 948 | main.log.error( self.name + ": EOF exception found" ) |
| 949 | main.log.error( self.name + ": " + self.handle.before ) |
| 950 | return main.FALSE |
| 951 | except Exception: |
| 952 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 953 | return main.FALSE |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 954 | |
| 955 | # TODO: How is this different from exitFromCmd used elsewhere? |
| 956 | def exitFromProcess( self ): |
| 957 | """ |
| 958 | Send ctrl-c, which should close and exit the program |
| 959 | """ |
| 960 | try: |
| 961 | cmdStr = "\x03" |
| 962 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 963 | self.handle.send( cmdStr) |
| 964 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ] ) |
| 965 | if i == 0: |
| 966 | return main.TRUE |
| 967 | else: |
| 968 | main.log.error( self.name + ": Error exiting process" ) |
| 969 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 970 | return main.FALSE |
| 971 | except pexpect.EOF: |
| 972 | main.log.error( self.name + ": EOF exception found" ) |
| 973 | main.log.error( self.name + ": " + self.handle.before ) |
| 974 | return main.FALSE |
| 975 | except Exception: |
| 976 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 977 | return main.FALSE |
| 978 | |
| 979 | def preDisconnect( self ): |
| 980 | """ |
| 981 | A Stub for a function that will be called before disconnect. |
| 982 | This can be set if for instance, the shell is running a program |
| 983 | and needs to exit the program before disconnecting from the component |
| 984 | """ |
| 985 | print "preDisconnect" |
| 986 | return main.TRUE |
| 987 | |
Jon Hall | be3a2ac | 2021-03-15 12:28:06 -0700 | [diff] [blame] | 988 | def kubectlGetPodNames( self, kubeconfig=None, namespace=None, app=None, name=None, nodeName=None ): |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 989 | """ |
| 990 | Use kubectl to get the names of pods |
| 991 | Optional Arguments: |
| 992 | - kubeconfig: The path to a kubeconfig file |
| 993 | - namespace: The namespace to search in |
| 994 | - app: Get pods belonging to a specific app |
| 995 | - name: Get pods with a specific name label |
Jon Hall | be3a2ac | 2021-03-15 12:28:06 -0700 | [diff] [blame] | 996 | - nodeName: Get pods on a specific node |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 997 | Returns a list containing the names of the pods or |
| 998 | main.FALSE on Error |
| 999 | """ |
| 1000 | |
| 1001 | try: |
Jon Hall | be3a2ac | 2021-03-15 12:28:06 -0700 | [diff] [blame] | 1002 | cmdStr = "kubectl %s %s get pods %s %s %s --output=jsonpath='{.items..metadata.name}{\"\\n\"}'" % ( |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 1003 | "--kubeconfig %s" % kubeconfig if kubeconfig else "", |
| 1004 | "-n %s" % namespace if namespace else "", |
| 1005 | "-l app=%s" % app if app else "", |
Jon Hall | be3a2ac | 2021-03-15 12:28:06 -0700 | [diff] [blame] | 1006 | "-l name=%s" % name if name else "", |
| 1007 | "--field-selector=spec.nodeName=%s" % nodeName if nodeName else "" ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 1008 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 1009 | self.handle.sendline( cmdStr ) |
Jon Hall | 2941fce | 2021-04-13 10:38:23 -0700 | [diff] [blame] | 1010 | i = self.handle.expect( [ "not found", "error", "The connection to the server", "Unable to find", "No resources found", self.prompt ] ) |
| 1011 | if i == 4: |
| 1012 | # Command worked, but returned no pods |
| 1013 | output = self.handle.before + self.handle.after |
| 1014 | main.log.warn( self.name + ": " + output ) |
| 1015 | return [] |
| 1016 | elif i == 5: |
| 1017 | # Command returned pods |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 1018 | output = self.handle.before + self.handle.after |
| 1019 | names = output.split( '\r\n' )[1].split() |
| 1020 | return names |
| 1021 | else: |
Jon Hall | 2941fce | 2021-04-13 10:38:23 -0700 | [diff] [blame] | 1022 | # Some error occured |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 1023 | main.log.error( self.name + ": Error executing command" ) |
| 1024 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 1025 | return main.FALSE |
| 1026 | except pexpect.EOF: |
| 1027 | main.log.error( self.name + ": EOF exception found" ) |
| 1028 | main.log.error( self.name + ": " + self.handle.before ) |
| 1029 | return main.FALSE |
| 1030 | except pexpect.TIMEOUT: |
| 1031 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 1032 | main.log.error( self.name + ": " + self.handle.before ) |
| 1033 | return main.FALSE |
| 1034 | except Exception: |
| 1035 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1036 | return main.FALSE |
| 1037 | |
| 1038 | def kubectlDescribe( self, describeString, dstPath, kubeconfig=None, namespace=None ): |
| 1039 | """ |
| 1040 | Use kubectl to get the logs from a pod |
| 1041 | Required Arguments: |
| 1042 | - describeString: The string passed to the cli. Example: "pods" |
| 1043 | - dstPath: The location to save the logs to |
| 1044 | Optional Arguments: |
| 1045 | - kubeconfig: The path to a kubeconfig file |
| 1046 | - namespace: The namespace to search in |
| 1047 | Returns main.TRUE or |
| 1048 | main.FALSE on Error |
| 1049 | """ |
| 1050 | |
| 1051 | try: |
| 1052 | cmdStr = "kubectl %s %s describe %s > %s " % ( |
| 1053 | "--kubeconfig %s" % kubeconfig if kubeconfig else "", |
| 1054 | "-n %s" % namespace if namespace else "", |
| 1055 | describeString, |
| 1056 | dstPath ) |
| 1057 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 1058 | self.handle.sendline( cmdStr ) |
| 1059 | i = self.handle.expect( [ "not found", "error", "The connection to the server", self.prompt ] ) |
| 1060 | if i == 3: |
| 1061 | main.log.debug( self.name + ": " + self.handle.before ) |
| 1062 | return main.TRUE |
| 1063 | else: |
| 1064 | main.log.error( self.name + ": Error executing command" ) |
| 1065 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 1066 | return main.FALSE |
| 1067 | except pexpect.EOF: |
| 1068 | main.log.error( self.name + ": EOF exception found" ) |
| 1069 | main.log.error( self.name + ": " + self.handle.before ) |
| 1070 | return main.FALSE |
| 1071 | except pexpect.TIMEOUT: |
| 1072 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 1073 | main.log.error( self.name + ": " + self.handle.before ) |
| 1074 | return main.FALSE |
| 1075 | except Exception: |
| 1076 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1077 | return main.FALSE |
| 1078 | |
| 1079 | def kubectlPodNodes( self, dstPath=None, kubeconfig=None, namespace=None ): |
| 1080 | """ |
| 1081 | Use kubectl to get the logs from a pod |
| 1082 | Optional Arguments: |
| 1083 | - dstPath: The location to save the logs to |
| 1084 | - kubeconfig: The path to a kubeconfig file |
| 1085 | - namespace: The namespace to search in |
| 1086 | Returns main.TRUE if dstPath is given, else the output of the command or |
| 1087 | main.FALSE on Error |
| 1088 | """ |
| 1089 | |
| 1090 | try: |
Jon Hall | 50a0001 | 2021-03-08 11:06:11 -0800 | [diff] [blame] | 1091 | cmdStr = "kubectl %s %s get pods -o wide %s " % ( |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 1092 | "--kubeconfig %s" % kubeconfig if kubeconfig else "", |
| 1093 | "-n %s" % namespace if namespace else "", |
| 1094 | " > %s" % dstPath if dstPath else "" ) |
| 1095 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 1096 | self.handle.sendline( cmdStr ) |
| 1097 | i = self.handle.expect( [ "not found", "error", "The connection to the server", self.prompt ] ) |
| 1098 | if i == 3: |
| 1099 | output = self.handle.before |
| 1100 | main.log.debug( self.name + ": " + output ) |
| 1101 | return output if dstPath else main.TRUE |
| 1102 | else: |
| 1103 | main.log.error( self.name + ": Error executing command" ) |
| 1104 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 1105 | return main.FALSE |
| 1106 | except pexpect.EOF: |
| 1107 | main.log.error( self.name + ": EOF exception found" ) |
| 1108 | main.log.error( self.name + ": " + self.handle.before ) |
| 1109 | return main.FALSE |
| 1110 | except pexpect.TIMEOUT: |
| 1111 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 1112 | main.log.error( self.name + ": " + self.handle.before ) |
| 1113 | return main.FALSE |
| 1114 | except Exception: |
| 1115 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1116 | return main.FALSE |
| 1117 | |
Jon Hall | a7b27e6 | 2021-06-29 12:13:51 -0700 | [diff] [blame] | 1118 | def sternLogs( self, podString, dstPath, kubeconfig=None, namespace=None, since='1h', wait=60 ): |
| 1119 | """ |
| 1120 | Use stern to get the logs from a pod |
| 1121 | Required Arguments: |
| 1122 | - podString: The name of the pod or partial name of the pods to get the logs of |
| 1123 | - dstPath: The location to save the logs to |
| 1124 | Optional Arguments: |
| 1125 | - kubeconfig: The path to a kubeconfig file |
| 1126 | - namespace: The namespace to search in |
| 1127 | - since: Return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 1h |
| 1128 | - wait: How long to wait, in seconds, before killing the process. Stern does not currently |
| 1129 | support a way to exit if cought up to present time. Defaults to 60 seconds |
| 1130 | Returns main.TRUE or |
| 1131 | main.FALSE on Error |
| 1132 | """ |
| 1133 | import time |
| 1134 | try: |
| 1135 | cmdStr = "stern %s %s %s %s > %s " % ( |
| 1136 | "--kubeconfig %s" % kubeconfig if kubeconfig else "", |
| 1137 | "-n %s" % namespace if namespace else "", |
| 1138 | "--since %s" % since if since else "", |
| 1139 | podString, |
| 1140 | dstPath ) |
| 1141 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 1142 | self.handle.sendline( cmdStr ) |
| 1143 | time.sleep( int( wait ) ) |
| 1144 | self.handle.send( '\x03' ) # CTRL-C |
| 1145 | i = self.handle.expect( [ "not found", "Error: ", "The connection to the server", self.prompt ] ) |
| 1146 | if i == 3: |
| 1147 | main.log.debug( self.name + ": " + self.handle.before ) |
| 1148 | return main.TRUE |
| 1149 | else: |
| 1150 | main.log.error( self.name + ": Error executing command" ) |
| 1151 | response = self.handle.before + str( self.handle.after ) |
| 1152 | self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout=5 ) |
| 1153 | response += self.handle.before + str( self.handle.after ) |
| 1154 | main.log.debug( self.name + ": " + response ) |
| 1155 | return main.FALSE |
| 1156 | except pexpect.EOF: |
| 1157 | main.log.error( self.name + ": EOF exception found" ) |
| 1158 | main.log.error( self.name + ": " + self.handle.before ) |
| 1159 | return main.FALSE |
| 1160 | except pexpect.TIMEOUT: |
| 1161 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 1162 | main.log.error( self.name + ": " + self.handle.before ) |
| 1163 | return main.FALSE |
| 1164 | except Exception: |
| 1165 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1166 | return main.FALSE |
| 1167 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 1168 | def kubectlLogs( self, podName, dstPath, kubeconfig=None, namespace=None, timeout=240 ): |
| 1169 | """ |
| 1170 | Use kubectl to get the logs from a pod |
| 1171 | Required Arguments: |
| 1172 | - podName: The name of the pod to get the logs of |
| 1173 | - dstPath: The location to save the logs to |
| 1174 | Optional Arguments: |
| 1175 | - kubeconfig: The path to a kubeconfig file |
| 1176 | - namespace: The namespace to search in |
| 1177 | - timeout: Timeout for command to return. The longer the logs, the longer it will take to fetch them. |
| 1178 | Returns main.TRUE or |
| 1179 | main.FALSE on Error |
| 1180 | """ |
| 1181 | |
| 1182 | try: |
| 1183 | cmdStr = "kubectl %s %s logs %s > %s " % ( |
| 1184 | "--kubeconfig %s" % kubeconfig if kubeconfig else "", |
| 1185 | "-n %s" % namespace if namespace else "", |
| 1186 | podName, |
| 1187 | dstPath ) |
| 1188 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 1189 | self.handle.sendline( cmdStr ) |
| 1190 | i = self.handle.expect( [ "not found", "error", "The connection to the server", self.prompt ], timeout=timeout ) |
| 1191 | if i == 3: |
| 1192 | main.log.debug( self.name + ": " + self.handle.before ) |
| 1193 | return main.TRUE |
| 1194 | else: |
| 1195 | main.log.error( self.name + ": Error executing command" ) |
| 1196 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 1197 | return main.FALSE |
| 1198 | except pexpect.EOF: |
| 1199 | main.log.error( self.name + ": EOF exception found" ) |
| 1200 | main.log.error( self.name + ": " + self.handle.before ) |
| 1201 | return main.FALSE |
| 1202 | except pexpect.TIMEOUT: |
| 1203 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 1204 | main.log.error( self.name + ": " + self.handle.before ) |
| 1205 | return main.FALSE |
| 1206 | except Exception: |
| 1207 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1208 | return main.FALSE |
| 1209 | |
Jon Hall | be3a2ac | 2021-03-15 12:28:06 -0700 | [diff] [blame] | 1210 | def kubectlCp( self, podName, srcPath, dstPath, kubeconfig=None, namespace=None, timeout=240 ): |
| 1211 | """ |
| 1212 | Use kubectl to get a file from a pod |
| 1213 | Required Arguments: |
| 1214 | - podName: The name of the pod to get the logs of |
| 1215 | - srcPath: The file to copy from the pod |
| 1216 | - dstPath: The location to save the file to locally |
| 1217 | Optional Arguments: |
| 1218 | - kubeconfig: The path to a kubeconfig file |
| 1219 | - namespace: The namespace to search in |
| 1220 | - timeout: Timeout for command to return. The longer the logs, the longer it will take to fetch them. |
| 1221 | Returns main.TRUE or |
| 1222 | main.FALSE on Error |
| 1223 | """ |
| 1224 | |
| 1225 | try: |
| 1226 | cmdStr = "kubectl %s %s cp %s:%s %s" % ( |
| 1227 | "--kubeconfig %s" % kubeconfig if kubeconfig else "", |
| 1228 | "-n %s" % namespace if namespace else "", |
| 1229 | podName, |
| 1230 | srcPath, |
| 1231 | dstPath ) |
| 1232 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 1233 | self.handle.sendline( cmdStr ) |
| 1234 | i = self.handle.expect( [ "not found", "error", "The connection to the server", self.prompt ], timeout=timeout ) |
| 1235 | if i == 3: |
| 1236 | main.log.debug( self.name + ": " + self.handle.before ) |
| 1237 | return main.TRUE |
| 1238 | else: |
| 1239 | main.log.error( self.name + ": Error executing command" ) |
| 1240 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 1241 | return main.FALSE |
| 1242 | except pexpect.EOF: |
| 1243 | main.log.error( self.name + ": EOF exception found" ) |
| 1244 | main.log.error( self.name + ": " + self.handle.before ) |
| 1245 | return main.FALSE |
| 1246 | except pexpect.TIMEOUT: |
| 1247 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 1248 | main.log.error( self.name + ": " + self.handle.before ) |
| 1249 | return main.FALSE |
| 1250 | except Exception: |
| 1251 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1252 | return main.FALSE |
| 1253 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 1254 | def kubectlPortForward( self, podName, portsList, kubeconfig=None, namespace=None, ): |
| 1255 | """ |
| 1256 | Use kubectl to setup port forwarding from the local machine to the kubernetes pod |
| 1257 | |
| 1258 | Note: This command does not return until the port forwarding session is ended. |
| 1259 | |
| 1260 | Required Arguments: |
| 1261 | - podName: The name of the pod as a string |
| 1262 | - portsList: The list of ports to forward, as a string. see kubectl help for details |
| 1263 | Optional Arguments: |
| 1264 | - kubeconfig: The path to a kubeconfig file |
| 1265 | - namespace: The namespace to search in |
| 1266 | - app: Get pods belonging to a specific app |
| 1267 | Returns a list containing the names of the pods or |
| 1268 | main.FALSE on Error |
| 1269 | |
| 1270 | |
| 1271 | """ |
| 1272 | try: |
| 1273 | cmdStr = "kubectl %s %s port-forward pod/%s %s" % ( |
| 1274 | "--kubeconfig %s" % kubeconfig if kubeconfig else "", |
| 1275 | "-n %s" % namespace if namespace else "", |
| 1276 | podName, |
| 1277 | portsList ) |
| 1278 | main.log.info( self.name + ": sending: " + repr( cmdStr ) ) |
| 1279 | self.handle.sendline( cmdStr ) |
| 1280 | i = self.handle.expect( [ "not found", "error", "closed/timedout", |
| 1281 | self.prompt, "The connection to the server", "Forwarding from" ] ) |
| 1282 | # NOTE: This won't clear the buffer entirely, and each time the port forward |
| 1283 | # is used, another line will be added to the buffer. We need to make |
| 1284 | # sure we clear the buffer before using this component again. |
| 1285 | |
| 1286 | if i == 5: |
| 1287 | # Setup preDisconnect function |
| 1288 | self.preDisconnect = self.exitFromProcess |
| 1289 | return main.TRUE |
| 1290 | else: |
| 1291 | main.log.error( self.name + ": Error executing command" ) |
| 1292 | main.log.debug( self.name + ": " + self.handle.before + str( self.handle.after ) ) |
| 1293 | return main.FALSE |
| 1294 | except pexpect.EOF: |
| 1295 | main.log.error( self.name + ": EOF exception found" ) |
| 1296 | main.log.error( self.name + ": " + self.handle.before ) |
| 1297 | return main.FALSE |
| 1298 | except pexpect.TIMEOUT: |
| 1299 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 1300 | main.log.error( self.name + ": " + self.handle.before ) |
| 1301 | return main.FALSE |
| 1302 | except Exception: |
| 1303 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1304 | return main.FALSE |