1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pytition_ynh.git synced 2024-09-03 20:16:08 +02:00
pytition_ynh/scripts/install

272 lines
10 KiB
Text
Raw Normal View History

2014-10-20 18:55:53 +02:00
#!/bin/bash
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# GENERIC START
2017-06-02 18:23:51 +02:00
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2017-06-05 13:11:48 +02:00
source _common.sh
2017-06-02 18:23:51 +02:00
source /usr/share/yunohost/helpers
#=================================================
2017-06-17 17:49:26 +02:00
# MANAGE SCRIPT FAILURE
2017-06-02 18:23:51 +02:00
#=================================================
2018-06-28 22:05:35 +02:00
ynh_clean_setup () {
true
}
2017-06-17 17:49:26 +02:00
# Exit if an error occurs during the execution of the script
2017-06-02 18:23:51 +02:00
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC
language=$YNH_APP_ARG_LANGUAGE
2022-04-05 16:15:54 +02:00
# admin=$YNH_APP_ARG_ADMIN
2018-06-28 22:05:35 +02:00
password=$YNH_APP_ARG_PASSWORD
app=$YNH_APP_INSTANCE_NAME
2014-10-20 18:55:53 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
2017-06-02 18:23:51 +02:00
#=================================================
2019-04-18 19:58:47 +02:00
ynh_script_progression --message="Validating installation parameters..." --time --weight=1
2014-10-20 18:55:53 +02:00
2017-08-28 23:55:51 +02:00
final_path=/var/www/$app
2019-05-07 12:03:25 +02:00
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
2017-08-28 23:55:51 +02:00
2019-04-16 00:32:39 +02:00
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
2014-10-20 18:55:53 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
2019-04-18 19:58:47 +02:00
ynh_script_progression --message="Storing installation settings..." --time --weight=1
2019-04-16 00:32:39 +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=language --value=$language
2022-04-05 16:15:54 +02:00
ynh_app_setting_set --app=$app --key=password --value=$password
2017-06-02 18:23:51 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2019-04-18 19:58:47 +02:00
ynh_script_progression --message="Installing dependencies..." --time --weight=1
2017-06-02 18:23:51 +02:00
2022-04-05 16:15:54 +02:00
ynh_install_app_dependencies "${pkg_dependencies[@]}"
2017-06-02 18:23:51 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..." --time --weight=1
# Create a system user
ynh_system_user_create --username=$app --home_dir="$final_path"
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# CREATE A MYSQL DATABASE
2017-06-02 18:23:51 +02:00
#=================================================
2019-04-18 19:58:47 +02:00
ynh_script_progression --message="Creating a MySQL database..." --time --weight=1
2018-06-28 22:05:35 +02:00
2019-05-07 12:03:25 +02:00
db_name=$(ynh_sanitize_dbid --db_name=$app)
2019-05-02 21:04:03 +02:00
db_user=$db_name
2019-04-16 00:32:39 +02:00
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
2019-05-02 21:04:03 +02:00
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name
2017-06-02 18:23:51 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2019-04-18 19:58:47 +02:00
ynh_script_progression --message="Setting up source files..." --time --weight=1
2017-06-02 18:23:51 +02:00
2019-04-16 00:32:39 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
2022-04-05 16:15:54 +02:00
mkdir -p "$final_path/static"
mkdir -p "$final_path/mediaroot"
ynh_setup_source --dest_dir="$final_path/pytition"
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
2021-04-23 19:54:08 +02:00
chown -R $app:www-data "$final_path"
2017-06-02 18:23:51 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2020-08-02 15:41:28 +02:00
ynh_script_progression --message="Configuring NGINX web server..." --time --weight=1
ynh_add_nginx_config
2017-06-02 18:23:51 +02:00
#=================================================
# SPECIFIC SETUP
#=================================================
# ...
#=================================================
2022-04-05 16:15:54 +02:00
ynh_script_progression --message="Installing python dependencies..." --time --weight=1
# Setup virtualenv and install dependencies
virtualenv --python=python3 --system-site-packages "${final_path}/venv"
(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
python3 -m pip install --upgrade pip
python3 -m pip install -r "$final_path/pytition/requirements.txt"
)
2017-06-02 18:23:51 +02:00
2021-04-29 20:58:35 +02:00
#=================================================
# CREATE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Creating a data directory..." --time --weight=1
2022-04-05 16:15:54 +02:00
mkdir -p "/var/log/$app"
chmod 750 "/var/log/$app"
chown -R $app:www-data "/var/log/$app"
2021-04-29 20:58:35 +02:00
2022-04-05 16:15:54 +02:00
# ### Use these lines if you need to create a directory to store "persistent files" for the application.
# ### Usually this directory is used to store uploaded files or any file that won't be updated during
# ### an upgrade and that won't be deleted during app removal unless "--purge" option is used.
# ### If you're not using these lines:
# ### - Remove the section "BACKUP THE DATA DIR" in the backup script
# ### - Remove the section "RESTORE THE DATA DIRECTORY" in the restore script
# ### - As well as the section "REMOVE DATA DIR" in the remove script
2021-04-29 20:58:35 +02:00
2022-04-05 16:15:54 +02:00
# datadir=/home/yunohost.app/$app
# ynh_app_setting_set --app=$app --key=datadir --value=$datadir
2021-04-29 20:58:35 +02:00
2022-04-05 16:15:54 +02:00
# mkdir -p $datadir
# # FIXME: this should be managed by the core in the future
# # Here, as a packager, you may have to tweak the ownerhsip/permissions
# # such that the appropriate users (e.g. maybe www-data) can access
# # files in some cases.
# # But FOR THE LOVE OF GOD, do not allow r/x for "others" on the entire folder -
# # this will be treated as a security issue.
# chmod 750 "$datadir"
# chmod -R o-rwx "$datadir"
# chown -R $app:www-data "$datadir"
2021-04-29 20:58:35 +02:00
2021-04-23 20:00:41 +02:00
#=================================================
# ADD A CONFIGURATION
#=================================================
2022-04-05 16:15:54 +02:00
ynh_script_progression --message="Adding configuration files..." --time --weight=1
2021-04-23 20:00:41 +02:00
2022-04-05 16:15:54 +02:00
secret_key=$(
set +o nounset
source "${final_path}/venv/bin/activate"
set -o nounset
python3 -c "from django.core.management.utils import get_random_secret_key as g; print(g())"
)
2021-04-23 20:00:41 +02:00
2022-04-05 16:15:54 +02:00
config_path="$final_path/pytition/pytition/pytition/settings/config.py"
ynh_add_config --template="../conf/config.py" --destination="$config_path"
2021-04-23 20:00:41 +02:00
# FIXME: this should be handled by the core in the future
# You may need to use chmod 600 instead of 400,
# for example if the app is expected to be able to modify its own config
2022-04-05 16:15:54 +02:00
chmod 400 "$config_path"
chown $app:$app "$config_path"
2021-04-23 20:00:41 +02:00
2022-04-05 16:15:54 +02:00
ynh_add_config --template="../conf/uwsgi.yaml" --destination="/etc/uwsgi/apps-available/$app.yaml"
chmod 400 "/etc/uwsgi/apps-available/$app.yaml"
chown $app:$app "/etc/uwsgi/apps-available/$app.yaml"
2021-04-23 20:00:41 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
2022-04-05 16:15:54 +02:00
# Run app configuration
2017-06-02 18:23:51 +02:00
#=================================================
2022-04-05 16:15:54 +02:00
ynh_script_progression --message="Running app configuration..." --time --weight=1
2017-06-02 18:23:51 +02:00
2022-04-05 16:15:54 +02:00
ynh_exec_as $app bash -c "
set +o nounset
source '$final_path/venv/bin/activate'
set -o nounset
cd '$final_path/pytition/pytition'
export DJANGO_SETTINGS_MODULE=pytition.settings.config
python3 manage.py migrate
python3 manage.py collectstatic
python3 manage.py compilemessages
DJANGO_SUPERUSER_PASSWORD='$password' \
python3 manage.py createsuperuser --noinput --username admin --email 'admin@$domain'
"
2018-06-28 22:05:35 +02:00
2022-04-05 16:15:54 +02:00
chmod 400 "$final_path/static"
chown -R $app:www-data "$final_path/static"
2017-06-02 18:23:51 +02:00
#=================================================
2022-04-05 16:15:54 +02:00
# SETUP SYSTEMD
2017-06-02 18:23:51 +02:00
#=================================================
2022-04-05 16:15:54 +02:00
ynh_script_progression --message="Configuring a systemd service..." --time --weight=1
2017-06-02 18:23:51 +02:00
2022-04-05 16:15:54 +02:00
# Create a dedicated systemd config
ynh_add_systemd_config
2018-06-28 22:05:35 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
2017-06-17 17:49:26 +02:00
# GENERIC FINALIZATION
2017-06-02 18:23:51 +02:00
#=================================================
# SETUP LOGROTATE
#=================================================
2019-04-18 19:58:47 +02:00
ynh_script_progression --message="Configuring log rotation..." --time --weight=1
2017-06-02 18:23:51 +02:00
2017-06-17 17:49:26 +02:00
# Use logrotate to manage application logfile(s)
2022-04-05 16:15:54 +02:00
# TODO: ynh_use_logrotate
2017-06-02 18:23:51 +02:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
2017-06-02 18:23:51 +02:00
#=================================================
2020-06-04 16:51:14 +02:00
ynh_script_progression --message="Integrating service in YunoHost..." --time --weight=1
2017-06-02 18:23:51 +02:00
2022-04-05 16:15:54 +02:00
yunohost service add $app --description="Pytition uWSGI app $app" --log="/var/log/$app/$app.log"
2017-06-02 18:23:51 +02:00
2019-05-02 20:44:22 +02:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --time --weight=1
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
2019-04-18 19:58:47 +02:00
#=================================================
# SETUP FAIL2BAN
#=================================================
ynh_script_progression --message="Configuring Fail2Ban..." --time --weight=1
2019-04-18 19:58:47 +02:00
# Create a dedicated Fail2Ban config
2022-04-05 16:15:54 +02:00
# TODO: ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failregex="Regex to match into the log for a failed login"
2019-04-18 19:58:47 +02:00
2017-06-02 18:23:51 +02:00
#=================================================
# SETUP SSOWAT
#=================================================
2020-02-08 17:16:25 +01:00
ynh_script_progression --message="Configuring permissions..." --time --weight=1
2017-06-02 18:23:51 +02:00
# Make app public if necessary
if [ $is_public -eq 1 ]
then
2022-04-05 16:15:54 +02:00
# Everyone can access the app.
# The "main" permission is automatically created before the install script.
ynh_permission_update --permission="main" --add="visitors"
2014-10-20 18:55:53 +02:00
fi
2020-02-08 17:16:25 +01:00
# Only the admin can access the admin panel of the app (if the app has an admin panel)
2022-04-05 16:15:54 +02:00
# ynh_permission_create --permission="admin" --url="/admin" --allowed=$admin
2021-01-06 13:31:32 +01:00
2021-09-16 12:40:15 +02:00
# Everyone can access the API part
# We don't want to display the tile in the SSO so we put --show_tile="false"
# And we don't want the YunoHost admin to be able to remove visitors group to this permission, so we put --protected="true"
2022-04-05 16:15:54 +02:00
# ynh_permission_create --permission="api" --url="/api" --allowed="visitors" --show_tile="false" --protected="true"
2020-02-08 17:16:25 +01:00
2017-06-02 18:23:51 +02:00
#=================================================
# RELOAD NGINX
#=================================================
2020-08-02 15:41:28 +02:00
ynh_script_progression --message="Reloading NGINX web server..." --time --weight=1
2019-02-10 15:02:38 +01:00
2019-04-18 19:58:47 +02:00
ynh_systemd_action --service_name=nginx --action=reload
2019-02-10 15:02:38 +01:00
#=================================================
# END OF SCRIPT
#=================================================
2019-04-18 19:58:47 +02:00
ynh_script_progression --message="Installation of $app completed" --time --last