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    r"""
79    Run the given command in an OS SSH session and return the stdout, stderr and the return code.
80
81    This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
82
83    Description of arguments:
84    cmd_buf                         The command string to be run in an SSH session.
85    print_out                       If this is set, this function will print the stdout/stderr generated by
86                                    the shell command.
87    print_err                       If show_err is set, this function will print a standardized error report
88                                    if the shell command returns non-zero.
89    ignore_err                      Indicates that errors encountered on the sshlib.execute_command are to be
90                                    ignored.
91    fork                            Indicates that sshlib.start is to be used rather than
92                                    sshlib.execute_command.
93    quiet                           Indicates whether this function should run the pissuing() function prints
94                                    an "Issuing: <cmd string>" to stdout.  This defaults to the global quiet
95                                    value.
96    test_mode                       If test_mode is set, this function will not actually run the command.
97                                    This defaults to the global test_mode value.
98    time_out                        The amount of time to allow for the execution of cmd_buf.  A value of
99                                    None means that there is no limit to how long the command may take.
100    """
101
102    # Get global OS variable values.
103    os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
104    os_username = BuiltIn().get_variable_value("${OS_USERNAME}",
105                                               default="")
106    os_password = BuiltIn().get_variable_value("${OS_PASSWORD}",
107                                               default="")
108
109    if not gv.valid_value(os_host):
110        return "", "", 1
111    if not gv.valid_value(os_username):
112        return "", "", 1
113    if not gv.valid_value(os_password):
114        return "", "", 1
115
116    open_connection_args = {'host': os_host, 'alias': 'os_connection'}
117    login_args = {'username': os_username, 'password': os_password}
118
119    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
120                                   print_out, print_err, ignore_err, fork,
121                                   quiet, test_mode, time_out)
122
123
124def xcat_execute_command(cmd_buf,
125                         print_out=0,
126                         print_err=0,
127                         ignore_err=0,
128                         fork=0,
129                         quiet=None,
130                         test_mode=None):
131    r"""
132    Run the given command in an XCAT SSH session and return the stdout, stderr and the return code.
133
134    This function will obtain the global values for XCAT_HOST, XCAT_USERNAME, etc.
135
136    Description of arguments:
137    cmd_buf                         The command string to be run in an SSH session.
138    print_out                       If this is set, this function will print the stdout/stderr generated by
139                                    the shell command.
140    print_err                       If show_err is set, this function will print a standardized error report
141                                    if the shell command returns non-zero.
142    ignore_err                      Indicates that errors encountered on the sshlib.execute_command are to be
143                                    ignored.
144    fork                            Indicates that sshlib.start is to be used rather than
145                                    sshlib.execute_command.
146    quiet                           Indicates whether this function should run the pissuing() function prints
147                                    an "Issuing: <cmd string>" to stdout.  This defaults to the global quiet
148                                    value.
149    test_mode                       If test_mode is set, this function will not actually run the command.
150                                    This defaults to the global test_mode value.
151    """
152
153    # Get global XCAT variable values.
154    xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
155    xcat_username = BuiltIn().get_variable_value("${XCAT_USERNAME}",
156                                                 default="")
157    xcat_password = BuiltIn().get_variable_value("${XCAT_PASSWORD}",
158                                                 default="")
159    xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}",
160                                             default="22")
161
162    if not gv.valid_value(xcat_host):
163        return "", "", 1
164    if not gv.valid_value(xcat_username):
165        return "", "", 1
166    if not gv.valid_value(xcat_password):
167        return "", "", 1
168    if not gv.valid_value(xcat_port):
169        return "", "", 1
170
171    open_connection_args = {'host': xcat_host, 'alias': 'xcat_connection',
172                            'port': xcat_port}
173    login_args = {'username': xcat_username, 'password': xcat_password}
174
175    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
176                                   print_out, print_err, ignore_err, fork,
177                                   quiet, test_mode)
178
179
180def device_write(cmd_buf,
181                 print_out=0,
182                 quiet=None,
183                 test_mode=None):
184    r"""
185    Write the given command in a device SSH session and return the stdout, stderr and the return code.
186
187    This function is useful for writing to a switch.
188
189    This function will obtain the global values for DEVICE_HOST, DEVICE_USERNAME, etc.
190
191    Description of arguments:
192    cmd_buf                         The command string to be run in an SSH session.
193    print_out                       If this is set, this function will print the stdout/stderr generated by
194                                    the shell command.
195    print_err                       If show_err is set, this function will print a standardized error report
196                                    if the shell command returns non-zero.
197    ignore_err                      Indicates that errors encountered on the sshlib.execute_command are to be
198                                    ignored.
199    fork                            Indicates that sshlib.start is to be used rather than
200                                    sshlib.execute_command.
201    quiet                           Indicates whether this function should run the pissuing() function prints
202                                    an "Issuing: <cmd string>" to stdout.  This defaults to the global quiet
203                                    value.
204    test_mode                       If test_mode is set, this function will not actually run the command.
205                                    This defaults to the global test_mode value.
206    """
207
208    # Get global DEVICE variable values.
209    device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="")
210    device_username = BuiltIn().get_variable_value("${DEVICE_USERNAME}",
211                                                   default="")
212    device_password = BuiltIn().get_variable_value("${DEVICE_PASSWORD}",
213                                                   default="")
214    device_port = BuiltIn().get_variable_value("${DEVICE_PORT}",
215                                               default="22")
216
217    if not gv.valid_value(device_host):
218        return "", "", 1
219    if not gv.valid_value(device_username):
220        return "", "", 1
221    if not gv.valid_value(device_password):
222        return "", "", 1
223    if not gv.valid_value(device_port):
224        return "", "", 1
225
226    open_connection_args = {'host': device_host, 'alias': 'device_connection',
227                            'port': device_port}
228    login_args = {'username': device_username, 'password': device_password}
229
230    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
231                                   print_out, print_err=0, ignore_err=1,
232                                   fork=0, quiet=quiet, test_mode=test_mode)
233