| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python | 
|  | 2 |  | 
|  | 3 | r""" | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 4 | CLI FFDC Collector. | 
|  | 5 | """ | 
|  | 6 |  | 
|  | 7 | import os | 
|  | 8 | import sys | 
|  | 9 | import click | 
|  | 10 |  | 
|  | 11 | # ---------Set sys.path for cli command execution--------------------------------------- | 
|  | 12 | # Absolute path to openbmc-test-automation/ffdc | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 13 | abs_path = os.path.abspath(os.path.dirname(sys.argv[0])) | 
|  | 14 | full_path = abs_path.split('ffdc')[0] | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 15 | sys.path.append(full_path) | 
|  | 16 | # Walk path and append to sys.path | 
|  | 17 | for root, dirs, files in os.walk(full_path): | 
|  | 18 | for found_dir in dirs: | 
|  | 19 | sys.path.append(os.path.join(root, found_dir)) | 
|  | 20 |  | 
|  | 21 | from ffdc_collector import FFDCCollector | 
|  | 22 |  | 
|  | 23 |  | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 24 | @click.command(context_settings=dict(help_option_names=['-h', '--help'])) | 
|  | 25 | @click.option('-r', '--remote', envvar='OPENBMC_HOST', | 
|  | 26 | help="Name/IP of the remote (targeting) host. [default: OPENBMC_HOST]") | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 27 | @click.option('-u', '--username', envvar='OPENBMC_USERNAME', | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 28 | help="User on the remote host with access to FFDC files.[default: OPENBMC_USERNAME]") | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 29 | @click.option('-p', '--password', envvar='OPENBMC_PASSWORD', | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 30 | help="Password for user on remote host. [default: OPENBMC_PASSWORD]") | 
| Peter D  Phan | 0c42a94 | 2021-06-29 09:17:27 -0500 | [diff] [blame] | 31 | @click.option('-c', '--ffdc_config', default=abs_path + "/ffdc_config.yaml", | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 32 | show_default=True, help="YAML Configuration file listing commands and files for FFDC.") | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 33 | @click.option('-l', '--location', default="/tmp", | 
|  | 34 | show_default=True, help="Location to store collected FFDC data") | 
| Peter D  Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 35 | @click.option('-t', '--remote_type', | 
| George Keishing | 5b43c22 | 2021-07-09 01:10:00 -0500 | [diff] [blame] | 36 | help="OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU, SLES, AIX") | 
| Peter D  Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 37 | @click.option('-rp', '--remote_protocol', default="ALL", | 
|  | 38 | show_default=True, | 
|  | 39 | help="Select protocol (SSH, SCP, REDFISH) to communicate with remote host. \ | 
|  | 40 | Default: all available.") | 
| George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 41 | @click.option('-e', '--env_vars', show_default=True, | 
|  | 42 | help="Environment variables as dictionary e.g: {'var':value}") | 
| Peter D  Phan | e86d9a5 | 2021-07-15 10:42:25 -0500 | [diff] [blame] | 43 | @click.option('--log_level', default="INFO", | 
|  | 44 | show_default=True, | 
|  | 45 | help="python logging level (CRITICAL, ERROR, WARNING, INFO, DEBUG)") | 
| George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 46 | def cli_ffdc(remote, username, password, | 
|  | 47 | ffdc_config, location, remote_type, | 
|  | 48 | remote_protocol, env_vars, log_level): | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 49 | r""" | 
|  | 50 | Stand alone CLI to generate and collect FFDC from the selected target. | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 51 | """ | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 52 |  | 
| George Keishing | 772c977 | 2021-06-16 23:23:42 -0500 | [diff] [blame] | 53 | click.echo("\n********** FFDC (First Failure Data Collection) Starts **********") | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 54 |  | 
| Peter D  Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 55 | if input_options_ok(remote, username, password, ffdc_config, remote_type): | 
| Peter D  Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 56 | thisFFDC = FFDCCollector(remote, username, password, | 
| George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 57 | ffdc_config, location, remote_type, | 
|  | 58 | remote_protocol, env_vars, log_level) | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 59 | thisFFDC.collect_ffdc() | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 60 |  | 
| Peter D  Phan | 56429a6 | 2021-06-23 08:38:29 -0500 | [diff] [blame] | 61 | if len(os.listdir(thisFFDC.ffdc_dir_path)) == 0: | 
| Peter D  Phan | 733df63 | 2021-06-17 13:13:36 -0500 | [diff] [blame] | 62 | click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n") | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 63 | else: | 
| Peter D  Phan | 0c66977 | 2021-06-24 13:52:42 -0500 | [diff] [blame] | 64 | click.echo(str("\n\t" + str(len(os.listdir(thisFFDC.ffdc_dir_path))) | 
| George Keishing | 4885b2f | 2021-07-21 15:22:45 -0500 | [diff] [blame] | 65 | + " files were retrieved from " + remote)) | 
| Peter D  Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 66 | click.echo("\tFiles are stored in " + thisFFDC.ffdc_dir_path) | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 67 |  | 
| Peter D  Phan | 7610bc4 | 2021-07-06 06:31:05 -0500 | [diff] [blame] | 68 | click.echo("\tTotal elapsed time " + thisFFDC.elapsed_time + "\n\n") | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 69 | click.echo("\n********** FFDC Finishes **********\n\n") | 
|  | 70 |  | 
|  | 71 |  | 
| Peter D  Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 72 | def input_options_ok(remote, username, password, ffdc_config, remote_type): | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 73 | r""" | 
|  | 74 | Verify script options exist via CLI options or environment variables. | 
|  | 75 | """ | 
|  | 76 |  | 
|  | 77 | all_options_ok = True | 
|  | 78 |  | 
|  | 79 | if not remote: | 
|  | 80 | all_options_ok = False | 
|  | 81 | print("\ | 
| George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame^] | 82 | \n\tERROR: Name/IP of the remote host is not specified in CLI options or env OPENBMC_HOST.") | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 83 | if not username: | 
|  | 84 | all_options_ok = False | 
|  | 85 | print("\ | 
| George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame^] | 86 | \n\tERROR: User on the remote host is not specified in CLI options or env OPENBMC_USERNAME.") | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 87 | if not password: | 
|  | 88 | all_options_ok = False | 
|  | 89 | print("\ | 
| George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame^] | 90 | \n\tERROR: Password for user on remote host is not specified in CLI options " | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 91 | + "or env OPENBMC_PASSWORD.") | 
| Peter D  Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 92 | if not remote_type: | 
|  | 93 | all_options_ok = False | 
|  | 94 | print("\ | 
| George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame^] | 95 | \n\tERROR: Remote host os type is not specified in CLI options.") | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 96 | if not os.path.isfile(ffdc_config): | 
|  | 97 | all_options_ok = False | 
|  | 98 | print("\ | 
| George Keishing | 7bf5509 | 2021-07-22 12:33:34 -0500 | [diff] [blame^] | 99 | \n\tERROR: Config file %s is not found.  Please verify path and filename." % ffdc_config) | 
| Peter D  Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 100 |  | 
|  | 101 | return all_options_ok | 
|  | 102 |  | 
|  | 103 |  | 
| Peter D  Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 104 | if __name__ == '__main__': | 
|  | 105 | cli_ffdc() |