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