From e3fc274f7729ca25dffb9f25d91d795b5963463f Mon Sep 17 00:00:00 2001 From: Josh Date: Sat, 28 Oct 2023 17:32:37 +0000 Subject: [PATCH] feature - adds dockerfile, helper script, build action --- .github/workflows/docker.yml | 26 ++++++++++++++++++++++++ docker/Dockerfile.troute | 25 +++++++++++++++++++++++ docker/troute.sh | 39 ++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 .github/workflows/docker.yml create mode 100644 docker/Dockerfile.troute create mode 100755 docker/troute.sh diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 000000000..4a4a36f0f --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,26 @@ +name: Build and Test Docker Image + +on: + push: + branches: + - master + pull_request: + branches: + - master +jobs: + build_and_test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Build Docker image + run: docker build -t troute -f docker/Dockerfile.troute . + + # To run tests in a separate job + # either save image artifact or upload to dockerhub + - name: Run LowerColorado Test + continue-on-error: true # Continue with the next steps even if this step fails + run: docker/troute.sh -V3 -f test/LowerColorado_TX/test_AnA.yaml + diff --git a/docker/Dockerfile.troute b/docker/Dockerfile.troute new file mode 100644 index 000000000..ea572541c --- /dev/null +++ b/docker/Dockerfile.troute @@ -0,0 +1,25 @@ +FROM rockylinux:9.2 as rocky-base +RUN yum install -y epel-release +RUN yum install -y netcdf netcdf-fortran netcdf-fortran-devel netcdf-openmpi + +RUN yum install -y git cmake python python-devel pip +RUN git clone https://github.com/NOAA-OWP/t-route.git + +WORKDIR "/t-route/" + +RUN ln -s /usr/lib64/gfortran/modules/netcdf.mod /usr/include/openmpi-x86_64/netcdf.mod + +ENV VIRTUAL_ENV=/opt/venv +RUN python3 -m venv $VIRTUAL_ENV + +# Equivalent to source /opt/venv/bin/activate +ENV PATH="$VIRTUAL_ENV/bin:$PATH" + +RUN python -m pip install -r requirements.txt + +RUN ./compiler.sh no-e + +# increase max open files soft limit +RUN ulimit -n 10000 +ENTRYPOINT ["/opt/venv/bin/python", "-m", "nwm_routing"] + diff --git a/docker/troute.sh b/docker/troute.sh new file mode 100755 index 000000000..1da2f17d5 --- /dev/null +++ b/docker/troute.sh @@ -0,0 +1,39 @@ +#!/bin/bash +################################################################################### +# Helper script that allows you to execute the docker container like +# it was a local install of t-route e.g. +# ~ python -m nwm_routing -V3 -f test/LowerColorado_TX/test_AnA.yaml +# Can be executed as +# ~ docker/troute.sh -V3 -f test/LowerColorado_TX/test_AnA.yaml +# Although the former would fail as you need to be in the same dir as the .yaml +# This script mounts the .yaml directory and makes it the workdir of the container +################################################################################### + +image_name="troute" +container_yaml_dir="/config" # The directory in the container where the YAML file will be mounted +yaml_file_path="" +yaml_filename="" +parent_folder="" +# Loop through all arguments to find the .yaml file +for arg in "$@"; do + if [[ $arg == *.yaml ]]; then + # Get absolute path and just the filename + yaml_file_path="$(realpath "$arg")" + yaml_filename="$(basename "$arg")" + parent_folder="$(dirname "$yaml_file_path")" + break + fi +done + +# Check if yaml_file_path was found +if [ -z "$yaml_file_path" ]; then + echo "No .yaml file provided in arguments." + exit 1 +fi + +# Uncomment to print command +#echo "docker run -v ${parent_folder}:${container_yaml_dir}/ -w ${container_yaml_dir} ${image_name} ${@/$arg/$yaml_filename}" + +# Mount the YAML file, set the working directory, and run the Docker container with all arguments (replacing the full YAML path with just its filename) +docker run -v "${parent_folder}:${container_yaml_dir}/" -w "${container_yaml_dir}" ${image_name} "${@/$arg/$yaml_filename}" +