1#!/usr/bin/env python
2
3r"""
4This module provides many valuable bmc ssh functions such as
5bmc_execute_command.
6"""
7
8import sys
9import exceptions
10import re
11import gen_print as gp
12import gen_valid as gv
13import gen_robot_ssh as grs
14from robot.libraries.BuiltIn import BuiltIn
15
16
17def bmc_execute_command(cmd_buf,
18                        print_out=0,
19                        print_err=0,
20                        ignore_err=0,
21                        fork=0,
22                        quiet=None,
23                        test_mode=None):
24
25    r"""
26    Run the given command in an BMC SSH session and return the stdout, stderr
27    and the return code.
28
29    This function will obtain the global values for OPENBMC_HOST,
30    OPENBMC_USERNAME, etc.
31
32    Description of arguments:
33    cmd_buf                         The command string to be run in an SSH
34                                    session.
35    print_out                       If this is set, this function will print
36                                    the stdout/stderr generated by the shell
37                                    command.
38    print_err                       If show_err is set, this function will
39                                    print a standardized error report if the
40                                    shell command returns non-zero.
41    ignore_err                      Indicates that errors encountered on the
42                                    sshlib.execute_command are to be ignored.
43    fork                            Indicates that sshlib.start is to be used
44                                    rather than sshlib.execute_command.
45    quiet                           Indicates whether this function should run
46                                    the pissuing() function prints an
47                                    "Issuing: <cmd string>" to stdout.  This
48                                    defaults to the global quiet value.
49    test_mode                       If test_mode is set, this function will
50                                    not actually run the command.  This
51                                    defaults to the global test_mode value.
52    """
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,
74                                   quiet, test_mode)
75
76
77def os_execute_command(cmd_buf,
78                       print_out=0,
79                       print_err=0,
80                       ignore_err=0,
81                       fork=0,
82                       quiet=None,
83                       test_mode=None):
84
85    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.
111    """
112
113    # Get global OS variable values.
114    os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
115    os_username = BuiltIn().get_variable_value("${OS_USERNAME}",
116                                               default="")
117    os_password = BuiltIn().get_variable_value("${OS_PASSWORD}",
118                                               default="")
119
120    if not gv.valid_value(os_host):
121        return "", "", 1
122    if not gv.valid_value(os_username):
123        return "", "", 1
124    if not gv.valid_value(os_password):
125        return "", "", 1
126
127    open_connection_args = {'host': os_host, 'alias': 'os_connection'}
128    login_args = {'username': os_username, 'password': os_password}
129
130    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
131                                   print_out, print_err, ignore_err, fork,
132                                   quiet, test_mode)
133
134
135def xcat_execute_command(cmd_buf,
136                         print_out=0,
137                         print_err=0,
138                         ignore_err=0,
139                         fork=0,
140                         quiet=None,
141                         test_mode=None):
142
143    r"""
144    Run the given command in an XCAT SSH session and return the stdout, stderr
145    and the return code.
146
147    This function will obtain the global values for XCAT_HOST, XCAT_USERNAME,
148    etc.
149
150    Description of arguments:
151    cmd_buf                         The command string to be run in an SSH
152                                    session.
153    print_out                       If this is set, this function will print
154                                    the stdout/stderr generated by the shell
155                                    command.
156    print_err                       If show_err is set, this function will
157                                    print a standardized error report if the
158                                    shell command returns non-zero.
159    ignore_err                      Indicates that errors encountered on the
160                                    sshlib.execute_command are to be ignored.
161    fork                            Indicates that sshlib.start is to be used
162                                    rather than sshlib.execute_command.
163    quiet                           Indicates whether this function should run
164                                    the pissuing() function prints an
165                                    "Issuing: <cmd string>" to stdout.  This
166                                    defaults to the global quiet value.
167    test_mode                       If test_mode is set, this function will
168                                    not actually run the command.  This
169                                    defaults to the global test_mode value.
170    """
171
172    # Get global XCAT variable values.
173    xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
174    xcat_username = BuiltIn().get_variable_value("${XCAT_USERNAME}",
175                                                 default="")
176    xcat_password = BuiltIn().get_variable_value("${XCAT_PASSWORD}",
177                                                 default="")
178    xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}",
179                                             default="22")
180
181    if not gv.valid_value(xcat_host):
182        return "", "", 1
183    if not gv.valid_value(xcat_username):
184        return "", "", 1
185    if not gv.valid_value(xcat_password):
186        return "", "", 1
187    if not gv.valid_value(xcat_port):
188        return "", "", 1
189
190    open_connection_args = {'host': xcat_host, 'alias': 'xcat_connection',
191                            'port': xcat_port}
192    login_args = {'username': xcat_username, 'password': xcat_password}
193
194    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
195                                   print_out, print_err, ignore_err, fork,
196                                   quiet, test_mode)
197