Log output results instead of print on the console
Let the user log the results as a Json file instead of printing the output to the console. It will be better for scripting and automation Change-Id: I4b2c669528e9b459971eb60a5d45607b2eeb4a4c
This commit is contained in:
parent
f70ad1a428
commit
1403d41c7a
|
@ -15,6 +15,7 @@
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
from prettytable import PrettyTable
|
from prettytable import PrettyTable
|
||||||
|
|
||||||
|
@ -75,6 +76,9 @@ class Validation(argparse.ArgumentParser):
|
||||||
default=constants.DEFAULT_VALIDATIONS_BASEDIR,
|
default=constants.DEFAULT_VALIDATIONS_BASEDIR,
|
||||||
help=("Path where the ansible roles, library "
|
help=("Path where the ansible roles, library "
|
||||||
"and plugins is located."))
|
"and plugins is located."))
|
||||||
|
parser.add_argument('--output-log', dest='output_log',
|
||||||
|
default=None,
|
||||||
|
help=("Path where the run result will be stored"))
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
def _print_dict_table(self, data):
|
def _print_dict_table(self, data):
|
||||||
|
@ -128,6 +132,12 @@ class Validation(argparse.ArgumentParser):
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("Wrong data type.")
|
raise RuntimeError("Wrong data type.")
|
||||||
|
|
||||||
|
def _write_output(self, output_log, results):
|
||||||
|
"""Write output log file as Json format"""
|
||||||
|
with open(output_log, 'w') as output:
|
||||||
|
output.write(json.dumps({'results': results}, indent=4,
|
||||||
|
sort_keys=True))
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
"""Take validation action"""
|
"""Take validation action"""
|
||||||
# Get parameters:
|
# Get parameters:
|
||||||
|
@ -149,15 +159,24 @@ class Validation(argparse.ArgumentParser):
|
||||||
base_dir=ansible_base_dir,
|
base_dir=ansible_base_dir,
|
||||||
quiet=quiet)
|
quiet=quiet)
|
||||||
if results:
|
if results:
|
||||||
self._print_dict_table(results)
|
if parsed_args.output_log:
|
||||||
|
self._write_output(parsed_args.output_log, results)
|
||||||
|
else:
|
||||||
|
self._print_dict_table(results)
|
||||||
elif 'list' in action:
|
elif 'list' in action:
|
||||||
results = v_actions.list_validations()
|
results = v_actions.list_validations()
|
||||||
if results:
|
if results:
|
||||||
self._print_tuple_table(results)
|
if parsed_args.output_log:
|
||||||
|
self._write_output(parsed_args.output_log, results)
|
||||||
|
else:
|
||||||
|
self._print_tuple_table(results)
|
||||||
elif 'show' in action:
|
elif 'show' in action:
|
||||||
results = v_actions.show_history(validation_name)
|
results = v_actions.show_history(validation_name)
|
||||||
if results:
|
if results:
|
||||||
self._print_tuple_table(data=results, status_col=2)
|
if parsed_args.output_log:
|
||||||
|
self._write_output(parsed_args.output_log, results)
|
||||||
|
else:
|
||||||
|
self._print_tuple_table(data=results, status_col=2)
|
||||||
else:
|
else:
|
||||||
msg = "Unknown Action: {}".format(action)
|
msg = "Unknown Action: {}".format(action)
|
||||||
raise RuntimeError(msg)
|
raise RuntimeError(msg)
|
||||||
|
|
Loading…
Reference in New Issue