blob: eb6aacd14001bf0bd22bd72133f536c2b4e26715 [file] [log] [blame]
Michael Walshb973f9b2018-09-19 16:00:06 -05001#!/usr/bin/env python
2
3r"""
4This module provides many valuable openbmctool.py functions such as
5openbmctool_execute_command.
6"""
7
8import gen_cmd as gc
9import gen_valid as gv
10from robot.libraries.BuiltIn import BuiltIn
11import re
12
13
14def openbmctool_execute_command(command_string,
15 *args,
16 **kwargs):
17 r"""
18 Run the command string as an argument to the openbmctool.py program and
19 return the stdout and the return code.
20
21 This function provides several benefits versus calling shell_cmd directly:
22 - This function will obtain the global values for OPENBMC_HOST,
23 OPENBMC_USERNAME, etc.
24 - This function will compose the openbmctool.py command string which
25 includes the caller's command_string.
26 - The openbmctool.py produces additional text that clutters the output.
27 This function will remove such text. Example:
28 Attempting login...
29 <actual output>
30 User root has been logged out
31
32 NOTE: If you have pipe symbols in your command_string, they must be
33 surrounded by a single space on each side (see example below).
34
35 Example code:
36 ${rc} ${output}= Openbmctool Execute Command fru status | head -n 2
37
38 Example output:
39 #(CDT) 2018/09/19 15:16:58 - Issuing: set -o pipefail ; openbmctool.py -H hostname -U root -P ********
40 ... fru status | tail -n +1 | egrep -v 'Attempting login|User [^ ]+ hasbeen logged out' | head -n 2
41 Component | Is a FRU | Present | Functional | Has Logs
42 cpu0 | Yes | Yes | Yes | No
43
44 Description of arguments:
45 command_string The command string to be passed to the
46 openbmctool.py program.
Michael Walsh58b11ac2018-09-20 15:24:37 -050047 All remaining arguments are passed directly to shell_cmd. See the
48 shell_cmd prolog for details on allowable arguments. The caller may code
49 them directly as in this example:
50 openbmctool_execute_command("my command", quiet=1, max_attempts=2).
51 Python will do the work of putting these values into args/kwargs.
Michael Walshb973f9b2018-09-19 16:00:06 -050052 """
53
54 if not gv.valid_value(command_string):
55 return "", "", 1
56
57 # Get global BMC variable values.
58 openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}", default="")
59 openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}",
60 default="")
61 openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}",
62 default="")
63 if not gv.valid_value(openbmc_host):
64 return "", "", 1
65 if not gv.valid_value(openbmc_username):
66 return "", "", 1
67 if not gv.valid_value(openbmc_password):
68 return "", "", 1
69
70 # Break the caller's command up into separate piped commands. For
71 # example, the user may have specified "fru status | head -n 2" which
72 # would be broken into 2 list elements.
73 pipeline = map(str.strip, re.split(r' \| ', str(command_string)))
74 # The "tail" command below prevents a "egrep: write error: Broken pipe"
75 # error if the user is piping the output to a sub-process.
76 # Use "egrep -v" to get rid of editorial output from openbmctool.py.
77 pipeline.insert(1, "tail -n +1 | egrep -v 'Attempting login|User [^ ]+ has"
78 " been logged out'")
79
80 command_string = "set -o pipefail ; openbmctool.py -H " + openbmc_host\
81 + " -U " + openbmc_username + " -P " + openbmc_password + " "\
82 + " | ".join(pipeline)
83
84 return gc.shell_cmd(command_string, *args, **kwargs)