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