From 47f45b6d8724bfe5d32fafcd0e2d22d2541190ff Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Thu, 2 May 2019 19:00:45 +0200 Subject: [PATCH] r_connector: add script and cron job that unmount stale sessions. --- smartgears/r_connector/defaults/main.yml | 4 ++ smartgears/r_connector/tasks/main.yml | 36 +++++++------- .../templates/rconnector_check_stale_sessions | 49 +++++++++++++++++++ smartgears/r_connector/templates/rusersadd.j2 | 4 +- 4 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 smartgears/r_connector/templates/rconnector_check_stale_sessions diff --git a/smartgears/r_connector/defaults/main.yml b/smartgears/r_connector/defaults/main.yml index de6d63e0..b64ef870 100644 --- a/smartgears/r_connector/defaults/main.yml +++ b/smartgears/r_connector/defaults/main.yml @@ -10,6 +10,7 @@ r_connector_usershome: /home/ r_connector_userconfig: userconfig.csv r_connector_adduserscript: /usr/local/bin/rusersadd r_connector_workspace_unmount: /usr/local/bin/rconnector_unmount +r_connector_check_stale_sessions: /usr/local/bin/rconnector_check_stale_sessions r_connector_workspace_mountpoint: workspace r_connector_sleep_after_mount: 5 r_connector_rstudio_cookie_key: /var/lib/rstudio-server/secure-cookie-key @@ -42,3 +43,6 @@ fuse_integration_name: sh-fuse-integration fuse_integration_group_id: org.gcube.data-access fuse_integration_version: latest fuse_integration_classifier: 'jar-with-dependencies' +r_connector_fuse_packages: + - fuse + - libfuse2 diff --git a/smartgears/r_connector/tasks/main.yml b/smartgears/r_connector/tasks/main.yml index edfa2899..80ab1e5a 100644 --- a/smartgears/r_connector/tasks/main.yml +++ b/smartgears/r_connector/tasks/main.yml @@ -69,29 +69,29 @@ become_user: root template: src=rconnector_unmount dest={{ r_connector_workspace_unmount }} owner=root group=root mode=0555 + - name: Install the script that checks for stale workspace mounts + become_user: root + template: src=rconnector_check_stale_sessions dest={{ r_connector_check_stale_sessions }} owner=root group=root mode=0555 + + - name: Install the cron job that regulary wipes out the stale workspace mounts + become: root + cron: name="Unmount the stale workspace sessions" special_time=hourly job="{{ r_connector_check_stale_sessions }} >/dev/null 2>&1" + - name: Crete the directory that will host the RConfiguration stuff become_user: root file: dest={{ r_connector_rprofile_base_dir }} owner={{ d4science_user }} group={{ d4science_user }} state=directory - name: Download the fuse jar-with-dependencies become_user: root - get_url: - url: '{{ fuse_integration_jar_url }}' - dest: /usr/local/lib/{{ fuse_integration_file }} - force: yes - mode: 0555 - - - name: Download the fuse jar-with-dependencies - maven_artifact: artifact_id={{ fuse_integration_name }} version={{ fuse_integration_version | default(omit) }} group_id={{ fuse_integration_group_id }} extension={{ fuse_integration_extension | default('jar') }} repository_url={{ smartgears_global_base_url }} classifier={{ fuse_integration_classifier }} dest=/usr/local/lib/{{ fuse_integration_file } - -# shell: cd /usr/local/lib/ ; wget http://maven.research-infrastructures.eu/nexus/content/repositories/gcube-snapshots/org/gcube/data-access/sh-fuse-integration/1.0.0-SNAPSHOT/sh-fuse-integration-1.0.0-20190312.161452-1-jar-with-dependencies.jar -# args: -# creates: /usr/local/lib/sh-fuse-integration-jar-with-dependencies.jar - + maven_artifact: artifact_id={{ fuse_integration_name }} version={{ fuse_integration_version }} group_id={{ fuse_integration_group_id }} extension={{ fuse_integration_extension }} repository_url={{ smartgears_global_base_url }} classifier={{ fuse_integration_classifier }} dest=/usr/local/lib/{{ fuse_integration_file }} - name: Get the svn repository that provides the .Rprofile subversion: repo={{ r_connector_rprofile_svn_url }} dest={{ r_connector_rprofile_path }} + - name: Install /etc/R/Rprofile.site as a symlink to the svn repository + become_user: root + file: src={{ r_connector_rprofile_path }}/.Rprofile dest=/etc/R/Rprofile.site state=link force=yes + - name: Install the cron job that regulary updates the Rprofile cron: name="Update the RStudioConfiguration repo" special_time=daily job="cd {{ r_connector_rprofile_path }} ; svn update >/dev/null 2>&1" @@ -102,15 +102,15 @@ - block: # Additional fuse packages - - name: Install the libfuse package - apt: pkg=libfuse2 state=present update_cache=yes cache_valid_time=3600 + - name: Install the fuse required packages + apt: pkg={{ r_connector_fuse_packages }} state=present update_cache=yes cache_valid_time=3600 - - name: Install the fuse package - apt: pkg=fuse state=present update_cache=yes cache_valid_time=3600 + when: r_connector_install + tags: [ 'smartgears', 'r_connector', 'tomcat' ] - block: - name: Reconfigure the Java environment for R - shell: export JAVA_HOME={{ jdk_java_home }} ; export J2SDKDIR={{ jdk_java_home ; export J2REDIR={{ jdk_java_home }}/jre ; R CMD javareconf + shell: export JAVA_HOME={{ jdk_java_home }} ; export J2SDKDIR={{ jdk_java_home }} ; export J2REDIR={{ jdk_java_home }}/jre ; R CMD javareconf when: jdk_java_home is defined become: True diff --git a/smartgears/r_connector/templates/rconnector_check_stale_sessions b/smartgears/r_connector/templates/rconnector_check_stale_sessions new file mode 100644 index 00000000..0f2eebc3 --- /dev/null +++ b/smartgears/r_connector/templates/rconnector_check_stale_sessions @@ -0,0 +1,49 @@ +#!/bin/bash +DEBUG=1 +if [ $DEBUG -eq 0 ] ; then + set -x + exec 2>/var/tmp/rconnector_check_stale_sessions.log +fi +# We use logger to log directly to syslog +LOG_PREFIX="r-connector check_stale_sessions:" + +UNMOUNT_SCRIPT="{{ r_connector_workspace_unmount }}" +RSTUDIO_BIN="/usr/sbin/rstudio-server" +ACTIVE_MOUNTS= +ACTIVE_MOUNTS_F=$( mktemp /tmp/active_mounts.XXXXX ) +ACTIVE_SESSIONS_LIST= +ACTIVE_SESSIONS_LIST_F=$( mktemp /tmp/active_sessions.XXXXX ) +STALE_SESSIONS_LIST= + +list_mounts() { + ACTIVE_MOUNTS=$( /bin/ps auwwx | grep -i java | grep "fuse-integration" | grep sudo | grep -v grep | awk '{print $13}' | sort > "$ACTIVE_MOUNTS_F" ) + if [ $DEBUG -eq 0 ] ; then + for m in $ACTIVE_MOUNTS ; do + logger "$LOG_PREFIX Active session: $m" + done + fi +} +check_running_sessions() { + ACTIVE_SESSIONS_LIST=$( $RSTUDIO_BIN active-sessions | grep rsession | grep -v grep | awk '{print $5 }' | sort > "$ACTIVE_SESSIONS_LIST_F" ) + if [ $DEBUG -eq 0 ] ; then + for ses in $ACTIVE_SESSIONS_LIST ; do + logger "$LOG_PREFIX Active mount: $ses" + done + fi +} + +umount_stale_sessions() { + STALE_SESSIONS_LIST=$( /usr/bin/comm -23 "$ACTIVE_MOUNTS_F" "$ACTIVE_SESSIONS_LIST_F" ) + for stale_s in $STALE_SESSIONS_LIST ; do + if [ $DEBUG -eq 0 ] ; then + logger "$LOG_PREFIX Stale session: $stale_s" + fi + $UNMOUNT_SCRIPT "$stale_s" + done +} + +list_mounts +check_running_sessions +umount_stale_sessions +rm -f "$ACTIVE_MOUNTS_F" "$ACTIVE_SESSIONS_LIST_F" +exit 0 diff --git a/smartgears/r_connector/templates/rusersadd.j2 b/smartgears/r_connector/templates/rusersadd.j2 index f13ea766..e35fdc23 100755 --- a/smartgears/r_connector/templates/rusersadd.j2 +++ b/smartgears/r_connector/templates/rusersadd.j2 @@ -30,13 +30,13 @@ WORKSPACE_MOUNT_DIR="$HDIR/{{ r_connector_workspace_mountpoint }}" workspace_mount () { if [ $REMOTE_WORKSPACE_MOUNT -eq 0 ] ; then - {{ r_connector_workspace_unmount }} "$USER" + "{{ r_connector_workspace_unmount }}" "$USER" logger "$LOG_PREFIX: Trying to mount the remote workspace" [ $DEBUG -eq 0 ] && logger "$LOG_PREFIX: Mount command is sudo -u $USER /usr/bin/java -jar $FUSE_INTEGRATION_JAR $GCUBE_TOKEN $GCUBE_SCOPES $WORKSPACE_MOUNT_DIR" sudo /bin/mkdir -p "$WORKSPACE_MOUNT_DIR" sudo /bin/chown "${USER}" "$WORKSPACE_MOUNT_DIR" sudo -u "$USER" /usr/bin/java -jar $FUSE_INTEGRATION_JAR $GCUBE_TOKEN $GCUBE_SCOPES $WORKSPACE_MOUNT_DIR > "/var/tmp/workspace_${USER}.log" 2>&1 & - sleep $SLEEP_AFTER_MOUNT + sleep "$SLEEP_AFTER_MOUNT" fi }