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