From 45e71fff021c7b331df20161fa715918bfabe26a Mon Sep 17 00:00:00 2001
From: Lukas Werner <lks.werner@fau.de>
Date: Tue, 3 Aug 2021 14:27:16 +0200
Subject: [PATCH] Better auth messages, check for max slurm time limit

---
 runner_scripts/root/cleanup.sh |  2 +-
 runner_scripts/root/run.sh     | 16 ++++++++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/runner_scripts/root/cleanup.sh b/runner_scripts/root/cleanup.sh
index 32d568a..9291068 100755
--- a/runner_scripts/root/cleanup.sh
+++ b/runner_scripts/root/cleanup.sh
@@ -38,7 +38,7 @@ do
     diff <(ssh-keygen -y -e -f /dev/stdin <<< "$AUTH_KEY") <(ssh-keygen -y -e -f /dev/stdin <<< "$PUB") && exit 0
 done < "$AUTH_PUB"
 exit 1
-) || error "Authentication failed."
+) || error "Authentication for user $AUTH_USER failed."
 
 ### Env cleanup
 
diff --git a/runner_scripts/root/run.sh b/runner_scripts/root/run.sh
index c2e899f..227cc04 100755
--- a/runner_scripts/root/run.sh
+++ b/runner_scripts/root/run.sh
@@ -2,6 +2,8 @@
 set -euf -o pipefail
 shopt -s inherit_errexit
 
+MAX_CI_JOB_RUNTIME=120
+
 hash awk
 hash chown
 hash diff
@@ -40,7 +42,7 @@ do
     diff --color=never <(ssh-keygen -y -e -f /dev/stdin <<< "$AUTH_KEY") <(ssh-keygen -y -e -f /dev/stdin <<< "$PUB") > /dev/null && exit 0
 done < "$AUTH_PUB"
 exit 1
-) || error "Authentication failed."
+) || error "Authentication for user $AUTH_USER failed."
 
 ### Env setup
 
@@ -56,8 +58,7 @@ BASE_DIR=$AUTH_USER_WORK/gitlab-runner/builds/${CUSTOM_ENV_CI_CONCURRENT_PROJECT
 
 runuser "$AUTH_USER" --login --command "mkdir -p $BASE_DIR"
 
-if [[ ("$2" == "step_script" || "$2" == "build_script") && ${CUSTOM_ENV_SUBMIT_TO_SLURM:-} ]]
-then
+if [[ ("$2" == "step_script" || "$2" == "build_script") && ${CUSTOM_ENV_SUBMIT_TO_SLURM:-} ]]; then
     ## The script is the one specified in the gitlab-ci.yml script directive and the SUBMIT_TO_SLURM variable is set
 
     for E in $(env | grep -E "^CUSTOM_ENV_SLURM_")
@@ -66,13 +67,20 @@ then
     done
 
     : "${SLURM_JOB_NAME:="gitlab-ci-${CUSTOM_ENV_CI_PROJECT_NAME:?}-${CUSTOM_ENV_CI_PIPELINE_ID:?}-${CUSTOM_ENV_CI_JOB_ID:?}"}"
-    : "${SLURM_TIMELIMIT:="240"}"
+    : "${SLURM_TIMELIMIT:=$MAX_CI_JOB_RUNTIME}"
     : "${SLURM_TIME:=$SLURM_TIMELIMIT}"
 
     export SLURM_JOB_NAME
     export SLURM_TIME
     unset SLURM_TIMELIMIT
 
+    # limit max job run time
+    if [[ $SLURM_TIME > $MAX_CI_JOB_RUNTIME ]]; then
+        echo "SLURM_TIMELIMIT or SLURM_TIME larger than $MAX_CI_JOB_RUNTIME (s), limiting to $MAX_CI_JOB_RUNTIME." 1>&2
+        SLURM_TIME=$MAX_CI_JOB_RUNTIME
+        export SLURM_TIME
+    fi
+
     # Generate salloc arguments from SLURM_* environment variables
     SALLOC_OPTIONS=()
     for E in $(env | grep -E "^SLURM_")
-- 
GitLab