2020-12-07 16:20:37 +01: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
|
2020-12-08 09:31:10 +01:00
|
|
|
ynh_webpath_register --app="$app" --domain="$domain" --path_url="$path_url"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
mkdir -p "$public_path/media" "$public_path/static"
|
|
|
|
mkdir -p "$final_path"
|
|
|
|
|
2020-12-09 17:49:11 +01:00
|
|
|
mkdir -p "$log_path"
|
|
|
|
touch "${log_file}"
|
2020-12-08 09:31:10 +01:00
|
|
|
|
2020-12-07 16:20:37 +01:00
|
|
|
#=================================================
|
|
|
|
# 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"
|
2020-12-08 09:31:10 +01:00
|
|
|
ynh_app_setting_set --app="$app" --key=log_path --value="$log_file"
|
|
|
|
|
2020-12-22 18:00:10 +01:00
|
|
|
ynh_app_setting_set --app="$app" --key=domain --value="$domain"
|
|
|
|
ynh_app_setting_set --app="$app" --key=path --value="$path_url"
|
|
|
|
|
2020-12-08 09:31:10 +01:00
|
|
|
# Find a free port
|
|
|
|
port=$(ynh_find_port --port=8000)
|
|
|
|
# Set port as application setting
|
|
|
|
# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/setting
|
|
|
|
ynh_app_setting_set --app="$app" --key=port --value="$port"
|
|
|
|
|
|
|
|
db_pwd=$(ynh_app_setting_get --app="$app" --key=psqlpwd)
|
|
|
|
admin_mail=$(ynh_user_get_info --username="$admin" --key=mail)
|
|
|
|
redis_db=$(ynh_redis_get_free_db)
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STANDARD MODIFICATIONS
|
|
|
|
#=================================================
|
|
|
|
# INSTALL DEPENDENCIES
|
|
|
|
#=================================================
|
2022-01-18 09:02:51 +01:00
|
|
|
ynh_script_progression --message="Installing dependencies..." --weight=20
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/nginx
|
2020-12-08 09:31:10 +01:00
|
|
|
ynh_add_nginx_config "public_path" "port"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# PIP INSTALLATION
|
|
|
|
#=================================================
|
2022-01-18 09:02:51 +01:00
|
|
|
ynh_script_progression --message="Install project via pip..." --weight=50
|
|
|
|
|
|
|
|
# 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"
|
2022-01-18 08:53:55 +01:00
|
|
|
|
|
|
|
cp ../conf/requirements.txt "$final_path/requirements.txt"
|
|
|
|
chown -R "$app:" "$final_path"
|
|
|
|
|
|
|
|
#run source in a 'sub shell'
|
|
|
|
(
|
|
|
|
set +o nounset
|
|
|
|
source "${final_path}/venv/bin/activate"
|
|
|
|
set -o nounset
|
2022-01-18 09:02:51 +01:00
|
|
|
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"
|
2022-01-18 08:53:55 +01:00
|
|
|
)
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
#=================================================
|
2020-12-08 09:31:10 +01:00
|
|
|
# copy config files
|
|
|
|
# ================================================
|
2020-12-29 13:58:40 +01:00
|
|
|
ynh_script_progression --message="Create project configuration files..."
|
2020-12-09 20:24:36 +01:00
|
|
|
|
2020-12-07 16:20:37 +01:00
|
|
|
gunicorn_conf="$final_path/gunicorn.conf.py"
|
|
|
|
cp "../conf/gunicorn.conf.py" "$gunicorn_conf"
|
|
|
|
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$gunicorn_conf"
|
2020-12-08 09:31:10 +01:00
|
|
|
ynh_replace_string --match_string="__LOG_FILE__" --replace_string="$log_file" --target_file="$gunicorn_conf"
|
2020-12-07 16:20:37 +01:00
|
|
|
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$gunicorn_conf"
|
2020-12-09 12:05:24 +01:00
|
|
|
ynh_store_file_checksum --file="$gunicorn_conf"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
cp ../conf/manage.py "$final_path/manage.py"
|
2022-07-26 19:50:37 +02:00
|
|
|
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$final_path/manage.py"
|
|
|
|
ynh_store_file_checksum --file="$final_path/manage.py"
|
2020-12-07 16:20:37 +01:00
|
|
|
chmod +x "$final_path/manage.py"
|
|
|
|
|
2020-12-29 13:58:40 +01:00
|
|
|
settings="$final_path/settings.py"
|
|
|
|
cp "../conf/settings.py" "$settings"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$settings"
|
|
|
|
ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$settings"
|
|
|
|
ynh_replace_string --match_string="__ADMIN__" --replace_string="$admin" --target_file="$settings"
|
|
|
|
ynh_replace_string --match_string="__ADMINMAIL__" --replace_string="$admin_mail" --target_file="$settings"
|
|
|
|
ynh_replace_string --match_string="__FINAL_HOME_PATH__" --replace_string="$final_path" --target_file="$settings"
|
|
|
|
ynh_replace_string --match_string="__FINAL_WWW_PATH__" --replace_string="$public_path" --target_file="$settings"
|
2020-12-08 09:31:10 +01:00
|
|
|
ynh_replace_string --match_string="__LOG_FILE__" --replace_string="$log_file" --target_file="$settings"
|
2020-12-07 16:20:37 +01:00
|
|
|
ynh_replace_string --match_string="__REDIS_DB__" --replace_string="$redis_db" --target_file="$settings"
|
2020-12-08 09:31:10 +01:00
|
|
|
|
2020-12-22 18:00:10 +01:00
|
|
|
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$settings"
|
|
|
|
ynh_replace_string --match_string="__PATH_URL__" --replace_string="$path_url" --target_file="$settings"
|
|
|
|
|
2020-12-08 09:31:10 +01:00
|
|
|
# Calculate and store the config file checksum into the app settings
|
|
|
|
ynh_store_file_checksum --file="$settings"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db"
|
|
|
|
|
2020-12-29 13:58:40 +01:00
|
|
|
cp ../conf/setup_user.py "$final_path/setup_user.py"
|
|
|
|
cp ../conf/urls.py "$final_path/urls.py"
|
|
|
|
cp ../conf/wsgi.py "$final_path/wsgi.py"
|
2020-12-08 09:31:10 +01:00
|
|
|
|
2020-12-29 13:58:40 +01:00
|
|
|
touch "$final_path/local_settings.py"
|
2020-12-09 12:05:24 +01:00
|
|
|
|
2020-12-07 16:20:37 +01:00
|
|
|
#=================================================
|
|
|
|
# MIGRATE / COLLECTSTATIC / CREATEADMIN
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="migrate/collectstatic/createadmin..." --weight=10
|
|
|
|
|
2022-07-26 19:50:37 +02:00
|
|
|
cd "$final_path" || exit
|
2020-12-07 16:20:37 +01:00
|
|
|
|
2022-07-26 19:50:37 +02:00
|
|
|
# Just for debugging:
|
|
|
|
./manage.py diffsettings
|
2020-12-07 16:20:37 +01:00
|
|
|
|
2022-07-26 19:50:37 +02:00
|
|
|
./manage.py migrate --no-input
|
|
|
|
./manage.py collectstatic --no-input
|
2020-12-29 13:58:40 +01:00
|
|
|
|
2022-07-26 19:50:37 +02:00
|
|
|
# Create/update Django superuser (set unusable password, because auth done via SSOwat):
|
|
|
|
./manage.py create_superuser --username="$admin" --email="$admin_mail"
|
|
|
|
|
|
|
|
# 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
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
#=================================================
|
2020-12-08 09:31:10 +01:00
|
|
|
# SETUP LOGROTATE
|
2020-12-07 16:20:37 +01:00
|
|
|
#=================================================
|
2020-12-08 09:31:10 +01:00
|
|
|
ynh_script_progression --message="Configuring log rotation..."
|
2020-12-07 16:20:37 +01:00
|
|
|
|
2020-12-08 09:31:10 +01:00
|
|
|
# Use logrotate to manage app-specific logfile(s)
|
|
|
|
ynh_use_logrotate "$log_file"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
#=================================================
|
2020-12-08 09:31:10 +01:00
|
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
2020-12-07 16:20:37 +01:00
|
|
|
#=================================================
|
2020-12-08 09:31:10 +01:00
|
|
|
ynh_script_progression --message="Integrating service in YunoHost..."
|
2020-12-07 16:20:37 +01:00
|
|
|
|
2020-12-09 17:58:19 +01:00
|
|
|
yunohost service add $app --description="Web based management to catalog things" --log="${log_file}"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
|
|
|
# SECURE FILES AND DIRECTORIES
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
# Set permissions to app files
|
2021-10-10 12:07:30 +02:00
|
|
|
chown -R "$app:" "$log_path"
|
2022-01-06 18:16:38 +01:00
|
|
|
chown -R "$app:www-data" "$public_path"
|
2021-10-10 12:07:30 +02:00
|
|
|
chown -R "$app:" "$final_path"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
2022-01-01 14:28:52 +01:00
|
|
|
chmod o-rwx "$log_path"
|
|
|
|
chmod o-rwx "$public_path"
|
|
|
|
chmod o-rwx "$final_path"
|
|
|
|
|
2020-12-07 16:20:37 +01:00
|
|
|
#=================================================
|
2020-12-08 09:31:10 +01:00
|
|
|
# SETUP SYSTEMD
|
2020-12-07 16:20:37 +01:00
|
|
|
#=================================================
|
2020-12-08 09:31:10 +01:00
|
|
|
ynh_script_progression --message="Configuring a systemd service..."
|
2020-12-07 16:20:37 +01:00
|
|
|
|
2020-12-08 09:31:10 +01:00
|
|
|
# https://github.com/YunoHost/yunohost/blob/dev/data/helpers.d/systemd
|
|
|
|
ynh_add_systemd_config --service="$app" --template="pyinventory.service"
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
#=================================================
|
2020-12-08 09:31:10 +01:00
|
|
|
# Start pyinventory via systemd
|
2020-12-07 16:20:37 +01:00
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Starting PyInventory's services..." --weight=5
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2020-12-09 17:49:11 +01:00
|
|
|
#=================================================
|
|
|
|
# END OF SCRIPT
|
|
|
|
#=================================================
|
2020-12-07 16:20:37 +01:00
|
|
|
|
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|