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
17###############################################################################
18def bmc_execute_command(cmd_buf,
19                        print_out=0,
20                        print_err=0,
21                        ignore_err=0,
22                        fork=0,
23                        quiet=None,
24                        test_mode=None):
25
26    r"""
27    Run the given command in an BMC SSH session and return the stdout, stderr
28    and the return code.
29
30    This function will obtain the global values for OPENBMC_HOST,
31    OPENBMC_USERNAME, etc.
32
33    Description of arguments:
34    cmd_buf                         The command string to be run in an SSH
35                                    session.
36    print_out                       If this is set, this function will print
37                                    the stdout/stderr generated by the shell
38                                    command.
39    print_err                       If show_err is set, this function will
40                                    print a standardized error report if the
41                                    shell command returns non-zero.
42    ignore_err                      Indicates that errors encountered on the
43                                    sshlib.execute_command are to be ignored.
44    fork                            Indicates that sshlib.start is to be used
45                                    rather than sshlib.execute_command.
46    quiet                           Indicates whether this function should run
47                                    the pissuing() function prints an
48                                    "Issuing: <cmd string>" to stdout.  This
49                                    defaults to the global quiet value.
50    test_mode                       If test_mode is set, this function will
51                                    not actually run the command.  This
52                                    defaults to the global test_mode value.
53    """
54
55    # Get global BMC variable values.
56    openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}", default="")
57    openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}",
58                                                    default="")
59    openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}",
60                                                    default="")
61
62    if not gv.valid_value(openbmc_host):
63        return "", "", 1
64    if not gv.valid_value(openbmc_username):
65        return "", "", 1
66    if not gv.valid_value(openbmc_password):
67        return "", "", 1
68
69    open_connection_args = {'host': openbmc_host, 'alias': 'bmc_connection',
70                            'timeout': '25.0', 'prompt': '# '}
71    login_args = {'username': openbmc_username, 'password': openbmc_password}
72
73    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
74                                   print_out, print_err, ignore_err, fork,
75                                   quiet, test_mode)
76
77###############################################################################
78
79
80###############################################################################
81def os_execute_command(cmd_buf,
82                       print_out=0,
83                       print_err=0,
84                       ignore_err=0,
85                       fork=0,
86                       quiet=None,
87                       test_mode=None):
88
89    r"""
90    Run the given command in an OS SSH session and return the stdout, stderr
91    and the return code.
92
93    This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
94
95    Description of arguments:
96    cmd_buf                         The command string to be run in an SSH
97                                    session.
98    print_out                       If this is set, this function will print
99                                    the stdout/stderr generated by the shell
100                                    command.
101    print_err                       If show_err is set, this function will
102                                    print a standardized error report if the
103                                    shell command returns non-zero.
104    ignore_err                      Indicates that errors encountered on the
105                                    sshlib.execute_command are to be ignored.
106    fork                            Indicates that sshlib.start is to be used
107                                    rather than sshlib.execute_command.
108    quiet                           Indicates whether this function should run
109                                    the pissuing() function prints an
110                                    "Issuing: <cmd string>" to stdout.  This
111                                    defaults to the global quiet value.
112    test_mode                       If test_mode is set, this function will
113                                    not actually run the command.  This
114                                    defaults to the global test_mode value.
115    """
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,
136                                   quiet, test_mode)
137
138###############################################################################
139
140
141###############################################################################
142def xcat_execute_command(cmd_buf,
143                         print_out=0,
144                         print_err=0,
145                         ignore_err=0,
146                         fork=0,
147                         quiet=None,
148                         test_mode=None):
149
150    r"""
151    Run the given command in an XCAT SSH session and return the stdout, stderr
152    and the return code.
153
154    This function will obtain the global values for XCAT_HOST, XCAT_USERNAME,
155    etc.
156
157    Description of arguments:
158    cmd_buf                         The command string to be run in an SSH
159                                    session.
160    print_out                       If this is set, this function will print
161                                    the stdout/stderr generated by the shell
162                                    command.
163    print_err                       If show_err is set, this function will
164                                    print a standardized error report if the
165                                    shell command returns non-zero.
166    ignore_err                      Indicates that errors encountered on the
167                                    sshlib.execute_command are to be ignored.
168    fork                            Indicates that sshlib.start is to be used
169                                    rather than sshlib.execute_command.
170    quiet                           Indicates whether this function should run
171                                    the pissuing() function prints an
172                                    "Issuing: <cmd string>" to stdout.  This
173                                    defaults to the global quiet value.
174    test_mode                       If test_mode is set, this function will
175                                    not actually run the command.  This
176                                    defaults to the global test_mode value.
177    """
178
179    # Get global XCAT variable values.
180    xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
181    xcat_username = BuiltIn().get_variable_value("${XCAT_USERNAME}",
182                                                 default="")
183    xcat_password = BuiltIn().get_variable_value("${XCAT_PASSWORD}",
184                                                 default="")
185    xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}",
186                                             default="22")
187
188    if not gv.valid_value(xcat_host):
189        return "", "", 1
190    if not gv.valid_value(xcat_username):
191        return "", "", 1
192    if not gv.valid_value(xcat_password):
193        return "", "", 1
194    if not gv.valid_value(xcat_port):
195        return "", "", 1
196
197    open_connection_args = {'host': xcat_host, 'alias': 'xcat_connection',
198                            'port': xcat_port}
199    login_args = {'username': xcat_username, 'password': xcat_password}
200
201    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
202                                   print_out, print_err, ignore_err, fork,
203                                   quiet, test_mode)
204
205###############################################################################
206