George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | See class prolog below for details. |
| 5 | """ |
| 6 | |
Sridevi Ramesh | 47375aa | 2022-12-08 05:05:31 -0600 | [diff] [blame] | 7 | from ssh_utility import SSHRemoteclient |
| 8 | from telnet_utility import TelnetRemoteclient |
| 9 | from errno import EACCES, EPERM |
| 10 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 11 | import os |
George Keishing | 0813e71 | 2021-07-26 08:29:20 -0500 | [diff] [blame] | 12 | import re |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 13 | import sys |
| 14 | import yaml |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 15 | import json |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 16 | import time |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 17 | import logging |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 18 | import platform |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 19 | import subprocess |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 20 | |
Peter D Phan | cb791d7 | 2022-02-08 12:23:03 -0600 | [diff] [blame] | 21 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 22 | sys.path.append(script_dir) |
| 23 | # Walk path and append to sys.path |
| 24 | for root, dirs, files in os.walk(script_dir): |
| 25 | for dir in dirs: |
| 26 | sys.path.append(os.path.join(root, dir)) |
| 27 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 28 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 29 | r""" |
| 30 | User define plugins python functions. |
| 31 | |
| 32 | It will imports files from directory plugins |
| 33 | |
| 34 | plugins |
| 35 | ├── file1.py |
| 36 | └── file2.py |
| 37 | |
| 38 | Example how to define in YAML: |
| 39 | - plugin: |
| 40 | - plugin_name: plugin.foo_func.foo_func_yaml |
| 41 | - plugin_args: |
| 42 | - arg1 |
| 43 | - arg2 |
| 44 | """ |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 45 | plugin_dir = __file__.split(__file__.split("/")[-1])[0] + '/plugins' |
| 46 | sys.path.append(plugin_dir) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 47 | try: |
| 48 | for module in os.listdir(plugin_dir): |
| 49 | if module == '__init__.py' or module[-3:] != '.py': |
| 50 | continue |
| 51 | plugin_module = "plugins." + module[:-3] |
| 52 | # To access the module plugin.<module name>.<function> |
| 53 | # Example: plugin.foo_func.foo_func_yaml() |
| 54 | try: |
| 55 | plugin = __import__(plugin_module, globals(), locals(), [], 0) |
| 56 | except Exception as e: |
| 57 | print("PLUGIN: Module import failed: %s" % module) |
| 58 | pass |
| 59 | except FileNotFoundError as e: |
| 60 | print("PLUGIN: %s" % e) |
| 61 | pass |
| 62 | |
| 63 | r""" |
| 64 | This is for plugin functions returning data or responses to the caller |
| 65 | in YAML plugin setup. |
| 66 | |
| 67 | Example: |
| 68 | |
| 69 | - plugin: |
| 70 | - plugin_name: version = plugin.ssh_execution.ssh_execute_cmd |
| 71 | - plugin_args: |
| 72 | - ${hostname} |
| 73 | - ${username} |
| 74 | - ${password} |
| 75 | - "cat /etc/os-release | grep VERSION_ID | awk -F'=' '{print $2}'" |
| 76 | - plugin: |
| 77 | - plugin_name: plugin.print_vars.print_vars |
| 78 | - plugin_args: |
| 79 | - version |
| 80 | |
| 81 | where first plugin "version" var is used by another plugin in the YAML |
| 82 | block or plugin |
| 83 | |
| 84 | """ |
| 85 | global global_log_store_path |
| 86 | global global_plugin_dict |
| 87 | global global_plugin_list |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 88 | |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 89 | # Hold the plugin return values in dict and plugin return vars in list. |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 90 | # Dict is to reference and update vars processing in parser where as |
| 91 | # list is for current vars from the plugin block which needs processing. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 92 | global_plugin_dict = {} |
| 93 | global_plugin_list = [] |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 94 | |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 95 | # Hold the plugin return named declared if function returned values are list,dict. |
| 96 | # Refer this name list to look up the plugin dict for eval() args function |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 97 | # Example ['version'] |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 98 | global_plugin_type_list = [] |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 99 | |
| 100 | # Path where logs are to be stored or written. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 101 | global_log_store_path = '' |
| 102 | |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 103 | # Plugin error state defaults. |
| 104 | plugin_error_dict = { |
| 105 | 'exit_on_error': False, |
| 106 | 'continue_on_error': False, |
| 107 | } |
| 108 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 109 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 110 | class ffdc_collector: |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 111 | |
| 112 | r""" |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 113 | Execute commands from configuration file to collect log files. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 114 | Fetch and store generated files at the specified location. |
| 115 | |
| 116 | """ |
| 117 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 118 | def __init__(self, |
| 119 | hostname, |
| 120 | username, |
| 121 | password, |
| 122 | ffdc_config, |
| 123 | location, |
| 124 | remote_type, |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 125 | remote_protocol, |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 126 | env_vars, |
George Keishing | 8e94f8c | 2021-07-23 15:06:32 -0500 | [diff] [blame] | 127 | econfig, |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 128 | log_level): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 129 | r""" |
| 130 | Description of argument(s): |
| 131 | |
George Keishing | 8e94f8c | 2021-07-23 15:06:32 -0500 | [diff] [blame] | 132 | hostname name/ip of the targeted (remote) system |
| 133 | username user on the targeted system with access to FFDC files |
| 134 | password password for user on targeted system |
| 135 | ffdc_config configuration file listing commands and files for FFDC |
| 136 | location where to store collected FFDC |
| 137 | remote_type os type of the remote host |
| 138 | remote_protocol Protocol to use to collect data |
| 139 | env_vars User define CLI env vars '{"key : "value"}' |
| 140 | econfig User define env vars YAML file |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 141 | |
| 142 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 143 | |
| 144 | self.hostname = hostname |
| 145 | self.username = username |
| 146 | self.password = password |
| 147 | self.ffdc_config = ffdc_config |
| 148 | self.location = location + "/" + remote_type.upper() |
| 149 | self.ssh_remoteclient = None |
| 150 | self.telnet_remoteclient = None |
| 151 | self.ffdc_dir_path = "" |
| 152 | self.ffdc_prefix = "" |
| 153 | self.target_type = remote_type.upper() |
| 154 | self.remote_protocol = remote_protocol.upper() |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 155 | self.env_vars = env_vars |
| 156 | self.econfig = econfig |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 157 | self.start_time = 0 |
| 158 | self.elapsed_time = '' |
| 159 | self.logger = None |
| 160 | |
| 161 | # Set prefix values for scp files and directory. |
| 162 | # Since the time stamp is at second granularity, these values are set here |
| 163 | # to be sure that all files for this run will have same timestamps |
| 164 | # and they will be saved in the same directory. |
| 165 | # self.location == local system for now |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 166 | self.set_ffdc_default_store_path() |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 167 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 168 | # Logger for this run. Need to be after set_ffdc_default_store_path() |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 169 | self.script_logging(getattr(logging, log_level.upper())) |
| 170 | |
| 171 | # Verify top level directory exists for storage |
| 172 | self.validate_local_store(self.location) |
| 173 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 174 | if self.verify_script_env(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 175 | # Load default or user define YAML configuration file. |
| 176 | with open(self.ffdc_config, 'r') as file: |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 177 | try: |
Yunyun Lin | f87cc0a | 2022-06-08 16:57:04 -0700 | [diff] [blame] | 178 | self.ffdc_actions = yaml.load(file, Loader=yaml.SafeLoader) |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 179 | except yaml.YAMLError as e: |
| 180 | self.logger.error(e) |
| 181 | sys.exit(-1) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 182 | |
| 183 | if self.target_type not in self.ffdc_actions.keys(): |
| 184 | self.logger.error( |
| 185 | "\n\tERROR: %s is not listed in %s.\n\n" % (self.target_type, self.ffdc_config)) |
| 186 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 187 | else: |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 188 | sys.exit(-1) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 189 | |
George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 190 | # Load ENV vars from user. |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 191 | self.logger.info("\n\tENV: User define input YAML variables") |
| 192 | self.env_dict = {} |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 193 | self.load_env() |
George Keishing | aa1f848 | 2021-07-22 00:54:55 -0500 | [diff] [blame] | 194 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 195 | def verify_script_env(self): |
| 196 | |
| 197 | # Import to log version |
| 198 | import click |
| 199 | import paramiko |
| 200 | |
| 201 | run_env_ok = True |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 202 | |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 203 | redfishtool_version = self.run_tool_cmd('redfishtool -V').split(' ')[2].strip('\n') |
| 204 | ipmitool_version = self.run_tool_cmd('ipmitool -V').split(' ')[2] |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 205 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 206 | self.logger.info("\n\t---- Script host environment ----") |
| 207 | self.logger.info("\t{:<10} {:<10}".format('Script hostname', os.uname()[1])) |
| 208 | self.logger.info("\t{:<10} {:<10}".format('Script host os', platform.platform())) |
| 209 | self.logger.info("\t{:<10} {:>10}".format('Python', platform.python_version())) |
| 210 | self.logger.info("\t{:<10} {:>10}".format('PyYAML', yaml.__version__)) |
| 211 | self.logger.info("\t{:<10} {:>10}".format('click', click.__version__)) |
| 212 | self.logger.info("\t{:<10} {:>10}".format('paramiko', paramiko.__version__)) |
| 213 | self.logger.info("\t{:<10} {:>9}".format('redfishtool', redfishtool_version)) |
| 214 | self.logger.info("\t{:<10} {:>12}".format('ipmitool', ipmitool_version)) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 215 | |
Peter D Phan | b289081 | 2022-01-25 05:39:12 -0600 | [diff] [blame] | 216 | if eval(yaml.__version__.replace('.', ',')) < (5, 3, 0): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 217 | self.logger.error("\n\tERROR: Python or python packages do not meet minimum version requirement.") |
Peter D Phan | b289081 | 2022-01-25 05:39:12 -0600 | [diff] [blame] | 218 | self.logger.error("\tERROR: PyYAML version 5.3.0 or higher is needed.\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 219 | run_env_ok = False |
| 220 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 221 | self.logger.info("\t---- End script host environment ----") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 222 | return run_env_ok |
| 223 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 224 | def script_logging(self, |
| 225 | log_level_attr): |
| 226 | r""" |
| 227 | Create logger |
| 228 | |
| 229 | """ |
| 230 | self.logger = logging.getLogger() |
| 231 | self.logger.setLevel(log_level_attr) |
| 232 | log_file_handler = logging.FileHandler(self.ffdc_dir_path + "collector.log") |
| 233 | |
| 234 | stdout_handler = logging.StreamHandler(sys.stdout) |
| 235 | self.logger.addHandler(log_file_handler) |
| 236 | self.logger.addHandler(stdout_handler) |
| 237 | |
| 238 | # Turn off paramiko INFO logging |
| 239 | logging.getLogger("paramiko").setLevel(logging.WARNING) |
| 240 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 241 | def target_is_pingable(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 242 | r""" |
| 243 | Check if target system is ping-able. |
| 244 | |
| 245 | """ |
George Keishing | 0662e94 | 2021-07-13 05:12:20 -0500 | [diff] [blame] | 246 | response = os.system("ping -c 1 %s 2>&1 >/dev/null" % self.hostname) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 247 | if response == 0: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 248 | self.logger.info("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 249 | return True |
| 250 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 251 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 252 | "\n\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 253 | sys.exit(-1) |
| 254 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 255 | def collect_ffdc(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 256 | r""" |
| 257 | Initiate FFDC Collection depending on requested protocol. |
| 258 | |
| 259 | """ |
| 260 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 261 | self.logger.info("\n\t---- Start communicating with %s ----" % self.hostname) |
Peter D Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 262 | self.start_time = time.time() |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 263 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 264 | # Find the list of target and protocol supported. |
| 265 | check_protocol_list = [] |
| 266 | config_dict = self.ffdc_actions |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 267 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 268 | for target_type in config_dict.keys(): |
| 269 | if self.target_type != target_type: |
| 270 | continue |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 271 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 272 | for k, v in config_dict[target_type].items(): |
| 273 | if config_dict[target_type][k]['PROTOCOL'][0] not in check_protocol_list: |
| 274 | check_protocol_list.append(config_dict[target_type][k]['PROTOCOL'][0]) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 275 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 276 | self.logger.info("\n\t %s protocol type: %s" % (self.target_type, check_protocol_list)) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 277 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 278 | verified_working_protocol = self.verify_protocol(check_protocol_list) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 279 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 280 | if verified_working_protocol: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 281 | self.logger.info("\n\t---- Completed protocol pre-requisite check ----\n") |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 282 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 283 | # Verify top level directory exists for storage |
| 284 | self.validate_local_store(self.location) |
| 285 | |
| 286 | if ((self.remote_protocol not in verified_working_protocol) and (self.remote_protocol != 'ALL')): |
| 287 | self.logger.info("\n\tWorking protocol list: %s" % verified_working_protocol) |
| 288 | self.logger.error( |
| 289 | '\tERROR: Requested protocol %s is not in working protocol list.\n' |
| 290 | % self.remote_protocol) |
| 291 | sys.exit(-1) |
| 292 | else: |
| 293 | self.generate_ffdc(verified_working_protocol) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 294 | |
| 295 | def ssh_to_target_system(self): |
| 296 | r""" |
| 297 | Open a ssh connection to targeted system. |
| 298 | |
| 299 | """ |
| 300 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 301 | self.ssh_remoteclient = SSHRemoteclient(self.hostname, |
| 302 | self.username, |
| 303 | self.password) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 304 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 305 | if self.ssh_remoteclient.ssh_remoteclient_login(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 306 | self.logger.info("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname) |
Peter D Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 307 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 308 | # Check scp connection. |
| 309 | # If scp connection fails, |
| 310 | # continue with FFDC generation but skip scp files to local host. |
| 311 | self.ssh_remoteclient.scp_connection() |
| 312 | return True |
| 313 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 314 | self.logger.info("\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" % self.hostname) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 315 | return False |
| 316 | |
| 317 | def telnet_to_target_system(self): |
| 318 | r""" |
| 319 | Open a telnet connection to targeted system. |
| 320 | """ |
| 321 | self.telnet_remoteclient = TelnetRemoteclient(self.hostname, |
| 322 | self.username, |
| 323 | self.password) |
| 324 | if self.telnet_remoteclient.tn_remoteclient_login(): |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 325 | self.logger.info("\n\t[Check] %s Telnet connection established.\t [OK]" % self.hostname) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 326 | return True |
| 327 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 328 | self.logger.info("\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" % self.hostname) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 329 | return False |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 330 | |
George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 331 | def generate_ffdc(self, working_protocol_list): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 332 | r""" |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 333 | Determine actions based on remote host type |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 334 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 335 | Description of argument(s): |
| 336 | working_protocol_list list of confirmed working protocols to connect to remote host. |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 337 | """ |
| 338 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 339 | self.logger.info("\n\t---- Executing commands on " + self.hostname + " ----") |
| 340 | self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 341 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 342 | config_dict = self.ffdc_actions |
| 343 | for target_type in config_dict.keys(): |
| 344 | if self.target_type != target_type: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 345 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 346 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 347 | self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 348 | global_plugin_dict['global_log_store_path'] = self.ffdc_dir_path |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 349 | self.logger.info("\tSystem Type: %s" % target_type) |
| 350 | for k, v in config_dict[target_type].items(): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 351 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 352 | if self.remote_protocol not in working_protocol_list \ |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 353 | and self.remote_protocol != 'ALL': |
| 354 | continue |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 355 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 356 | protocol = config_dict[target_type][k]['PROTOCOL'][0] |
| 357 | |
| 358 | if protocol not in working_protocol_list: |
| 359 | continue |
| 360 | |
George Keishing | b760761 | 2021-07-27 13:31:23 -0500 | [diff] [blame] | 361 | if protocol in working_protocol_list: |
| 362 | if protocol == 'SSH' or protocol == 'SCP': |
George Keishing | 12fd065 | 2021-07-27 13:57:11 -0500 | [diff] [blame] | 363 | self.protocol_ssh(protocol, target_type, k) |
George Keishing | b760761 | 2021-07-27 13:31:23 -0500 | [diff] [blame] | 364 | elif protocol == 'TELNET': |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 365 | self.protocol_telnet(target_type, k) |
George Keishing | b760761 | 2021-07-27 13:31:23 -0500 | [diff] [blame] | 366 | elif protocol == 'REDFISH' or protocol == 'IPMI' or protocol == 'SHELL': |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 367 | self.protocol_execute(protocol, target_type, k) |
George Keishing | b760761 | 2021-07-27 13:31:23 -0500 | [diff] [blame] | 368 | else: |
| 369 | self.logger.error("\n\tERROR: %s is not available for %s." % (protocol, self.hostname)) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 370 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 371 | # Close network connection after collecting all files |
Peter D Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 372 | self.elapsed_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - self.start_time)) |
Peter D Phan | bff617a | 2021-07-22 08:41:35 -0500 | [diff] [blame] | 373 | if self.ssh_remoteclient: |
| 374 | self.ssh_remoteclient.ssh_remoteclient_disconnect() |
| 375 | if self.telnet_remoteclient: |
| 376 | self.telnet_remoteclient.tn_remoteclient_disconnect() |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 377 | |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 378 | def protocol_ssh(self, |
George Keishing | 12fd065 | 2021-07-27 13:57:11 -0500 | [diff] [blame] | 379 | protocol, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 380 | target_type, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 381 | sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 382 | r""" |
| 383 | Perform actions using SSH and SCP protocols. |
| 384 | |
| 385 | Description of argument(s): |
George Keishing | 12fd065 | 2021-07-27 13:57:11 -0500 | [diff] [blame] | 386 | protocol Protocol to execute. |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 387 | target_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 388 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 389 | """ |
| 390 | |
George Keishing | 12fd065 | 2021-07-27 13:57:11 -0500 | [diff] [blame] | 391 | if protocol == 'SCP': |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 392 | self.group_copy(self.ffdc_actions[target_type][sub_type]) |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 393 | else: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 394 | self.collect_and_copy_ffdc(self.ffdc_actions[target_type][sub_type]) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 395 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 396 | def protocol_telnet(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 397 | target_type, |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 398 | sub_type): |
| 399 | r""" |
| 400 | Perform actions using telnet protocol. |
| 401 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 402 | target_type OS Type of remote host. |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 403 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 404 | self.logger.info("\n\t[Run] Executing commands on %s using %s" % (self.hostname, 'TELNET')) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 405 | telnet_files_saved = [] |
| 406 | progress_counter = 0 |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 407 | list_of_commands = self.ffdc_actions[target_type][sub_type]['COMMANDS'] |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 408 | for index, each_cmd in enumerate(list_of_commands, start=0): |
| 409 | command_txt, command_timeout = self.unpack_command(each_cmd) |
| 410 | result = self.telnet_remoteclient.execute_command(command_txt, command_timeout) |
| 411 | if result: |
| 412 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 413 | targ_file = self.ffdc_actions[target_type][sub_type]['FILES'][index] |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 414 | except IndexError: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 415 | targ_file = command_txt |
| 416 | self.logger.warning( |
| 417 | "\n\t[WARN] Missing filename to store data from telnet %s." % each_cmd) |
| 418 | self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file) |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 419 | targ_file_with_path = (self.ffdc_dir_path |
| 420 | + self.ffdc_prefix |
| 421 | + targ_file) |
| 422 | # Creates a new file |
Peter D Phan | b76e175 | 2021-08-03 12:50:05 -0500 | [diff] [blame] | 423 | with open(targ_file_with_path, 'w') as fp: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 424 | fp.write(result) |
| 425 | fp.close |
| 426 | telnet_files_saved.append(targ_file) |
| 427 | progress_counter += 1 |
| 428 | self.print_progress(progress_counter) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 429 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 430 | for file in telnet_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 431 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 432 | |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 433 | def protocol_execute(self, |
| 434 | protocol, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 435 | target_type, |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 436 | sub_type): |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 437 | r""" |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 438 | Perform actions for a given protocol. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 439 | |
| 440 | Description of argument(s): |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 441 | protocol Protocol to execute. |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 442 | target_type OS Type of remote host. |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 443 | sub_type Group type of commands. |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 444 | """ |
| 445 | |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 446 | self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, protocol)) |
| 447 | executed_files_saved = [] |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 448 | progress_counter = 0 |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 449 | list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type]) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 450 | for index, each_cmd in enumerate(list_of_cmd, start=0): |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 451 | plugin_call = False |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 452 | if isinstance(each_cmd, dict): |
| 453 | if 'plugin' in each_cmd: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 454 | # If the error is set and plugin explicitly |
| 455 | # requested to skip execution on error.. |
| 456 | if plugin_error_dict['exit_on_error'] and \ |
| 457 | self.plugin_error_check(each_cmd['plugin']): |
| 458 | self.logger.info("\n\t[PLUGIN-ERROR] exit_on_error: %s" % |
| 459 | plugin_error_dict['exit_on_error']) |
| 460 | self.logger.info("\t[PLUGIN-SKIP] %s" % |
| 461 | each_cmd['plugin'][0]) |
| 462 | continue |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 463 | plugin_call = True |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 464 | # call the plugin |
| 465 | self.logger.info("\n\t[PLUGIN-START]") |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 466 | result = self.execute_plugin_block(each_cmd['plugin']) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 467 | self.logger.info("\t[PLUGIN-END]\n") |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 468 | else: |
George Keishing | 2b83e04 | 2021-08-03 12:56:11 -0500 | [diff] [blame] | 469 | each_cmd = self.yaml_env_and_plugin_vars_populate(each_cmd) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 470 | |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 471 | if not plugin_call: |
| 472 | result = self.run_tool_cmd(each_cmd) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 473 | if result: |
| 474 | try: |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 475 | file_name = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 476 | # If file is specified as None. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 477 | if file_name == "None": |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 478 | continue |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 479 | targ_file = self.yaml_env_and_plugin_vars_populate(file_name) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 480 | except IndexError: |
George Keishing | 6ea92b0 | 2021-07-01 11:20:50 -0500 | [diff] [blame] | 481 | targ_file = each_cmd.split('/')[-1] |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 482 | self.logger.warning( |
| 483 | "\n\t[WARN] Missing filename to store data from %s." % each_cmd) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 484 | self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 485 | |
| 486 | targ_file_with_path = (self.ffdc_dir_path |
| 487 | + self.ffdc_prefix |
| 488 | + targ_file) |
| 489 | |
| 490 | # Creates a new file |
| 491 | with open(targ_file_with_path, 'w') as fp: |
George Keishing | 91308ea | 2021-08-10 14:43:15 -0500 | [diff] [blame] | 492 | if isinstance(result, dict): |
| 493 | fp.write(json.dumps(result)) |
| 494 | else: |
| 495 | fp.write(result) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 496 | fp.close |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 497 | executed_files_saved.append(targ_file) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 498 | |
| 499 | progress_counter += 1 |
| 500 | self.print_progress(progress_counter) |
| 501 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 502 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 503 | |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 504 | for file in executed_files_saved: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 505 | self.logger.info("\n\t\tSuccessfully save file " + file + ".") |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 506 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 507 | def collect_and_copy_ffdc(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 508 | ffdc_actions_for_target_type, |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 509 | form_filename=False): |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 510 | r""" |
| 511 | Send commands in ffdc_config file to targeted system. |
| 512 | |
| 513 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 514 | ffdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 515 | form_filename if true, pre-pend self.target_type to filename |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 516 | """ |
| 517 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 518 | # Executing commands, if any |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 519 | self.ssh_execute_ffdc_commands(ffdc_actions_for_target_type, |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 520 | form_filename) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 521 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 522 | # Copying files |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 523 | if self.ssh_remoteclient.scpclient: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 524 | self.logger.info("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname) |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 525 | |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 526 | # Retrieving files from target system |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 527 | list_of_files = self.get_file_list(ffdc_actions_for_target_type) |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 528 | self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 529 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 530 | self.logger.info("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname) |
Peter D Phan | 04aca3b | 2021-06-21 10:37:18 -0500 | [diff] [blame] | 531 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 532 | def get_command_list(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 533 | ffdc_actions_for_target_type): |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 534 | r""" |
| 535 | Fetch list of commands from configuration file |
| 536 | |
| 537 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 538 | ffdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 539 | """ |
| 540 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 541 | list_of_commands = ffdc_actions_for_target_type['COMMANDS'] |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 542 | except KeyError: |
| 543 | list_of_commands = [] |
| 544 | return list_of_commands |
| 545 | |
| 546 | def get_file_list(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 547 | ffdc_actions_for_target_type): |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 548 | r""" |
| 549 | Fetch list of commands from configuration file |
| 550 | |
| 551 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 552 | ffdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 553 | """ |
| 554 | try: |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 555 | list_of_files = ffdc_actions_for_target_type['FILES'] |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 556 | except KeyError: |
| 557 | list_of_files = [] |
| 558 | return list_of_files |
| 559 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 560 | def unpack_command(self, |
| 561 | command): |
| 562 | r""" |
| 563 | Unpack command from config file |
| 564 | |
| 565 | Description of argument(s): |
| 566 | command Command from config file. |
| 567 | """ |
| 568 | if isinstance(command, dict): |
| 569 | command_txt = next(iter(command)) |
| 570 | command_timeout = next(iter(command.values())) |
| 571 | elif isinstance(command, str): |
| 572 | command_txt = command |
| 573 | # Default command timeout 60 seconds |
| 574 | command_timeout = 60 |
| 575 | |
| 576 | return command_txt, command_timeout |
| 577 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 578 | def ssh_execute_ffdc_commands(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 579 | ffdc_actions_for_target_type, |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 580 | form_filename=False): |
| 581 | r""" |
| 582 | Send commands in ffdc_config file to targeted system. |
| 583 | |
| 584 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 585 | ffdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 586 | form_filename if true, pre-pend self.target_type to filename |
| 587 | """ |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 588 | self.logger.info("\n\t[Run] Executing commands on %s using %s" |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 589 | % (self.hostname, ffdc_actions_for_target_type['PROTOCOL'][0])) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 590 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 591 | list_of_commands = self.get_command_list(ffdc_actions_for_target_type) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 592 | # If command list is empty, returns |
| 593 | if not list_of_commands: |
| 594 | return |
| 595 | |
| 596 | progress_counter = 0 |
| 597 | for command in list_of_commands: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 598 | command_txt, command_timeout = self.unpack_command(command) |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 599 | |
| 600 | if form_filename: |
| 601 | command_txt = str(command_txt % self.target_type) |
| 602 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 603 | cmd_exit_code, err, response = \ |
| 604 | self.ssh_remoteclient.execute_command(command_txt, command_timeout) |
| 605 | |
| 606 | if cmd_exit_code: |
| 607 | self.logger.warning( |
| 608 | "\n\t\t[WARN] %s exits with code %s." % (command_txt, str(cmd_exit_code))) |
| 609 | self.logger.warning("\t\t[WARN] %s " % err) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 610 | |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 611 | progress_counter += 1 |
| 612 | self.print_progress(progress_counter) |
| 613 | |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 614 | self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]") |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 615 | |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 616 | def group_copy(self, |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 617 | ffdc_actions_for_target_type): |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 618 | r""" |
| 619 | scp group of files (wild card) from remote host. |
| 620 | |
| 621 | Description of argument(s): |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 622 | fdc_actions_for_target_type commands and files for the selected remote host type. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 623 | """ |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 624 | |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 625 | if self.ssh_remoteclient.scpclient: |
George Keishing | 12fd065 | 2021-07-27 13:57:11 -0500 | [diff] [blame] | 626 | self.logger.info("\n\tCopying files from remote system %s via SCP.\n" % self.hostname) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 627 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 628 | list_of_commands = self.get_command_list(ffdc_actions_for_target_type) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 629 | # If command list is empty, returns |
| 630 | if not list_of_commands: |
| 631 | return |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 632 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 633 | for command in list_of_commands: |
| 634 | try: |
George Keishing | b4540e7 | 2021-08-02 13:48:46 -0500 | [diff] [blame] | 635 | command = self.yaml_env_and_plugin_vars_populate(command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 636 | except IndexError: |
George Keishing | b4540e7 | 2021-08-02 13:48:46 -0500 | [diff] [blame] | 637 | self.logger.error("\t\tInvalid command %s" % command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 638 | continue |
| 639 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 640 | cmd_exit_code, err, response = \ |
| 641 | self.ssh_remoteclient.execute_command(command) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 642 | |
Peter D Phan | 2b6cb3a | 2021-07-19 06:55:42 -0500 | [diff] [blame] | 643 | # If file does not exist, code take no action. |
| 644 | # cmd_exit_code is ignored for this scenario. |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 645 | if response: |
George Keishing | a56e87b | 2021-08-06 00:24:19 -0500 | [diff] [blame] | 646 | scp_result = \ |
| 647 | self.ssh_remoteclient.scp_file_from_remote(response.split('\n'), |
| 648 | self.ffdc_dir_path) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 649 | if scp_result: |
George Keishing | a56e87b | 2021-08-06 00:24:19 -0500 | [diff] [blame] | 650 | self.logger.info("\t\tSuccessfully copied from " + self.hostname + ':' + command) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 651 | else: |
George Keishing | a56e87b | 2021-08-06 00:24:19 -0500 | [diff] [blame] | 652 | self.logger.info("\t\t%s has no result" % command) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 653 | |
| 654 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 655 | self.logger.info("\n\n\tSkip copying files from remote system %s.\n" % self.hostname) |
Peter D Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 656 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 657 | def scp_ffdc(self, |
| 658 | targ_dir_path, |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 659 | targ_file_prefix, |
| 660 | form_filename, |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 661 | file_list=None, |
| 662 | quiet=None): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 663 | r""" |
| 664 | SCP all files in file_dict to the indicated directory on the local system. |
| 665 | |
| 666 | Description of argument(s): |
| 667 | targ_dir_path The path of the directory to receive the files. |
| 668 | targ_file_prefix Prefix which will be pre-pended to each |
| 669 | target file's name. |
| 670 | file_dict A dictionary of files to scp from targeted system to this system |
| 671 | |
| 672 | """ |
| 673 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 674 | progress_counter = 0 |
| 675 | for filename in file_list: |
Peter D Phan | 2b8052d | 2021-06-22 10:55:41 -0500 | [diff] [blame] | 676 | if form_filename: |
| 677 | filename = str(filename % self.target_type) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 678 | source_file_path = filename |
| 679 | targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1] |
| 680 | |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 681 | # If source file name contains wild card, copy filename as is. |
| 682 | if '*' in source_file_path: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 683 | scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, self.ffdc_dir_path) |
Peter D Phan | babf296 | 2021-07-07 11:24:40 -0500 | [diff] [blame] | 684 | else: |
Peter D Phan | 5963d63 | 2021-07-12 09:58:55 -0500 | [diff] [blame] | 685 | scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, targ_file_path) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 686 | |
| 687 | if not quiet: |
| 688 | if scp_result: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 689 | self.logger.info( |
| 690 | "\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 691 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 692 | self.logger.info( |
| 693 | "\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 694 | else: |
| 695 | progress_counter += 1 |
| 696 | self.print_progress(progress_counter) |
| 697 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 698 | def set_ffdc_default_store_path(self): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 699 | r""" |
| 700 | Set a default value for self.ffdc_dir_path and self.ffdc_prefix. |
| 701 | Collected ffdc file will be stored in dir /self.location/hostname_timestr/. |
| 702 | Individual ffdc file will have timestr_filename. |
| 703 | |
| 704 | Description of class variables: |
| 705 | self.ffdc_dir_path The dir path where collected ffdc data files should be put. |
| 706 | |
| 707 | self.ffdc_prefix The prefix to be given to each ffdc file name. |
| 708 | |
| 709 | """ |
| 710 | |
| 711 | timestr = time.strftime("%Y%m%d-%H%M%S") |
| 712 | self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/" |
| 713 | self.ffdc_prefix = timestr + "_" |
| 714 | self.validate_local_store(self.ffdc_dir_path) |
| 715 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 716 | # Need to verify local store path exists prior to instantiate this class. |
| 717 | # This class method is used to share the same code between CLI input parm |
| 718 | # and Robot Framework "${EXECDIR}/logs" before referencing this class. |
| 719 | @classmethod |
| 720 | def validate_local_store(cls, dir_path): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 721 | r""" |
| 722 | Ensure path exists to store FFDC files locally. |
| 723 | |
| 724 | Description of variable: |
| 725 | dir_path The dir path where collected ffdc data files will be stored. |
| 726 | |
| 727 | """ |
| 728 | |
| 729 | if not os.path.exists(dir_path): |
| 730 | try: |
George Keishing | 7b3a513 | 2021-07-13 09:24:02 -0500 | [diff] [blame] | 731 | os.makedirs(dir_path, 0o755) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 732 | except (IOError, OSError) as e: |
| 733 | # PermissionError |
| 734 | if e.errno == EPERM or e.errno == EACCES: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 735 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 736 | '\tERROR: os.makedirs %s failed with PermissionError.\n' % dir_path) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 737 | else: |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 738 | self.logger.error( |
George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame] | 739 | '\tERROR: os.makedirs %s failed with %s.\n' % (dir_path, e.strerror)) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 740 | sys.exit(-1) |
| 741 | |
| 742 | def print_progress(self, progress): |
| 743 | r""" |
| 744 | Print activity progress + |
| 745 | |
| 746 | Description of variable: |
| 747 | progress Progress counter. |
| 748 | |
| 749 | """ |
| 750 | |
| 751 | sys.stdout.write("\r\t" + "+" * progress) |
| 752 | sys.stdout.flush() |
| 753 | time.sleep(.1) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 754 | |
| 755 | def verify_redfish(self): |
| 756 | r""" |
| 757 | Verify remote host has redfish service active |
| 758 | |
| 759 | """ |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 760 | redfish_parm = 'redfishtool -r ' \ |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 761 | + self.hostname + ' -S Always raw GET /redfish/v1/' |
George Keishing | 6224635 | 2022-08-01 01:20:06 -0500 | [diff] [blame] | 762 | return (self.run_tool_cmd(redfish_parm, True)) |
Peter D Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 763 | |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 764 | def verify_ipmi(self): |
| 765 | r""" |
| 766 | Verify remote host has IPMI LAN service active |
| 767 | |
| 768 | """ |
George Keishing | 484f824 | 2021-07-27 01:42:02 -0500 | [diff] [blame] | 769 | if self.target_type == 'OPENBMC': |
| 770 | ipmi_parm = 'ipmitool -I lanplus -C 17 -U ' + self.username + ' -P ' \ |
| 771 | + self.password + ' -H ' + self.hostname + ' power status' |
| 772 | else: |
| 773 | ipmi_parm = 'ipmitool -I lanplus -P ' \ |
| 774 | + self.password + ' -H ' + self.hostname + ' power status' |
| 775 | |
George Keishing | 6224635 | 2022-08-01 01:20:06 -0500 | [diff] [blame] | 776 | return (self.run_tool_cmd(ipmi_parm, True)) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 777 | |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 778 | def run_tool_cmd(self, |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 779 | parms_string, |
| 780 | quiet=False): |
| 781 | r""" |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 782 | Run CLI standard tool or scripts. |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 783 | |
| 784 | Description of variable: |
George Keishing | 506b058 | 2021-07-27 09:31:22 -0500 | [diff] [blame] | 785 | parms_string tool command options. |
| 786 | quiet do not print tool error message if True |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 787 | """ |
| 788 | |
George Keishing | 484f824 | 2021-07-27 01:42:02 -0500 | [diff] [blame] | 789 | result = subprocess.run([parms_string], |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 790 | stdout=subprocess.PIPE, |
| 791 | stderr=subprocess.PIPE, |
| 792 | shell=True, |
| 793 | universal_newlines=True) |
| 794 | |
| 795 | if result.stderr and not quiet: |
George Keishing | 484f824 | 2021-07-27 01:42:02 -0500 | [diff] [blame] | 796 | self.logger.error('\n\t\tERROR with %s ' % parms_string) |
Peter D Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 797 | self.logger.error('\t\t' + result.stderr) |
George Keishing | eafba18 | 2021-06-29 13:44:58 -0500 | [diff] [blame] | 798 | |
| 799 | return result.stdout |
George Keishing | 04d2910 | 2021-07-16 02:05:57 -0500 | [diff] [blame] | 800 | |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 801 | def verify_protocol(self, protocol_list): |
| 802 | r""" |
| 803 | Perform protocol working check. |
| 804 | |
| 805 | Description of argument(s): |
| 806 | protocol_list List of protocol. |
| 807 | """ |
| 808 | |
| 809 | tmp_list = [] |
| 810 | if self.target_is_pingable(): |
| 811 | tmp_list.append("SHELL") |
| 812 | |
| 813 | for protocol in protocol_list: |
| 814 | if self.remote_protocol != 'ALL': |
| 815 | if self.remote_protocol != protocol: |
| 816 | continue |
| 817 | |
| 818 | # Only check SSH/SCP once for both protocols |
| 819 | if protocol == 'SSH' or protocol == 'SCP' and protocol not in tmp_list: |
| 820 | if self.ssh_to_target_system(): |
George Keishing | aa63870 | 2021-07-26 11:48:28 -0500 | [diff] [blame] | 821 | # Add only what user asked. |
| 822 | if self.remote_protocol != 'ALL': |
| 823 | tmp_list.append(self.remote_protocol) |
| 824 | else: |
| 825 | tmp_list.append('SSH') |
| 826 | tmp_list.append('SCP') |
George Keishing | f5a5750 | 2021-07-22 16:43:47 -0500 | [diff] [blame] | 827 | |
| 828 | if protocol == 'TELNET': |
| 829 | if self.telnet_to_target_system(): |
| 830 | tmp_list.append(protocol) |
| 831 | |
| 832 | if protocol == 'REDFISH': |
| 833 | if self.verify_redfish(): |
| 834 | tmp_list.append(protocol) |
| 835 | self.logger.info("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname) |
| 836 | else: |
| 837 | self.logger.info("\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname) |
| 838 | |
| 839 | if protocol == 'IPMI': |
| 840 | if self.verify_ipmi(): |
| 841 | tmp_list.append(protocol) |
| 842 | self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname) |
| 843 | else: |
| 844 | self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname) |
| 845 | |
| 846 | return tmp_list |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 847 | |
| 848 | def load_env(self): |
| 849 | r""" |
| 850 | Perform protocol working check. |
| 851 | |
| 852 | """ |
| 853 | # This is for the env vars a user can use in YAML to load it at runtime. |
| 854 | # Example YAML: |
| 855 | # -COMMANDS: |
| 856 | # - my_command ${hostname} ${username} ${password} |
| 857 | os.environ['hostname'] = self.hostname |
| 858 | os.environ['username'] = self.username |
| 859 | os.environ['password'] = self.password |
| 860 | |
| 861 | # Append default Env. |
| 862 | self.env_dict['hostname'] = self.hostname |
| 863 | self.env_dict['username'] = self.username |
| 864 | self.env_dict['password'] = self.password |
| 865 | |
| 866 | try: |
| 867 | tmp_env_dict = {} |
| 868 | if self.env_vars: |
| 869 | tmp_env_dict = json.loads(self.env_vars) |
| 870 | # Export ENV vars default. |
| 871 | for key, value in tmp_env_dict.items(): |
| 872 | os.environ[key] = value |
| 873 | self.env_dict[key] = str(value) |
| 874 | |
| 875 | if self.econfig: |
| 876 | with open(self.econfig, 'r') as file: |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 877 | try: |
Yunyun Lin | f87cc0a | 2022-06-08 16:57:04 -0700 | [diff] [blame] | 878 | tmp_env_dict = yaml.load(file, Loader=yaml.SafeLoader) |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 879 | except yaml.YAMLError as e: |
| 880 | self.logger.error(e) |
| 881 | sys.exit(-1) |
George Keishing | e168675 | 2021-07-27 12:55:28 -0500 | [diff] [blame] | 882 | # Export ENV vars. |
| 883 | for key, value in tmp_env_dict['env_params'].items(): |
| 884 | os.environ[key] = str(value) |
| 885 | self.env_dict[key] = str(value) |
| 886 | except json.decoder.JSONDecodeError as e: |
| 887 | self.logger.error("\n\tERROR: %s " % e) |
| 888 | sys.exit(-1) |
| 889 | |
| 890 | # This to mask the password from displaying on the console. |
| 891 | mask_dict = self.env_dict.copy() |
| 892 | for k, v in mask_dict.items(): |
| 893 | if k.lower().find("password") != -1: |
| 894 | hidden_text = [] |
| 895 | hidden_text.append(v) |
| 896 | password_regex = '(' +\ |
| 897 | '|'.join([re.escape(x) for x in hidden_text]) + ')' |
| 898 | mask_dict[k] = re.sub(password_regex, "********", v) |
| 899 | |
| 900 | self.logger.info(json.dumps(mask_dict, indent=8, sort_keys=False)) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 901 | |
| 902 | def execute_python_eval(self, eval_string): |
| 903 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 904 | Execute qualified python function string using eval. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 905 | |
| 906 | Description of argument(s): |
| 907 | eval_string Execute the python object. |
| 908 | |
| 909 | Example: |
| 910 | eval(plugin.foo_func.foo_func(10)) |
| 911 | """ |
| 912 | try: |
George Keishing | dda48ce | 2021-08-12 07:02:27 -0500 | [diff] [blame] | 913 | self.logger.info("\tExecuting plugin func()") |
| 914 | self.logger.debug("\tCall func: %s" % eval_string) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 915 | result = eval(eval_string) |
| 916 | self.logger.info("\treturn: %s" % str(result)) |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 917 | except (ValueError, |
| 918 | SyntaxError, |
| 919 | NameError, |
| 920 | AttributeError, |
| 921 | TypeError) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 922 | self.logger.error("\tERROR: execute_python_eval: %s" % e) |
| 923 | # Set the plugin error state. |
| 924 | plugin_error_dict['exit_on_error'] = True |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 925 | self.logger.info("\treturn: PLUGIN_EVAL_ERROR") |
| 926 | return 'PLUGIN_EVAL_ERROR' |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 927 | |
| 928 | return result |
| 929 | |
| 930 | def execute_plugin_block(self, plugin_cmd_list): |
| 931 | r""" |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 932 | Pack the plugin command to qualifed python string object. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 933 | |
| 934 | Description of argument(s): |
| 935 | plugin_list_dict Plugin block read from YAML |
| 936 | [{'plugin_name': 'plugin.foo_func.my_func'}, |
| 937 | {'plugin_args': [10]}] |
| 938 | |
| 939 | Example: |
| 940 | - plugin: |
| 941 | - plugin_name: plugin.foo_func.my_func |
| 942 | - plugin_args: |
| 943 | - arg1 |
| 944 | - arg2 |
| 945 | |
| 946 | - plugin: |
| 947 | - plugin_name: result = plugin.foo_func.my_func |
| 948 | - plugin_args: |
| 949 | - arg1 |
| 950 | - arg2 |
| 951 | |
| 952 | - plugin: |
| 953 | - plugin_name: result1,result2 = plugin.foo_func.my_func |
| 954 | - plugin_args: |
| 955 | - arg1 |
| 956 | - arg2 |
| 957 | """ |
| 958 | try: |
George Keishing | e9b23d3 | 2021-08-13 12:57:58 -0500 | [diff] [blame] | 959 | idx = self.key_index_list_dict('plugin_name', plugin_cmd_list) |
| 960 | plugin_name = plugin_cmd_list[idx]['plugin_name'] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 961 | # Equal separator means plugin function returns result. |
| 962 | if ' = ' in plugin_name: |
| 963 | # Ex. ['result', 'plugin.foo_func.my_func'] |
| 964 | plugin_name_args = plugin_name.split(' = ') |
| 965 | # plugin func return data. |
| 966 | for arg in plugin_name_args: |
| 967 | if arg == plugin_name_args[-1]: |
| 968 | plugin_name = arg |
| 969 | else: |
| 970 | plugin_resp = arg.split(',') |
| 971 | # ['result1','result2'] |
| 972 | for x in plugin_resp: |
| 973 | global_plugin_list.append(x) |
| 974 | global_plugin_dict[x] = "" |
| 975 | |
| 976 | # Walk the plugin args ['arg1,'arg2'] |
| 977 | # If the YAML plugin statement 'plugin_args' is not declared. |
| 978 | if any('plugin_args' in d for d in plugin_cmd_list): |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 979 | idx = self.key_index_list_dict('plugin_args', plugin_cmd_list) |
| 980 | plugin_args = plugin_cmd_list[idx]['plugin_args'] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 981 | if plugin_args: |
| 982 | plugin_args = self.yaml_args_populate(plugin_args) |
| 983 | else: |
| 984 | plugin_args = [] |
| 985 | else: |
| 986 | plugin_args = self.yaml_args_populate([]) |
| 987 | |
| 988 | # Pack the args arg1, arg2, .... argn into |
| 989 | # "arg1","arg2","argn" string as params for function. |
| 990 | parm_args_str = self.yaml_args_string(plugin_args) |
| 991 | if parm_args_str: |
| 992 | plugin_func = plugin_name + '(' + parm_args_str + ')' |
| 993 | else: |
| 994 | plugin_func = plugin_name + '()' |
| 995 | |
| 996 | # Execute plugin function. |
| 997 | if global_plugin_dict: |
| 998 | resp = self.execute_python_eval(plugin_func) |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 999 | # Update plugin vars dict if there is any. |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1000 | if resp != 'PLUGIN_EVAL_ERROR': |
| 1001 | self.response_args_data(resp) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1002 | else: |
George Keishing | caa97e6 | 2021-08-03 14:00:09 -0500 | [diff] [blame] | 1003 | resp = self.execute_python_eval(plugin_func) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1004 | except Exception as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1005 | # Set the plugin error state. |
| 1006 | plugin_error_dict['exit_on_error'] = True |
| 1007 | self.logger.error("\tERROR: execute_plugin_block: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1008 | pass |
| 1009 | |
George Keishing | 73b95d1 | 2021-08-13 14:30:52 -0500 | [diff] [blame] | 1010 | # There is a real error executing the plugin function. |
| 1011 | if resp == 'PLUGIN_EVAL_ERROR': |
| 1012 | return resp |
| 1013 | |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1014 | # Check if plugin_expects_return (int, string, list,dict etc) |
| 1015 | if any('plugin_expects_return' in d for d in plugin_cmd_list): |
| 1016 | idx = self.key_index_list_dict('plugin_expects_return', plugin_cmd_list) |
| 1017 | plugin_expects = plugin_cmd_list[idx]['plugin_expects_return'] |
| 1018 | if plugin_expects: |
| 1019 | if resp: |
| 1020 | if self.plugin_expect_type(plugin_expects, resp) == 'INVALID': |
| 1021 | self.logger.error("\tWARN: Plugin error check skipped") |
| 1022 | elif not self.plugin_expect_type(plugin_expects, resp): |
| 1023 | self.logger.error("\tERROR: Plugin expects return data: %s" |
| 1024 | % plugin_expects) |
| 1025 | plugin_error_dict['exit_on_error'] = True |
| 1026 | elif not resp: |
| 1027 | self.logger.error("\tERROR: Plugin func failed to return data") |
| 1028 | plugin_error_dict['exit_on_error'] = True |
| 1029 | |
| 1030 | return resp |
| 1031 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1032 | def response_args_data(self, plugin_resp): |
| 1033 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1034 | Parse the plugin function response and update plugin return variable. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1035 | |
| 1036 | plugin_resp Response data from plugin function. |
| 1037 | """ |
| 1038 | resp_list = [] |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1039 | resp_data = "" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1040 | |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1041 | # There is nothing to update the plugin response. |
| 1042 | if len(global_plugin_list) == 0 or plugin_resp == 'None': |
| 1043 | return |
| 1044 | |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1045 | if isinstance(plugin_resp, str): |
| 1046 | resp_data = plugin_resp.strip('\r\n\t') |
| 1047 | resp_list.append(resp_data) |
| 1048 | elif isinstance(plugin_resp, bytes): |
| 1049 | resp_data = str(plugin_resp, 'UTF-8').strip('\r\n\t') |
| 1050 | resp_list.append(resp_data) |
| 1051 | elif isinstance(plugin_resp, tuple): |
| 1052 | if len(global_plugin_list) == 1: |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1053 | resp_list.append(plugin_resp) |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1054 | else: |
| 1055 | resp_list = list(plugin_resp) |
| 1056 | resp_list = [x.strip('\r\n\t') for x in resp_list] |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1057 | elif isinstance(plugin_resp, list): |
George Keishing | 5765f79 | 2021-08-02 13:08:53 -0500 | [diff] [blame] | 1058 | if len(global_plugin_list) == 1: |
| 1059 | resp_list.append([x.strip('\r\n\t') for x in plugin_resp]) |
| 1060 | else: |
| 1061 | resp_list = [x.strip('\r\n\t') for x in plugin_resp] |
| 1062 | elif isinstance(plugin_resp, int) or isinstance(plugin_resp, float): |
| 1063 | resp_list.append(plugin_resp) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1064 | |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1065 | # Iterate if there is a list of plugin return vars to update. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1066 | for idx, item in enumerate(resp_list, start=0): |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1067 | # Exit loop, done required loop. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1068 | if idx >= len(global_plugin_list): |
| 1069 | break |
| 1070 | # Find the index of the return func in the list and |
| 1071 | # update the global func return dictionary. |
| 1072 | try: |
| 1073 | dict_idx = global_plugin_list[idx] |
| 1074 | global_plugin_dict[dict_idx] = item |
| 1075 | except (IndexError, ValueError) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1076 | self.logger.warn("\tWARN: response_args_data: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1077 | pass |
| 1078 | |
| 1079 | # Done updating plugin dict irrespective of pass or failed, |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1080 | # clear all the list element for next plugin block execute. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1081 | global_plugin_list.clear() |
| 1082 | |
| 1083 | def yaml_args_string(self, plugin_args): |
| 1084 | r""" |
| 1085 | Pack the args into string. |
| 1086 | |
| 1087 | plugin_args arg list ['arg1','arg2,'argn'] |
| 1088 | """ |
| 1089 | args_str = '' |
| 1090 | for args in plugin_args: |
| 1091 | if args: |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1092 | if isinstance(args, (int, float)): |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1093 | args_str += str(args) |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1094 | elif args in global_plugin_type_list: |
| 1095 | args_str += str(global_plugin_dict[args]) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1096 | else: |
| 1097 | args_str += '"' + str(args.strip('\r\n\t')) + '"' |
| 1098 | # Skip last list element. |
| 1099 | if args != plugin_args[-1]: |
| 1100 | args_str += "," |
| 1101 | return args_str |
| 1102 | |
| 1103 | def yaml_args_populate(self, yaml_arg_list): |
| 1104 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1105 | Decode env and plugin vars and populate. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1106 | |
| 1107 | Description of argument(s): |
| 1108 | yaml_arg_list arg list read from YAML |
| 1109 | |
| 1110 | Example: |
| 1111 | - plugin_args: |
| 1112 | - arg1 |
| 1113 | - arg2 |
| 1114 | |
| 1115 | yaml_arg_list: [arg2, arg2] |
| 1116 | """ |
| 1117 | # Get the env loaded keys as list ['hostname', 'username', 'password']. |
| 1118 | env_vars_list = list(self.env_dict) |
| 1119 | |
| 1120 | if isinstance(yaml_arg_list, list): |
| 1121 | tmp_list = [] |
| 1122 | for arg in yaml_arg_list: |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1123 | if isinstance(arg, (int, float)): |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1124 | tmp_list.append(arg) |
| 1125 | continue |
| 1126 | elif isinstance(arg, str): |
| 1127 | arg_str = self.yaml_env_and_plugin_vars_populate(str(arg)) |
| 1128 | tmp_list.append(arg_str) |
| 1129 | else: |
| 1130 | tmp_list.append(arg) |
| 1131 | |
| 1132 | # return populated list. |
| 1133 | return tmp_list |
| 1134 | |
| 1135 | def yaml_env_and_plugin_vars_populate(self, yaml_arg_str): |
| 1136 | r""" |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1137 | Update ${MY_VAR} and plugin vars. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1138 | |
| 1139 | Description of argument(s): |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1140 | yaml_arg_str arg string read from YAML. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1141 | |
| 1142 | Example: |
| 1143 | - cat ${MY_VAR} |
| 1144 | - ls -AX my_plugin_var |
| 1145 | """ |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1146 | # Parse the string for env vars ${env_vars}. |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1147 | try: |
| 1148 | # Example, list of matching env vars ['username', 'password', 'hostname'] |
| 1149 | # Extra escape \ for special symbols. '\$\{([^\}]+)\}' works good. |
| 1150 | var_name_regex = '\\$\\{([^\\}]+)\\}' |
| 1151 | env_var_names_list = re.findall(var_name_regex, yaml_arg_str) |
| 1152 | for var in env_var_names_list: |
| 1153 | env_var = os.environ[var] |
| 1154 | env_replace = '${' + var + '}' |
| 1155 | yaml_arg_str = yaml_arg_str.replace(env_replace, env_var) |
| 1156 | except Exception as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1157 | self.logger.error("\tERROR:yaml_env_vars_populate: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1158 | pass |
| 1159 | |
| 1160 | # Parse the string for plugin vars. |
| 1161 | try: |
| 1162 | # Example, list of plugin vars ['my_username', 'my_data'] |
| 1163 | plugin_var_name_list = global_plugin_dict.keys() |
| 1164 | for var in plugin_var_name_list: |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1165 | # skip env var list already populated above code block list. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1166 | if var in env_var_names_list: |
| 1167 | continue |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1168 | # If this plugin var exist but empty in dict, don't replace. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1169 | # This is either a YAML plugin statement incorrectly used or |
George Keishing | 9348b40 | 2021-08-13 12:22:35 -0500 | [diff] [blame] | 1170 | # user added a plugin var which is not going to be populated. |
George Keishing | 0581cb0 | 2021-08-05 15:08:58 -0500 | [diff] [blame] | 1171 | if yaml_arg_str in global_plugin_dict: |
| 1172 | if isinstance(global_plugin_dict[var], (list, dict)): |
| 1173 | # List data type or dict can't be replaced, use directly |
| 1174 | # in eval function call. |
| 1175 | global_plugin_type_list.append(var) |
| 1176 | else: |
| 1177 | yaml_arg_str = yaml_arg_str.replace(str(var), str(global_plugin_dict[var])) |
| 1178 | # Just a string like filename or command. |
| 1179 | else: |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1180 | yaml_arg_str = yaml_arg_str.replace(str(var), str(global_plugin_dict[var])) |
| 1181 | except (IndexError, ValueError) as e: |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1182 | self.logger.error("\tERROR: yaml_plugin_vars_populate: %s" % e) |
George Keishing | b97a904 | 2021-07-29 07:41:20 -0500 | [diff] [blame] | 1183 | pass |
| 1184 | |
| 1185 | return yaml_arg_str |
George Keishing | 1e7b018 | 2021-08-06 14:05:54 -0500 | [diff] [blame] | 1186 | |
| 1187 | def plugin_error_check(self, plugin_dict): |
| 1188 | r""" |
| 1189 | Plugin error dict processing. |
| 1190 | |
| 1191 | Description of argument(s): |
| 1192 | plugin_dict Dictionary of plugin error. |
| 1193 | """ |
| 1194 | if any('plugin_error' in d for d in plugin_dict): |
| 1195 | for d in plugin_dict: |
| 1196 | if 'plugin_error' in d: |
| 1197 | value = d['plugin_error'] |
| 1198 | # Reference if the error is set or not by plugin. |
| 1199 | return plugin_error_dict[value] |
George Keishing | de79a9b | 2021-08-12 16:14:43 -0500 | [diff] [blame] | 1200 | |
| 1201 | def key_index_list_dict(self, key, list_dict): |
| 1202 | r""" |
| 1203 | Iterate list of dictionary and return index if the key match is found. |
| 1204 | |
| 1205 | Description of argument(s): |
| 1206 | key Valid Key in a dict. |
| 1207 | list_dict list of dictionary. |
| 1208 | """ |
| 1209 | for i, d in enumerate(list_dict): |
| 1210 | if key in d.keys(): |
| 1211 | return i |
| 1212 | |
| 1213 | def plugin_expect_type(self, type, data): |
| 1214 | r""" |
| 1215 | Plugin expect directive type check. |
| 1216 | """ |
| 1217 | if type == 'int': |
| 1218 | return isinstance(data, int) |
| 1219 | elif type == 'float': |
| 1220 | return isinstance(data, float) |
| 1221 | elif type == 'str': |
| 1222 | return isinstance(data, str) |
| 1223 | elif type == 'list': |
| 1224 | return isinstance(data, list) |
| 1225 | elif type == 'dict': |
| 1226 | return isinstance(data, dict) |
| 1227 | elif type == 'tuple': |
| 1228 | return isinstance(data, tuple) |
| 1229 | else: |
| 1230 | self.logger.info("\tInvalid data type requested: %s" % type) |
| 1231 | return 'INVALID' |