1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/paperless-ngx_ynh.git synced 2024-09-03 19:56:33 +02:00
paperless-ngx_ynh/scripts/install

285 lines
10 KiB
Text
Raw Normal View History

2022-03-29 11:08:51 +02:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
2022-12-30 12:24:18 +01:00
source ynh_redis
2022-03-29 11:08:51 +02:00
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
### Remove this function if there's nothing to clean before calling the remove script.
true
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
2022-03-29 17:50:14 +02:00
path_url="/"
2022-12-30 12:24:18 +01:00
is_public=$YNH_APP_ARG_IS_PUBLIC
is_public_api=$YNH_APP_ARG_IS_PUBLIC_API
2022-03-29 11:08:51 +02:00
admin=$YNH_APP_ARG_ADMIN
2022-12-30 12:24:18 +01:00
admin_pw=$YNH_APP_ARG_ADMIN_PW
2022-03-29 17:50:14 +02:00
2022-03-29 11:08:51 +02:00
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Validating installation parameters..." --weight=1
2022-03-29 17:50:14 +02:00
final_path=/opt/yunohost/$app
2022-03-29 11:08:51 +02:00
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Storing installation settings..." --weight=1
2022-03-29 11:08:51 +02:00
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=admin --value=$admin
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Finding an available port..." --weight=1
2022-03-29 11:08:51 +02:00
# Find an available port
port=$(ynh_find_port --port=8095)
ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Installing dependencies..." --weight=1
2022-03-29 11:08:51 +02:00
2022-03-29 17:50:14 +02:00
# FIXME: Only on a Raspberry Pi (armv6 v7?)
# ynh_add_app_dependencies $raspberry_pkg_dependencies
2022-12-30 12:24:18 +01:00
ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies $ocr_pkg_dependencies
2022-03-29 11:08:51 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Configuring system user..." --weight=1
2022-03-29 11:08:51 +02:00
# Create a system user
ynh_system_user_create --username=$app --home_dir="$final_path"
#=================================================
2022-03-29 17:50:14 +02:00
# CREATE A POSTGRESQL DATABASE
2022-03-29 11:08:51 +02:00
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Creating a PostgreSQL database..." --weight=1
2022-03-29 11:08:51 +02:00
db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
2022-12-30 12:24:18 +01:00
2022-03-29 17:50:14 +02:00
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name
2022-03-29 11:08:51 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Setting up source files..." --weight=1
2022-03-29 11:08:51 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
2022-03-29 17:50:14 +02:00
2022-03-29 11:08:51 +02:00
ynh_setup_source --dest_dir="$final_path"
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
2022-03-29 17:50:14 +02:00
chown -R $app:$app "$final_path"
2022-03-29 11:08:51 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Configuring NGINX web server..." --weight=1
2022-03-29 11:08:51 +02:00
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC SETUP
#=================================================
2022-03-29 17:50:14 +02:00
# INSTALL PYTHON DEPENDENCIES
2022-03-29 11:08:51 +02:00
#=================================================
2022-03-29 17:50:14 +02:00
ynh_script_progression --message="Installing Python dependencies..."
pushd $final_path
python3 -m venv venv
2022-12-30 12:24:18 +01:00
chown -R "$app:" "$final_path"
(
source "$final_path/venv/bin/activate"
ynh_exec_as $app $final_path/venv/bin/pip3 install --upgrade pip setuptools wheel
ynh_exec_as $app $final_path/venv/bin/pip3 install -r "$final_path/requirements.txt"
2023-01-11 21:13:25 +01:00
# Installing NLTK data
mkdir -p "$final_path/nltk_data"
chown -R "$app:" "$final_path/nltk_data"
ynh_exec_warn_less ynh_exec_as $app $final_path/venv/bin/python3 -m nltk.downloader -d "$final_path/nltk_data" snowball_data
ynh_exec_warn_less ynh_exec_as $app $final_path/venv/bin/python3 -m nltk.downloader -d "$final_path/nltk_data" stopwords
ynh_exec_warn_less ynh_exec_as $app $final_path/venv/bin/python3 -m nltk.downloader -d "$final_path/nltk_data" punkt
2022-12-30 12:24:18 +01:00
deactivate
)
2022-03-29 17:50:14 +02:00
popd
2022-03-29 11:08:51 +02:00
#=================================================
# CREATE DATA DIRECTORY
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Creating a data directory..." --weight=1
2022-03-29 11:08:51 +02:00
datadir=/home/yunohost.app/$app
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
2022-12-30 12:24:18 +01:00
mkdir -p "$datadir/consume"
mkdir -p "$datadir/data"
mkdir -p "$datadir/media"
2022-03-29 11:08:51 +02:00
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
chown -R $app:www-data "$datadir"
#=================================================
# ADD A CONFIGURATION
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Adding a configuration file..." --weight=1
redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app=$app --key=redis_db --value=$redis_db
2022-03-29 11:08:51 +02:00
2022-03-29 17:50:14 +02:00
paperless_secret_key=$(ynh_string_random)
2022-12-30 12:24:18 +01:00
ynh_app_setting_set --app=$app --key=paperless_secret_key --value=$paperless_secret_key
2022-03-29 17:50:14 +02:00
2023-01-11 21:12:19 +01:00
ocr_language="eng"
ynh_app_setting_set --app=$app --key=ocr_language --value=$ocr_language
2022-03-29 17:50:14 +02:00
ynh_add_config --template="paperless.conf.example" --destination="$final_path/paperless.conf"
chmod 400 "$final_path/paperless.conf"
chown $app:$app "$final_path/paperless.conf"
2022-03-29 11:08:51 +02:00
#=================================================
2022-03-29 17:50:14 +02:00
# SETUP THE DATABASE
2022-03-29 11:08:51 +02:00
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Setting up the database..." --weight=1
2022-03-29 11:08:51 +02:00
2022-12-30 12:24:18 +01:00
pushd "$final_path/src"
(
source "$final_path/venv/bin/activate"
ynh_exec_as $app $final_path/venv/bin/python manage.py migrate
deactivate
)
2022-03-29 17:50:14 +02:00
popd
2022-03-29 11:08:51 +02:00
2022-03-29 17:50:14 +02:00
#=================================================
# CREATE THE ADMIN USER
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Creating the admin user..." --weight=1
2022-03-29 11:08:51 +02:00
2022-12-30 12:24:18 +01:00
pushd "$final_path/src"
(
source "$final_path/venv/bin/activate"
2022-03-29 17:50:14 +02:00
email=$(ynh_user_get_info $admin 'mail')
2022-12-30 12:24:18 +01:00
ynh_exec_as $app env "DJANGO_SUPERUSER_PASSWORD=$admin_pw" $final_path/venv/bin/python3 manage.py createsuperuser --noinput --username "$admin" --email "$email"
deactivate
)
2022-03-29 17:50:14 +02:00
popd
2022-03-29 11:08:51 +02:00
2022-12-30 12:24:18 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Configuring a systemd service..." --weight=1
# Create a dedicated systemd config
ynh_add_systemd_config --service="$app" --template="systemd.service"
2022-12-30 19:30:56 +01:00
ynh_add_systemd_config --service="$app-consumer" --template="systemd-consumer.service"
ynh_add_systemd_config --service="$app-scheduler" --template="systemd-scheduler.service"
ynh_add_systemd_config --service="$app-task-queue" --template="systemd-task-queue.service"
2022-12-30 12:24:18 +01:00
2022-03-29 11:08:51 +02:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Configuring log rotation..." --weight=1
2022-03-29 11:08:51 +02:00
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2022-03-29 11:08:51 +02:00
2022-12-30 12:24:18 +01:00
yunohost service add "$app" --log="/var/log/$app/$app.log"
2022-12-30 19:30:56 +01:00
yunohost service add "$app-consumer" --log="/var/log/$app/$app-consumer.log"
yunohost service add "$app-scheduler" --log="/var/log/$app/$app-scheduler.log"
yunohost service add "$app-task-queue" --log="/var/log/$app/$app-task-queue.log"
2022-03-29 11:08:51 +02:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Starting a systemd service..." --weight=1
2022-03-29 11:08:51 +02:00
2022-12-30 12:24:18 +01:00
ynh_systemd_action --service_name="$app" --action="start" --log_path="/var/log/$app/$app.log"
2022-12-30 19:30:56 +01:00
ynh_systemd_action --service_name="$app-consumer" --action="start" --log_path="/var/log/$app/$app-consumer.log"
ynh_systemd_action --service_name="$app-scheduler" --action="start" --log_path="/var/log/$app/$app-scheduler.log"
ynh_systemd_action --service_name="$app-task-queue" --action="start" --log_path="/var/log/$app/$app-task-queue.log"
2022-03-29 11:08:51 +02:00
#=================================================
# SETUP FAIL2BAN
#=================================================
2022-12-30 12:24:18 +01:00
#ynh_script_progression --message="Configuring Fail2Ban..." --weight=1
2022-03-29 11:08:51 +02:00
# Create a dedicated Fail2Ban config
2022-12-30 12:24:18 +01:00
#ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failregex="Regex to match into the log for a failed login"
2022-03-29 17:50:14 +02:00
# FIXME fail2ban
# ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-access.log" --failregex="<HOST>.* \"POST /api/v1/token/ HTTP/1.1\" 400 68.*$" --max_retry=5
2022-03-29 11:08:51 +02:00
#=================================================
# SETUP SSOWAT
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Configuring permissions..." --weight=1
# Make app public if necessary
if [ $is_public -eq 1 ]
then
ynh_permission_update --permission="main" --add="visitors"
fi
2022-03-29 11:08:51 +02:00
2022-12-30 12:24:18 +01:00
ynh_permission_create --permission="api" --url="/api" --allowed="all_users" --auth_header="false" --label="$app API" --show_tile="false" --protected="false"
if [ $is_public_api -eq 1 ]
then
ynh_permission_update --permission="api" --add="visitors"
fi
2022-03-29 11:08:51 +02:00
#=================================================
# RELOAD NGINX
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
2022-03-29 11:08:51 +02:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2022-12-30 12:24:18 +01:00
ynh_script_progression --message="Installation of $app completed" --last