From 09ec7626ba53fd19a1ec6a10c2a6a26605d62344 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Mon, 31 May 2021 14:25:53 -0400 Subject: [PATCH] Inline bash functions --- scripts/_common.sh | 64 ---------------------------------------------- scripts/backup | 12 ++++++++- scripts/change_url | 27 +++++++++++++++++-- scripts/install | 36 +++++++++++++++++++++----- scripts/remove | 12 ++++++++- scripts/restore | 20 +++++++++++++-- scripts/upgrade | 50 ++++++++++++++++++++++++++++++------ 7 files changed, 137 insertions(+), 84 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index cd200de..06b6cdd 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -15,70 +15,6 @@ jupyterlab_version="3.0.16" # 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 { - set_permissions - - pushd "$final_path" - 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 - popd -} - -function set_permissions { - # Set permissions to app files - mkdir -p "$final_path/.venv" - - chown -R $app:$app "$final_path" - chmod -R g=u,g-w,o-rwx "$final_path" - - setfacl -nR -m g:$app.main:rx -m d:g:$app.main:rx "$final_path/.venv" - setfacl -n -m g:$app.main:x "$final_path" -} - -function add_configs { - mkdir -p "$final_path/config" - path="${path_url%/}" - - ynh_add_config --template="jupyterhub_config.py" --destination="$final_path/config/jupyterhub_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="sudospawner-singleuser" --destination="$final_path/.venv/bin/sudospawner-singleuser" - - - 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 { - chmod 550 "$final_path/.venv/bin/sudospawner-singleuser" - set_permissions - chown root:root /etc/sudoers.d/$app-sudoers - chmod 440 /etc/sudoers.d/$app-sudoers -} - #================================================= # EXPERIMENTAL HELPERS #================================================= diff --git a/scripts/backup b/scripts/backup index c10f524..96c61f1 100644 --- a/scripts/backup +++ b/scripts/backup @@ -25,7 +25,17 @@ ynh_abort_if_errors #================================================= ynh_print_info --message="Loading installation settings..." -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)" #================================================= # DECLARE DATA AND CONF FILES TO BACKUP diff --git a/scripts/change_url b/scripts/change_url index 40548c0..b9731de 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -24,7 +24,17 @@ new_path=$YNH_APP_NEW_PATH #================================================= ynh_script_progression --message="Loading installation settings..." -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)" #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP @@ -106,7 +116,20 @@ fi domain=$new_domain path=${new_path%/} -add_configs +mkdir -p "$final_path/config" + +ynh_add_config --template="jupyterhub_config.py" --destination="$final_path/config/jupyterhub_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="sudospawner-singleuser" --destination="$final_path/.venv/bin/sudospawner-singleuser" + +chmod 550 "$final_path/.venv/bin/sudospawner-singleuser" +chown -R $app:$app "$final_path" +chmod -R g=u,g-w,o-rwx "$final_path" +setfacl -nR -m g:$app.main:rx -m d:g:$app.main:rx "$final_path/.venv" +setfacl -n -m g:$app.main:x "$final_path" +chown root:root "/etc/sudoers.d/$app-sudoers" +chmod 440 "/etc/sudoers.d/$app-sudoers" #================================================= # GENERIC FINALISATION diff --git a/scripts/install b/scripts/install index 90ce19d..2410fb8 100644 --- a/scripts/install +++ b/scripts/install @@ -98,9 +98,17 @@ ynh_script_progression --message="Setting up source files..." --weight=64 ynh_app_setting_set --app=$app --key=final_path --value=$final_path -mkdir -p $final_path +# Set permissions to app files +mkdir -p "$final_path/.venv" +chown -R $app:$app "$final_path" +chmod -R g=u,g-w,o-rwx "$final_path" +setfacl -nR -m g:$app.main:rx -m d:g:$app.main:rx "$final_path/.venv" +setfacl -n -m g:$app.main:x "$final_path" -python_setup +pushd "$final_path" + 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 +popd #================================================= # NGINX CONFIGURATION @@ -126,7 +134,13 @@ ynh_add_systemd_config # MODIFY A CONFIG FILE #================================================= -add_configs +mkdir -p "$final_path/config" +path="${path_url%/}" + +ynh_add_config --template="jupyterhub_config.py" --destination="$final_path/config/jupyterhub_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="sudospawner-singleuser" --destination="$final_path/.venv/bin/sudospawner-singleuser" #================================================= # GENERIC FINALIZATION @@ -135,16 +149,26 @@ add_configs #================================================= # Set permissions to app files -set_permissions +chmod 550 "$final_path/.venv/bin/sudospawner-singleuser" +chown -R $app:$app "$final_path" +chmod -R g=u,g-w,o-rwx "$final_path" +setfacl -nR -m g:$app.main:rx -m d:g:$app.main:rx "$final_path/.venv" +setfacl -n -m g:$app.main:x "$final_path" +chown root:root "/etc/sudoers.d/$app-sudoers" +chmod 440 "/etc/sudoers.d/$app-sudoers" #================================================= # 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 + ynh_use_nodejs + for user in $(ynh_user_list); do + 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" "$final_path/.venv/bin/jupyter" lab build --app-dir="$JUPYTERLAB_DIR" + done fi #================================================= diff --git a/scripts/remove b/scripts/remove index f2fc035..c4af611 100644 --- a/scripts/remove +++ b/scripts/remove @@ -14,7 +14,17 @@ source /usr/share/yunohost/helpers #================================================= ynh_script_progression --message="Loading installation settings..." --weight=1 -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)" #================================================= # STANDARD REMOVE diff --git a/scripts/restore b/scripts/restore index c87d8c2..afa4fa2 100644 --- a/scripts/restore +++ b/scripts/restore @@ -25,7 +25,17 @@ ynh_abort_if_errors #================================================= ynh_script_progression --message="Loading settings..." --weight=1 -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)" #================================================= # CHECK IF THE APP CAN BE RESTORED @@ -65,7 +75,13 @@ ynh_restore_file --origin_path="$final_path" #================================================= # Restore permissions on app files -set_permissions +mkdir -p "$final_path/.venv" + +chown -R $app:$app "$final_path" +chmod -R g=u,g-w,o-rwx "$final_path" + +setfacl -nR -m g:$app.main:rx -m d:g:$app.main:rx "$final_path/.venv" +setfacl -n -m g:$app.main:x "$final_path" #================================================= # SPECIFIC RESTORATION diff --git a/scripts/upgrade b/scripts/upgrade index 63a9f1d..0b537b6 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -14,7 +14,17 @@ source /usr/share/yunohost/helpers #================================================= ynh_script_progression --message="Loading installation settings..." --weight=3 -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)" #================================================= # CHECK VERSION @@ -111,10 +121,17 @@ if [ "$upgrade_type" == "UPGRADE_APP" ] then ynh_script_progression --message="Upgrading source files..." --weight=160 - # Download, check integrity, uncompress and patch the source from app.src - mkdir -p $final_path + # Set permissions to app files + mkdir -p "$final_path/.venv" + chown -R $app:$app "$final_path" + chmod -R g=u,g-w,o-rwx "$final_path" + setfacl -nR -m g:$app.main:rx -m d:g:$app.main:rx "$final_path/.venv" + setfacl -n -m g:$app.main:x "$final_path" - python_setup + pushd "$final_path" + 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 + popd fi #================================================= @@ -123,7 +140,13 @@ fi # STORE THE CONFIG FILE CHECKSUM #================================================= -add_configs +mkdir -p "$final_path/config" +path="${path_url%/}" + +ynh_add_config --template="jupyterhub_config.py" --destination="$final_path/config/jupyterhub_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="sudospawner-singleuser" --destination="$final_path/.venv/bin/sudospawner-singleuser" #================================================= # SETUP SYSTEMD @@ -142,16 +165,27 @@ ynh_add_systemd_config #================================================= # Set permissions on app files -set_permissions +chmod 550 "$final_path/.venv/bin/sudospawner-singleuser" +chown -R $app:$app "$final_path" +chmod -R g=u,g-w,o-rwx "$final_path" +setfacl -nR -m g:$app.main:rx -m d:g:$app.main:rx "$final_path/.venv" +setfacl -n -m g:$app.main:x "$final_path" +chown root:root "/etc/sudoers.d/$app-sudoers" +chmod 440 "/etc/sudoers.d/$app-sudoers" + #================================================= # 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 + ynh_use_nodejs + for user in $(ynh_user_list); do + 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" "$final_path/.venv/bin/jupyter" lab build --app-dir="$JUPYTERLAB_DIR" + done fi #=================================================