#!/bin/bash #================================================= # GENERIC START #================================================= # IMPORT GENERIC HELPERS #================================================= source _common.sh source /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= #REMOVEME? ynh_clean_setup () { ynh_clean_check_starting } # Exit if an error occurs during the execution of the script #REMOVEME? ynh_abort_if_errors #================================================= # RETRIEVE ARGUMENTS FROM THE MANIFEST #================================================= #REMOVEME? domain=$YNH_APP_ARG_DOMAIN path="/" #REMOVEME? admin=$YNH_APP_ARG_ADMIN #REMOVEME? is_public=$YNH_APP_ARG_IS_PUBLIC #REMOVEME? allow_multimedia_write=$YNH_APP_ARG_ALLOW_MULTIMEDIA_WRITE #REMOVEME? app=$YNH_APP_INSTANCE_NAME #================================================= # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS #================================================= #REMOVEME? ynh_script_progression --message="Validating installation parameters..." --weight=1 #REMOVEME? install_dir=/opt/yunohost/$app #REMOVEME? test ! -e "$install_dir" || ynh_die --message="This path already contains a folder" #REMOVEME? ynh_app_setting_set --app=$app --key=install_dir --value="$install_dir" data_path=/home/yunohost.app/$app test ! -e "$data_path" || ynh_die --message="This path already contains a folder" #REMOVEME? ynh_app_setting_set --app=$app --key=data_path --value="$data_path" # Register (book) web path #REMOVEME? ynh_webpath_register --app=$app --domain=$domain --path=$path #================================================= # STORE SETTINGS FROM MANIFEST #================================================= #REMOVEME? ynh_script_progression --message="Storing installation settings..." --weight=1 #REMOVEME? ynh_app_setting_set --app=$app --key=domain --value=$domain #REMOVEME? ynh_app_setting_set --app=$app --key=path --value=$path #REMOVEME? ynh_app_setting_set --app=$app --key=admin --value=$admin ynh_app_setting_set --app=$app --key=allow_multimedia_write --value=$allow_multimedia_write #================================================= # STANDARD MODIFICATIONS #================================================= # FIND PORTS #================================================= ynh_script_progression --message="Finding available ports..." --weight=1 # Find an available port #REMOVEME? port=$(ynh_find_port --port=3000) #REMOVEME? ynh_app_setting_set --app=$app --key=port --value=$port #REMOVEME? backend_port=$(ynh_find_port --port=8001) #REMOVEME? ynh_app_setting_set --app=$app --key=backend_port --value=$backend_port #REMOVEME? similarity_port=$(ynh_find_port --port=$(expr $backend_port + 1)) #REMOVEME? ynh_app_setting_set --app=$app --key=similarity_port --value=$similarity_port #================================================= # INSTALL DEPENDENCIES #================================================= #REMOVEME? ynh_script_progression --message="Installing dependencies..." --weight=5 #REMOVEME? ynh_install_app_dependencies $pkg_dependencies #REMOVEME? ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key="https://dl.yarnpkg.com/debian/pubkey.gpg" #================================================= # CREATE A POSTGRESQL DATABASE #================================================= #REMOVEME? ynh_script_progression --message="Creating a PostgreSQL database..." --weight=1 #REMOVEME? db_name=$(ynh_sanitize_dbid --db_name=$app) #REMOVEME? db_user=$db_name #REMOVEME? ynh_psql_test_if_first_run #REMOVEME? ynh_app_setting_set --app=$app --key=db_name --value=$db_name #REMOVEME? ynh_psql_setup_db --db_user=$db_user --db_name=$db_name #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= ynh_script_progression --message="Setting up source files..." --weight=5 unpack_source #================================================= # NGINX CONFIGURATION #================================================= ynh_script_progression --message="Configuring NGINX web server..." --weight=1 # Create a dedicated NGINX config ynh_add_nginx_config backend_port #================================================= # CREATE DEDICATED USER #================================================= #REMOVEME? ynh_script_progression --message="Configuring system user..." --weight=1 # Create a system user #REMOVEME? ynh_system_user_create --username=$app --home_dir=$data_path #================================================= # SPECIFIC SETUP #================================================= # SET UP BACKEND #================================================= ynh_script_progression --message="Setting up backend..." --weight=60 set_up_backend #================================================= # SET UP FRONTEND #================================================= ynh_script_progression --message="Setting up frontend..." --weight=15 set_up_frontend #================================================= # SETUP SYSTEMD #================================================= ynh_script_progression --message="Configuring systemd services..." --weight=1 # Create dedicated systemd configs ynh_add_systemd_config --service=$app-backend --template=backend.service ynh_add_systemd_config --service=$app-frontend --template=frontend.service ynh_add_systemd_config --service=$app-image-similarity --template=image-similarity.service ynh_add_systemd_config --service=$app-worker --template=worker.service #================================================= # ADD CONFIGURATIONS #================================================= ynh_script_progression --message="Generating configuration files..." --weight=1 add_configuations #================================================= # FINALIZE DATABASE #================================================= ynh_script_progression --message="Finalizing database..." --weight=1 upgrade_db pushd "$install_dir/backend" admin_mail="$(ynh_user_get_info $admin 'mail')" sudo -u $app bash -c " set -a export PATH=\"$path_prefix:"'$PATH'"\" source \"$install_dir\"/librephotos.env python3 manage.py createsuperuser --noinput --username \"$admin\" --email \"$admin_mail\" " 2>&1 for user in $(ynh_user_list); do mail=$(ynh_user_get_info --username="$user" --key=mail) sudo -u $app bash -c " set -a export PATH=\"$path_prefix:"'$PATH'"\" source \"$install_dir\"/librephotos.env python3 manage.py shell " <<< " from django.contrib.auth import get_user_model User = get_user_model() try: user = User.objects.get(username='$user') user.scan_directory='/home/yunohost.multimedia/$user/Picture' user.save() except User.DoesNotExist: User.objects.create_user('$user', email='$mail', scan_directory='/home/yunohost.multimedia/$user/Picture') " 2>&1 done popd #================================================= # YUNOHOST MULTIMEDIA INTEGRATION #================================================= ynh_script_progression --message="Adding multimedia directories..." --weight=1 # Build YunoHost multimedia directories ynh_multimedia_build_main_dir if [ $allow_multimedia_write -eq 1 ]; then ynh_multimedia_addaccess $app fi #================================================= # GENERIC FINALIZATION #================================================= # SECURE FILES AND DIRECTORIES #================================================= # Set permissions to app files set_permissions #================================================= # SETUP LOGROTATE #================================================= ynh_script_progression --message="Configuring log rotation..." --weight=1 # Use logrotate to manage application logfiles set_up_logrotate #================================================= # INTEGRATE SERVICES IN YUNOHOST #================================================= ynh_script_progression --message="Integrating services in YunoHost..." --weight=1 yunohost service add $app-backend --description="Backend for librephotos" --log="/var/log/$app/gunicorn_django.log" yunohost service add $app-frontend --description="Frontend for librephotos" --log="/var/log/$app/$app-frontend.log" yunohost service add $app-image-similarity --description="Image similarity server for librephotos" --log="/var/log/$app/image_similarity.log" yunohost service add $app-worker --description="Worker for librephotos" --log="/var/log/$app/$app-worker.log" #================================================= # START SYSTEMD SERVICES #================================================= ynh_script_progression --message="Starting systemd services..." --weight=1 # Start systemd services ynh_systemd_action --service_name=$app-backend --action="start" --log_path="/var/log/$app/gunicorn_django.log" --line_match="Listening at: http" ynh_systemd_action --service_name=$app-frontend --action="start" --log_path="systemd" --line_match="INFO: Accepting connections at http:" ynh_systemd_action --service_name=$app-image-similarity --action="start" --log_path="/var/log/$app/image_similarity.log" ynh_systemd_action --service_name=$app-worker --action="start" --log_path="/var/log/$app/$app-worker.log" #================================================= # SETUP SSOWAT #================================================= #REMOVEME? ynh_script_progression --message="Configuring permissions..." --weight=1 # Make app public if necessary #REMOVEME? if [ $is_public -eq 1 ] then # Everyone can access the app. # The "main" permission is automatically created before the install script. #REMOVEME? ynh_permission_update --permission="main" --add="visitors" fi #================================================= # RELOAD NGINX #================================================= #REMOVEME? ynh_script_progression --message="Reloading NGINX web server..." --weight=1 #REMOVEME? ynh_systemd_action --service_name=nginx --action=reload #================================================= # END OF SCRIPT #================================================= ynh_script_progression --message="Installation of $app completed" --last