1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/jupyterlab_ynh.git synced 2024-09-03 19:26:35 +02:00

Allow users to install extensions on their own instances by creating separate builds for each user

This commit is contained in:
Jules Bertholet 2021-05-31 00:20:02 -04:00
parent 4a70278f45
commit 7e3e324bb7
14 changed files with 1552 additions and 45 deletions

File diff suppressed because it is too large Load diff

View file

@ -171,7 +171,7 @@ c.JupyterHub.bind_url = 'http://:__PORT____PATH__'
#c.JupyterHub.cookie_secret_file = 'jupyterhub_cookie_secret' #c.JupyterHub.cookie_secret_file = 'jupyterhub_cookie_secret'
## The location of jupyterhub data files (e.g. /usr/local/share/jupyterhub) ## The location of jupyterhub data files (e.g. /usr/local/share/jupyterhub)
#c.JupyterHub.data_files_path = '/opt/jupyterlab/.venv/share/jupyterhub' c.JupyterHub.data_files_path = '__FINAL_PATH__/.venv/share/jupyterhub'
## Include any kwargs to pass to the database connection. See ## Include any kwargs to pass to the database connection. See
# sqlalchemy.create_engine for details. # sqlalchemy.create_engine for details.
@ -685,7 +685,7 @@ c.Spawner.default_url = '/lab'
# This whitelist is used to ensure that sensitive information in the JupyterHub # This whitelist is used to ensure that sensitive information in the JupyterHub
# process's environment (such as `CONFIGPROXY_AUTH_TOKEN`) is not passed to the # process's environment (such as `CONFIGPROXY_AUTH_TOKEN`) is not passed to the
# single-user server's process. # single-user server's process.
#c.Spawner.env_keep = ['PATH', 'PYTHONPATH', 'CONDA_ROOT', 'CONDA_DEFAULT_ENV', 'VIRTUAL_ENV', 'LANG', 'LC_ALL'] c.Spawner.env_keep = ['PATH', 'PYTHONPATH', 'CONDA_ROOT', 'CONDA_DEFAULT_ENV', 'VIRTUAL_ENV', 'LANG', 'LC_ALL', 'JUPYTERHUB_SINGLEUSER_APP']
## Extra environment variables to set for the single-user server's process. ## Extra environment variables to set for the single-user server's process.
# #
@ -762,7 +762,7 @@ c.Spawner.default_url = '/lab'
# #
# Note that this does *not* prevent users from accessing files outside of this # Note that this does *not* prevent users from accessing files outside of this
# path! They can do so with many other means. # path! They can do so with many other means.
#c.Spawner.notebook_dir = '' c.Spawner.notebook_dir = '~'
## An HTML form for options a user can specify on launching their server. ## An HTML form for options a user can specify on launching their server.
# #

View file

@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -e
if [ __ENABLE_EXTENSIONS__ -eq 1 ]; then
export JUPYTERLAB_DIR="$HOME/.local/share/__APP__/lab"
export PATH="__NODEJS_PATH__:$PATH"
fi
# Delegate the notebook server launch to the jupyterhub-singleuser script.
# this is how most sudospawner-singleuser scripts should end.
exec "$(dirname "$0")/jupyterhub-singleuser" $@

View file

@ -6,6 +6,7 @@ After=syslog.target network.target
Environment="LC_ALL=C.UTF-8" Environment="LC_ALL=C.UTF-8"
Environment="LANG=C.UTF-8" Environment="LANG=C.UTF-8"
Environment="PATH=__NODE_PATH__:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" Environment="PATH=__NODE_PATH__:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Environment="JUPYTERHUB_SINGLEUSER_APP=jupyter_server.serverapp.ServerApp"
User=__APP__ User=__APP__
Group=__APP__ Group=__APP__
ExecStart=/usr/local/bin/pipenv run jupyterhub -f __FINALPATH__/config/jupyterhub_config.py ExecStart=/usr/local/bin/pipenv run jupyterhub -f __FINALPATH__/config/jupyterhub_config.py

18
hooks/post_user_create Normal file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -e
source /usr/share/yunohost/helpers
app="${0//.\/50-}"
user=$1
final_path="$(ynh_app_setting_get --app=$app --key=final_path)"
enable_extensions="$(ynh_app_setting_get --app=$app --key=enable_extensions)"
ynh_use_nodejs
if [ $enable_extensions -eq 1 ]; then
export JUPYTERLAB_DIR="$(getent passwd $user | cut -d: -f6)/.local/share/$app/lab"
node_path="$nodejs_path:$(sudo -u $user sh -c 'echo $PATH')"
sudo -u $user env "PATH=$node_path:$PATH" "$final_path/.venv/bin/jupyter" lab build --app-dir="$JUPYTERLAB_DIR"
fi

View file

@ -51,6 +51,15 @@
"fr": "Activer le terminal dans le lab ?" "fr": "Activer le terminal dans le lab ?"
}, },
"default": true "default": true
},
{
"name": "enable_extensions",
"type": "boolean",
"ask": {
"en": "Allow users to install extensions?",
"fr": "Permettre aux utilisateurs d'installer des extensions ?"
},
"default": true
} }
] ]
} }

View file

@ -15,11 +15,25 @@ jupyterlab_version="3.0.16"
# PERSONAL HELPERS # PERSONAL HELPERS
#================================================= #=================================================
function load_settings {
app=$YNH_APP_INSTANCE_NAME
domain="$(ynh_app_setting_get --app=$app --key=domain)"
path_url="$(ynh_app_setting_get --app=$app --key=path)"
admin="$(ynh_app_setting_get --app=$app --key=admin)"
final_path="$(ynh_app_setting_get --app=$app --key=final_path)"
port="$(ynh_app_setting_get --app=$app --key=port)"
port_hub="$(ynh_app_setting_get --app=$app --key=port_hub)"
port_http_proxy="$(ynh_app_setting_get --app=$app --key=port_http_proxy)"
enable_terminal="$(ynh_app_setting_get --app=$app --key=enable_terminal)"
enable_extensions="$(ynh_app_setting_get --app=$app --key=enable_extensions)"
}
function python_setup { function python_setup {
set_permissions set_permissions
pushd "$final_path" pushd "$final_path"
sudo -u $app PIPENV_VENV_IN_PROJECT="enabled" PIPENV_SKIP_LOCK=true python3 -m pipenv install jupyterlab==$jupyterlab_version jupyterhub notebook jupyterhub-ldapauthenticator pyzmq sudospawner --three 2>&1 sudo -u $app PIPENV_VENV_IN_PROJECT="enabled" PIPENV_SKIP_LOCK=true python3 -m pipenv install jupyterlab==$jupyterlab_version jupyterhub notebook jupyter-server jupyterhub-ldapauthenticator pyzmq sudospawner --three 2>&1
sudo -u $app python3 -m pipenv run jupyterhub upgrade-db 2>&1 sudo -u $app python3 -m pipenv run jupyterhub upgrade-db 2>&1
popd popd
} }
@ -37,17 +51,29 @@ function set_permissions {
function add_configs { function add_configs {
mkdir -p "$final_path/config" mkdir -p "$final_path/config"
path=${path_url%/} path="${path_url%/}"
ynh_add_config --template="jupyterhub_config.py" --destination="$final_path/config/jupyterhub_config.py" ynh_add_config --template="jupyterhub_config.py" --destination="$final_path/config/jupyterhub_config.py"
ynh_add_config --template="jupyter_notebook_config.py" --destination="$final_path/config/jupyter_notebook_config.py" ynh_add_config --template="jupyter_server_config.py" --destination="$final_path/config/jupyter_server_config.py"
ynh_add_config --template="app-sudoers" --destination="/etc/sudoers.d/$app-sudoers" ynh_add_config --template="app-sudoers" --destination="/etc/sudoers.d/$app-sudoers"
ynh_add_config --template="sudospawner-singleuser" --destination="$final_path/.venv/bin/sudospawner-singleuser"
set_config_permissions set_config_permissions
} }
function build_local_labs {
ynh_use_nodejs
for user in $(ynh_user_list); do
local JUPYTERLAB_DIR="$(getent passwd $user | cut -d: -f6)/.local/share/$app/lab"
local node_path="$nodejs_path:$(sudo -u $user sh -c 'echo $PATH')"
sudo -u $user env "PATH=$node_path" "$final_path/.venv/bin/jupyter" lab build --app-dir="$JUPYTERLAB_DIR"
done
}
function set_config_permissions { function set_config_permissions {
chmod 550 "$final_path/.venv/bin/sudospawner-singleuser"
set_permissions set_permissions
chown root:root /etc/sudoers.d/$app-sudoers chown root:root /etc/sudoers.d/$app-sudoers
chmod 440 /etc/sudoers.d/$app-sudoers chmod 440 /etc/sudoers.d/$app-sudoers

View file

@ -25,10 +25,7 @@ ynh_abort_if_errors
#================================================= #=================================================
ynh_print_info --message="Loading installation settings..." ynh_print_info --message="Loading installation settings..."
app=$YNH_APP_INSTANCE_NAME load_settings
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
domain=$(ynh_app_setting_get --app=$app --key=domain)
#================================================= #=================================================
# DECLARE DATA AND CONF FILES TO BACKUP # DECLARE DATA AND CONF FILES TO BACKUP

View file

@ -19,19 +19,12 @@ old_path=$YNH_APP_OLD_PATH
new_domain=$YNH_APP_NEW_DOMAIN new_domain=$YNH_APP_NEW_DOMAIN
new_path=$YNH_APP_NEW_PATH new_path=$YNH_APP_NEW_PATH
app=$YNH_APP_INSTANCE_NAME
#================================================= #=================================================
# LOAD SETTINGS # LOAD SETTINGS
#================================================= #=================================================
ynh_script_progression --message="Loading installation settings..." ynh_script_progression --message="Loading installation settings..."
# Needed for helper "ynh_add_nginx_config" load_settings
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
port=$(ynh_app_setting_get --app=$app --key=port)
port_hub=$(ynh_app_setting_get --app=$app --key=port_hub)
port_http_proxy=$(ynh_app_setting_get --app=$app --key=port_http_proxy)
admin=$(ynh_app_setting_get --app=$app --key=admin)
#================================================= #=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP

View file

@ -28,6 +28,7 @@ path_url=$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC is_public=$YNH_APP_ARG_IS_PUBLIC
admin=$YNH_APP_ARG_ADMIN admin=$YNH_APP_ARG_ADMIN
enable_terminal=$YNH_APP_ARG_ENABLE_TERMINAL enable_terminal=$YNH_APP_ARG_ENABLE_TERMINAL
enable_extensions=$YNH_APP_ARG_ENABLE_EXTENSIONS
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
@ -51,6 +52,7 @@ ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=admin --value=$admin ynh_app_setting_set --app=$app --key=admin --value=$admin
ynh_app_setting_set --app=$app --key=enable_terminal --value=$enable_terminal ynh_app_setting_set --app=$app --key=enable_terminal --value=$enable_terminal
ynh_app_setting_set --app=$app --key=enable_extensions --value=$enable_extensions
#================================================= #=================================================
# STANDARD MODIFICATIONS # STANDARD MODIFICATIONS
@ -135,6 +137,16 @@ add_configs
# Set permissions to app files # Set permissions to app files
set_permissions set_permissions
#=================================================
# BUILD USER LABS
#=================================================
ynh_script_progression --message="Building Jupyter lab for each user..." --weight=10
# Set permissions to app files
if [ $enable_extensions -eq 1 ]; then
build_local_labs
fi
#================================================= #=================================================
# ADVERTISE SERVICE IN ADMIN PANEL # ADVERTISE SERVICE IN ADMIN PANEL
#================================================= #=================================================
@ -155,8 +167,7 @@ ynh_systemd_action --service_name=$app --action="start" --line_match="JupyterHub
ynh_script_progression --message="Configuring permissions..." --weight=4 ynh_script_progression --message="Configuring permissions..." --weight=4
# Make app public if necessary # Make app public if necessary
if [ $is_public -eq 1 ] if [ $is_public -eq 1 ]; then
then
ynh_permission_update --permission="main" --add="visitors" ynh_permission_update --permission="main" --add="visitors"
fi fi

View file

@ -14,13 +14,7 @@ source /usr/share/yunohost/helpers
#================================================= #=================================================
ynh_script_progression --message="Loading installation settings..." --weight=1 ynh_script_progression --message="Loading installation settings..." --weight=1
app=$YNH_APP_INSTANCE_NAME load_settings
domain=$(ynh_app_setting_get --app=$app --key=domain)
port=$(ynh_app_setting_get --app=$app --key=port)
port_hub=$(ynh_app_setting_get --app=$app --key=port_hub)
port_http_proxy=$(ynh_app_setting_get --app=$app --key=port_http_proxy)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
#================================================= #=================================================
# STANDARD REMOVE # STANDARD REMOVE

View file

@ -25,12 +25,7 @@ ynh_abort_if_errors
#================================================= #=================================================
ynh_script_progression --message="Loading settings..." --weight=1 ynh_script_progression --message="Loading settings..." --weight=1
app=$YNH_APP_INSTANCE_NAME load_settings
admin=$(ynh_app_setting_get --app=$app --key=admin)
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
#================================================= #=================================================
# CHECK IF THE APP CAN BE RESTORED # CHECK IF THE APP CAN BE RESTORED
@ -101,7 +96,7 @@ systemctl enable $app.service --quiet
#================================================= #=================================================
ynh_script_progression --message="Restoring sudo configuration..." --weight=2 ynh_script_progression --message="Restoring sudo configuration..." --weight=2
ynh_restore_file --origin_path="/etc/sudoers/$app-sudoers" ynh_restore_file --origin_path="/etc/sudoers.d/$app-sudoers"
set_config_permissions set_config_permissions
#================================================= #=================================================

View file

@ -14,16 +14,7 @@ source /usr/share/yunohost/helpers
#================================================= #=================================================
ynh_script_progression --message="Loading installation settings..." --weight=3 ynh_script_progression --message="Loading installation settings..." --weight=3
app=$YNH_APP_INSTANCE_NAME load_settings
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
admin=$(ynh_app_setting_get --app=$app --key=admin)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
port=$(ynh_app_setting_get --app=$app --key=port)
port_hub=$(ynh_app_setting_get --app=$app --key=port_hub)
port_http_proxy=$(ynh_app_setting_get --app=$app --key=port_http_proxy)
enable_terminal=$(ynh_app_setting_get --app=$app --key=enable_terminal)
#================================================= #=================================================
# CHECK VERSION # CHECK VERSION
@ -36,6 +27,12 @@ upgrade_type=$(ynh_check_app_version_changed)
#================================================= #=================================================
ynh_script_progression --message="Ensuring downward compatibility..." ynh_script_progression --message="Ensuring downward compatibility..."
if [ -z "$enable_extensions" ]; then
enable_extensions=0
ynh_app_setting_set --app=$app --key=enable_extensions --value=$enable_extensions
fi
# If final_path doesn't exist, create it # If final_path doesn't exist, create it
if [ -z "$final_path" ]; then if [ -z "$final_path" ]; then
final_path=/opt/yunohost/$app final_path=/opt/yunohost/$app
@ -147,6 +144,16 @@ ynh_add_systemd_config
# Set permissions on app files # Set permissions on app files
set_permissions set_permissions
#=================================================
# BUILD USER LABS
#=================================================
ynh_script_progression --message="Building Jupyter lab for each user..." --weight=10
# Set permissions to app files
if [ $enable_extensions -eq 1 ]; then
build_local_labs
fi
#================================================= #=================================================
# ADVERTISE SERVICE IN ADMIN PANEL # ADVERTISE SERVICE IN ADMIN PANEL
#================================================= #=================================================