Add --env_vars option support for CLI for user env
Changes:
- added new option --env_vars
- added code to parse the env and load it
How to test: (Assuming YAML has this env var)
--env_vars '{"foo_name" : "value"}'
OEM:
- mycmd ${foo_name}
Change-Id: I001e6ee1105ab5d2a5ab06126b2039bdb1fb9e9a
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/ffdc/collect_ffdc.py b/ffdc/collect_ffdc.py
index e753857..730f830 100644
--- a/ffdc/collect_ffdc.py
+++ b/ffdc/collect_ffdc.py
@@ -38,10 +38,14 @@
show_default=True,
help="Select protocol (SSH, SCP, REDFISH) to communicate with remote host. \
Default: all available.")
+@click.option('-e', '--env_vars', show_default=True,
+ help="Environment variables as dictionary e.g: {'var':value}")
@click.option('--log_level', default="INFO",
show_default=True,
help="python logging level (CRITICAL, ERROR, WARNING, INFO, DEBUG)")
-def cli_ffdc(remote, username, password, ffdc_config, location, remote_type, remote_protocol, log_level):
+def cli_ffdc(remote, username, password,
+ ffdc_config, location, remote_type,
+ remote_protocol, env_vars, log_level):
r"""
Stand alone CLI to generate and collect FFDC from the selected target.
"""
@@ -50,15 +54,15 @@
if input_options_ok(remote, username, password, ffdc_config, remote_type):
thisFFDC = FFDCCollector(remote, username, password,
- ffdc_config, location,
- remote_type, remote_protocol, log_level)
+ ffdc_config, location, remote_type,
+ remote_protocol, env_vars, log_level)
thisFFDC.collect_ffdc()
if len(os.listdir(thisFFDC.ffdc_dir_path)) == 0:
click.echo("\n\tFFDC Collection from " + remote + " has failed.\n\n")
else:
click.echo(str("\n\t" + str(len(os.listdir(thisFFDC.ffdc_dir_path)))
- + " files were retrieved from " + remote))
+ + " files were retrieved from " + remote))
click.echo("\tFiles are stored in " + thisFFDC.ffdc_dir_path)
click.echo("\tTotal elapsed time " + thisFFDC.elapsed_time + "\n\n")
diff --git a/ffdc/ffdc_collector.py b/ffdc/ffdc_collector.py
index 8673818..c717e2d 100644
--- a/ffdc/ffdc_collector.py
+++ b/ffdc/ffdc_collector.py
@@ -7,6 +7,7 @@
import os
import sys
import yaml
+import json
import time
import logging
import platform
@@ -32,6 +33,7 @@
location,
remote_type,
remote_protocol,
+ env_vars,
log_level):
r"""
Description of argument(s):
@@ -93,6 +95,20 @@
else:
sys.exit(-1)
+ # Load ENV vars from user.
+ try:
+ if env_vars:
+ env_dict = json.loads(env_vars)
+ self.logger.info("\n\tENV: User define input YAML variables")
+ self.logger.info(json.dumps(env_dict, indent=8, sort_keys=True))
+
+ # Export ENV vars default.
+ for key, value in env_dict.items():
+ os.environ[key] = value
+ except json.decoder.JSONDecodeError as e:
+ self.logger.error("\n\tERROR: %s " % e)
+ sys.exit(-1)
+
def verify_script_env(self):
# Import to log version