Skip to content

Commit

Permalink
feature: provision UU workspaces with run_and_pause script
Browse files Browse the repository at this point in the history
  • Loading branch information
dometto committed Jan 22, 2025
1 parent 20a7db4 commit 632955f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions playbooks/roles/uu_generic/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
uu_generic_documentation_link: https://utrechtuniversity.github.io/vre-docs/docs/workspace-catalogue.html
uu_generic_contact_email: research.engineering@uu.nl
uu_generic_src_api_endpoint: https://gw.live.surfresearchcloud.nl
8 changes: 8 additions & 0 deletions playbooks/roles/uu_generic/tasks/apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
ansible.builtin.command: snap install codium --classic
register: uu_generic_install_codium
changed_when: '"already installed" not in uu_generic_install_codium.stderr'

- name: Install run_and_pause script
ansible.builtin.template:
src: templates/run_and_pause.sh.j2
dest: /usr/local/bin/run_and_pause
mode: "0755"
owner: "root"
group: "root"
55 changes: 55 additions & 0 deletions playbooks/roles/uu_generic/templates/run_and_pause.sh.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
set -euo pipefail

if [ "$#" -lt 1 ]; then
echo "Usage: $0 mycommand ..." >&2
exit 1
fi

read -s -p "Please enter your ResearchCloud API token: " token
echo


COMMAND="$*"
WORKSPACE="{{ fact_workspace_info['workspace_id'] }}"
ENDPOINT_URL="{{ uu_generic_src_api_endpoint }}/v1/workspace/workspaces/$WORKSPACE"
PAUSE_COMMAND="curl -X 'POST' \
'$ENDPOINT_URL/actions/pause/' \
-H 'accept: application/json;Compute' \
-H 'authorization: $token' \
-H 'Content-Type: application/json;pause' \
-d '{}'
"

echo "Testing whether you are authorized to pause this workspace..."
result=$(curl -X "GET" "$ENDPOINT_URL/" \
-H "accept: application/json;Compute" \
-H "authorization: $token" \
2> /dev/null
)
trap "Could not login to the ReserachCloud API with your token." ERR

if [[ $(echo "$result" | jq '.allowed_actions | any(. == "pause")' ) == "true" ]]; then
echo "You are allowed to pause this workspace. Continuing..."
else
echo "You are NOT allowed to pause this workspace. Stopping..."
exit 100
fi

echo $PAUSE_COMMAND

TMUX_SESSION="run_and_pause$(date '+%d%m%Y%H%M%S')"

echo "Will run the following command, and pause the workspace when it exits:"
echo "$COMMAND"
echo
echo "The command will be ran using tmux. You can exit the tmux window using"
echo "To re-attach to the window later, use:"
echo "tmux attach-session -t $TMUX_SESSION"
echo
read -p "Press any key to continue... " -n1 -s

FULL_COMMAND="$COMMAND; $PAUSE_COMMAND; bash"
echo $FULL_COMMAND

tmux new-session -s "$TMUX_SESSION" "$FULL_COMMAND"

0 comments on commit 632955f

Please sign in to comment.