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',
26              help="Hostname/IP of the remote host")
27@click.option('-u', '--username',
28              help="Username of the remote host.")
29@click.option('-p', '--password',
30              help="Password of the remote host.")
31@click.option('-c', '--config', default=abs_path + "/ffdc_config.yaml",
32              show_default=True, help="YAML Configuration file for log collection.")
33@click.option('-l', '--location', default="/tmp",
34              show_default=True, help="Location to save logs")
35@click.option('-t', '--type',
36              help="OS type of the remote (targeting) host. OPENBMC, RHEL, UBUNTU, SLES, AIX")
37@click.option('-rp', '--protocol', default="ALL",
38              show_default=True,
39              help="Select protocol to communicate with remote host.")
40@click.option('-e', '--env_vars', show_default=True,
41              help="Environment variables e.g: {'var':value}")
42@click.option('--log_level', default="INFO",
43              show_default=True,
44              help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)")
45def cli_ffdc(remote, username, password,
46             config, location, type,
47             protocol, env_vars, log_level):
48    r"""
49    Stand alone CLI to generate and collect FFDC from the selected target.
50    """
51
52    click.echo("\n********** FFDC (First Failure Data Collection) Starts **********")
53
54    if input_options_ok(remote, username, password, config, type):
55        thisFFDC = FFDCCollector(remote, username, password,
56                                 config, location, type,
57                                 protocol, env_vars, log_level)
58        thisFFDC.collect_ffdc()
59
60        if len(os.listdir(thisFFDC.ffdc_dir_path)) == 0:
61            click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n")
62        else:
63            click.echo(str("\n\t" + str(len(os.listdir(thisFFDC.ffdc_dir_path)))
64                           + " files were retrieved from " + remote))
65            click.echo("\tFiles are stored in " + thisFFDC.ffdc_dir_path)
66
67        click.echo("\tTotal elapsed time " + thisFFDC.elapsed_time + "\n\n")
68    click.echo("\n********** FFDC Finishes **********\n\n")
69
70
71def input_options_ok(remote, username, password, config, type):
72    r"""
73    Verify script options exist via CLI options or environment variables.
74    """
75
76    all_options_ok = True
77
78    if not remote:
79        all_options_ok = False
80        print("\
81        \n\tERROR: Name/IP of the remote host is not specified in CLI options or env OPENBMC_HOST.")
82    if not username:
83        all_options_ok = False
84        print("\
85        \n\tERROR: User on the remote host is not specified in CLI options or env OPENBMC_USERNAME.")
86    if not password:
87        all_options_ok = False
88        print("\
89        \n\tERROR: Password for user on remote host is not specified in CLI options "
90              + "or env OPENBMC_PASSWORD.")
91    if not type:
92        all_options_ok = False
93        print("\
94        \n\tERROR: Remote host os type is not specified in CLI options.")
95    if not os.path.isfile(config):
96        all_options_ok = False
97        print("\
98        \n\tERROR: Config file %s is not found.  Please verify path and filename." % config)
99
100    return all_options_ok
101
102
103if __name__ == '__main__':
104    cli_ffdc()
105