1#!/usr/bin/env python
2
3r"""
4CLI FFDC Collector.
5"""
6
7import os
8import sys
9import click
10
11# ---------Set sys.path for cli command execution---------------------------------------
12# Absolute path to openbmc-test-automation/ffdc
13abs_path = os.path.abspath(os.path.dirname(sys.argv[0]))
14full_path = abs_path.split('ffdc')[0]
15sys.path.append(full_path)
16# Walk path and append to sys.path
17for 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
21from ffdc_collector import FFDCCollector
22
23
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]")
27@click.option('-u', '--username', envvar='OPENBMC_USERNAME',
28              help="User on the remote host with access to FFDC files.[default: OPENBMC_USERNAME]")
29@click.option('-p', '--password', envvar='OPENBMC_PASSWORD',
30              help="Password for user on remote host. [default: OPENBMC_PASSWORD]")
31@click.option('-c', '--ffdc_config', default=abs_path + "/ffdc_config.yaml",
32              show_default=True, help="YAML Configuration file listing commands and files for FFDC.")
33@click.option('-l', '--location', default="/tmp",
34              show_default=True, help="Location to store collected FFDC data")
35@click.option('-t', '--remote_type', default="OPENBMC",
36              show_default=True,
37              help="OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU, AIX")
38@click.option('-rp', '--remote_protocol', default="ALL",
39              show_default=True,
40              help="Select protocol (SSH, SCP, REDFISH) to communicate with remote host. \
41                    Default: all available.")
42def cli_ffdc(remote, username, password, ffdc_config, location, remote_type, remote_protocol):
43    r"""
44    Stand alone CLI to generate and collect FFDC from the selected target.
45    """
46
47    click.echo("\n********** FFDC (First Failure Data Collection) Starts **********")
48
49    if input_options_ok(remote, username, password, ffdc_config):
50        thisFFDC = FFDCCollector(remote, username, password,
51                                 ffdc_config, location, remote_type, remote_protocol)
52        thisFFDC.collect_ffdc()
53
54        if len(os.listdir(thisFFDC.ffdc_dir_path)) == 0:
55            click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n")
56        else:
57            click.echo(str("\n\t" + str(len(os.listdir(thisFFDC.ffdc_dir_path)))
58                       + " files were retrieved from " + remote))
59            click.echo("\tFiles are stored in " + thisFFDC.ffdc_dir_path + "\n\n")
60
61    click.echo("\n********** FFDC Finishes **********\n\n")
62
63
64def input_options_ok(remote, username, password, ffdc_config):
65    r"""
66    Verify script options exist via CLI options or environment variables.
67    """
68
69    all_options_ok = True
70
71    if not remote:
72        all_options_ok = False
73        print("\
74        \n>>>>>\tERROR: Name/IP of the remote host is not specified in CLI options or env OPENBMC_HOST.")
75    if not username:
76        all_options_ok = False
77        print("\
78        \n>>>>>\tERROR: User on the remote host is not specified in CLI options or env OPENBMC_USERNAME.")
79    if not password:
80        all_options_ok = False
81        print("\
82        \n>>>>>\tERROR: Password for user on remote host is not specified in CLI options "
83              + "or env OPENBMC_PASSWORD.")
84    if not os.path.isfile(ffdc_config):
85        all_options_ok = False
86        print("\
87        \n>>>>>\tERROR: Config file %s is not found.  Please verify path and filename." % ffdc_config)
88
89    return all_options_ok
90
91
92if __name__ == '__main__':
93    cli_ffdc()
94