2024-01-31 18:38:32 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
2024-02-09 15:22:18 +00:00
|
|
|
export CONTAINER_ID=$(basename $(cat /proc/1/cpuset))
|
2024-02-08 18:40:01 +00:00
|
|
|
GITLAB_TOKEN_SECRET=$(cat /var/run/secrets/dicom_bot_gitlab_token 2>/dev/null)
|
2024-02-09 15:22:18 +00:00
|
|
|
export GITLAB_TOKEN=${GITLAB_TOKEN_SECRET:=$GITLAB_TOKEN}
|
2024-03-08 20:58:58 +00:00
|
|
|
S3_ID=$(cat /var/run/secrets/s3_id 2>/dev/null)
|
|
|
|
|
S3_SECRET=$(cat /var/run/secrets/s3_secret 2>/dev/null)
|
|
|
|
|
export AWS_ACCESS_KEY_ID=${S3_ID:=$AWS_ACCESS_KEY_ID} AWS_SECRET_ACCESS_KEY=${S3_SECRET:=$AWS_SECRET_ACCESS_KEY}
|
2024-02-09 15:22:18 +00:00
|
|
|
export GITLAB_API_URL=https://${CI_SERVER_HOST}/api/v4
|
|
|
|
|
export GIT_SSH_PORT=${GIT_SSH_PORT:=222}
|
2024-01-31 18:38:32 +00:00
|
|
|
|
2024-02-09 15:22:18 +00:00
|
|
|
mkdir -p ~/.ssh
|
2024-01-31 18:38:32 +00:00
|
|
|
# only export keys when deploying as a service on swarm
|
|
|
|
|
# TODO: should try using gitlab runner mechanism if not
|
2024-02-08 18:40:01 +00:00
|
|
|
if [ -n "${GITLAB_TOKEN}" ] ; then
|
2024-01-31 18:38:32 +00:00
|
|
|
# generate container specific ssh-key
|
|
|
|
|
ssh-keygen -f /root/.ssh/id_rsa -N ''
|
|
|
|
|
# register it for dicom_bot user
|
2024-02-08 18:40:01 +00:00
|
|
|
echo 'registering the ssh key'
|
2024-02-09 15:22:18 +00:00
|
|
|
export ssh_key_json=$(curl -X POST -F "private_token=${GITLAB_TOKEN}" \
|
|
|
|
|
-F "title="${HOSTNAME} -F "key=$(cat ~/.ssh/id_rsa.pub)" \
|
2024-02-08 18:40:01 +00:00
|
|
|
"${GITLAB_API_URL}/user/keys")
|
2024-02-09 15:22:18 +00:00
|
|
|
export ssh_key_id=$(jq .id <<< "$ssh_key_json")
|
|
|
|
|
fi
|
2024-01-31 18:38:32 +00:00
|
|
|
|
|
|
|
|
git config --global init.defaultBranch main
|
2024-02-09 15:22:18 +00:00
|
|
|
ssh-keyscan -p ${GIT_SSH_PORT} -H ${CI_SERVER_HOST} | install -m 600 /dev/stdin $HOME/.ssh/known_hosts
|
2024-01-31 18:38:32 +00:00
|
|
|
|
|
|
|
|
# run whatever command was passed (storescp or python index_dicoms directly)
|
2024-02-08 18:40:01 +00:00
|
|
|
$@
|
2024-01-31 18:38:32 +00:00
|
|
|
|
2024-02-08 18:40:01 +00:00
|
|
|
if [ -n "${GITLAB_TOKEN}" ] ; then
|
2024-01-31 18:38:32 +00:00
|
|
|
# unregister the temporary ssh key
|
2024-02-09 15:22:18 +00:00
|
|
|
curl -X DELETE -F "private_token=${GITLAB_TOKEN}" "${GITLAB_API_URL}/user/keys/${ssh_key_id}"
|
2024-01-31 18:38:32 +00:00
|
|
|
fi
|