George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
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 |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 9 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 10 | import click |
| 11 | |
| 12 | # ---------Set sys.path for cli command execution--------------------------------------- |
| 13 | # Absolute path to openbmc-test-automation/ffdc |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 14 | abs_path = os.path.abspath(os.path.dirname(sys.argv[0])) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 15 | full_path = abs_path.split("ffdc")[0] |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 16 | sys.path.append(full_path) |
| 17 | # Walk path and append to sys.path |
| 18 | for root, dirs, files in os.walk(full_path): |
| 19 | for found_dir in dirs: |
| 20 | sys.path.append(os.path.join(root, found_dir)) |
| 21 | |
George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 22 | from ffdc_collector import ffdc_collector # NOQA |
George Keishing | 37c58c8 | 2022-12-08 07:42:54 -0600 | [diff] [blame] | 23 | |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 24 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 25 | @click.command(context_settings=dict(help_option_names=["-h", "--help"])) |
| 26 | @click.option("-r", "--remote", help="Hostname/IP of the remote host") |
| 27 | @click.option("-u", "--username", help="Username of the remote host.") |
| 28 | @click.option("-p", "--password", help="Password of the remote host.") |
| 29 | @click.option( |
| 30 | "-c", |
| 31 | "--config", |
| 32 | default=abs_path + "/ffdc_config.yaml", |
| 33 | show_default=True, |
| 34 | help="YAML Configuration file for log collection.", |
| 35 | ) |
| 36 | @click.option( |
| 37 | "-l", |
| 38 | "--location", |
| 39 | default="/tmp", |
| 40 | show_default=True, |
| 41 | help="Location to save logs", |
| 42 | ) |
| 43 | @click.option( |
| 44 | "-t", |
| 45 | "--type", |
| 46 | help=( |
| 47 | "OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU," |
| 48 | " SLES, AIX" |
| 49 | ), |
| 50 | ) |
| 51 | @click.option( |
| 52 | "-rp", |
| 53 | "--protocol", |
| 54 | default="ALL", |
| 55 | show_default=True, |
| 56 | help="Select protocol to communicate with remote host.", |
| 57 | ) |
| 58 | @click.option( |
| 59 | "-e", |
| 60 | "--env_vars", |
| 61 | show_default=True, |
| 62 | help="Environment variables e.g: {'var':value}", |
| 63 | ) |
| 64 | @click.option( |
| 65 | "-ec", |
| 66 | "--econfig", |
| 67 | show_default=True, |
| 68 | help="Predefine environment variables, refer en_vars_template.yaml ", |
| 69 | ) |
| 70 | @click.option( |
| 71 | "--log_level", |
| 72 | default="INFO", |
| 73 | show_default=True, |
| 74 | help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)", |
| 75 | ) |
| 76 | def cli_ffdc( |
| 77 | remote, |
| 78 | username, |
| 79 | password, |
| 80 | config, |
| 81 | location, |
| 82 | type, |
| 83 | protocol, |
| 84 | env_vars, |
| 85 | econfig, |
| 86 | log_level, |
| 87 | ): |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 88 | r""" |
| 89 | 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] | 90 | """ |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 91 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 92 | click.echo( |
| 93 | "\n********** FFDC (First Failure Data Collection) Starts **********" |
| 94 | ) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 95 | |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 96 | if input_options_ok(remote, username, password, config, type): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 97 | this_ffdc = ffdc_collector( |
| 98 | remote, |
| 99 | username, |
| 100 | password, |
| 101 | config, |
| 102 | location, |
| 103 | type, |
| 104 | protocol, |
| 105 | env_vars, |
| 106 | econfig, |
| 107 | log_level, |
| 108 | ) |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 109 | this_ffdc.collect_ffdc() |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 110 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 111 | if len(os.listdir(this_ffdc.ffdc_dir_path)) == 0: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 112 | click.echo( |
| 113 | "\n\tFFDC Collection from " + remote + " has failed.\n\n" |
| 114 | ) |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 115 | else: |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 116 | click.echo( |
| 117 | str( |
| 118 | "\n\t" |
| 119 | + str(len(os.listdir(this_ffdc.ffdc_dir_path))) |
| 120 | + " files were retrieved from " |
| 121 | + remote |
| 122 | ) |
| 123 | ) |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 124 | click.echo("\tFiles are stored in " + this_ffdc.ffdc_dir_path) |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 125 | |
Peter D Phan | 5e56f52 | 2021-12-20 13:19:41 -0600 | [diff] [blame] | 126 | click.echo("\tTotal elapsed time " + this_ffdc.elapsed_time + "\n\n") |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 127 | click.echo("\n********** FFDC Finishes **********\n\n") |
| 128 | |
| 129 | |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 130 | def input_options_ok(remote, username, password, config, type): |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 131 | r""" |
| 132 | Verify script options exist via CLI options or environment variables. |
| 133 | """ |
| 134 | |
| 135 | all_options_ok = True |
| 136 | |
| 137 | if not remote: |
| 138 | all_options_ok = False |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 139 | print( |
| 140 | " \n\tERROR: Name/IP of the remote host is not specified in" |
| 141 | " CLI options." |
| 142 | ) |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 143 | if not username: |
| 144 | all_options_ok = False |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 145 | print( |
| 146 | " \n\tERROR: User of the remote host is not specified in" |
| 147 | " CLI options." |
| 148 | ) |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 149 | if not password: |
| 150 | all_options_ok = False |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 151 | print( |
| 152 | " \n\tERROR: Password of the user remote host is not" |
| 153 | " specified in CLI options." |
| 154 | ) |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 155 | if not type: |
Peter D Phan | 3beb02e | 2021-07-06 13:25:17 -0500 | [diff] [blame] | 156 | all_options_ok = False |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 157 | print( |
| 158 | " \n\tERROR: Remote host os type is not specified in CLI" |
| 159 | " options." |
| 160 | ) |
George Keishing | e33094d | 2021-07-22 12:59:03 -0500 | [diff] [blame] | 161 | if not os.path.isfile(config): |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 162 | all_options_ok = False |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 163 | print( |
| 164 | " \n\tERROR: Config file %s is not found. Please verify" |
| 165 | " path and filename." % config |
| 166 | ) |
Peter D Phan | 8462faf | 2021-06-16 12:24:15 -0500 | [diff] [blame] | 167 | |
| 168 | return all_options_ok |
| 169 | |
| 170 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 171 | if __name__ == "__main__": |
Peter D Phan | 72ce6b8 | 2021-06-03 06:18:26 -0500 | [diff] [blame] | 172 | cli_ffdc() |