ansible-roles/smartgears/r_connector/templates/rusersadd.j2

93 lines
3.4 KiB
Django/Jinja
Executable File

#!/bin/bash
DEBUG=1
if [ $DEBUG -eq 0 ] ; then
set -x
exec 2>/var/tmp/rusersadd.log
fi
# We use logger to log directly to syslog
LOG_PREFIX="r-connector rusersadd:"
USER="$1"
GCUBE_TOKEN=
GCUBE_SCOPES=
REMOTE_WORKSPACE_MOUNT=0
SLEEP_AFTER_MOUNT={{ r_connector_sleep_after_mount }}
if [ $# -ne 3 ] ; then
REMOTE_WORKSPACE_MOUNT=1
logger "$LOG_PREFIX: this rconnector does not support the remote workspace mount"
else
GCUBE_TOKEN="$2"
GCUBE_SCOPES="$3"
fi
HDIR="{{ r_connector_usershome }}$USER"
# 0: allowed
# 1: not allowed
ALLOW_LOCAL_USERS=1
RPROFILE_FILE='{{ r_connector_rprofile_path }}/{{ r_connector_rprofile_filename }}'
FUSE_INTEGRATION_JAR=/usr/local/lib/sh-fuse-integration-1.0.0-SNAPSHOT-jar-with-dependencies.jar
WORKSPACE_MOUNT_DIR="$HDIR/{{ r_connector_workspace_mountpoint }}"
workspace_mount () {
if [ $REMOTE_WORKSPACE_MOUNT -eq 0 ] ; then
{{ 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
fi
}
fix_userconfig_perms () {
sudo /usr/bin/touch "${HDIR}/{{ r_connector_userconfig }}"
sudo /bin/chmod 660 "$HDIR/{{ r_connector_userconfig }}"
sudo /bin/chown "${USER}:{{ smartgears_user }}" "$HDIR/{{ r_connector_userconfig }}"
sudo /usr/bin/setfacl -m u:${USER}:rw,g:{{ smartgears_user }}:rw $HDIR/{{ r_connector_userconfig }}
}
#
# Main
#
if [ -d "$HDIR" ] ; then
if id -u "$USER" >/dev/null 2>&1
then
logger "$LOG_PREFIX user $HDIR directory exists. Touching the userconfig.csv file to ensure that it exists with the correct permissions"
fix_userconfig_perms
sudo /bin/ln -s "$RPROFILE_FILE" "$HDIR/{{ r_connector_rprofile_filename }}"
workspace_mount
exit 0
else
logger "$LOG_PREFIX user $HDIR directory exists but the user not. Aborting."
exit 1
fi
else
if id -u "$USER" >/dev/null 2>&1
then
logger "$LOG_PREFIX ldap user $USER first login"
sudo /bin/mkdir -p "$HDIR"
sudo /bin/chown -R "$USER" "$HDIR"
sudo /bin/chmod g-wr,o-rwx "$HDIR"
fix_userconfig_perms
sudo /bin/chgrp {{ smartgears_user }} "$HDIR"
sudo /bin/ln -s "$RPROFILE_FILE" "$HDIR/{{ r_connector_rprofile_filename }}"
workspace_mount
exit 0
else
logger "$LOG_PREFIX user $USER does not exist locally and is not an ldap user"
if [ $ALLOW_LOCAL_USERS -eq 1 ] ; then
logger "$LOG_PREFIX non ldap users not allowed, refusing to create the user."
exit 1
else
logger "$LOG_PREFIX non ldap users allowed, adding the user $USER locally"
sudo /usr/sbin/useradd -m -s /bin/false -g {{ smartgears_user }} "$USER"
sudo /bin/chmod g+ws "$HDIR"
fix_userconfig_perms
sudo /bin/chgrp -R {{ smartgears_user }} "$HDIR"
sudo /bin/ln -s "$RPROFILE_FILE" "$HDIR/{{ r_connector_rprofile_filename }}"
exit 0
fi
fi
fi