1#!/usr/bin/env python3
2
3r"""
4This file contains functions which are useful for processing BMC dumps.
5"""
6
7import imp
8import os
9import sys
10
11import bmc_ssh_utils as bsu
12import gen_misc as gm
13import gen_print as gp
14import gen_robot_keyword as grk
15from robot.libraries.BuiltIn import BuiltIn
16
17base_path = (
18    os.path.dirname(os.path.dirname(imp.find_module("gen_robot_print")[1]))
19    + os.sep
20)
21sys.path.append(base_path + "data/")
22import variables as var  # NOQA
23
24
25def get_dump_dict(quiet=None):
26    r"""
27    Get dump information and return as an ordered dictionary where the keys
28    are the dump IDs and the values are the full path names of the dumps.
29
30    Example robot program call:
31
32    ${dump_dict}=  Get Dump Dict
33    Rprint Vars  dump_dict
34
35    Example output:
36
37    dump_dict:
38      [1]:
39      /var/lib/phosphor-debug-collector/dumps/1/obmcdump_1_1508255216.tar.xz
40      [2]:
41      /var/lib/phosphor-debug-collector/dumps/2/obmcdump_2_1508255245.tar.xz
42      [3]:
43      /var/lib/phosphor-debug-collector/dumps/3/obmcdump_3_1508255267.tar.xz
44      [4]:
45      /var/lib/phosphor-debug-collector/dumps/4/obmcdump_4_1508255283.tar.xz
46
47    Description of argument(s):
48    quiet                           If quiet is set to 1, this function will
49                                    NOT write status messages to stdout.
50    """
51
52    quiet = int(gp.get_var_value(quiet, 1))
53    cmd_buf = "find /var/lib/phosphor-debug-collector/ -maxdepth 4 -type f"
54    output, stderr, rc = bsu.bmc_execute_command(cmd_buf, quiet=quiet)
55
56    BuiltIn().log_to_console(output)
57    return output.split("\n")
58
59
60def valid_dump(dump_id, dump_dict=None, quiet=None):
61    r"""
62    Verify that dump_id is a valid.  If it is not valid, issue robot failure
63    message.
64
65    A dump is valid if the indicated dump_id refers to an existing dump with a
66    valid associated dump file.
67
68    Description of argument(s):
69    dump_id                         A dump ID (e.g. "1", "2", etc.)
70    dump_dict                       A dump dictionary such as the one returned
71                                    by get_dump_dict.  If this value is None,
72                                    this function will call get_dump_dict on
73                                    the caller's behalf.
74    quiet                           If quiet is set to 1, this function will
75                                    NOT write status messages to stdout.
76    """
77
78    if dump_dict is None:
79        dump_dict = get_dump_dict(quiet=quiet)
80
81    if dump_id not in dump_dict:
82        message = (
83            "The specified dump ID was not found among the existing"
84            + " dumps:\n"
85        )
86        message += gp.sprint_var(dump_id)
87        message += gp.sprint_var(dump_dict)
88        BuiltIn().fail(gp.sprint_error(message))
89
90    if not dump_dict[dump_id].endswith("tar.xz"):
91        message = (
92            'There is no "tar.xz" file associated with the given'
93            + " dump_id:\n"
94        )
95        message += gp.sprint_var(dump_id)
96        dump_file_path = dump_dict[dump_id]
97        message += gp.sprint_var(dump_file_path)
98        BuiltIn().fail(gp.sprint_error(message))
99
100
101def scp_dumps(targ_dir_path, targ_file_prefix="", dump_dict=None, quiet=None):
102    r"""
103    SCP all dumps from the BMC to the indicated directory on the local system
104    and return a list of the new files.
105
106    Description of argument(s):
107    targ_dir_path                   The path of the directory to receive the
108                                    dump files.
109    targ_file_prefix                Prefix which will be prepended to each
110                                    target file's name.
111    dump_dict                       A dump dictionary such as the one returned
112                                    by get_dump_dict.  If this value is None,
113                                    this function will call get_dump_dict on
114                                    the caller's behalf.
115    quiet                           If quiet is set to 1, this function will
116                                    NOT write status messages to stdout.
117    """
118
119    targ_dir_path = gm.add_trailing_slash(targ_dir_path)
120
121    if dump_dict is None:
122        dump_list = get_dump_dict(quiet=quiet)
123
124    status, ret_values = grk.run_key("Open Connection for SCP", quiet=quiet)
125
126    dump_file_list = []
127    for file_path in dump_list:
128        targ_file_path = (
129            targ_dir_path + targ_file_prefix + os.path.basename(file_path)
130        )
131        status, ret_values = grk.run_key(
132            "scp.Get File  " + file_path + "  " + targ_file_path, quiet=quiet
133        )
134        dump_file_list.append(targ_file_path)
135
136    return dump_file_list
137