Support user define environment variable YAML

Changes:
    - Add new template env_vars_template.yaml
    - Add new CLI option -ec/-econfig to load user
      predefine env variables file.
    - Add code to load and export as environment variables
    - Minor cosmetic code changes

Tested:
     - without -ec option
     - with -ec option

Change-Id: I4752fb81bbc4e0046dd71884b25012e1d3586514
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/ffdc/collect_ffdc.py b/ffdc/collect_ffdc.py
index d52b1ec..78791e5 100644
--- a/ffdc/collect_ffdc.py
+++ b/ffdc/collect_ffdc.py
@@ -39,12 +39,21 @@
               help="Select protocol to communicate with remote host.")
 @click.option('-e', '--env_vars', show_default=True,
               help="Environment variables e.g: {'var':value}")
+@click.option('-ec', '--econfig', show_default=True,
+              help="Predefine environment variables, refer en_vars_template.yaml ")
 @click.option('--log_level', default="INFO",
               show_default=True,
               help="Log level (CRITICAL, ERROR, WARNING, INFO, DEBUG)")
-def cli_ffdc(remote, username, password,
-             config, location, type,
-             protocol, env_vars, log_level):
+def cli_ffdc(remote,
+             username,
+             password,
+             config,
+             location,
+             type,
+             protocol,
+             env_vars,
+             econfig,
+             log_level):
     r"""
     Stand alone CLI to generate and collect FFDC from the selected target.
     """
@@ -52,9 +61,16 @@
     click.echo("\n********** FFDC (First Failure Data Collection) Starts **********")
 
     if input_options_ok(remote, username, password, config, type):
-        thisFFDC = FFDCCollector(remote, username, password,
-                                 config, location, type,
-                                 protocol, env_vars, log_level)
+        thisFFDC = FFDCCollector(remote,
+                                 username,
+                                 password,
+                                 config,
+                                 location,
+                                 type,
+                                 protocol,
+                                 env_vars,
+                                 econfig,
+                                 log_level)
         thisFFDC.collect_ffdc()
 
         if len(os.listdir(thisFFDC.ffdc_dir_path)) == 0:
@@ -78,16 +94,15 @@
     if not remote:
         all_options_ok = False
         print("\
-        \n\tERROR: Name/IP of the remote host is not specified in CLI options or env OPENBMC_HOST.")
+        \n\tERROR: Name/IP of the remote host is not specified in CLI options.")
     if not username:
         all_options_ok = False
         print("\
-        \n\tERROR: User on the remote host is not specified in CLI options or env OPENBMC_USERNAME.")
+        \n\tERROR: User of the remote host is not specified in CLI options.")
     if not password:
         all_options_ok = False
         print("\
-        \n\tERROR: Password for user on remote host is not specified in CLI options "
-              + "or env OPENBMC_PASSWORD.")
+        \n\tERROR: Password of the user remote host is not specified in CLI options.")
     if not type:
         all_options_ok = False
         print("\
diff --git a/ffdc/env_vars_template.yaml b/ffdc/env_vars_template.yaml
new file mode 100644
index 0000000..e0e9e3d
--- /dev/null
+++ b/ffdc/env_vars_template.yaml
@@ -0,0 +1,10 @@
+# This is config file for user who wants to load pre-define variables
+# to be loaded at runtime
+# Key pair vlue , used in the main config ffdc_config.yaml file.
+# NOTE:
+# env_params is a reserved key name, don't rename.
+# var1, var2, var3 and so on you can add and populate as needed.
+env_params:
+    var1: 1
+    var2: 2
+    var3: 3
diff --git a/ffdc/ffdc_collector.py b/ffdc/ffdc_collector.py
index 6b7ac7d..98ae49b 100644
--- a/ffdc/ffdc_collector.py
+++ b/ffdc/ffdc_collector.py
@@ -34,16 +34,20 @@
                  remote_type,
                  remote_protocol,
                  env_vars,
+                 econfig,
                  log_level):
         r"""
         Description of argument(s):
 
-        hostname                name/ip of the targeted (remote) system
-        username                user on the targeted system with access to FFDC files
-        password                password for user on targeted system
-        ffdc_config             configuration file listing commands and files for FFDC
-        location                where to store collected FFDC
-        remote_type             os type of the remote host
+        hostname            name/ip of the targeted (remote) system
+        username            user on the targeted system with access to FFDC files
+        password            password for user on targeted system
+        ffdc_config         configuration file listing commands and files for FFDC
+        location            where to store collected FFDC
+        remote_type         os type of the remote host
+        remote_protocol     Protocol to use to collect data
+        env_vars            User define CLI env vars '{"key : "value"}'
+        econfig             User define env vars YAML file
 
         """
 
@@ -107,6 +111,14 @@
                 for key, value in self.env_dict.items():
                     os.environ[key] = value
 
+            if econfig:
+                with open(econfig, 'r') as file:
+                    env_config_dict = yaml.load(file, Loader=yaml.FullLoader)
+                # Export ENV vars.
+                for key, value in env_config_dict['env_params'].items():
+                    os.environ[key] = str(value)
+                    self.env_dict[key] = str(value)
+
         except json.decoder.JSONDecodeError as e:
             self.logger.error("\n\tERROR: %s " % e)
             sys.exit(-1)