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