Refactoring of the cli run module
Removal of unnecessary imports. Minor stylistic adjustements. New string format function. Logging expansion for the extra vars loading. Extra vars file handling factored out. Signed-off-by: Jiri Podivin <jpodivin@redhat.com> Change-Id: I744f1f215750eb5b0bc3cb7005d4a81a35612fe9
This commit is contained in:
parent
b14fb3765b
commit
ac9a504c92
|
@ -18,6 +18,8 @@ import json
|
|||
import logging
|
||||
from prettytable import PrettyTable
|
||||
import re
|
||||
import yaml
|
||||
|
||||
try:
|
||||
from junit_xml import TestSuite, TestCase, to_xml_report_string
|
||||
JUNIT_XML_FOUND = True
|
||||
|
@ -98,3 +100,16 @@ def write_junitxml(output_junitxml, results):
|
|||
ts = TestSuite("Validations", test_cases)
|
||||
with open(output_junitxml, 'w') as output:
|
||||
output.write(to_xml_report_string([ts]))
|
||||
|
||||
|
||||
def read_extra_vars_file(extra_vars_file):
|
||||
"""Read file containing extra variables.
|
||||
"""
|
||||
try:
|
||||
with open(extra_vars_file, 'r') as env_file:
|
||||
return yaml.safe_load(env_file.read())
|
||||
except yaml.YAMLError as error:
|
||||
error_msg = (
|
||||
"The extra_vars file must be properly formatted YAML/JSON."
|
||||
"Details: {}.").format(error)
|
||||
raise RuntimeError(error_msg)
|
||||
|
|
|
@ -15,14 +15,12 @@
|
|||
# under the License.
|
||||
|
||||
import getpass
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
from cliff.command import Command
|
||||
|
||||
from validations_libs import constants
|
||||
from validations_libs import utils
|
||||
from validations_libs.validation_actions import ValidationActions
|
||||
from validations_libs.cli import common
|
||||
from validations_libs.cli.parseractions import CommaListAction, KeyValueAction
|
||||
|
@ -39,8 +37,7 @@ class Run(Command):
|
|||
"A string that identifies a single node or comma-separated "
|
||||
"list of nodes to be upgraded in parallel in this upgrade "
|
||||
" run invocation. For example: --limit \"compute-0,"
|
||||
" compute-1, compute-5\".")
|
||||
)
|
||||
" compute-1, compute-5\"."))
|
||||
|
||||
parser.add_argument(
|
||||
'--ssh-user',
|
||||
|
@ -78,8 +75,7 @@ class Run(Command):
|
|||
default="{}".format(
|
||||
sys.executable if sys.executable else "/usr/bin/python"
|
||||
),
|
||||
help=("Python interpreter for Ansible execution. ")
|
||||
)
|
||||
help=("Python interpreter for Ansible execution. "))
|
||||
|
||||
parser.add_argument(
|
||||
'--extra-env-vars',
|
||||
|
@ -91,8 +87,7 @@ class Run(Command):
|
|||
"to provide to your Ansible execution "
|
||||
"as KEY=VALUE pairs. Note that if you pass the same "
|
||||
"KEY multiple times, the last given VALUE for that same KEY "
|
||||
"will override the other(s)")
|
||||
)
|
||||
"will override the other(s)"))
|
||||
|
||||
extra_vars_group = parser.add_mutually_exclusive_group(required=False)
|
||||
extra_vars_group.add_argument(
|
||||
|
@ -104,8 +99,7 @@ class Run(Command):
|
|||
"Add Ansible extra variables to the validation(s) execution "
|
||||
"as KEY=VALUE pair(s). Note that if you pass the same "
|
||||
"KEY multiple times, the last given VALUE for that same KEY "
|
||||
"will override the other(s)")
|
||||
)
|
||||
"will override the other(s)"))
|
||||
|
||||
extra_vars_group.add_argument(
|
||||
'--extra-vars-file',
|
||||
|
@ -114,9 +108,7 @@ class Run(Command):
|
|||
help=(
|
||||
"Add a JSON/YAML file containing extra variable "
|
||||
"to a validation: "
|
||||
"--extra-vars-file /home/stack/vars.[json|yaml]."
|
||||
)
|
||||
)
|
||||
"--extra-vars-file /home/stack/vars.[json|yaml]."))
|
||||
|
||||
ex_group = parser.add_mutually_exclusive_group(required=True)
|
||||
ex_group.add_argument(
|
||||
|
@ -129,8 +121,7 @@ class Run(Command):
|
|||
"if more than one validation is required "
|
||||
"separate the names with commas: "
|
||||
"--validation check-ftype,512e | "
|
||||
"--validation 512e")
|
||||
)
|
||||
"--validation 512e"))
|
||||
|
||||
ex_group.add_argument(
|
||||
'--group', '-g',
|
||||
|
@ -141,8 +132,7 @@ class Run(Command):
|
|||
"if more than one group is required "
|
||||
"separate the group names with commas: "
|
||||
"--group pre-upgrade,prep | "
|
||||
"--group openshift-on-openstack")
|
||||
)
|
||||
"--group openshift-on-openstack"))
|
||||
|
||||
return parser
|
||||
|
||||
|
@ -162,14 +152,11 @@ class Run(Command):
|
|||
|
||||
extra_vars = parsed_args.extra_vars
|
||||
if parsed_args.extra_vars_file:
|
||||
try:
|
||||
with open(parsed_args.extra_vars_file, 'r') as env_file:
|
||||
extra_vars = yaml.safe_load(env_file.read())
|
||||
except yaml.YAMLError as e:
|
||||
error_msg = (
|
||||
"The extra_vars file must be properly formatted YAML/JSON."
|
||||
"Details: %s." % e)
|
||||
raise RuntimeError(error_msg)
|
||||
self.app.LOG.debug(
|
||||
"Loading extra vars file {}".format(
|
||||
parsed_args.extra_vars_file))
|
||||
|
||||
extra_vars = common.read_extra_vars_file(parsed_args.extra_vars_file)
|
||||
|
||||
try:
|
||||
results = v_actions.run_validations(
|
||||
|
@ -189,13 +176,13 @@ class Run(Command):
|
|||
raise RuntimeError(e)
|
||||
|
||||
if results:
|
||||
_rc = any([r for r in results if r['Status'] == 'FAILED'])
|
||||
failed_rc = any([r for r in results if r['Status'] == 'FAILED'])
|
||||
if parsed_args.output_log:
|
||||
common.write_output(parsed_args.output_log, results)
|
||||
if parsed_args.junitxml:
|
||||
common.write_junitxml(parsed_args.junitxml, results)
|
||||
common.print_dict(results)
|
||||
if _rc:
|
||||
if failed_rc:
|
||||
raise RuntimeError("One or more validations have failed.")
|
||||
else:
|
||||
msg = ("No validation has been run, please check "
|
||||
|
|
Loading…
Reference in New Issue