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