blob: a34436b4fb254d87c9a2af441f7c5eac267007a1 [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.
47 args/kwargs These are passed directly to shell_cmd. See the shell_cmd
48 prolog for details.
49 """
50
51 if not gv.valid_value(command_string):
52 return "", "", 1
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 if not gv.valid_value(openbmc_host):
61 return "", "", 1
62 if not gv.valid_value(openbmc_username):
63 return "", "", 1
64 if not gv.valid_value(openbmc_password):
65 return "", "", 1
66
67 # Break the caller's command up into separate piped commands. For
68 # example, the user may have specified "fru status | head -n 2" which
69 # would be broken into 2 list elements.
70 pipeline = map(str.strip, re.split(r' \| ', str(command_string)))
71 # The "tail" command below prevents a "egrep: write error: Broken pipe"
72 # error if the user is piping the output to a sub-process.
73 # Use "egrep -v" to get rid of editorial output from openbmctool.py.
74 pipeline.insert(1, "tail -n +1 | egrep -v 'Attempting login|User [^ ]+ has"
75 " been logged out'")
76
77 command_string = "set -o pipefail ; openbmctool.py -H " + openbmc_host\
78 + " -U " + openbmc_username + " -P " + openbmc_password + " "\
79 + " | ".join(pipeline)
80
81 return gc.shell_cmd(command_string, *args, **kwargs)