blob: ee70850c40cca3db1524a745a6c82ee3445ce4d1 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walshe11a1362017-10-19 15:35:26 -05002
3r"""
4This file contains functions which are useful for processing BMC dumps.
5"""
6
Patrick Williams20f38712022-12-08 06:18:26 -06007import imp
Michael Walsh4f3ce172017-10-19 17:28:49 -05008import os
Michael Walshe11a1362017-10-19 15:35:26 -05009import sys
Patrick Williams20f38712022-12-08 06:18:26 -060010
11import bmc_ssh_utils as bsu
12import gen_misc as gm
13import gen_print as gp
14import gen_robot_keyword as grk
15import var_funcs as vf
16from robot.libraries.BuiltIn import BuiltIn
17
18base_path = (
19 os.path.dirname(os.path.dirname(imp.find_module("gen_robot_print")[1]))
20 + os.sep
21)
Michael Walshe11a1362017-10-19 15:35:26 -050022sys.path.append(base_path + "data/")
Patrick Williams20f38712022-12-08 06:18:26 -060023import variables as var # NOQA
Michael Walshe11a1362017-10-19 15:35:26 -050024
25
26def get_dump_dict(quiet=None):
Michael Walshe11a1362017-10-19 15:35:26 -050027 r"""
28 Get dump information and return as an ordered dictionary where the keys
29 are the dump IDs and the values are the full path names of the dumps.
30
31 Example robot program call:
32
33 ${dump_dict}= Get Dump Dict
Michael Walsh39c00512019-07-17 10:54:06 -050034 Rprint Vars dump_dict
Michael Walshe11a1362017-10-19 15:35:26 -050035
36 Example output:
37
38 dump_dict:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050039 [1]:
40 /var/lib/phosphor-debug-collector/dumps/1/obmcdump_1_1508255216.tar.xz
41 [2]:
42 /var/lib/phosphor-debug-collector/dumps/2/obmcdump_2_1508255245.tar.xz
43 [3]:
44 /var/lib/phosphor-debug-collector/dumps/3/obmcdump_3_1508255267.tar.xz
45 [4]:
46 /var/lib/phosphor-debug-collector/dumps/4/obmcdump_4_1508255283.tar.xz
Michael Walshe11a1362017-10-19 15:35:26 -050047
48 Description of argument(s):
49 quiet If quiet is set to 1, this function will
50 NOT write status messages to stdout.
51 """
52
53 quiet = int(gp.get_var_value(quiet, 1))
George Keishingbd8ec922022-03-31 13:39:35 -050054 cmd_buf = "find /var/lib/phosphor-debug-collector/ -maxdepth 4 -type f"
Michael Walshe11a1362017-10-19 15:35:26 -050055 output, stderr, rc = bsu.bmc_execute_command(cmd_buf, quiet=quiet)
56
George Keishingbd8ec922022-03-31 13:39:35 -050057 BuiltIn().log_to_console(output)
58 return output.split("\n")
Michael Walshe11a1362017-10-19 15:35:26 -050059
60
Patrick Williams20f38712022-12-08 06:18:26 -060061def valid_dump(dump_id, dump_dict=None, quiet=None):
Michael Walshe11a1362017-10-19 15:35:26 -050062 r"""
63 Verify that dump_id is a valid. If it is not valid, issue robot failure
64 message.
65
66 A dump is valid if the indicated dump_id refers to an existing dump with a
67 valid associated dump file.
68
69 Description of argument(s):
70 dump_id A dump ID (e.g. "1", "2", etc.)
71 dump_dict A dump dictionary such as the one returned
72 by get_dump_dict. If this value is None,
73 this function will call get_dump_dict on
74 the caller's behalf.
75 quiet If quiet is set to 1, this function will
76 NOT write status messages to stdout.
77 """
78
79 if dump_dict is None:
80 dump_dict = get_dump_dict(quiet=quiet)
81
82 if dump_id not in dump_dict:
Patrick Williams20f38712022-12-08 06:18:26 -060083 message = (
84 "The specified dump ID was not found among the existing"
Michael Walshe11a1362017-10-19 15:35:26 -050085 + " dumps:\n"
Patrick Williams20f38712022-12-08 06:18:26 -060086 )
Michael Walshe11a1362017-10-19 15:35:26 -050087 message += gp.sprint_var(dump_id)
88 message += gp.sprint_var(dump_dict)
89 BuiltIn().fail(gp.sprint_error(message))
90
91 if not dump_dict[dump_id].endswith("tar.xz"):
Patrick Williams20f38712022-12-08 06:18:26 -060092 message = (
93 'There is no "tar.xz" file associated with the given'
Michael Walshe11a1362017-10-19 15:35:26 -050094 + " dump_id:\n"
Patrick Williams20f38712022-12-08 06:18:26 -060095 )
Michael Walshe11a1362017-10-19 15:35:26 -050096 message += gp.sprint_var(dump_id)
97 dump_file_path = dump_dict[dump_id]
98 message += gp.sprint_var(dump_file_path)
99 BuiltIn().fail(gp.sprint_error(message))
Michael Walsh4f3ce172017-10-19 17:28:49 -0500100
101
Patrick Williams20f38712022-12-08 06:18:26 -0600102def scp_dumps(targ_dir_path, targ_file_prefix="", dump_dict=None, quiet=None):
Michael Walsh4f3ce172017-10-19 17:28:49 -0500103 r"""
104 SCP all dumps from the BMC to the indicated directory on the local system
105 and return a list of the new files.
106
107 Description of argument(s):
108 targ_dir_path The path of the directory to receive the
109 dump files.
110 targ_file_prefix Prefix which will be pre-pended to each
111 target file's name.
112 dump_dict A dump dictionary such as the one returned
113 by get_dump_dict. If this value is None,
114 this function will call get_dump_dict on
115 the caller's behalf.
116 quiet If quiet is set to 1, this function will
117 NOT write status messages to stdout.
118 """
119
120 targ_dir_path = gm.add_trailing_slash(targ_dir_path)
121
122 if dump_dict is None:
George Keishingbd8ec922022-03-31 13:39:35 -0500123 dump_list = get_dump_dict(quiet=quiet)
Michael Walsh4f3ce172017-10-19 17:28:49 -0500124
125 status, ret_values = grk.run_key("Open Connection for SCP", quiet=quiet)
126
127 dump_file_list = []
George Keishingbd8ec922022-03-31 13:39:35 -0500128 for file_path in dump_list:
Patrick Williams20f38712022-12-08 06:18:26 -0600129 targ_file_path = (
130 targ_dir_path + targ_file_prefix + os.path.basename(file_path)
131 )
132 status, ret_values = grk.run_key(
133 "scp.Get File " + file_path + " " + targ_file_path, quiet=quiet
134 )
George Keishing1165a022021-05-19 06:23:13 -0500135 dump_file_list.append(targ_file_path)
136
137 return dump_file_list