Commit aa9bfe2c authored by Lukas Werner's avatar Lukas Werner
Browse files

Prepare run script for testcluster

parent d79e49b1
#!/bin/bash -l
#!/bin/bash
set -euf -o pipefail
shopt -s inherit_errexit
module load slurm
hash awk
hash chown
hash diff
......@@ -15,14 +13,13 @@ hash sed
hash srun
hash ssh-keygen
function error {
: "${RV:=${2:-$BUILD_FAILURE_EXIT_CODE}}"
echo "$1" >&2
return "$RV"
}
## User authentification
## User authentication
: "${CUSTOM_ENV_AUTH_USER:?"AUTH_USER CI/CD variable has not been set."}"
: "${CUSTOM_ENV_AUTH_KEY:?"AUTH_KEY CI/CD variable has not been set."}"
......@@ -30,7 +27,7 @@ function error {
AUTH_USER=$CUSTOM_ENV_AUTH_USER
AUTH_USER_HOME=$(getent passwd "$AUTH_USER" | awk -F ":" '{print $6}')
AUTH_USER_SHELL=$(getent passwd "$AUTH_USER" | awk -F ":" '{print $7}')
AUTH_USER_WORK=$(runuser "$AUTH_USER" --command "echo \$WORK")
AUTH_USER_WORK=$(runuser "$AUTH_USER" --login --command "echo \$WORK")
## Check if the user exists and the validity of its ID
id -u "$AUTH_USER" >/dev/null 2>&1 || error "User $AUTH_USER does not exist"
......@@ -43,22 +40,21 @@ AUTH_PUB=/etc/gitlab-runner/authorized_keys
(
while read -r PUB
do
diff <(ssh-keygen -y -e -f /dev/stdin <<< "$AUTH_KEY") <(ssh-keygen -y -e -f /dev/stdin <<< "$PUB") && exit 0
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."
### Env setup
chown -R "$AUTH_USER" "$TMPDIR"
BASE_DIR=$AUTH_USER_WORK/gitlab-runner/builds/${CUSTOM_ENV_CI_CONCURRENT_PROJECT_ID:?}/${CUSTOM_ENV_CI_PROJECT_NAMESPACE:?}/${CUSTOM_ENV_CI_PROJECT_NAME:?}
runuser "$AUTH_USER" --command "mkdir -p $BASE_DIR"
runuser "$AUTH_USER" --login --command "mkdir -p $BASE_DIR"
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,
## 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_")
do
......@@ -79,12 +75,19 @@ then
do
SALLOC_OPTIONS+=("$(echo "${E#SLURM_}" | awk -F "=" '{gsub("_", "-", $1); print "--"tolower($1)"="$2}')")
done
runuser --login "$AUTH_USER" --command "cp $1 $BASE_DIR.tmp/$CUSTOM_ENV_CI_JOB_ID.sh"
exec salloc --quiet --uid "$AUTH_USER" --gid "$(id -g -n "$AUTH_USER")" --chdir "$BASE_DIR" "${SALLOC_OPTIONS[@]}" \
srun --cpu-bind none --export=HOME="$AUTH_USER_HOME",SHELL="$AUTH_USER_SHELL" --wait 0 --kill-on-bad-exit=1 \
"$AUTH_USER_SHELL" --login "$BASE_DIR.tmp/$CUSTOM_ENV_CI_JOB_ID.sh"
echo "#!/bin/bash" > "$TMPDIR"/salloc.sh
echo "salloc --quiet --chdir \"$BASE_DIR\" ${SALLOC_OPTIONS[@]} \
srun --cpu-bind none --wait 0 --kill-on-bad-exit=1 \
\"$AUTH_USER_SHELL\" --login \"$BASE_DIR.tmp/$CUSTOM_ENV_CI_JOB_ID.sh\"" >> "$TMPDIR"/salloc.sh
chmod +x "$TMPDIR"/salloc.sh
exec runuser --login "$AUTH_USER" --command "$TMPDIR"/salloc.sh
#exec runuser --login "$AUTH_USER" --command "salloc --quiet --chdir \"$BASE_DIR\" ${SALLOC_OPTIONS[@]} \
# srun --cpu-bind none --wait 0 --kill-on-bad-exit=1 \
# \"$AUTH_USER_SHELL\" --login \"$BASE_DIR.tmp/$CUSTOM_ENV_CI_JOB_ID.sh\""
else
runuser --login "$AUTH_USER" --command "cd \"$BASE_DIR\"; $1"
fi
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment