1#!/usr/bin/env python3
2
3r"""
4This module provides many valuable bmc ssh functions such as bmc_execute_command.
5"""
6
7import os
8
9import gen_robot_ssh as grs
10import gen_valid as gv
11from robot.libraries.BuiltIn import BuiltIn
12
13
14def bmc_execute_command(
15    cmd_buf,
16    print_out=0,
17    print_err=0,
18    ignore_err=0,
19    fork=0,
20    quiet=None,
21    test_mode=None,
22    time_out=None,
23):
24    r"""
25    Run the given command in an BMC SSH session and return the stdout, stderr and the return code.
26
27    This function will obtain the global values for OPENBMC_HOST, OPENBMC_USERNAME, etc.
28
29    Description of arguments:
30    cmd_buf                         The command string to be run in an SSH session.
31    print_out                       If this is set, this function will print the stdout/stderr generated by
32                                    the shell command.
33    print_err                       If show_err is set, this function will print a standardized error report
34                                    if the shell command returns non-zero.
35    ignore_err                      Indicates that errors encountered on the sshlib.execute_command are to be
36                                    ignored.
37    fork                            Indicates that sshlib.start is to be used rather than
38                                    sshlib.execute_command.
39    quiet                           Indicates whether this function should run the pissuing() function prints
40                                    an "Issuing: <cmd string>" to stdout.  This defaults to the global quiet
41                                    value.
42    test_mode                       If test_mode is set, this function will not actually run the command.
43                                    This defaults to the global test_mode value.
44    time_out                        The amount of time to allow for the execution of cmd_buf.  A value of
45                                    None means that there is no limit to how long the command may take.
46    """
47
48    # Get global BMC variable values.
49    openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}", default="")
50    ssh_port = BuiltIn().get_variable_value("${SSH_PORT}", default="22")
51    openbmc_username = BuiltIn().get_variable_value(
52        "${OPENBMC_USERNAME}", default=""
53    )
54    openbmc_password = BuiltIn().get_variable_value(
55        "${OPENBMC_PASSWORD}", default=""
56    )
57
58    if not gv.valid_value(openbmc_host):
59        return "", "", 1
60    if not gv.valid_value(openbmc_username):
61        return "", "", 1
62    if not gv.valid_value(openbmc_password):
63        return "", "", 1
64    if not gv.valid_value(ssh_port):
65        return "", "", 1
66
67    open_connection_args = {
68        "host": openbmc_host,
69        "alias": "bmc_connection",
70        "timeout": "25.0",
71        "prompt": "# ",
72        "port": ssh_port,
73    }
74    login_args = {"username": openbmc_username, "password": openbmc_password}
75
76    openbmc_user_type = os.environ.get(
77        "USER_TYPE", ""
78    ) or BuiltIn().get_variable_value("${USER_TYPE}", default="")
79    if openbmc_user_type == "sudo":
80        cmd_buf = "sudo -i " + cmd_buf
81    return grs.execute_ssh_command(
82        cmd_buf,
83        open_connection_args,
84        login_args,
85        print_out,
86        print_err,
87        ignore_err,
88        fork,
89        quiet,
90        test_mode,
91        time_out,
92    )
93
94
95def os_execute_command(
96    cmd_buf,
97    print_out=0,
98    print_err=0,
99    ignore_err=0,
100    fork=0,
101    quiet=None,
102    test_mode=None,
103    time_out=None,
104    os_host="",
105    os_username="",
106    os_password="",
107):
108    r"""
109    Run the given command in an OS SSH session and return the stdout, stderr and the return code.
110
111    This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
112
113    Description of arguments:
114    cmd_buf                         The command string to be run in an SSH session.
115    print_out                       If this is set, this function will print the stdout/stderr generated by
116                                    the shell command.
117    print_err                       If show_err is set, this function will print a standardized error report
118                                    if the shell command returns non-zero.
119    ignore_err                      Indicates that errors encountered on the sshlib.execute_command are to be
120                                    ignored.
121    fork                            Indicates that sshlib.start is to be used rather than
122                                    sshlib.execute_command.
123    quiet                           Indicates whether this function should run the pissuing() function prints
124                                    an "Issuing: <cmd string>" to stdout.  This defaults to the global quiet
125                                    value.
126    test_mode                       If test_mode is set, this function will not actually run the command.
127                                    This defaults to the global test_mode value.
128    time_out                        The amount of time to allow for the execution of cmd_buf.  A value of
129                                    None means that there is no limit to how long the command may take.
130    """
131
132    # Get global OS variable values.
133    if os_host == "":
134        os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
135    if os_username == "":
136        os_username = BuiltIn().get_variable_value(
137            "${OS_USERNAME}", default=""
138        )
139    if os_password == "":
140        os_password = BuiltIn().get_variable_value(
141            "${OS_PASSWORD}", default=""
142        )
143
144    if not gv.valid_value(os_host):
145        return "", "", 1
146    if not gv.valid_value(os_username):
147        return "", "", 1
148    if not gv.valid_value(os_password):
149        return "", "", 1
150
151    open_connection_args = {"host": os_host, "alias": "os_connection"}
152    login_args = {"username": os_username, "password": os_password}
153
154    return grs.execute_ssh_command(
155        cmd_buf,
156        open_connection_args,
157        login_args,
158        print_out,
159        print_err,
160        ignore_err,
161        fork,
162        quiet,
163        test_mode,
164        time_out,
165    )
166
167
168def xcat_execute_command(
169    cmd_buf,
170    print_out=0,
171    print_err=0,
172    ignore_err=0,
173    fork=0,
174    quiet=None,
175    test_mode=None,
176):
177    r"""
178    Run the given command in an XCAT SSH session and return the stdout, stderr and the return code.
179
180    This function will obtain the global values for XCAT_HOST, XCAT_USERNAME, etc.
181
182    Description of arguments:
183    cmd_buf                         The command string to be run in an SSH session.
184    print_out                       If this is set, this function will print the stdout/stderr generated by
185                                    the shell command.
186    print_err                       If show_err is set, this function will print a standardized error report
187                                    if the shell command returns non-zero.
188    ignore_err                      Indicates that errors encountered on the sshlib.execute_command are to be
189                                    ignored.
190    fork                            Indicates that sshlib.start is to be used rather than
191                                    sshlib.execute_command.
192    quiet                           Indicates whether this function should run the pissuing() function prints
193                                    an "Issuing: <cmd string>" to stdout.  This defaults to the global quiet
194                                    value.
195    test_mode                       If test_mode is set, this function will not actually run the command.
196                                    This defaults to the global test_mode value.
197    """
198
199    # Get global XCAT variable values.
200    xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
201    xcat_username = BuiltIn().get_variable_value(
202        "${XCAT_USERNAME}", default=""
203    )
204    xcat_password = BuiltIn().get_variable_value(
205        "${XCAT_PASSWORD}", default=""
206    )
207    xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}", default="22")
208
209    if not gv.valid_value(xcat_host):
210        return "", "", 1
211    if not gv.valid_value(xcat_username):
212        return "", "", 1
213    if not gv.valid_value(xcat_password):
214        return "", "", 1
215    if not gv.valid_value(xcat_port):
216        return "", "", 1
217
218    open_connection_args = {
219        "host": xcat_host,
220        "alias": "xcat_connection",
221        "port": xcat_port,
222    }
223    login_args = {"username": xcat_username, "password": xcat_password}
224
225    return grs.execute_ssh_command(
226        cmd_buf,
227        open_connection_args,
228        login_args,
229        print_out,
230        print_err,
231        ignore_err,
232        fork,
233        quiet,
234        test_mode,
235    )
236
237
238def device_write(cmd_buf, print_out=0, quiet=None, test_mode=None):
239    r"""
240    Write the given command in a device SSH session and return the stdout, stderr and the return code.
241
242    This function is useful for writing to a switch.
243
244    This function will obtain the global values for DEVICE_HOST, DEVICE_USERNAME, etc.
245
246    Description of arguments:
247    cmd_buf                         The command string to be run in an SSH session.
248    print_out                       If this is set, this function will print the stdout/stderr generated by
249                                    the shell command.
250    print_err                       If show_err is set, this function will print a standardized error report
251                                    if the shell command returns non-zero.
252    ignore_err                      Indicates that errors encountered on the sshlib.execute_command are to be
253                                    ignored.
254    fork                            Indicates that sshlib.start is to be used rather than
255                                    sshlib.execute_command.
256    quiet                           Indicates whether this function should run the pissuing() function prints
257                                    an "Issuing: <cmd string>" to stdout.  This defaults to the global quiet
258                                    value.
259    test_mode                       If test_mode is set, this function will not actually run the command.
260                                    This defaults to the global test_mode value.
261    """
262
263    # Get global DEVICE variable values.
264    device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="")
265    device_username = BuiltIn().get_variable_value(
266        "${DEVICE_USERNAME}", default=""
267    )
268    device_password = BuiltIn().get_variable_value(
269        "${DEVICE_PASSWORD}", default=""
270    )
271    device_port = BuiltIn().get_variable_value("${DEVICE_PORT}", default="22")
272
273    if not gv.valid_value(device_host):
274        return "", "", 1
275    if not gv.valid_value(device_username):
276        return "", "", 1
277    if not gv.valid_value(device_password):
278        return "", "", 1
279    if not gv.valid_value(device_port):
280        return "", "", 1
281
282    open_connection_args = {
283        "host": device_host,
284        "alias": "device_connection",
285        "port": device_port,
286    }
287    login_args = {"username": device_username, "password": device_password}
288
289    return grs.execute_ssh_command(
290        cmd_buf,
291        open_connection_args,
292        login_args,
293        print_out,
294        print_err=0,
295        ignore_err=1,
296        fork=0,
297        quiet=quiet,
298        test_mode=test_mode,
299    )
300