blob: 27b088cca0dd8d60893bf5a2ad3495e9431a1cd4 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Peter D Phan72ce6b82021-06-03 06:18:26 -05002
3r"""
4See class prolog below for details.
5"""
6
George Keishinge635ddc2022-12-08 07:38:02 -06007import os
8import re
9import sys
10import yaml
11import json
12import time
13import logging
14import platform
George Keishing37c58c82022-12-08 07:42:54 -060015from errno import EACCES, EPERM
George Keishinge635ddc2022-12-08 07:38:02 -060016import subprocess
Peter D Phan5e56f522021-12-20 13:19:41 -060017
Peter D Phancb791d72022-02-08 12:23:03 -060018script_dir = os.path.dirname(os.path.abspath(__file__))
19sys.path.append(script_dir)
20# Walk path and append to sys.path
21for root, dirs, files in os.walk(script_dir):
22 for dir in dirs:
23 sys.path.append(os.path.join(root, dir))
24
George Keishing37c58c82022-12-08 07:42:54 -060025from ssh_utility import SSHRemoteclient
26from telnet_utility import TelnetRemoteclient
Peter D Phan72ce6b82021-06-03 06:18:26 -050027
George Keishingb97a9042021-07-29 07:41:20 -050028r"""
29User define plugins python functions.
30
31It will imports files from directory plugins
32
33plugins
34├── file1.py
35└── file2.py
36
37Example how to define in YAML:
38 - plugin:
39 - plugin_name: plugin.foo_func.foo_func_yaml
40 - plugin_args:
41 - arg1
42 - arg2
43"""
George Keishinge635ddc2022-12-08 07:38:02 -060044plugin_dir = __file__.split(__file__.split("/")[-1])[0] + '/plugins'
Peter D Phan5e56f522021-12-20 13:19:41 -060045sys.path.append(plugin_dir)
George Keishingb97a9042021-07-29 07:41:20 -050046try:
47 for module in os.listdir(plugin_dir):
George Keishinge635ddc2022-12-08 07:38:02 -060048 if module == '__init__.py' or module[-3:] != '.py':
George Keishingb97a9042021-07-29 07:41:20 -050049 continue
50 plugin_module = "plugins." + module[:-3]
51 # To access the module plugin.<module name>.<function>
52 # Example: plugin.foo_func.foo_func_yaml()
53 try:
54 plugin = __import__(plugin_module, globals(), locals(), [], 0)
55 except Exception as e:
56 print("PLUGIN: Module import failed: %s" % module)
57 pass
58except FileNotFoundError as e:
59 print("PLUGIN: %s" % e)
60 pass
61
62r"""
63This is for plugin functions returning data or responses to the caller
64in YAML plugin setup.
65
66Example:
67
68 - plugin:
69 - plugin_name: version = plugin.ssh_execution.ssh_execute_cmd
70 - plugin_args:
71 - ${hostname}
72 - ${username}
73 - ${password}
74 - "cat /etc/os-release | grep VERSION_ID | awk -F'=' '{print $2}'"
75 - plugin:
76 - plugin_name: plugin.print_vars.print_vars
77 - plugin_args:
78 - version
79
80where first plugin "version" var is used by another plugin in the YAML
81block or plugin
82
83"""
84global global_log_store_path
85global global_plugin_dict
86global global_plugin_list
George Keishing9348b402021-08-13 12:22:35 -050087
George Keishing0581cb02021-08-05 15:08:58 -050088# Hold the plugin return values in dict and plugin return vars in list.
George Keishing9348b402021-08-13 12:22:35 -050089# Dict is to reference and update vars processing in parser where as
90# list is for current vars from the plugin block which needs processing.
George Keishingb97a9042021-07-29 07:41:20 -050091global_plugin_dict = {}
92global_plugin_list = []
George Keishing9348b402021-08-13 12:22:35 -050093
George Keishing0581cb02021-08-05 15:08:58 -050094# Hold the plugin return named declared if function returned values are list,dict.
95# Refer this name list to look up the plugin dict for eval() args function
George Keishing9348b402021-08-13 12:22:35 -050096# Example ['version']
George Keishing0581cb02021-08-05 15:08:58 -050097global_plugin_type_list = []
George Keishing9348b402021-08-13 12:22:35 -050098
99# Path where logs are to be stored or written.
George Keishinge635ddc2022-12-08 07:38:02 -0600100global_log_store_path = ''
George Keishingb97a9042021-07-29 07:41:20 -0500101
George Keishing1e7b0182021-08-06 14:05:54 -0500102# Plugin error state defaults.
103plugin_error_dict = {
George Keishinge635ddc2022-12-08 07:38:02 -0600104 'exit_on_error': False,
105 'continue_on_error': False,
George Keishing1e7b0182021-08-06 14:05:54 -0500106}
107
Peter D Phan72ce6b82021-06-03 06:18:26 -0500108
Peter D Phan5e56f522021-12-20 13:19:41 -0600109class ffdc_collector:
George Keishinge635ddc2022-12-08 07:38:02 -0600110
Peter D Phan72ce6b82021-06-03 06:18:26 -0500111 r"""
George Keishing1e7b0182021-08-06 14:05:54 -0500112 Execute commands from configuration file to collect log files.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500113 Fetch and store generated files at the specified location.
114
115 """
116
George Keishinge635ddc2022-12-08 07:38:02 -0600117 def __init__(self,
118 hostname,
119 username,
120 password,
121 ffdc_config,
122 location,
123 remote_type,
124 remote_protocol,
125 env_vars,
126 econfig,
127 log_level):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500128 r"""
129 Description of argument(s):
130
George Keishing8e94f8c2021-07-23 15:06:32 -0500131 hostname name/ip of the targeted (remote) system
132 username user on the targeted system with access to FFDC files
133 password password for user on targeted system
134 ffdc_config configuration file listing commands and files for FFDC
135 location where to store collected FFDC
136 remote_type os type of the remote host
137 remote_protocol Protocol to use to collect data
138 env_vars User define CLI env vars '{"key : "value"}'
139 econfig User define env vars YAML file
Peter D Phan72ce6b82021-06-03 06:18:26 -0500140
141 """
Peter D Phane86d9a52021-07-15 10:42:25 -0500142
143 self.hostname = hostname
144 self.username = username
145 self.password = password
146 self.ffdc_config = ffdc_config
147 self.location = location + "/" + remote_type.upper()
148 self.ssh_remoteclient = None
149 self.telnet_remoteclient = None
150 self.ffdc_dir_path = ""
151 self.ffdc_prefix = ""
152 self.target_type = remote_type.upper()
153 self.remote_protocol = remote_protocol.upper()
George Keishinge1686752021-07-27 12:55:28 -0500154 self.env_vars = env_vars
155 self.econfig = econfig
Peter D Phane86d9a52021-07-15 10:42:25 -0500156 self.start_time = 0
George Keishinge635ddc2022-12-08 07:38:02 -0600157 self.elapsed_time = ''
Peter D Phane86d9a52021-07-15 10:42:25 -0500158 self.logger = None
159
160 # Set prefix values for scp files and directory.
161 # Since the time stamp is at second granularity, these values are set here
162 # to be sure that all files for this run will have same timestamps
163 # and they will be saved in the same directory.
164 # self.location == local system for now
Peter D Phan5e56f522021-12-20 13:19:41 -0600165 self.set_ffdc_default_store_path()
Peter D Phane86d9a52021-07-15 10:42:25 -0500166
Peter D Phan5e56f522021-12-20 13:19:41 -0600167 # Logger for this run. Need to be after set_ffdc_default_store_path()
Peter D Phane86d9a52021-07-15 10:42:25 -0500168 self.script_logging(getattr(logging, log_level.upper()))
169
170 # Verify top level directory exists for storage
171 self.validate_local_store(self.location)
172
Peter D Phan72ce6b82021-06-03 06:18:26 -0500173 if self.verify_script_env():
Peter D Phane86d9a52021-07-15 10:42:25 -0500174 # Load default or user define YAML configuration file.
George Keishinge635ddc2022-12-08 07:38:02 -0600175 with open(self.ffdc_config, 'r') as file:
George Keishinge9b23d32021-08-13 12:57:58 -0500176 try:
Yunyun Linf87cc0a2022-06-08 16:57:04 -0700177 self.ffdc_actions = yaml.load(file, Loader=yaml.SafeLoader)
George Keishinge9b23d32021-08-13 12:57:58 -0500178 except yaml.YAMLError as e:
179 self.logger.error(e)
180 sys.exit(-1)
Peter D Phane86d9a52021-07-15 10:42:25 -0500181
182 if self.target_type not in self.ffdc_actions.keys():
183 self.logger.error(
George Keishinge635ddc2022-12-08 07:38:02 -0600184 "\n\tERROR: %s is not listed in %s.\n\n" % (self.target_type, self.ffdc_config))
Peter D Phane86d9a52021-07-15 10:42:25 -0500185 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500186 else:
Peter D Phan8462faf2021-06-16 12:24:15 -0500187 sys.exit(-1)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500188
George Keishing4885b2f2021-07-21 15:22:45 -0500189 # Load ENV vars from user.
George Keishingaa1f8482021-07-22 00:54:55 -0500190 self.logger.info("\n\tENV: User define input YAML variables")
191 self.env_dict = {}
Peter D Phan5e56f522021-12-20 13:19:41 -0600192 self.load_env()
George Keishingaa1f8482021-07-22 00:54:55 -0500193
Peter D Phan72ce6b82021-06-03 06:18:26 -0500194 def verify_script_env(self):
George Keishinge635ddc2022-12-08 07:38:02 -0600195
Peter D Phan72ce6b82021-06-03 06:18:26 -0500196 # Import to log version
197 import click
198 import paramiko
199
200 run_env_ok = True
Peter D Phan0c669772021-06-24 13:52:42 -0500201
George Keishinge635ddc2022-12-08 07:38:02 -0600202 redfishtool_version = self.run_tool_cmd('redfishtool -V').split(' ')[2].strip('\n')
203 ipmitool_version = self.run_tool_cmd('ipmitool -V').split(' ')[2]
Peter D Phan0c669772021-06-24 13:52:42 -0500204
Peter D Phane86d9a52021-07-15 10:42:25 -0500205 self.logger.info("\n\t---- Script host environment ----")
George Keishinge635ddc2022-12-08 07:38:02 -0600206 self.logger.info("\t{:<10} {:<10}".format('Script hostname', os.uname()[1]))
207 self.logger.info("\t{:<10} {:<10}".format('Script host os', platform.platform()))
208 self.logger.info("\t{:<10} {:>10}".format('Python', platform.python_version()))
209 self.logger.info("\t{:<10} {:>10}".format('PyYAML', yaml.__version__))
210 self.logger.info("\t{:<10} {:>10}".format('click', click.__version__))
211 self.logger.info("\t{:<10} {:>10}".format('paramiko', paramiko.__version__))
212 self.logger.info("\t{:<10} {:>9}".format('redfishtool', redfishtool_version))
213 self.logger.info("\t{:<10} {:>12}".format('ipmitool', ipmitool_version))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500214
George Keishinge635ddc2022-12-08 07:38:02 -0600215 if eval(yaml.__version__.replace('.', ',')) < (5, 3, 0):
216 self.logger.error("\n\tERROR: Python or python packages do not meet minimum version requirement.")
217 self.logger.error("\tERROR: PyYAML version 5.3.0 or higher is needed.\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500218 run_env_ok = False
219
Peter D Phane86d9a52021-07-15 10:42:25 -0500220 self.logger.info("\t---- End script host environment ----")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500221 return run_env_ok
222
George Keishinge635ddc2022-12-08 07:38:02 -0600223 def script_logging(self,
224 log_level_attr):
Peter D Phane86d9a52021-07-15 10:42:25 -0500225 r"""
226 Create logger
227
228 """
229 self.logger = logging.getLogger()
230 self.logger.setLevel(log_level_attr)
George Keishinge635ddc2022-12-08 07:38:02 -0600231 log_file_handler = logging.FileHandler(self.ffdc_dir_path + "collector.log")
Peter D Phane86d9a52021-07-15 10:42:25 -0500232
233 stdout_handler = logging.StreamHandler(sys.stdout)
234 self.logger.addHandler(log_file_handler)
235 self.logger.addHandler(stdout_handler)
236
237 # Turn off paramiko INFO logging
238 logging.getLogger("paramiko").setLevel(logging.WARNING)
239
Peter D Phan72ce6b82021-06-03 06:18:26 -0500240 def target_is_pingable(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500241 r"""
242 Check if target system is ping-able.
243
244 """
George Keishing0662e942021-07-13 05:12:20 -0500245 response = os.system("ping -c 1 %s 2>&1 >/dev/null" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500246 if response == 0:
George Keishinge635ddc2022-12-08 07:38:02 -0600247 self.logger.info("\n\t[Check] %s is ping-able.\t\t [OK]" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500248 return True
249 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500250 self.logger.error(
George Keishinge635ddc2022-12-08 07:38:02 -0600251 "\n\tERROR: %s is not ping-able. FFDC collection aborted.\n" % self.hostname)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500252 sys.exit(-1)
253
Peter D Phan72ce6b82021-06-03 06:18:26 -0500254 def collect_ffdc(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500255 r"""
256 Initiate FFDC Collection depending on requested protocol.
257
258 """
259
George Keishinge635ddc2022-12-08 07:38:02 -0600260 self.logger.info("\n\t---- Start communicating with %s ----" % self.hostname)
Peter D Phan7610bc42021-07-06 06:31:05 -0500261 self.start_time = time.time()
Peter D Phan0c669772021-06-24 13:52:42 -0500262
George Keishingf5a57502021-07-22 16:43:47 -0500263 # Find the list of target and protocol supported.
264 check_protocol_list = []
265 config_dict = self.ffdc_actions
Peter D Phan0c669772021-06-24 13:52:42 -0500266
George Keishingf5a57502021-07-22 16:43:47 -0500267 for target_type in config_dict.keys():
268 if self.target_type != target_type:
269 continue
George Keishingeafba182021-06-29 13:44:58 -0500270
George Keishingf5a57502021-07-22 16:43:47 -0500271 for k, v in config_dict[target_type].items():
George Keishinge635ddc2022-12-08 07:38:02 -0600272 if config_dict[target_type][k]['PROTOCOL'][0] not in check_protocol_list:
273 check_protocol_list.append(config_dict[target_type][k]['PROTOCOL'][0])
Peter D Phanbff617a2021-07-22 08:41:35 -0500274
George Keishinge635ddc2022-12-08 07:38:02 -0600275 self.logger.info("\n\t %s protocol type: %s" % (self.target_type, check_protocol_list))
Peter D Phanbff617a2021-07-22 08:41:35 -0500276
George Keishingf5a57502021-07-22 16:43:47 -0500277 verified_working_protocol = self.verify_protocol(check_protocol_list)
Peter D Phanbff617a2021-07-22 08:41:35 -0500278
George Keishingf5a57502021-07-22 16:43:47 -0500279 if verified_working_protocol:
George Keishinge635ddc2022-12-08 07:38:02 -0600280 self.logger.info("\n\t---- Completed protocol pre-requisite check ----\n")
Peter D Phan0c669772021-06-24 13:52:42 -0500281
George Keishingf5a57502021-07-22 16:43:47 -0500282 # Verify top level directory exists for storage
283 self.validate_local_store(self.location)
284
George Keishinge635ddc2022-12-08 07:38:02 -0600285 if ((self.remote_protocol not in verified_working_protocol) and (self.remote_protocol != 'ALL')):
286 self.logger.info("\n\tWorking protocol list: %s" % verified_working_protocol)
George Keishingf5a57502021-07-22 16:43:47 -0500287 self.logger.error(
George Keishinge635ddc2022-12-08 07:38:02 -0600288 '\tERROR: Requested protocol %s is not in working protocol list.\n'
289 % self.remote_protocol)
George Keishingf5a57502021-07-22 16:43:47 -0500290 sys.exit(-1)
291 else:
292 self.generate_ffdc(verified_working_protocol)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500293
294 def ssh_to_target_system(self):
295 r"""
296 Open a ssh connection to targeted system.
297
298 """
299
George Keishinge635ddc2022-12-08 07:38:02 -0600300 self.ssh_remoteclient = SSHRemoteclient(self.hostname,
301 self.username,
302 self.password)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500303
Peter D Phan5963d632021-07-12 09:58:55 -0500304 if self.ssh_remoteclient.ssh_remoteclient_login():
George Keishinge635ddc2022-12-08 07:38:02 -0600305 self.logger.info("\n\t[Check] %s SSH connection established.\t [OK]" % self.hostname)
Peter D Phan733df632021-06-17 13:13:36 -0500306
Peter D Phan5963d632021-07-12 09:58:55 -0500307 # Check scp connection.
308 # If scp connection fails,
309 # continue with FFDC generation but skip scp files to local host.
310 self.ssh_remoteclient.scp_connection()
311 return True
312 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600313 self.logger.info("\n\t[Check] %s SSH connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500314 return False
315
316 def telnet_to_target_system(self):
317 r"""
318 Open a telnet connection to targeted system.
319 """
George Keishinge635ddc2022-12-08 07:38:02 -0600320 self.telnet_remoteclient = TelnetRemoteclient(self.hostname,
321 self.username,
322 self.password)
Peter D Phan5963d632021-07-12 09:58:55 -0500323 if self.telnet_remoteclient.tn_remoteclient_login():
George Keishinge635ddc2022-12-08 07:38:02 -0600324 self.logger.info("\n\t[Check] %s Telnet connection established.\t [OK]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500325 return True
326 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600327 self.logger.info("\n\t[Check] %s Telnet connection.\t [NOT AVAILABLE]" % self.hostname)
Peter D Phan5963d632021-07-12 09:58:55 -0500328 return False
Peter D Phan72ce6b82021-06-03 06:18:26 -0500329
George Keishing772c9772021-06-16 23:23:42 -0500330 def generate_ffdc(self, working_protocol_list):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500331 r"""
Peter D Phan04aca3b2021-06-21 10:37:18 -0500332 Determine actions based on remote host type
Peter D Phan72ce6b82021-06-03 06:18:26 -0500333
Peter D Phan04aca3b2021-06-21 10:37:18 -0500334 Description of argument(s):
335 working_protocol_list list of confirmed working protocols to connect to remote host.
Peter D Phan72ce6b82021-06-03 06:18:26 -0500336 """
337
George Keishinge635ddc2022-12-08 07:38:02 -0600338 self.logger.info("\n\t---- Executing commands on " + self.hostname + " ----")
339 self.logger.info("\n\tWorking protocol list: %s" % working_protocol_list)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500340
George Keishingf5a57502021-07-22 16:43:47 -0500341 config_dict = self.ffdc_actions
342 for target_type in config_dict.keys():
343 if self.target_type != target_type:
George Keishing6ea92b02021-07-01 11:20:50 -0500344 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500345
Peter D Phane86d9a52021-07-15 10:42:25 -0500346 self.logger.info("\n\tFFDC Path: %s " % self.ffdc_dir_path)
George Keishinge635ddc2022-12-08 07:38:02 -0600347 global_plugin_dict['global_log_store_path'] = self.ffdc_dir_path
George Keishingf5a57502021-07-22 16:43:47 -0500348 self.logger.info("\tSystem Type: %s" % target_type)
349 for k, v in config_dict[target_type].items():
George Keishinge635ddc2022-12-08 07:38:02 -0600350
351 if self.remote_protocol not in working_protocol_list \
352 and self.remote_protocol != 'ALL':
George Keishing6ea92b02021-07-01 11:20:50 -0500353 continue
Peter D Phan72ce6b82021-06-03 06:18:26 -0500354
George Keishinge635ddc2022-12-08 07:38:02 -0600355 protocol = config_dict[target_type][k]['PROTOCOL'][0]
George Keishingf5a57502021-07-22 16:43:47 -0500356
357 if protocol not in working_protocol_list:
358 continue
359
George Keishingb7607612021-07-27 13:31:23 -0500360 if protocol in working_protocol_list:
George Keishinge635ddc2022-12-08 07:38:02 -0600361 if protocol == 'SSH' or protocol == 'SCP':
George Keishing12fd0652021-07-27 13:57:11 -0500362 self.protocol_ssh(protocol, target_type, k)
George Keishinge635ddc2022-12-08 07:38:02 -0600363 elif protocol == 'TELNET':
George Keishingf5a57502021-07-22 16:43:47 -0500364 self.protocol_telnet(target_type, k)
George Keishinge635ddc2022-12-08 07:38:02 -0600365 elif protocol == 'REDFISH' or protocol == 'IPMI' or protocol == 'SHELL':
George Keishing506b0582021-07-27 09:31:22 -0500366 self.protocol_execute(protocol, target_type, k)
George Keishingb7607612021-07-27 13:31:23 -0500367 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600368 self.logger.error("\n\tERROR: %s is not available for %s." % (protocol, self.hostname))
George Keishingeafba182021-06-29 13:44:58 -0500369
Peter D Phan04aca3b2021-06-21 10:37:18 -0500370 # Close network connection after collecting all files
George Keishinge635ddc2022-12-08 07:38:02 -0600371 self.elapsed_time = time.strftime("%H:%M:%S", time.gmtime(time.time() - self.start_time))
Peter D Phanbff617a2021-07-22 08:41:35 -0500372 if self.ssh_remoteclient:
373 self.ssh_remoteclient.ssh_remoteclient_disconnect()
374 if self.telnet_remoteclient:
375 self.telnet_remoteclient.tn_remoteclient_disconnect()
Peter D Phan04aca3b2021-06-21 10:37:18 -0500376
George Keishinge635ddc2022-12-08 07:38:02 -0600377 def protocol_ssh(self,
378 protocol,
379 target_type,
380 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500381 r"""
382 Perform actions using SSH and SCP protocols.
383
384 Description of argument(s):
George Keishing12fd0652021-07-27 13:57:11 -0500385 protocol Protocol to execute.
George Keishingf5a57502021-07-22 16:43:47 -0500386 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500387 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500388 """
389
George Keishinge635ddc2022-12-08 07:38:02 -0600390 if protocol == 'SCP':
George Keishingf5a57502021-07-22 16:43:47 -0500391 self.group_copy(self.ffdc_actions[target_type][sub_type])
George Keishing6ea92b02021-07-01 11:20:50 -0500392 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600393 self.collect_and_copy_ffdc(self.ffdc_actions[target_type][sub_type])
Peter D Phan0c669772021-06-24 13:52:42 -0500394
George Keishinge635ddc2022-12-08 07:38:02 -0600395 def protocol_telnet(self,
396 target_type,
397 sub_type):
Peter D Phan5963d632021-07-12 09:58:55 -0500398 r"""
399 Perform actions using telnet protocol.
400 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500401 target_type OS Type of remote host.
Peter D Phan5963d632021-07-12 09:58:55 -0500402 """
George Keishinge635ddc2022-12-08 07:38:02 -0600403 self.logger.info("\n\t[Run] Executing commands on %s using %s" % (self.hostname, 'TELNET'))
Peter D Phan5963d632021-07-12 09:58:55 -0500404 telnet_files_saved = []
405 progress_counter = 0
George Keishinge635ddc2022-12-08 07:38:02 -0600406 list_of_commands = self.ffdc_actions[target_type][sub_type]['COMMANDS']
Peter D Phan5963d632021-07-12 09:58:55 -0500407 for index, each_cmd in enumerate(list_of_commands, start=0):
408 command_txt, command_timeout = self.unpack_command(each_cmd)
George Keishinge635ddc2022-12-08 07:38:02 -0600409 result = self.telnet_remoteclient.execute_command(command_txt, command_timeout)
Peter D Phan5963d632021-07-12 09:58:55 -0500410 if result:
411 try:
George Keishinge635ddc2022-12-08 07:38:02 -0600412 targ_file = self.ffdc_actions[target_type][sub_type]['FILES'][index]
Peter D Phan5963d632021-07-12 09:58:55 -0500413 except IndexError:
Peter D Phane86d9a52021-07-15 10:42:25 -0500414 targ_file = command_txt
415 self.logger.warning(
George Keishinge635ddc2022-12-08 07:38:02 -0600416 "\n\t[WARN] Missing filename to store data from telnet %s." % each_cmd)
417 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
418 targ_file_with_path = (self.ffdc_dir_path
419 + self.ffdc_prefix
420 + targ_file)
Peter D Phan5963d632021-07-12 09:58:55 -0500421 # Creates a new file
George Keishinge635ddc2022-12-08 07:38:02 -0600422 with open(targ_file_with_path, 'w') as fp:
Peter D Phan5963d632021-07-12 09:58:55 -0500423 fp.write(result)
424 fp.close
425 telnet_files_saved.append(targ_file)
426 progress_counter += 1
427 self.print_progress(progress_counter)
Peter D Phane86d9a52021-07-15 10:42:25 -0500428 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan5963d632021-07-12 09:58:55 -0500429 for file in telnet_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500430 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
Peter D Phan5963d632021-07-12 09:58:55 -0500431
George Keishinge635ddc2022-12-08 07:38:02 -0600432 def protocol_execute(self,
433 protocol,
434 target_type,
435 sub_type):
Peter D Phan0c669772021-06-24 13:52:42 -0500436 r"""
George Keishing506b0582021-07-27 09:31:22 -0500437 Perform actions for a given protocol.
Peter D Phan0c669772021-06-24 13:52:42 -0500438
439 Description of argument(s):
George Keishing506b0582021-07-27 09:31:22 -0500440 protocol Protocol to execute.
George Keishingf5a57502021-07-22 16:43:47 -0500441 target_type OS Type of remote host.
George Keishing6ea92b02021-07-01 11:20:50 -0500442 sub_type Group type of commands.
Peter D Phan0c669772021-06-24 13:52:42 -0500443 """
444
George Keishinge635ddc2022-12-08 07:38:02 -0600445 self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, protocol))
George Keishing506b0582021-07-27 09:31:22 -0500446 executed_files_saved = []
George Keishingeafba182021-06-29 13:44:58 -0500447 progress_counter = 0
George Keishinge635ddc2022-12-08 07:38:02 -0600448 list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type])
George Keishingeafba182021-06-29 13:44:58 -0500449 for index, each_cmd in enumerate(list_of_cmd, start=0):
George Keishingcaa97e62021-08-03 14:00:09 -0500450 plugin_call = False
George Keishingb97a9042021-07-29 07:41:20 -0500451 if isinstance(each_cmd, dict):
George Keishinge635ddc2022-12-08 07:38:02 -0600452 if 'plugin' in each_cmd:
George Keishing1e7b0182021-08-06 14:05:54 -0500453 # If the error is set and plugin explicitly
454 # requested to skip execution on error..
George Keishinge635ddc2022-12-08 07:38:02 -0600455 if plugin_error_dict['exit_on_error'] and \
456 self.plugin_error_check(each_cmd['plugin']):
457 self.logger.info("\n\t[PLUGIN-ERROR] exit_on_error: %s" %
458 plugin_error_dict['exit_on_error'])
459 self.logger.info("\t[PLUGIN-SKIP] %s" %
460 each_cmd['plugin'][0])
George Keishing1e7b0182021-08-06 14:05:54 -0500461 continue
George Keishingcaa97e62021-08-03 14:00:09 -0500462 plugin_call = True
George Keishingb97a9042021-07-29 07:41:20 -0500463 # call the plugin
464 self.logger.info("\n\t[PLUGIN-START]")
George Keishinge635ddc2022-12-08 07:38:02 -0600465 result = self.execute_plugin_block(each_cmd['plugin'])
George Keishingb97a9042021-07-29 07:41:20 -0500466 self.logger.info("\t[PLUGIN-END]\n")
George Keishingb97a9042021-07-29 07:41:20 -0500467 else:
George Keishing2b83e042021-08-03 12:56:11 -0500468 each_cmd = self.yaml_env_and_plugin_vars_populate(each_cmd)
George Keishingb97a9042021-07-29 07:41:20 -0500469
George Keishingcaa97e62021-08-03 14:00:09 -0500470 if not plugin_call:
471 result = self.run_tool_cmd(each_cmd)
George Keishingeafba182021-06-29 13:44:58 -0500472 if result:
473 try:
George Keishinge635ddc2022-12-08 07:38:02 -0600474 file_name = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
George Keishingb97a9042021-07-29 07:41:20 -0500475 # If file is specified as None.
George Keishing0581cb02021-08-05 15:08:58 -0500476 if file_name == "None":
George Keishingb97a9042021-07-29 07:41:20 -0500477 continue
George Keishinge635ddc2022-12-08 07:38:02 -0600478 targ_file = self.yaml_env_and_plugin_vars_populate(file_name)
George Keishingeafba182021-06-29 13:44:58 -0500479 except IndexError:
George Keishinge635ddc2022-12-08 07:38:02 -0600480 targ_file = each_cmd.split('/')[-1]
George Keishing506b0582021-07-27 09:31:22 -0500481 self.logger.warning(
George Keishinge635ddc2022-12-08 07:38:02 -0600482 "\n\t[WARN] Missing filename to store data from %s." % each_cmd)
483 self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500484
George Keishinge635ddc2022-12-08 07:38:02 -0600485 targ_file_with_path = (self.ffdc_dir_path
486 + self.ffdc_prefix
487 + targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500488
489 # Creates a new file
George Keishinge635ddc2022-12-08 07:38:02 -0600490 with open(targ_file_with_path, 'w') as fp:
George Keishing91308ea2021-08-10 14:43:15 -0500491 if isinstance(result, dict):
492 fp.write(json.dumps(result))
493 else:
494 fp.write(result)
George Keishingeafba182021-06-29 13:44:58 -0500495 fp.close
George Keishing506b0582021-07-27 09:31:22 -0500496 executed_files_saved.append(targ_file)
George Keishingeafba182021-06-29 13:44:58 -0500497
498 progress_counter += 1
499 self.print_progress(progress_counter)
500
Peter D Phane86d9a52021-07-15 10:42:25 -0500501 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
George Keishingeafba182021-06-29 13:44:58 -0500502
George Keishing506b0582021-07-27 09:31:22 -0500503 for file in executed_files_saved:
Peter D Phane86d9a52021-07-15 10:42:25 -0500504 self.logger.info("\n\t\tSuccessfully save file " + file + ".")
George Keishingeafba182021-06-29 13:44:58 -0500505
George Keishinge635ddc2022-12-08 07:38:02 -0600506 def collect_and_copy_ffdc(self,
507 ffdc_actions_for_target_type,
508 form_filename=False):
Peter D Phan04aca3b2021-06-21 10:37:18 -0500509 r"""
510 Send commands in ffdc_config file to targeted system.
511
512 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500513 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan2b8052d2021-06-22 10:55:41 -0500514 form_filename if true, pre-pend self.target_type to filename
Peter D Phan04aca3b2021-06-21 10:37:18 -0500515 """
516
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500517 # Executing commands, if any
George Keishinge635ddc2022-12-08 07:38:02 -0600518 self.ssh_execute_ffdc_commands(ffdc_actions_for_target_type,
519 form_filename)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500520
Peter D Phan3beb02e2021-07-06 13:25:17 -0500521 # Copying files
Peter D Phan5963d632021-07-12 09:58:55 -0500522 if self.ssh_remoteclient.scpclient:
George Keishinge635ddc2022-12-08 07:38:02 -0600523 self.logger.info("\n\n\tCopying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan2b8052d2021-06-22 10:55:41 -0500524
Peter D Phan04aca3b2021-06-21 10:37:18 -0500525 # Retrieving files from target system
George Keishingf5a57502021-07-22 16:43:47 -0500526 list_of_files = self.get_file_list(ffdc_actions_for_target_type)
George Keishinge635ddc2022-12-08 07:38:02 -0600527 self.scp_ffdc(self.ffdc_dir_path, self.ffdc_prefix, form_filename, list_of_files)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500528 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600529 self.logger.info("\n\n\tSkip copying FFDC files from remote system %s.\n" % self.hostname)
Peter D Phan04aca3b2021-06-21 10:37:18 -0500530
George Keishinge635ddc2022-12-08 07:38:02 -0600531 def get_command_list(self,
532 ffdc_actions_for_target_type):
Peter D Phanbabf2962021-07-07 11:24:40 -0500533 r"""
534 Fetch list of commands from configuration file
535
536 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500537 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phanbabf2962021-07-07 11:24:40 -0500538 """
539 try:
George Keishinge635ddc2022-12-08 07:38:02 -0600540 list_of_commands = ffdc_actions_for_target_type['COMMANDS']
Peter D Phanbabf2962021-07-07 11:24:40 -0500541 except KeyError:
542 list_of_commands = []
543 return list_of_commands
544
George Keishinge635ddc2022-12-08 07:38:02 -0600545 def get_file_list(self,
546 ffdc_actions_for_target_type):
Peter D Phanbabf2962021-07-07 11:24:40 -0500547 r"""
548 Fetch list of commands from configuration file
549
550 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500551 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phanbabf2962021-07-07 11:24:40 -0500552 """
553 try:
George Keishinge635ddc2022-12-08 07:38:02 -0600554 list_of_files = ffdc_actions_for_target_type['FILES']
Peter D Phanbabf2962021-07-07 11:24:40 -0500555 except KeyError:
556 list_of_files = []
557 return list_of_files
558
George Keishinge635ddc2022-12-08 07:38:02 -0600559 def unpack_command(self,
560 command):
Peter D Phan5963d632021-07-12 09:58:55 -0500561 r"""
562 Unpack command from config file
563
564 Description of argument(s):
565 command Command from config file.
566 """
567 if isinstance(command, dict):
568 command_txt = next(iter(command))
569 command_timeout = next(iter(command.values()))
570 elif isinstance(command, str):
571 command_txt = command
572 # Default command timeout 60 seconds
573 command_timeout = 60
574
575 return command_txt, command_timeout
576
George Keishinge635ddc2022-12-08 07:38:02 -0600577 def ssh_execute_ffdc_commands(self,
578 ffdc_actions_for_target_type,
579 form_filename=False):
Peter D Phan3beb02e2021-07-06 13:25:17 -0500580 r"""
581 Send commands in ffdc_config file to targeted system.
582
583 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500584 ffdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan3beb02e2021-07-06 13:25:17 -0500585 form_filename if true, pre-pend self.target_type to filename
586 """
George Keishinge635ddc2022-12-08 07:38:02 -0600587 self.logger.info("\n\t[Run] Executing commands on %s using %s"
588 % (self.hostname, ffdc_actions_for_target_type['PROTOCOL'][0]))
Peter D Phan3beb02e2021-07-06 13:25:17 -0500589
George Keishingf5a57502021-07-22 16:43:47 -0500590 list_of_commands = self.get_command_list(ffdc_actions_for_target_type)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500591 # If command list is empty, returns
592 if not list_of_commands:
593 return
594
595 progress_counter = 0
596 for command in list_of_commands:
Peter D Phan5963d632021-07-12 09:58:55 -0500597 command_txt, command_timeout = self.unpack_command(command)
Peter D Phan3beb02e2021-07-06 13:25:17 -0500598
599 if form_filename:
600 command_txt = str(command_txt % self.target_type)
601
George Keishinge635ddc2022-12-08 07:38:02 -0600602 cmd_exit_code, err, response = \
603 self.ssh_remoteclient.execute_command(command_txt, command_timeout)
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500604
605 if cmd_exit_code:
606 self.logger.warning(
George Keishinge635ddc2022-12-08 07:38:02 -0600607 "\n\t\t[WARN] %s exits with code %s." % (command_txt, str(cmd_exit_code)))
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500608 self.logger.warning("\t\t[WARN] %s " % err)
Peter D Phanbabf2962021-07-07 11:24:40 -0500609
Peter D Phan3beb02e2021-07-06 13:25:17 -0500610 progress_counter += 1
611 self.print_progress(progress_counter)
612
Peter D Phane86d9a52021-07-15 10:42:25 -0500613 self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
Peter D Phan3beb02e2021-07-06 13:25:17 -0500614
George Keishinge635ddc2022-12-08 07:38:02 -0600615 def group_copy(self,
616 ffdc_actions_for_target_type):
Peter D Phan56429a62021-06-23 08:38:29 -0500617 r"""
618 scp group of files (wild card) from remote host.
619
620 Description of argument(s):
George Keishingf5a57502021-07-22 16:43:47 -0500621 fdc_actions_for_target_type commands and files for the selected remote host type.
Peter D Phan56429a62021-06-23 08:38:29 -0500622 """
Peter D Phan3beb02e2021-07-06 13:25:17 -0500623
Peter D Phan5963d632021-07-12 09:58:55 -0500624 if self.ssh_remoteclient.scpclient:
George Keishinge635ddc2022-12-08 07:38:02 -0600625 self.logger.info("\n\tCopying files from remote system %s via SCP.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500626
George Keishinge635ddc2022-12-08 07:38:02 -0600627 list_of_commands = self.get_command_list(ffdc_actions_for_target_type)
Peter D Phanbabf2962021-07-07 11:24:40 -0500628 # If command list is empty, returns
629 if not list_of_commands:
630 return
Peter D Phan56429a62021-06-23 08:38:29 -0500631
Peter D Phanbabf2962021-07-07 11:24:40 -0500632 for command in list_of_commands:
633 try:
George Keishingb4540e72021-08-02 13:48:46 -0500634 command = self.yaml_env_and_plugin_vars_populate(command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500635 except IndexError:
George Keishingb4540e72021-08-02 13:48:46 -0500636 self.logger.error("\t\tInvalid command %s" % command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500637 continue
638
George Keishinge635ddc2022-12-08 07:38:02 -0600639 cmd_exit_code, err, response = \
640 self.ssh_remoteclient.execute_command(command)
Peter D Phanbabf2962021-07-07 11:24:40 -0500641
Peter D Phan2b6cb3a2021-07-19 06:55:42 -0500642 # If file does not exist, code take no action.
643 # cmd_exit_code is ignored for this scenario.
Peter D Phan56429a62021-06-23 08:38:29 -0500644 if response:
George Keishinge635ddc2022-12-08 07:38:02 -0600645 scp_result = \
646 self.ssh_remoteclient.scp_file_from_remote(response.split('\n'),
647 self.ffdc_dir_path)
Peter D Phan56429a62021-06-23 08:38:29 -0500648 if scp_result:
George Keishinge635ddc2022-12-08 07:38:02 -0600649 self.logger.info("\t\tSuccessfully copied from " + self.hostname + ':' + command)
Peter D Phan56429a62021-06-23 08:38:29 -0500650 else:
George Keishinga56e87b2021-08-06 00:24:19 -0500651 self.logger.info("\t\t%s has no result" % command)
Peter D Phan56429a62021-06-23 08:38:29 -0500652
653 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600654 self.logger.info("\n\n\tSkip copying files from remote system %s.\n" % self.hostname)
Peter D Phan56429a62021-06-23 08:38:29 -0500655
George Keishinge635ddc2022-12-08 07:38:02 -0600656 def scp_ffdc(self,
657 targ_dir_path,
658 targ_file_prefix,
659 form_filename,
660 file_list=None,
661 quiet=None):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500662 r"""
663 SCP all files in file_dict to the indicated directory on the local system.
664
665 Description of argument(s):
666 targ_dir_path The path of the directory to receive the files.
667 targ_file_prefix Prefix which will be pre-pended to each
668 target file's name.
669 file_dict A dictionary of files to scp from targeted system to this system
670
671 """
672
Peter D Phan72ce6b82021-06-03 06:18:26 -0500673 progress_counter = 0
674 for filename in file_list:
Peter D Phan2b8052d2021-06-22 10:55:41 -0500675 if form_filename:
676 filename = str(filename % self.target_type)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500677 source_file_path = filename
George Keishinge635ddc2022-12-08 07:38:02 -0600678 targ_file_path = targ_dir_path + targ_file_prefix + filename.split('/')[-1]
Peter D Phan72ce6b82021-06-03 06:18:26 -0500679
Peter D Phanbabf2962021-07-07 11:24:40 -0500680 # If source file name contains wild card, copy filename as is.
George Keishinge635ddc2022-12-08 07:38:02 -0600681 if '*' in source_file_path:
682 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, self.ffdc_dir_path)
Peter D Phanbabf2962021-07-07 11:24:40 -0500683 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600684 scp_result = self.ssh_remoteclient.scp_file_from_remote(source_file_path, targ_file_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500685
686 if not quiet:
687 if scp_result:
Peter D Phane86d9a52021-07-15 10:42:25 -0500688 self.logger.info(
George Keishinge635ddc2022-12-08 07:38:02 -0600689 "\t\tSuccessfully copied from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500690 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500691 self.logger.info(
George Keishinge635ddc2022-12-08 07:38:02 -0600692 "\t\tFail to copy from " + self.hostname + ':' + source_file_path + ".\n")
Peter D Phan72ce6b82021-06-03 06:18:26 -0500693 else:
694 progress_counter += 1
695 self.print_progress(progress_counter)
696
Peter D Phan5e56f522021-12-20 13:19:41 -0600697 def set_ffdc_default_store_path(self):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500698 r"""
699 Set a default value for self.ffdc_dir_path and self.ffdc_prefix.
700 Collected ffdc file will be stored in dir /self.location/hostname_timestr/.
701 Individual ffdc file will have timestr_filename.
702
703 Description of class variables:
704 self.ffdc_dir_path The dir path where collected ffdc data files should be put.
705
706 self.ffdc_prefix The prefix to be given to each ffdc file name.
707
708 """
709
710 timestr = time.strftime("%Y%m%d-%H%M%S")
George Keishinge635ddc2022-12-08 07:38:02 -0600711 self.ffdc_dir_path = self.location + "/" + self.hostname + "_" + timestr + "/"
Peter D Phan72ce6b82021-06-03 06:18:26 -0500712 self.ffdc_prefix = timestr + "_"
713 self.validate_local_store(self.ffdc_dir_path)
714
Peter D Phan5e56f522021-12-20 13:19:41 -0600715 # Need to verify local store path exists prior to instantiate this class.
716 # This class method is used to share the same code between CLI input parm
717 # and Robot Framework "${EXECDIR}/logs" before referencing this class.
718 @classmethod
719 def validate_local_store(cls, dir_path):
Peter D Phan72ce6b82021-06-03 06:18:26 -0500720 r"""
721 Ensure path exists to store FFDC files locally.
722
723 Description of variable:
724 dir_path The dir path where collected ffdc data files will be stored.
725
726 """
727
728 if not os.path.exists(dir_path):
729 try:
George Keishing7b3a5132021-07-13 09:24:02 -0500730 os.makedirs(dir_path, 0o755)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500731 except (IOError, OSError) as e:
732 # PermissionError
733 if e.errno == EPERM or e.errno == EACCES:
Peter D Phane86d9a52021-07-15 10:42:25 -0500734 self.logger.error(
George Keishinge635ddc2022-12-08 07:38:02 -0600735 '\tERROR: os.makedirs %s failed with PermissionError.\n' % dir_path)
Peter D Phan72ce6b82021-06-03 06:18:26 -0500736 else:
Peter D Phane86d9a52021-07-15 10:42:25 -0500737 self.logger.error(
George Keishinge635ddc2022-12-08 07:38:02 -0600738 '\tERROR: os.makedirs %s failed with %s.\n' % (dir_path, e.strerror))
Peter D Phan72ce6b82021-06-03 06:18:26 -0500739 sys.exit(-1)
740
741 def print_progress(self, progress):
742 r"""
743 Print activity progress +
744
745 Description of variable:
746 progress Progress counter.
747
748 """
749
750 sys.stdout.write("\r\t" + "+" * progress)
751 sys.stdout.flush()
George Keishinge635ddc2022-12-08 07:38:02 -0600752 time.sleep(.1)
Peter D Phan0c669772021-06-24 13:52:42 -0500753
754 def verify_redfish(self):
755 r"""
756 Verify remote host has redfish service active
757
758 """
George Keishinge635ddc2022-12-08 07:38:02 -0600759 redfish_parm = 'redfishtool -r ' \
760 + self.hostname + ' -S Always raw GET /redfish/v1/'
761 return (self.run_tool_cmd(redfish_parm, True))
Peter D Phan0c669772021-06-24 13:52:42 -0500762
George Keishingeafba182021-06-29 13:44:58 -0500763 def verify_ipmi(self):
764 r"""
765 Verify remote host has IPMI LAN service active
766
767 """
George Keishinge635ddc2022-12-08 07:38:02 -0600768 if self.target_type == 'OPENBMC':
769 ipmi_parm = 'ipmitool -I lanplus -C 17 -U ' + self.username + ' -P ' \
770 + self.password + ' -H ' + self.hostname + ' power status'
George Keishing484f8242021-07-27 01:42:02 -0500771 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600772 ipmi_parm = 'ipmitool -I lanplus -P ' \
773 + self.password + ' -H ' + self.hostname + ' power status'
George Keishing484f8242021-07-27 01:42:02 -0500774
George Keishinge635ddc2022-12-08 07:38:02 -0600775 return (self.run_tool_cmd(ipmi_parm, True))
George Keishingeafba182021-06-29 13:44:58 -0500776
George Keishinge635ddc2022-12-08 07:38:02 -0600777 def run_tool_cmd(self,
778 parms_string,
779 quiet=False):
George Keishingeafba182021-06-29 13:44:58 -0500780 r"""
George Keishing506b0582021-07-27 09:31:22 -0500781 Run CLI standard tool or scripts.
George Keishingeafba182021-06-29 13:44:58 -0500782
783 Description of variable:
George Keishing506b0582021-07-27 09:31:22 -0500784 parms_string tool command options.
785 quiet do not print tool error message if True
George Keishingeafba182021-06-29 13:44:58 -0500786 """
787
George Keishinge635ddc2022-12-08 07:38:02 -0600788 result = subprocess.run([parms_string],
789 stdout=subprocess.PIPE,
790 stderr=subprocess.PIPE,
791 shell=True,
792 universal_newlines=True)
George Keishingeafba182021-06-29 13:44:58 -0500793
794 if result.stderr and not quiet:
George Keishinge635ddc2022-12-08 07:38:02 -0600795 self.logger.error('\n\t\tERROR with %s ' % parms_string)
796 self.logger.error('\t\t' + result.stderr)
George Keishingeafba182021-06-29 13:44:58 -0500797
798 return result.stdout
George Keishing04d29102021-07-16 02:05:57 -0500799
George Keishingf5a57502021-07-22 16:43:47 -0500800 def verify_protocol(self, protocol_list):
801 r"""
802 Perform protocol working check.
803
804 Description of argument(s):
805 protocol_list List of protocol.
806 """
807
808 tmp_list = []
809 if self.target_is_pingable():
810 tmp_list.append("SHELL")
811
812 for protocol in protocol_list:
George Keishinge635ddc2022-12-08 07:38:02 -0600813 if self.remote_protocol != 'ALL':
George Keishingf5a57502021-07-22 16:43:47 -0500814 if self.remote_protocol != protocol:
815 continue
816
817 # Only check SSH/SCP once for both protocols
George Keishinge635ddc2022-12-08 07:38:02 -0600818 if protocol == 'SSH' or protocol == 'SCP' and protocol not in tmp_list:
George Keishingf5a57502021-07-22 16:43:47 -0500819 if self.ssh_to_target_system():
George Keishingaa638702021-07-26 11:48:28 -0500820 # Add only what user asked.
George Keishinge635ddc2022-12-08 07:38:02 -0600821 if self.remote_protocol != 'ALL':
George Keishingaa638702021-07-26 11:48:28 -0500822 tmp_list.append(self.remote_protocol)
823 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600824 tmp_list.append('SSH')
825 tmp_list.append('SCP')
George Keishingf5a57502021-07-22 16:43:47 -0500826
George Keishinge635ddc2022-12-08 07:38:02 -0600827 if protocol == 'TELNET':
George Keishingf5a57502021-07-22 16:43:47 -0500828 if self.telnet_to_target_system():
829 tmp_list.append(protocol)
830
George Keishinge635ddc2022-12-08 07:38:02 -0600831 if protocol == 'REDFISH':
George Keishingf5a57502021-07-22 16:43:47 -0500832 if self.verify_redfish():
833 tmp_list.append(protocol)
George Keishinge635ddc2022-12-08 07:38:02 -0600834 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [OK]" % self.hostname)
George Keishingf5a57502021-07-22 16:43:47 -0500835 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600836 self.logger.info("\n\t[Check] %s Redfish Service.\t\t [NOT AVAILABLE]" % self.hostname)
George Keishingf5a57502021-07-22 16:43:47 -0500837
George Keishinge635ddc2022-12-08 07:38:02 -0600838 if protocol == 'IPMI':
George Keishingf5a57502021-07-22 16:43:47 -0500839 if self.verify_ipmi():
840 tmp_list.append(protocol)
George Keishinge635ddc2022-12-08 07:38:02 -0600841 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [OK]" % self.hostname)
George Keishingf5a57502021-07-22 16:43:47 -0500842 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600843 self.logger.info("\n\t[Check] %s IPMI LAN Service.\t\t [NOT AVAILABLE]" % self.hostname)
George Keishingf5a57502021-07-22 16:43:47 -0500844
845 return tmp_list
George Keishinge1686752021-07-27 12:55:28 -0500846
847 def load_env(self):
848 r"""
849 Perform protocol working check.
850
851 """
852 # This is for the env vars a user can use in YAML to load it at runtime.
853 # Example YAML:
854 # -COMMANDS:
855 # - my_command ${hostname} ${username} ${password}
George Keishinge635ddc2022-12-08 07:38:02 -0600856 os.environ['hostname'] = self.hostname
857 os.environ['username'] = self.username
858 os.environ['password'] = self.password
George Keishinge1686752021-07-27 12:55:28 -0500859
860 # Append default Env.
George Keishinge635ddc2022-12-08 07:38:02 -0600861 self.env_dict['hostname'] = self.hostname
862 self.env_dict['username'] = self.username
863 self.env_dict['password'] = self.password
George Keishinge1686752021-07-27 12:55:28 -0500864
865 try:
866 tmp_env_dict = {}
867 if self.env_vars:
868 tmp_env_dict = json.loads(self.env_vars)
869 # Export ENV vars default.
870 for key, value in tmp_env_dict.items():
871 os.environ[key] = value
872 self.env_dict[key] = str(value)
873
874 if self.econfig:
George Keishinge635ddc2022-12-08 07:38:02 -0600875 with open(self.econfig, 'r') as file:
George Keishinge9b23d32021-08-13 12:57:58 -0500876 try:
Yunyun Linf87cc0a2022-06-08 16:57:04 -0700877 tmp_env_dict = yaml.load(file, Loader=yaml.SafeLoader)
George Keishinge9b23d32021-08-13 12:57:58 -0500878 except yaml.YAMLError as e:
879 self.logger.error(e)
880 sys.exit(-1)
George Keishinge1686752021-07-27 12:55:28 -0500881 # Export ENV vars.
George Keishinge635ddc2022-12-08 07:38:02 -0600882 for key, value in tmp_env_dict['env_params'].items():
George Keishinge1686752021-07-27 12:55:28 -0500883 os.environ[key] = str(value)
884 self.env_dict[key] = str(value)
885 except json.decoder.JSONDecodeError as e:
886 self.logger.error("\n\tERROR: %s " % e)
887 sys.exit(-1)
888
889 # This to mask the password from displaying on the console.
890 mask_dict = self.env_dict.copy()
891 for k, v in mask_dict.items():
892 if k.lower().find("password") != -1:
893 hidden_text = []
894 hidden_text.append(v)
George Keishinge635ddc2022-12-08 07:38:02 -0600895 password_regex = '(' +\
896 '|'.join([re.escape(x) for x in hidden_text]) + ')'
George Keishinge1686752021-07-27 12:55:28 -0500897 mask_dict[k] = re.sub(password_regex, "********", v)
898
899 self.logger.info(json.dumps(mask_dict, indent=8, sort_keys=False))
George Keishingb97a9042021-07-29 07:41:20 -0500900
901 def execute_python_eval(self, eval_string):
902 r"""
George Keishing9348b402021-08-13 12:22:35 -0500903 Execute qualified python function string using eval.
George Keishingb97a9042021-07-29 07:41:20 -0500904
905 Description of argument(s):
906 eval_string Execute the python object.
907
908 Example:
909 eval(plugin.foo_func.foo_func(10))
910 """
911 try:
George Keishingdda48ce2021-08-12 07:02:27 -0500912 self.logger.info("\tExecuting plugin func()")
913 self.logger.debug("\tCall func: %s" % eval_string)
George Keishingb97a9042021-07-29 07:41:20 -0500914 result = eval(eval_string)
915 self.logger.info("\treturn: %s" % str(result))
George Keishinge635ddc2022-12-08 07:38:02 -0600916 except (ValueError,
917 SyntaxError,
918 NameError,
919 AttributeError,
920 TypeError) as e:
George Keishing1e7b0182021-08-06 14:05:54 -0500921 self.logger.error("\tERROR: execute_python_eval: %s" % e)
922 # Set the plugin error state.
George Keishinge635ddc2022-12-08 07:38:02 -0600923 plugin_error_dict['exit_on_error'] = True
George Keishing73b95d12021-08-13 14:30:52 -0500924 self.logger.info("\treturn: PLUGIN_EVAL_ERROR")
George Keishinge635ddc2022-12-08 07:38:02 -0600925 return 'PLUGIN_EVAL_ERROR'
George Keishingb97a9042021-07-29 07:41:20 -0500926
927 return result
928
929 def execute_plugin_block(self, plugin_cmd_list):
930 r"""
Peter D Phan5e56f522021-12-20 13:19:41 -0600931 Pack the plugin command to qualifed python string object.
George Keishingb97a9042021-07-29 07:41:20 -0500932
933 Description of argument(s):
934 plugin_list_dict Plugin block read from YAML
935 [{'plugin_name': 'plugin.foo_func.my_func'},
936 {'plugin_args': [10]}]
937
938 Example:
939 - plugin:
940 - plugin_name: plugin.foo_func.my_func
941 - plugin_args:
942 - arg1
943 - arg2
944
945 - plugin:
946 - plugin_name: result = plugin.foo_func.my_func
947 - plugin_args:
948 - arg1
949 - arg2
950
951 - plugin:
952 - plugin_name: result1,result2 = plugin.foo_func.my_func
953 - plugin_args:
954 - arg1
955 - arg2
956 """
957 try:
George Keishinge635ddc2022-12-08 07:38:02 -0600958 idx = self.key_index_list_dict('plugin_name', plugin_cmd_list)
959 plugin_name = plugin_cmd_list[idx]['plugin_name']
George Keishingb97a9042021-07-29 07:41:20 -0500960 # Equal separator means plugin function returns result.
George Keishinge635ddc2022-12-08 07:38:02 -0600961 if ' = ' in plugin_name:
George Keishingb97a9042021-07-29 07:41:20 -0500962 # Ex. ['result', 'plugin.foo_func.my_func']
George Keishinge635ddc2022-12-08 07:38:02 -0600963 plugin_name_args = plugin_name.split(' = ')
George Keishingb97a9042021-07-29 07:41:20 -0500964 # plugin func return data.
965 for arg in plugin_name_args:
966 if arg == plugin_name_args[-1]:
967 plugin_name = arg
968 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600969 plugin_resp = arg.split(',')
George Keishingb97a9042021-07-29 07:41:20 -0500970 # ['result1','result2']
971 for x in plugin_resp:
972 global_plugin_list.append(x)
973 global_plugin_dict[x] = ""
974
975 # Walk the plugin args ['arg1,'arg2']
976 # If the YAML plugin statement 'plugin_args' is not declared.
George Keishinge635ddc2022-12-08 07:38:02 -0600977 if any('plugin_args' in d for d in plugin_cmd_list):
978 idx = self.key_index_list_dict('plugin_args', plugin_cmd_list)
979 plugin_args = plugin_cmd_list[idx]['plugin_args']
George Keishingb97a9042021-07-29 07:41:20 -0500980 if plugin_args:
981 plugin_args = self.yaml_args_populate(plugin_args)
982 else:
983 plugin_args = []
984 else:
985 plugin_args = self.yaml_args_populate([])
986
987 # Pack the args arg1, arg2, .... argn into
988 # "arg1","arg2","argn" string as params for function.
989 parm_args_str = self.yaml_args_string(plugin_args)
990 if parm_args_str:
George Keishinge635ddc2022-12-08 07:38:02 -0600991 plugin_func = plugin_name + '(' + parm_args_str + ')'
George Keishingb97a9042021-07-29 07:41:20 -0500992 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600993 plugin_func = plugin_name + '()'
George Keishingb97a9042021-07-29 07:41:20 -0500994
995 # Execute plugin function.
996 if global_plugin_dict:
997 resp = self.execute_python_eval(plugin_func)
George Keishing9348b402021-08-13 12:22:35 -0500998 # Update plugin vars dict if there is any.
George Keishinge635ddc2022-12-08 07:38:02 -0600999 if resp != 'PLUGIN_EVAL_ERROR':
George Keishing73b95d12021-08-13 14:30:52 -05001000 self.response_args_data(resp)
George Keishingb97a9042021-07-29 07:41:20 -05001001 else:
George Keishingcaa97e62021-08-03 14:00:09 -05001002 resp = self.execute_python_eval(plugin_func)
George Keishingb97a9042021-07-29 07:41:20 -05001003 except Exception as e:
George Keishing1e7b0182021-08-06 14:05:54 -05001004 # Set the plugin error state.
George Keishinge635ddc2022-12-08 07:38:02 -06001005 plugin_error_dict['exit_on_error'] = True
George Keishing1e7b0182021-08-06 14:05:54 -05001006 self.logger.error("\tERROR: execute_plugin_block: %s" % e)
George Keishingb97a9042021-07-29 07:41:20 -05001007 pass
1008
George Keishing73b95d12021-08-13 14:30:52 -05001009 # There is a real error executing the plugin function.
George Keishinge635ddc2022-12-08 07:38:02 -06001010 if resp == 'PLUGIN_EVAL_ERROR':
George Keishing73b95d12021-08-13 14:30:52 -05001011 return resp
1012
George Keishingde79a9b2021-08-12 16:14:43 -05001013 # Check if plugin_expects_return (int, string, list,dict etc)
George Keishinge635ddc2022-12-08 07:38:02 -06001014 if any('plugin_expects_return' in d for d in plugin_cmd_list):
1015 idx = self.key_index_list_dict('plugin_expects_return', plugin_cmd_list)
1016 plugin_expects = plugin_cmd_list[idx]['plugin_expects_return']
George Keishingde79a9b2021-08-12 16:14:43 -05001017 if plugin_expects:
1018 if resp:
George Keishinge635ddc2022-12-08 07:38:02 -06001019 if self.plugin_expect_type(plugin_expects, resp) == 'INVALID':
George Keishingde79a9b2021-08-12 16:14:43 -05001020 self.logger.error("\tWARN: Plugin error check skipped")
1021 elif not self.plugin_expect_type(plugin_expects, resp):
George Keishinge635ddc2022-12-08 07:38:02 -06001022 self.logger.error("\tERROR: Plugin expects return data: %s"
1023 % plugin_expects)
1024 plugin_error_dict['exit_on_error'] = True
George Keishingde79a9b2021-08-12 16:14:43 -05001025 elif not resp:
George Keishinge635ddc2022-12-08 07:38:02 -06001026 self.logger.error("\tERROR: Plugin func failed to return data")
1027 plugin_error_dict['exit_on_error'] = True
George Keishingde79a9b2021-08-12 16:14:43 -05001028
1029 return resp
1030
George Keishingb97a9042021-07-29 07:41:20 -05001031 def response_args_data(self, plugin_resp):
1032 r"""
George Keishing9348b402021-08-13 12:22:35 -05001033 Parse the plugin function response and update plugin return variable.
George Keishingb97a9042021-07-29 07:41:20 -05001034
1035 plugin_resp Response data from plugin function.
1036 """
1037 resp_list = []
George Keishing5765f792021-08-02 13:08:53 -05001038 resp_data = ""
George Keishing9348b402021-08-13 12:22:35 -05001039
George Keishingb97a9042021-07-29 07:41:20 -05001040 # There is nothing to update the plugin response.
George Keishinge635ddc2022-12-08 07:38:02 -06001041 if len(global_plugin_list) == 0 or plugin_resp == 'None':
George Keishingb97a9042021-07-29 07:41:20 -05001042 return
1043
George Keishing5765f792021-08-02 13:08:53 -05001044 if isinstance(plugin_resp, str):
George Keishinge635ddc2022-12-08 07:38:02 -06001045 resp_data = plugin_resp.strip('\r\n\t')
George Keishing5765f792021-08-02 13:08:53 -05001046 resp_list.append(resp_data)
1047 elif isinstance(plugin_resp, bytes):
George Keishinge635ddc2022-12-08 07:38:02 -06001048 resp_data = str(plugin_resp, 'UTF-8').strip('\r\n\t')
George Keishing5765f792021-08-02 13:08:53 -05001049 resp_list.append(resp_data)
1050 elif isinstance(plugin_resp, tuple):
1051 if len(global_plugin_list) == 1:
George Keishingb97a9042021-07-29 07:41:20 -05001052 resp_list.append(plugin_resp)
George Keishing5765f792021-08-02 13:08:53 -05001053 else:
1054 resp_list = list(plugin_resp)
George Keishinge635ddc2022-12-08 07:38:02 -06001055 resp_list = [x.strip('\r\n\t') for x in resp_list]
George Keishingb97a9042021-07-29 07:41:20 -05001056 elif isinstance(plugin_resp, list):
George Keishing5765f792021-08-02 13:08:53 -05001057 if len(global_plugin_list) == 1:
George Keishinge635ddc2022-12-08 07:38:02 -06001058 resp_list.append([x.strip('\r\n\t') for x in plugin_resp])
George Keishing5765f792021-08-02 13:08:53 -05001059 else:
George Keishinge635ddc2022-12-08 07:38:02 -06001060 resp_list = [x.strip('\r\n\t') for x in plugin_resp]
George Keishing5765f792021-08-02 13:08:53 -05001061 elif isinstance(plugin_resp, int) or isinstance(plugin_resp, float):
1062 resp_list.append(plugin_resp)
George Keishingb97a9042021-07-29 07:41:20 -05001063
George Keishing9348b402021-08-13 12:22:35 -05001064 # Iterate if there is a list of plugin return vars to update.
George Keishingb97a9042021-07-29 07:41:20 -05001065 for idx, item in enumerate(resp_list, start=0):
George Keishing9348b402021-08-13 12:22:35 -05001066 # Exit loop, done required loop.
George Keishingb97a9042021-07-29 07:41:20 -05001067 if idx >= len(global_plugin_list):
1068 break
1069 # Find the index of the return func in the list and
1070 # update the global func return dictionary.
1071 try:
1072 dict_idx = global_plugin_list[idx]
1073 global_plugin_dict[dict_idx] = item
1074 except (IndexError, ValueError) as e:
George Keishing1e7b0182021-08-06 14:05:54 -05001075 self.logger.warn("\tWARN: response_args_data: %s" % e)
George Keishingb97a9042021-07-29 07:41:20 -05001076 pass
1077
1078 # Done updating plugin dict irrespective of pass or failed,
George Keishing9348b402021-08-13 12:22:35 -05001079 # clear all the list element for next plugin block execute.
George Keishingb97a9042021-07-29 07:41:20 -05001080 global_plugin_list.clear()
1081
1082 def yaml_args_string(self, plugin_args):
1083 r"""
1084 Pack the args into string.
1085
1086 plugin_args arg list ['arg1','arg2,'argn']
1087 """
George Keishinge635ddc2022-12-08 07:38:02 -06001088 args_str = ''
George Keishingb97a9042021-07-29 07:41:20 -05001089 for args in plugin_args:
1090 if args:
George Keishing0581cb02021-08-05 15:08:58 -05001091 if isinstance(args, (int, float)):
George Keishingb97a9042021-07-29 07:41:20 -05001092 args_str += str(args)
George Keishing0581cb02021-08-05 15:08:58 -05001093 elif args in global_plugin_type_list:
1094 args_str += str(global_plugin_dict[args])
George Keishingb97a9042021-07-29 07:41:20 -05001095 else:
George Keishinge635ddc2022-12-08 07:38:02 -06001096 args_str += '"' + str(args.strip('\r\n\t')) + '"'
George Keishingb97a9042021-07-29 07:41:20 -05001097 # Skip last list element.
1098 if args != plugin_args[-1]:
1099 args_str += ","
1100 return args_str
1101
1102 def yaml_args_populate(self, yaml_arg_list):
1103 r"""
George Keishing9348b402021-08-13 12:22:35 -05001104 Decode env and plugin vars and populate.
George Keishingb97a9042021-07-29 07:41:20 -05001105
1106 Description of argument(s):
1107 yaml_arg_list arg list read from YAML
1108
1109 Example:
1110 - plugin_args:
1111 - arg1
1112 - arg2
1113
1114 yaml_arg_list: [arg2, arg2]
1115 """
1116 # Get the env loaded keys as list ['hostname', 'username', 'password'].
1117 env_vars_list = list(self.env_dict)
1118
1119 if isinstance(yaml_arg_list, list):
1120 tmp_list = []
1121 for arg in yaml_arg_list:
George Keishing0581cb02021-08-05 15:08:58 -05001122 if isinstance(arg, (int, float)):
George Keishingb97a9042021-07-29 07:41:20 -05001123 tmp_list.append(arg)
1124 continue
1125 elif isinstance(arg, str):
1126 arg_str = self.yaml_env_and_plugin_vars_populate(str(arg))
1127 tmp_list.append(arg_str)
1128 else:
1129 tmp_list.append(arg)
1130
1131 # return populated list.
1132 return tmp_list
1133
1134 def yaml_env_and_plugin_vars_populate(self, yaml_arg_str):
1135 r"""
George Keishing9348b402021-08-13 12:22:35 -05001136 Update ${MY_VAR} and plugin vars.
George Keishingb97a9042021-07-29 07:41:20 -05001137
1138 Description of argument(s):
George Keishing9348b402021-08-13 12:22:35 -05001139 yaml_arg_str arg string read from YAML.
George Keishingb97a9042021-07-29 07:41:20 -05001140
1141 Example:
1142 - cat ${MY_VAR}
1143 - ls -AX my_plugin_var
1144 """
George Keishing9348b402021-08-13 12:22:35 -05001145 # Parse the string for env vars ${env_vars}.
George Keishingb97a9042021-07-29 07:41:20 -05001146 try:
1147 # Example, list of matching env vars ['username', 'password', 'hostname']
1148 # Extra escape \ for special symbols. '\$\{([^\}]+)\}' works good.
George Keishinge635ddc2022-12-08 07:38:02 -06001149 var_name_regex = '\\$\\{([^\\}]+)\\}'
George Keishingb97a9042021-07-29 07:41:20 -05001150 env_var_names_list = re.findall(var_name_regex, yaml_arg_str)
1151 for var in env_var_names_list:
1152 env_var = os.environ[var]
George Keishinge635ddc2022-12-08 07:38:02 -06001153 env_replace = '${' + var + '}'
George Keishingb97a9042021-07-29 07:41:20 -05001154 yaml_arg_str = yaml_arg_str.replace(env_replace, env_var)
1155 except Exception as e:
George Keishing1e7b0182021-08-06 14:05:54 -05001156 self.logger.error("\tERROR:yaml_env_vars_populate: %s" % e)
George Keishingb97a9042021-07-29 07:41:20 -05001157 pass
1158
1159 # Parse the string for plugin vars.
1160 try:
1161 # Example, list of plugin vars ['my_username', 'my_data']
1162 plugin_var_name_list = global_plugin_dict.keys()
1163 for var in plugin_var_name_list:
George Keishing9348b402021-08-13 12:22:35 -05001164 # skip env var list already populated above code block list.
George Keishing0581cb02021-08-05 15:08:58 -05001165 if var in env_var_names_list:
1166 continue
George Keishing9348b402021-08-13 12:22:35 -05001167 # If this plugin var exist but empty in dict, don't replace.
George Keishing0581cb02021-08-05 15:08:58 -05001168 # This is either a YAML plugin statement incorrectly used or
George Keishing9348b402021-08-13 12:22:35 -05001169 # user added a plugin var which is not going to be populated.
George Keishing0581cb02021-08-05 15:08:58 -05001170 if yaml_arg_str in global_plugin_dict:
1171 if isinstance(global_plugin_dict[var], (list, dict)):
1172 # List data type or dict can't be replaced, use directly
1173 # in eval function call.
1174 global_plugin_type_list.append(var)
1175 else:
George Keishinge635ddc2022-12-08 07:38:02 -06001176 yaml_arg_str = yaml_arg_str.replace(str(var), str(global_plugin_dict[var]))
George Keishing0581cb02021-08-05 15:08:58 -05001177 # Just a string like filename or command.
1178 else:
George Keishinge635ddc2022-12-08 07:38:02 -06001179 yaml_arg_str = yaml_arg_str.replace(str(var), str(global_plugin_dict[var]))
George Keishingb97a9042021-07-29 07:41:20 -05001180 except (IndexError, ValueError) as e:
George Keishing1e7b0182021-08-06 14:05:54 -05001181 self.logger.error("\tERROR: yaml_plugin_vars_populate: %s" % e)
George Keishingb97a9042021-07-29 07:41:20 -05001182 pass
1183
1184 return yaml_arg_str
George Keishing1e7b0182021-08-06 14:05:54 -05001185
1186 def plugin_error_check(self, plugin_dict):
1187 r"""
1188 Plugin error dict processing.
1189
1190 Description of argument(s):
1191 plugin_dict Dictionary of plugin error.
1192 """
George Keishinge635ddc2022-12-08 07:38:02 -06001193 if any('plugin_error' in d for d in plugin_dict):
George Keishing1e7b0182021-08-06 14:05:54 -05001194 for d in plugin_dict:
George Keishinge635ddc2022-12-08 07:38:02 -06001195 if 'plugin_error' in d:
1196 value = d['plugin_error']
George Keishing1e7b0182021-08-06 14:05:54 -05001197 # Reference if the error is set or not by plugin.
1198 return plugin_error_dict[value]
George Keishingde79a9b2021-08-12 16:14:43 -05001199
1200 def key_index_list_dict(self, key, list_dict):
1201 r"""
1202 Iterate list of dictionary and return index if the key match is found.
1203
1204 Description of argument(s):
1205 key Valid Key in a dict.
1206 list_dict list of dictionary.
1207 """
1208 for i, d in enumerate(list_dict):
1209 if key in d.keys():
1210 return i
1211
1212 def plugin_expect_type(self, type, data):
1213 r"""
1214 Plugin expect directive type check.
1215 """
George Keishinge635ddc2022-12-08 07:38:02 -06001216 if type == 'int':
George Keishingde79a9b2021-08-12 16:14:43 -05001217 return isinstance(data, int)
George Keishinge635ddc2022-12-08 07:38:02 -06001218 elif type == 'float':
George Keishingde79a9b2021-08-12 16:14:43 -05001219 return isinstance(data, float)
George Keishinge635ddc2022-12-08 07:38:02 -06001220 elif type == 'str':
George Keishingde79a9b2021-08-12 16:14:43 -05001221 return isinstance(data, str)
George Keishinge635ddc2022-12-08 07:38:02 -06001222 elif type == 'list':
George Keishingde79a9b2021-08-12 16:14:43 -05001223 return isinstance(data, list)
George Keishinge635ddc2022-12-08 07:38:02 -06001224 elif type == 'dict':
George Keishingde79a9b2021-08-12 16:14:43 -05001225 return isinstance(data, dict)
George Keishinge635ddc2022-12-08 07:38:02 -06001226 elif type == 'tuple':
George Keishingde79a9b2021-08-12 16:14:43 -05001227 return isinstance(data, tuple)
1228 else:
1229 self.logger.info("\tInvalid data type requested: %s" % type)
George Keishinge635ddc2022-12-08 07:38:02 -06001230 return 'INVALID'