George Keishing | 4d6c1da | 2016-07-15 05:51:22 -0500 | [diff] [blame] | 1 | *** Settings *** |
| 2 | Documentation This module is for SSH connection override to QEMU |
| 3 | ... based openbmc systems. |
| 4 | |
George Keishing | ffcf02a | 2016-09-13 01:55:28 -0500 | [diff] [blame] | 5 | Library SSHLibrary timeout=30 seconds |
George Keishing | 4d6c1da | 2016-07-15 05:51:22 -0500 | [diff] [blame] | 6 | Library OperatingSystem |
root | 442f0ef | 2016-08-04 20:23:05 +0000 | [diff] [blame] | 7 | Library Collections |
George Keishing | 4d6c1da | 2016-07-15 05:51:22 -0500 | [diff] [blame] | 8 | |
| 9 | *** Variables *** |
| 10 | |
| 11 | *** Keywords *** |
| 12 | Open Connection And Log In |
root | 442f0ef | 2016-08-04 20:23:05 +0000 | [diff] [blame] | 13 | [Documentation] Opens a connection with the given arguments, and logs in. |
| 14 | ... Defaults to logging into the BMC. |
| 15 | [Arguments] ${username}=${OPENBMC_USERNAME} |
| 16 | ... ${password}=${OPENBMC_PASSWORD} &{connection_args} |
| 17 | |
| 18 | # username The username to log into the connection with. |
| 19 | # password The password to log into the connection with. |
| 20 | # connection_args A dictionary of acceptable inputs to the Open Connection |
| 21 | # keyword. This includes, but is not limited to, the |
| 22 | # following: |
| 23 | # host, alias, port, timeout, newline, prompt, term_type, |
| 24 | # width, height, path_separator, endcoding |
| 25 | # (For more information, please visit the SSHLibrary doc) |
| 26 | |
| 27 | # Of the above arguments to Open Connection, this keyword |
| 28 | # will provide the following default values: |
| 29 | # host ${OPENBMC_HOST} |
| 30 | |
| 31 | # If no host was provided, add ${OPENBMC_HOST} to the dictionary |
| 32 | ${has_host}= Run Keyword and Return Status |
| 33 | ... Dictionary Should Contain Key ${connection_args} host |
| 34 | Run Keyword If ${has_host} == ${FALSE} |
| 35 | ... Set To Dictionary ${connection_args} host=${OPENBMC_HOST} |
| 36 | |
| 37 | Run Keyword If |
| 38 | ... '${SSH_PORT}' != '${EMPTY}' and '${HTTPS_PORT}' != '${EMPTY}' |
George Keishing | 4d6c1da | 2016-07-15 05:51:22 -0500 | [diff] [blame] | 39 | ... User input SSH and HTTPs Ports |
| 40 | |
root | 442f0ef | 2016-08-04 20:23:05 +0000 | [diff] [blame] | 41 | # Check to see if a port to connect to was provided. |
| 42 | ${has_port}= Run Keyword and Return Status |
| 43 | ... Dictionary Should Contain Key ${connection_args} port |
George Keishing | 4d6c1da | 2016-07-15 05:51:22 -0500 | [diff] [blame] | 44 | |
root | 442f0ef | 2016-08-04 20:23:05 +0000 | [diff] [blame] | 45 | # If the ${SSH_PORT} is set and no port was provided, add the defined port |
| 46 | # to the dictionary and open the connection. Otherwise, open the connection |
| 47 | # with the either the provided port or the default port. |
| 48 | Run Keyword If '${SSH_PORT}' != '${EMPTY}' and ${has_port} == ${FALSE} |
| 49 | ... Run Keywords |
| 50 | ... Set To Dictionary ${connection_args} port=${SSH_PORT} AND |
George Keishing | 314cf85 | 2016-08-26 09:02:16 -0500 | [diff] [blame] | 51 | ... SSHLibrary.Open connection &{connection_args} |
| 52 | ... ELSE Run Keyword SSHLibrary.Open connection &{connection_args} |
root | 442f0ef | 2016-08-04 20:23:05 +0000 | [diff] [blame] | 53 | |
George Keishing | 939ac38 | 2017-10-28 10:35:41 -0500 | [diff] [blame] | 54 | SSHLibrary.Login ${username} ${password} |
George Keishing | 4d6c1da | 2016-07-15 05:51:22 -0500 | [diff] [blame] | 55 | |
George Keishing | 4346a41 | 2016-07-19 11:26:49 -0500 | [diff] [blame] | 56 | Open Connection for SCP |
Joy Onyerikwu | f4a807b | 2018-06-20 08:43:54 -0500 | [diff] [blame] | 57 | [Documentation] Open a connection for SCP. |
George Keishing | 4346a41 | 2016-07-19 11:26:49 -0500 | [diff] [blame] | 58 | Import Library SCPLibrary WITH NAME scp |
| 59 | Run Keyword If '${SSH_PORT}' == '${EMPTY}' scp.Open connection ${OPENBMC_HOST} |
| 60 | ... username=${OPENBMC_USERNAME} password=${OPENBMC_PASSWORD} |
| 61 | ... ELSE Run Keyword scp.Open connection ${OPENBMC_HOST} port=${SSH_PORT} |
| 62 | ... username=${OPENBMC_USERNAME} password=${OPENBMC_PASSWORD} |
| 63 | |
| 64 | |
George Keishing | 4d6c1da | 2016-07-15 05:51:22 -0500 | [diff] [blame] | 65 | User input SSH and HTTPs Ports |
| 66 | [Documentation] Update the global SSH and HTTPs port variable for QEMU |
| 67 | ${port_num}= Convert To Integer ${SSH_PORT} |
| 68 | ${SSH_PORT}= Replace Variables ${port_num} |
| 69 | |
| 70 | ${https_num}= Convert To Integer ${HTTPS_PORT} |
Sridevi Ramesh | 89a8692 | 2016-07-22 01:00:36 -0500 | [diff] [blame] | 71 | Set Global Variable ${AUTH_URI} https://${OPENBMC_HOST}:${https_num} |
Leah McNutt | c9c9cde | 2016-10-07 16:53:52 +0000 | [diff] [blame] | 72 | |
| 73 | Validate Or Open Connection |
| 74 | [Documentation] Checks for an open connection to a host or alias. |
| 75 | [Arguments] ${alias}=None ${host}=${EMPTY} &{connection_args} |
| 76 | |
| 77 | # alias The alias of the connection to validate. |
| 78 | # host The DNS name or IP of the host to validate. |
Gunnar Mills | 28e403b | 2017-10-25 16:16:38 -0500 | [diff] [blame] | 79 | # connection_args A dictionary of arguments to pass to Open Connection |
Leah McNutt | c9c9cde | 2016-10-07 16:53:52 +0000 | [diff] [blame] | 80 | # and Log In (see above) if the connection is not open. May |
| 81 | # contain, but does not need to contain, the host or alias. |
| 82 | |
| 83 | # Check to make sure we have an alias or host to search for. |
| 84 | Run Keyword If '${host}' == '${EMPTY}' Should Not Be Equal ${alias} None |
| 85 | ... msg=Need to provide a host or an alias. values=False |
| 86 | |
| 87 | # Search the dictionary to see if it includes the host and alias. |
| 88 | ${host_exists}= Run Keyword and Return Status |
| 89 | ... Dictionary Should Contain Key ${connection_args} host |
| 90 | ${alias_exists}= Run Keyword and Return Status |
| 91 | ... Dictionary Should Contain Key ${connection_args} alias |
| 92 | |
| 93 | # Add the alias and host back into the dictionary of connection arguments, |
| 94 | # if needed. |
| 95 | Run Keyword If '${host}' != '${EMPTY}' and ${host_exists} == ${FALSE} |
| 96 | ... Set to Dictionary ${connection_args} host ${host} |
| 97 | Run Keyword If '${alias}' != 'None' and ${alias_exists} == ${FALSE} |
| 98 | ... Set to Dictionary ${connection_args} alias ${alias} |
| 99 | |
| 100 | @{open_connections}= Get Connections |
| 101 | # If there are no open connections, open one and return. |
| 102 | Run Keyword If '${open_connections}' == '[]' |
| 103 | ... Open Connection and Log In &{connection_args} |
| 104 | Return From Keyword If '${open_connections}' == '[]' |
| 105 | |
| 106 | # Connect to the alias or host that matches. If both are given, only connect |
| 107 | # to a connection that has both. |
| 108 | :FOR ${connection} IN @{open_connections} |
| 109 | \ Log ${connection} |
| 110 | \ ${alias_match}= Evaluate '${alias}' == '${connection.alias}' |
| 111 | \ ${host_match}= Evaluate '${host}' == '${connection.host}' |
| 112 | \ ${given_alias}= Evaluate '${alias}' != 'None' |
| 113 | \ ${no_alias}= Evaluate '${alias}' == 'None' |
| 114 | \ ${given_host}= Evaluate '${host}' != '${EMPTY}' |
| 115 | \ ${no_host}= Evaluate '${host}' == '${EMPTY}' |
| 116 | \ Run Keyword If |
| 117 | ... ${given_alias} and ${given_host} and ${alias_match} and ${host_match} |
| 118 | ... Run Keywords |
| 119 | ... Switch Connection ${alias} AND |
| 120 | ... Log to Console Found connection. Switched to ${alias} ${host} AND |
| 121 | ... Return From Keyword If ${alias_match} and ${host_match} |
| 122 | ... ELSE Run Keyword If |
| 123 | ... ${given_alias} and ${no_host} and ${alias_match} |
| 124 | ... Run Keywords |
| 125 | ... Switch Connection ${alias} AND |
| 126 | ... Log to Console Found connection. Switched to: ${alias} AND |
| 127 | ... Return From Keyword If ${alias_match} |
| 128 | ... ELSE Run Keyword If |
| 129 | ... ${given_host} and ${no_alias} and ${host_match} |
| 130 | ... Run Keywords |
| 131 | ... Switch Connection ${connection.index} AND |
| 132 | ... Log to Console Found Connection. Switched to: ${host} AND |
| 133 | ... Return From Keyword If ${host_match} |
| 134 | |
| 135 | # If no connections are found, open a connection with the provided args. |
| 136 | Log No connection with provided arguments. Opening a connection. |
| 137 | Open Connection and Log In &{connection_args} |
Vijay | 1a52c99 | 2017-09-19 08:37:26 -0500 | [diff] [blame] | 138 | |
| 139 | |
| 140 | Clear System Entry From Knownhosts |
| 141 | [Documentation] Delete OPENBMC_HOST entry from known_hosts file. |
| 142 | ${cmd}= Set Variable sed '/${OPENBMC_HOST}/d' -i ~/.ssh/known_hosts |
| 143 | ${rc} ${output}= Run and Return RC and Output ${cmd} |
| 144 | |