1#!/usr/bin/env python
2
3r"""
4This module is the python counterpart to openbmc_ffdc.robot..
5"""
6
7import os
8
9import gen_print as gp
10import gen_robot_print as grp
11import gen_valid as gv
12import gen_robot_keyword as grk
13import state as st
14
15from robot.libraries.BuiltIn import BuiltIn
16
17
18def ffdc(ffdc_dir_path=None,
19         ffdc_prefix=None,
20         ffdc_function_list=""):
21    r"""
22    Gather First Failure Data Capture (FFDC).
23
24    This includes:
25    - Set global FFDC_TIME.
26    - Create FFDC work space directory.
27    - Write test info details.
28    - Call BMC methods to write/collect FFDC data.
29
30    Description of arguments:
31    ffdc_dir_path       The dir path where FFDC data should be put.
32    ffdc_prefix         The prefix to be given to each FFDC file name
33                        generated.
34    ffdc_function_list  A colon-delimited list of all the types of FFDC data
35                        you wish to have collected.  A blank value means that
36                        all possible kinds of FFDC are to be collected.  See
37                        FFDC_METHOD_CALL object in lib/openbmc_ffdc_list.py
38                        for possible choices.
39    """
40
41    ffdc_file_list = []
42
43    # Check if Ping and SSH connection is alive
44    OPENBMC_HOST = BuiltIn().get_variable_value("${OPENBMC_HOST}")
45
46    state = st.get_state(req_states=['ping', 'uptime'])
47    gp.qprint_var(state)
48    if not int(state['ping']):
49        gp.print_error("BMC is not ping-able.  Terminating FFDC collection.\n")
50        return ffdc_file_list
51
52    if state['uptime'] == "":
53        gp.print_error("BMC is not communicating.  Terminating FFDC" +
54                       " collection.\n")
55        return ffdc_file_list
56
57    gp.qprint_timen("Collecting FFDC.")
58
59    # Get default values for arguments.
60    ffdc_dir_path, ffdc_prefix = set_ffdc_defaults(ffdc_dir_path, ffdc_prefix)
61    gp.qprint_var(ffdc_dir_path)
62    gp.qprint_var(ffdc_prefix)
63
64    # LOG_PREFIX is used by subordinate functions.
65    LOG_PREFIX = ffdc_dir_path + ffdc_prefix
66    BuiltIn().set_global_variable("${LOG_PREFIX}", LOG_PREFIX)
67
68    cmd_buf = ["Create Directory", ffdc_dir_path]
69    grp.rqpissuing_keyword(cmd_buf)
70    status, output = BuiltIn().run_keyword_and_ignore_error(*cmd_buf)
71    if status != "PASS":
72        error_message = grp.sprint_error_report("Create Directory failed" +
73                                                " with the following" +
74                                                " error:\n" + output)
75        BuiltIn().fail(error_message)
76
77    # FFDC_FILE_PATH is used by Header Message.
78    FFDC_FILE_PATH = ffdc_dir_path + ffdc_prefix + "BMC_general.txt"
79    BuiltIn().set_global_variable("${FFDC_FILE_PATH}", FFDC_FILE_PATH)
80
81    status, ffdc_file_list = grk.run_key("Header Message")
82    status, ffdc_file_sub_list = \
83        grk.run_key_u("Call FFDC Methods  ffdc_function_list=" +
84                      ffdc_function_list)
85
86    # Combine lists, remove duplicates and sort.
87    ffdc_file_list = list(set(ffdc_file_list + ffdc_file_sub_list))
88    ffdc_file_list.sort()
89
90    gp.qprint_timen("Finished collecting FFDC.")
91
92    return ffdc_file_list
93
94
95def set_ffdc_defaults(ffdc_dir_path=None,
96                      ffdc_prefix=None):
97    r"""
98    Set a default value for ffdc_dir_path and ffdc_prefix if they don't
99    already have values.  Return both values.
100
101    Description of arguments:
102    ffdc_dir_path  The dir path where FFDC data should be put.
103    ffdc_prefix    The prefix to be given to each FFDC file name generated.
104
105    NOTE: If global variable ffdc_dir_path_style is set to ${1}, this function
106    will create default values in a newer way.  Otherwise, its behavior
107    will remain unchanged.
108    """
109
110    # Note: Several subordinate functions like 'Get Test Dir and Name' and
111    # 'Header Message' expect global variable FFDC_TIME to be set.
112    cmd_buf = ["Get Current Time Stamp"]
113    grp.rdpissuing_keyword(cmd_buf)
114    FFDC_TIME = BuiltIn().run_keyword(*cmd_buf)
115    BuiltIn().set_global_variable("${FFDC_TIME}", FFDC_TIME)
116
117    ffdc_dir_path_style = BuiltIn().get_variable_value(
118        "${ffdc_dir_path_style}")
119
120    if ffdc_dir_path is None:
121        if ffdc_dir_path_style:
122            try:
123                ffdc_dir_path = os.environ['FFDC_DIR_PATH']
124            except KeyError:
125                ffdc_dir_path = os.path.dirname(
126                    BuiltIn().get_variable_value("${LOG_FILE}")) + "/"
127        else:
128            FFDC_LOG_PATH = os.getcwd() + "/logs/"
129            if FFDC_LOG_PATH is None:
130                FFDC_LOG_PATH = ""
131            if FFDC_LOG_PATH == "":
132                FFDC_LOG_PATH = os.path.dirname(
133                    BuiltIn().get_variable_value("${LOG_FILE}")) + "/"
134            error_message = gv.svalid_value(FFDC_LOG_PATH,
135                                            var_name="FFDC_LOG_PATH")
136            if error_message != "":
137                error_message = grp.sprint_error_report(error_message)
138                BuiltIn().fail(error_message)
139            FFDC_LOG_PATH = os.path.normpath(FFDC_LOG_PATH) + os.sep
140
141            cmd_buf = ["Get Test Dir and Name"]
142            grp.rpissuing_keyword(cmd_buf)
143            suitename, testname = BuiltIn().run_keyword(*cmd_buf)
144
145            ffdc_dir_path = FFDC_LOG_PATH + suitename + "/" + testname + "/"
146
147    # Add trailing slash.
148    ffdc_dir_path = os.path.normpath(ffdc_dir_path) + os.sep
149
150    if ffdc_prefix is None:
151        FFDC_TIME = BuiltIn().get_variable_value("${FFDC_TIME}")
152        if ffdc_prefix is None:
153            if ffdc_dir_path_style:
154                OPENBMC_HOST = BuiltIn().get_variable_value("${OPENBMC_HOST}")
155                OPENBMC_NICKNAME = BuiltIn().get_variable_value(
156                    "${OPENBMC_NICKNAME}", default=OPENBMC_HOST)
157                ffdc_prefix = OPENBMC_NICKNAME + "." + FFDC_TIME[2:8] + "." +\
158                    FFDC_TIME[8:14] + "."
159            else:
160                ffdc_prefix = FFDC_TIME + "_"
161
162    BuiltIn().set_global_variable("${FFDC_DIR_PATH}", ffdc_dir_path)
163    BuiltIn().set_global_variable("${FFDC_PREFIX}", ffdc_prefix)
164
165    return ffdc_dir_path, ffdc_prefix
166