1*** Settings ***
2Documentation     This module is for SSH connection override to QEMU
3...               based openbmc systems.
4
5Library           SSHLibrary   timeout=30 seconds
6Library           OperatingSystem
7Library           Collections
8
9*** Variables ***
10
11*** Keywords ***
12Open Connection And Log In
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}'
39    ...   User input SSH and HTTPs Ports
40
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
44
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
51    ...            SSHLibrary.Open connection  &{connection_args}
52    ...   ELSE  Run Keyword   SSHLibrary.Open connection  &{connection_args}
53
54    SSHLibrary.Login  ${username}  ${password}
55
56Open Connection for SCP
57    Import Library      SCPLibrary      WITH NAME       scp
58    Run Keyword If  '${SSH_PORT}' == '${EMPTY}'  scp.Open connection  ${OPENBMC_HOST}
59    ...  username=${OPENBMC_USERNAME}  password=${OPENBMC_PASSWORD}
60    ...  ELSE   Run Keyword    scp.Open connection  ${OPENBMC_HOST}  port=${SSH_PORT}
61    ...  username=${OPENBMC_USERNAME}  password=${OPENBMC_PASSWORD}
62
63
64User input SSH and HTTPs Ports
65    [Documentation]   Update the global SSH and HTTPs port variable for QEMU
66    ${port_num}=    Convert To Integer    ${SSH_PORT}
67    ${SSH_PORT}=    Replace Variables     ${port_num}
68
69    ${https_num}=   Convert To Integer    ${HTTPS_PORT}
70    Set Global Variable     ${AUTH_URI}    https://${OPENBMC_HOST}:${https_num}
71
72Validate Or Open Connection
73    [Documentation]  Checks for an open connection to a host or alias.
74    [Arguments]  ${alias}=None  ${host}=${EMPTY}  &{connection_args}
75
76    # alias            The alias of the connection to validate.
77    # host             The DNS name or IP of the host to validate.
78    # connection_args  A dictionary of arguments to pass to Open Connection
79    #                  and Log In (see above) if the connection is not open. May
80    #                  contain, but does not need to contain, the host or alias.
81
82    # Check to make sure we have an alias or host to search for.
83    Run Keyword If  '${host}' == '${EMPTY}'  Should Not Be Equal  ${alias}  None
84    ...  msg=Need to provide a host or an alias.  values=False
85
86    # Search the dictionary to see if it includes the host and alias.
87    ${host_exists}=  Run Keyword and Return Status
88    ...              Dictionary Should Contain Key  ${connection_args}  host
89    ${alias_exists}=  Run Keyword and Return Status
90    ...               Dictionary Should Contain Key  ${connection_args}  alias
91
92    # Add the alias and host back into the dictionary of connection arguments,
93    # if needed.
94    Run Keyword If  '${host}' != '${EMPTY}' and ${host_exists} == ${FALSE}
95    ...             Set to Dictionary  ${connection_args}  host  ${host}
96    Run Keyword If  '${alias}' != 'None' and ${alias_exists} == ${FALSE}
97    ...             Set to Dictionary  ${connection_args}  alias  ${alias}
98
99    @{open_connections}=  Get Connections
100    # If there are no open connections, open one and return.
101    Run Keyword If  '${open_connections}' == '[]'
102    ...             Open Connection and Log In  &{connection_args}
103    Return From Keyword If  '${open_connections}' == '[]'
104
105    # Connect to the alias or host that matches. If both are given, only connect
106    # to a connection that has both.
107    :FOR  ${connection}  IN  @{open_connections}
108    \  Log  ${connection}
109    \  ${alias_match}=  Evaluate  '${alias}' == '${connection.alias}'
110    \  ${host_match}=  Evaluate  '${host}' == '${connection.host}'
111    \  ${given_alias}=  Evaluate  '${alias}' != 'None'
112    \  ${no_alias}=  Evaluate  '${alias}' == 'None'
113    \  ${given_host}=  Evaluate  '${host}' != '${EMPTY}'
114    \  ${no_host}=  Evaluate  '${host}' == '${EMPTY}'
115    \  Run Keyword If
116    ...    ${given_alias} and ${given_host} and ${alias_match} and ${host_match}
117    ...    Run Keywords
118    ...      Switch Connection  ${alias}  AND
119    ...      Log to Console  Found connection. Switched to ${alias} ${host}  AND
120    ...      Return From Keyword If  ${alias_match} and ${host_match}
121    ...    ELSE  Run Keyword If
122    ...      ${given_alias} and ${no_host} and ${alias_match}
123    ...      Run Keywords
124    ...        Switch Connection  ${alias}  AND
125    ...        Log to Console  Found connection. Switched to: ${alias}  AND
126    ...        Return From Keyword If  ${alias_match}
127    ...    ELSE  Run Keyword If
128    ...       ${given_host} and ${no_alias} and ${host_match}
129    ...       Run Keywords
130    ...         Switch Connection  ${connection.index}  AND
131    ...         Log to Console  Found Connection. Switched to: ${host}  AND
132    ...         Return From Keyword If  ${host_match}
133
134    # If no connections are found, open a connection with the provided args.
135    Log  No connection with provided arguments.  Opening a connection.
136    Open Connection and Log In  &{connection_args}
137
138
139Clear System Entry From Knownhosts
140    [Documentation]   Delete OPENBMC_HOST entry from known_hosts file.
141    ${cmd}=  Set Variable  sed '/${OPENBMC_HOST}/d' -i ~/.ssh/known_hosts
142    ${rc}  ${output}=  Run and Return RC and Output  ${cmd}
143
144