2022-04-02 17:44:57 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# IMPORT GENERIC HELPERS
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
source _common.sh
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# MANAGE SCRIPT FAILURE
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
# Exit if an error occurs during the execution of the script
|
|
|
|
ynh_abort_if_errors
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Validating installation parameters..."
|
|
|
|
|
|
|
|
# Path for e.g. "static" files, served by nginx:
|
|
|
|
test ! -e "$public_path" || ynh_die --message="This path already contains a folder"
|
|
|
|
|
|
|
|
# Path for own config files, e.g.: Django's settings.py:
|
|
|
|
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"
|
|
|
|
|
|
|
|
mkdir -p "$public_path/media" "$public_path/static"
|
|
|
|
mkdir -p "$final_path"
|
|
|
|
|
|
|
|
mkdir -p "$log_path"
|
|
|
|
touch "${log_file}"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STORE SETTINGS FROM MANIFEST
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Storing installation settings..."
|
|
|
|
|
|
|
|
ynh_app_setting_set --app="$app" --key=admin --value="$admin"
|
|
|
|
ynh_app_setting_set --app="$app" --key=public_path --value="$public_path"
|
|
|
|
ynh_app_setting_set --app="$app" --key=final_path --value="$final_path"
|
|
|
|
ynh_app_setting_set --app="$app" --key=log_path --value="$log_file"
|
|
|
|
|
|
|
|
ynh_app_setting_set --app="$app" --key=domain --value="$domain"
|
|
|
|
ynh_app_setting_set --app="$app" --key=path --value="$path_url"
|
|
|
|
|
|
|
|
# Find a free port
|
|
|
|
port=$(ynh_find_port --port=8000)
|
|
|
|
# Set port as application setting
|
2022-08-16 09:47:54 +02:00
|
|
|
# https://yunohost.org/en/contribute/packaging_apps/helpers
|
|
|
|
# https://github.com/YunoHost/yunohost/blob/dev/helpers/setting
|
2022-04-02 17:44:57 +02:00
|
|
|
ynh_app_setting_set --app="$app" --key=port --value="$port"
|
|
|
|
|
|
|
|
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
|
2022-08-16 09:47:54 +02:00
|
|
|
|
2022-04-02 17:44:57 +02:00
|
|
|
redis_db=$(ynh_redis_get_free_db)
|
2022-08-16 09:47:54 +02:00
|
|
|
ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db"
|
|
|
|
|
|
|
|
#-------------------------------------------------
|
|
|
|
# config_panel.toml settings:
|
2022-04-02 17:44:57 +02:00
|
|
|
|
2022-08-16 09:47:54 +02:00
|
|
|
ynh_app_setting_set --app="$app" --key=debug_enabled --value="$debug_enabled"
|
|
|
|
ynh_app_setting_set --app="$app" --key=log_level --value="$log_level"
|
|
|
|
ynh_app_setting_set --app="$app" --key=admin_email --value="$admin_email"
|
|
|
|
ynh_app_setting_set --app="$app" --key=default_from_email --value="$default_from_email"
|
2022-05-18 18:11:49 +02:00
|
|
|
|
2022-04-02 17:44:57 +02:00
|
|
|
#=================================================
|
|
|
|
# STANDARD MODIFICATIONS
|
|
|
|
#=================================================
|
|
|
|
# INSTALL DEPENDENCIES
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Installing dependencies..." --weight=20
|
|
|
|
|
|
|
|
ynh_exec_warn_less ynh_install_app_dependencies "$pkg_dependencies"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CREATE A PostgreSQL DATABASE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Creating a PostgreSQL database..."
|
|
|
|
|
|
|
|
db_name=$(ynh_sanitize_dbid --db_name="$app")
|
|
|
|
db_user=$db_name
|
|
|
|
ynh_app_setting_set --app="$app" --key=db_name --value="$db_name"
|
|
|
|
|
|
|
|
ynh_psql_test_if_first_run
|
|
|
|
|
|
|
|
# Initialize database and store postgres password for upgrade
|
|
|
|
ynh_psql_setup_db --db_user="$db_user" --db_name="$db_name"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# NGINX CONFIGURATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring nginx web server..."
|
|
|
|
|
|
|
|
# Create a dedicated nginx config
|
2022-08-16 09:47:54 +02:00
|
|
|
# https://yunohost.org/en/contribute/packaging_apps/helpers
|
|
|
|
# https://github.com/YunoHost/yunohost/blob/dev/helpers/nginx
|
2022-04-02 17:44:57 +02:00
|
|
|
ynh_add_nginx_config "public_path" "port"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# CREATE DEDICATED USER
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring system user..."
|
|
|
|
|
|
|
|
# A home directory for venv and settings etc.
|
|
|
|
ynh_system_user_create --username="$app" --home_dir="$final_path" --use_shell
|
|
|
|
|
|
|
|
#=================================================
|
2022-08-16 09:47:54 +02:00
|
|
|
# PYTHON VIRTUALENV
|
2022-04-02 17:44:57 +02:00
|
|
|
#=================================================
|
2022-08-16 09:47:54 +02:00
|
|
|
ynh_script_progression --message="Create Python virtualenv..." --weight=5
|
2022-04-02 17:44:57 +02:00
|
|
|
|
|
|
|
# Always recreate everything fresh with current python version
|
|
|
|
ynh_secure_remove "${final_path}/venv"
|
|
|
|
|
|
|
|
# Skip pip because of: https://github.com/YunoHost/issues/issues/1960
|
|
|
|
python3 -m venv --without-pip "${final_path}/venv"
|
|
|
|
|
|
|
|
cp ../conf/requirements.txt "$final_path/requirements.txt"
|
|
|
|
chown -R "$app:" "$final_path"
|
|
|
|
|
2022-08-16 09:47:54 +02:00
|
|
|
#=================================================
|
|
|
|
# PIP INSTALLATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Install project via pip..." --weight=45
|
|
|
|
|
2022-04-02 17:44:57 +02:00
|
|
|
#run source in a 'sub shell'
|
|
|
|
(
|
|
|
|
set +o nounset
|
|
|
|
source "${final_path}/venv/bin/activate"
|
|
|
|
set -o nounset
|
|
|
|
ynh_exec_as $app $final_path/venv/bin/python3 -m ensurepip
|
|
|
|
ynh_exec_as $app $final_path/venv/bin/pip3 install --upgrade wheel pip setuptools
|
|
|
|
ynh_exec_as $app $final_path/venv/bin/pip3 install --no-deps -r "$final_path/requirements.txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# copy config files
|
|
|
|
# ================================================
|
|
|
|
ynh_script_progression --message="Create project configuration files..."
|
|
|
|
|
2022-08-16 09:47:54 +02:00
|
|
|
ynh_add_config --template="gunicorn.conf.py" --destination="$final_path/gunicorn.conf.py"
|
2022-04-02 17:44:57 +02:00
|
|
|
|
2022-08-16 09:47:54 +02:00
|
|
|
ynh_add_config --template="manage.py" --destination="$final_path/manage.py"
|
2022-04-02 17:44:57 +02:00
|
|
|
chmod +x "$final_path/manage.py"
|
|
|
|
|
2022-08-16 09:47:54 +02:00
|
|
|
ynh_add_config --template="settings.py" --destination="$final_path/settings.py"
|
|
|
|
ynh_add_config --template="setup_user.py" --destination="$final_path/setup_user.py"
|
|
|
|
ynh_add_config --template="urls.py" --destination="$final_path/urls.py"
|
|
|
|
ynh_add_config --template="wsgi.py" --destination="$final_path/wsgi.py"
|
2022-04-02 17:44:57 +02:00
|
|
|
|
2022-08-16 09:47:54 +02:00
|
|
|
touch "$final_path/local_settings.py"
|
2022-04-02 17:44:57 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# MIGRATE / COLLECTSTATIC / CREATEADMIN
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight=10
|
|
|
|
|
2022-04-02 19:34:39 +02:00
|
|
|
cd "$final_path" || exit
|
2022-04-02 17:44:57 +02:00
|
|
|
|
2022-04-02 19:34:39 +02:00
|
|
|
# Just for debugging:
|
|
|
|
./manage.py diffsettings
|
2022-04-02 17:44:57 +02:00
|
|
|
|
2022-04-02 19:34:39 +02:00
|
|
|
./manage.py migrate --no-input
|
|
|
|
./manage.py collectstatic --no-input
|
2022-04-02 17:44:57 +02:00
|
|
|
|
2022-04-02 19:34:39 +02:00
|
|
|
# Create/update Django superuser (set unusable password, because auth done via SSOwat):
|
2022-08-16 09:47:54 +02:00
|
|
|
./manage.py create_superuser --username="$admin" --email="$(ynh_user_get_info "$admin" mail)"
|
2022-04-02 19:34:39 +02:00
|
|
|
|
|
|
|
# Check the configuration
|
|
|
|
# This may fail in some cases with errors, etc., but the app works and the user can fix issues later.
|
|
|
|
./manage.py check --deploy || true
|
2022-04-02 17:44:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SETUP LOGROTATE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring log rotation..."
|
|
|
|
|
|
|
|
# Use logrotate to manage app-specific logfile(s)
|
|
|
|
ynh_use_logrotate "$log_file"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Integrating service in YunoHost..."
|
|
|
|
|
|
|
|
yunohost service add $app --description="Web based management to catalog things" --log="${log_file}"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
|
|
|
# SECURE FILES AND DIRECTORIES
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
# Set permissions to app files
|
|
|
|
chown -R "$app:" "$log_path"
|
|
|
|
chown -R "$app:www-data" "$public_path"
|
|
|
|
chown -R "$app:" "$final_path"
|
|
|
|
|
|
|
|
chmod o-rwx "$log_path"
|
|
|
|
chmod o-rwx "$public_path"
|
|
|
|
chmod o-rwx "$final_path"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SETUP SYSTEMD
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring a systemd service..."
|
|
|
|
|
2022-08-16 09:47:54 +02:00
|
|
|
# https://yunohost.org/en/contribute/packaging_apps/helpers
|
|
|
|
# https://github.com/YunoHost/yunohost/blob/dev/helpers/systemd
|
|
|
|
ynh_add_systemd_config --service="$app" --template="systemd.service"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SETUP SSOWAT
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring SSOwat..."
|
|
|
|
|
|
|
|
# Currently not used: django-fritzconnection has no public pages, yet!
|
|
|
|
#
|
|
|
|
## Make app public if necessary or protect it
|
|
|
|
#if [ $is_public -eq 1 ]
|
|
|
|
#then
|
|
|
|
# # Everyone can access the app.
|
|
|
|
# # The "main" permission is automatically created before the install script.
|
|
|
|
# ynh_permission_update --permission "main" --add "visitors"
|
|
|
|
#fi
|
2022-04-02 17:44:57 +02:00
|
|
|
|
|
|
|
#=================================================
|
2022-08-16 09:47:54 +02:00
|
|
|
# Start the app server via systemd
|
2022-04-02 17:44:57 +02:00
|
|
|
#=================================================
|
2022-08-16 09:47:54 +02:00
|
|
|
ynh_script_progression --message="Starting the application..." --weight=5
|
2022-04-02 17:44:57 +02:00
|
|
|
|
|
|
|
ynh_systemd_action --service_name="$app" --action="start"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# RELOAD NGINX
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Reloading nginx web server..."
|
|
|
|
|
|
|
|
ynh_systemd_action --service_name="nginx" --action="reload"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# END OF SCRIPT
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|