Skip to content

Commit

Permalink
Moving logic for skipping hosts in pr cases (NOAA-EMC#2573)
Browse files Browse the repository at this point in the history
This PR removes the logic of skipping hosts for pr cases from
`create_experiment.py` and moves it to a test in the cron bash driver
using a `parse_yaml.py` python tool.

The Jenkins pipeline was not effected as it uses the
`get_host_case_list.py` utility to form the cases on a per host bases.

Co-authored-by: Rahul Mahajan <[email protected]>
  • Loading branch information
TerrenceMcGuinness-NOAA and aerorahul authored May 15, 2024
1 parent 3cd0c68 commit b5d113e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
16 changes: 9 additions & 7 deletions ci/scripts/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,20 @@ for pr in ${pr_list}; do
set +e
export LOGFILE_PATH="${HOMEgfs}/ci/scripts/create_experiment.log"
rm -f "${LOGFILE_PATH}"
yaml_case_file="${HOMEgfs}/ci/cases/pr/${case}.yaml"
skip_hosts=$("${HOMEgfs}/ci/scripts/utils/parse_yaml.py" --yaml "${yaml_case_file}" --key skip_ci_on_hosts --string)
if [[ "${skip_hosts}" == *"${MACHINE_ID}"* ]]; then
{
echo "Case setup: Skipped for experiment ${pslot}" || true
} >> "${output_ci}"
continue
fi
"${HOMEgfs}/workflow/create_experiment.py" --yaml "${HOMEgfs}/ci/cases/pr/${case}.yaml" > "${LOGFILE_PATH}" 2>&1
ci_status=$?
set -e
if [[ ${ci_status} -eq 0 ]]; then
last_line=$(tail -1 "${LOGFILE_PATH}")
if [[ "${last_line}" == *"Skipping creation"* ]]; then
action="Skipped"
else
action="Completed"
fi
{
echo "Case setup: ${action} for experiment ${pslot}" || true
echo "Case setup: Completed for experiment ${pslot}" || true
} >> "${output_ci}"
else
{
Expand Down
70 changes: 70 additions & 0 deletions ci/scripts/utils/parse_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3

"""
This script parses a yaml file and returns the value of a specified key.
"""

import os
import sys
from wxflow import AttrDict, parse_j2yaml
from argparse import ArgumentParser
from pathlib import Path

_here = os.path.dirname(__file__)
_top = os.path.abspath(os.path.join(os.path.abspath(_here), '../../..'))

description = """parse yaml file and return value of key"""


def parse_args():
"""
Parse command-line arguments.
Returns:
argparse.Namespace: The parsed command-line arguments.
"""

parser = ArgumentParser(description=description)
parser.add_argument('-y', '--yaml', help='full path to yaml file to parse', type=Path, required=True)
parser.add_argument('-k', '--key', help='key to return value of', type=str, required=True)
parser.add_argument('-s', '--string', help='output results as strings', action="store_true", required=False)
return parser.parse_args()


def yq(yamlfile, key):
"""
Parse a yaml file and return the value of a specified key.
Args:
yamlfile (Path): The path to the yaml file.
key (str): The key to return the value of.
Returns:
The value of the specified key in the yaml file.
"""

data = AttrDict(HOMEgfs=_top)
data.update({'HOMEgfs': _top})
ydict = parse_j2yaml(path=yamlfile, data=data)
if key == 'all':
return ydict
list_keys = key.split('.')
for k in list_keys:
ydict = ydict.get(k, None)
if ydict is None:
break
return ydict


if __name__ == '__main__':
"""
Main function. Parses command-line arguments and prints the value of the specified key in the specified yaml file.
"""

args = parse_args()
values = yq(args.yaml, args.key)
if args.string and isinstance(values, list):
for value in values:
print(value)
else:
print(values)
8 changes: 0 additions & 8 deletions workflow/create_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import setup_expt
import setup_xml

from hosts import Host

_here = os.path.dirname(__file__)
_top = os.path.abspath(os.path.join(os.path.abspath(_here), '..'))

Expand Down Expand Up @@ -79,12 +77,6 @@ def input_args():
data.update(os.environ)
testconf = parse_j2yaml(path=user_inputs.yaml, data=data)

if 'skip_ci_on_hosts' in testconf:
host = Host()
if host.machine.lower() in [machine.lower() for machine in testconf.skip_ci_on_hosts]:
logger.info(f'Skipping creation of case: {testconf.arguments.pslot} on {host.machine.capitalize()}')
sys.exit(0)

# Create a list of arguments to setup_expt.py
setup_expt_args = [testconf.experiment.system, testconf.experiment.mode]
for kk, vv in testconf.arguments.items():
Expand Down

0 comments on commit b5d113e

Please sign in to comment.