1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/piwigo_ynh.git synced 2024-09-03 20:06:03 +02:00
piwigo_ynh/scripts/install
Éric Gaspar 72f5a7a5aa v2
2023-10-05 21:38:07 +02:00

272 lines
10 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# 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
#REMOVEME? ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
#REMOVEME? domain=$YNH_APP_ARG_DOMAIN
#REMOVEME? path=$YNH_APP_ARG_PATH
#REMOVEME? is_public=$YNH_APP_ARG_IS_PUBLIC
#REMOVEME? language=$YNH_APP_ARG_LANGUAGE
#REMOVEME? admin=$YNH_APP_ARG_ADMIN
#REMOVEME? phpversion=$YNH_PHP_VERSION
#REMOVEME? app=$YNH_APP_INSTANCE_NAME
fpm_footprint="low"
fpm_free_footprint=0
fpm_usage="low"
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
#REMOVEME? ynh_script_progression --message="Validating installation parameters..."
#REMOVEME? install_dir=/var/www/$app
#REMOVEME? test ! -e "$install_dir" || ynh_die --message="This path already contains a folder"
# 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..."
#REMOVEME? ynh_app_setting_set --app=$app --key=domain --value=$domain
#REMOVEME? ynh_app_setting_set --app=$app --key=path --value=$path
ynh_app_setting_set --app=$app --key=language --value=$language
#REMOVEME? ynh_app_setting_set --app=$app --key=admin --value=$admin
ynh_app_setting_set --app=$app --key=fpm_footprint --value=$fpm_footprint
ynh_app_setting_set --app=$app --key=fpm_free_footprint --value=$fpm_free_footprint
ynh_app_setting_set --app=$app --key=fpm_usage --value=$fpm_usage
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# INSTALL DEPENDENCIES
#=================================================
#REMOVEME? ynh_script_progression --message="Installing dependencies..." --weight=40
#REMOVEME? ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE DEDICATED USER
#=================================================
#REMOVEME? ynh_script_progression --message="Configuring system user..." --weight=3
# Create a system user
#REMOVEME? ynh_system_user_create --username=$app --home_dir="$install_dir"
#=================================================
# CREATE A MYSQL DATABASE
#=================================================
#REMOVEME? ynh_script_progression --message="Creating a MySQL database..."
#REMOVEME? db_name=$(ynh_sanitize_dbid --db_name=$app)
#REMOVEME? db_user=$db_name
#REMOVEME? ynh_app_setting_set --app=$app --key=db_name --value=$db_name
#REMOVEME? ynh_mysql_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
#REMOVEME? ynh_app_setting_set --app=$app --key=install_dir --value=$install_dir
# Create tmp directory and fetch app inside
tmpdir="$(ynh_smart_mktemp --min_size=300)"
ynh_setup_source --dest_dir="$tmpdir"
# Fetch needed plugins
mkdir -p $tmpdir/plugins/Ldap_Login
ynh_setup_source --dest_dir="$tmpdir/plugins/Ldap_Login" --source_id=ldap_plugin
ynh_setup_source --dest_dir="$tmpdir/plugins" --source_id=log_failed_logins_plugin
# sets extended pattern matching options in the bash shell
shopt -s extglob
# Install files and set permissions
mkdir $install_dir
cp -a $tmpdir/!(upload|_data|galleries) $install_dir
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring PHP-FPM..." --weight=2
# Create a dedicated PHP-FPM config
ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint
#REMOVEME? phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring NGINX web server..."
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC SETUP
#=================================================
# CREATE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Creating a data directory..."
#REMOVEME? data_dir=/home/yunohost.app/$app
#REMOVEME? ynh_app_setting_set --app=$app --key=data_dir --value=$data_dir
mkdir -p $data_dir/_data
mkdir -p $data_dir/upload
mkdir -p $data_dir/galleries
ln -sd $data_dir/_data $install_dir/_data
ln -sd $data_dir/upload $install_dir/upload
ln -sd $data_dir/galleries $install_dir/galleries
cp -Rp $tmpdir/_data/. $install_dir/_data
cp -Rp $tmpdir/upload/. $install_dir/upload
cp -Rp $tmpdir/galleries/. $install_dir/galleries
ynh_secure_remove --file="$tmpdir"
chmod 750 "$data_dir"
chmod -R o-rwx "$data_dir"
chown -R $app:www-data "$data_dir"
#=================================================
# SETUP APPLICATION WITH CURL
#=================================================
ynh_script_progression --message="Setuping application with CURL..." --weight=5
# Set the app as temporarily public for curl call
ynh_script_progression --message="Configuring SSOwat..."
# Making the app public for curl
#REMOVEME? ynh_permission_update --permission="main" --add="visitors"
# Reload NGINX
#REMOVEME? ynh_systemd_action --service_name=nginx --action=reload
# Generate random password for admin
adm_pwd=$(ynh_string_random --length=24)
#REMOVEME? ynh_app_setting_set --app=$app --key=admin_pwd --value="$adm_pwd"
if [ "$language" = "fr" ]; then
applanguage="fr_FR"
else
applanguage="en_UK"
fi
# Configure Piwigo via cURL
mail="$(ynh_user_get_info --username=$admin --key=mail)"
# Installation with cURL
ynh_local_curl "/install.php?language=$applanguage" "install=true" "dbhost=127.0.0.1" "dbuser=$db_user" "dbpasswd=$db_pwd" "dbname=$db_name" "prefix=" "admin_name=$admin" "admin_pass1=$adm_pwd" "admin_pass2=$adm_pwd" "admin_mail=$mail"
#=================================================
# CONFIGURE PIWIGO
#=================================================
ynh_script_progression --message="Configuring Piwigo..."
# Change local config
ynh_add_config --template="../conf/config.inc.php" --destination="$install_dir/local/config/config.inc.php"
# Setup database in local/config/database.inc.php
ynh_add_config --template="../conf/database.inc.php" --destination="$install_dir/local/config/database.inc.php"
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"
#=================================================
# ADD LDAP PLUGIN
#=================================================
ynh_script_progression --message="Configuring LDAP plugin..."
# Activate the LDAP plugin using the WS API
# Login with admin account
ynh_local_curl "/ws.php?format=json" "method=pwg.session.login" "username=$admin" "password=$adm_pwd"
# Get session token
status=$(ynh_local_curl "/ws.php?format=json" "method=pwg.session.getStatus")
pwg_token=$(jq --raw-output .result.pwg_token <<< $status)
# Install the Ldap_Login plugin
ynh_local_curl "/ws.php?format=json" "method=pwg.plugins.performAction" "action=install" "plugin=Ldap_Login" "pwg_token=$pwg_token"
# Activate the Ldap_Login plugin
ynh_local_curl "/ws.php?format=json" "method=pwg.plugins.performAction" "action=activate" "plugin=Ldap_Login" "pwg_token=$pwg_token"
# Log out
ynh_local_curl "/ws.php?format=json" "method=pwg.session.logout"
# Edit Ldap_Login plugin configuration
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "UPDATE piwigo_ldap_login_config SET value='ou=users,dc=yunohost,dc=org' WHERE param = 'ld_basedn';
UPDATE piwigo_ldap_login_config SET value='uid' WHERE param = 'ld_user_attr';
UPDATE piwigo_ldap_login_config SET value='' WHERE param = 'ld_binddn';
UPDATE piwigo_ldap_login_config SET value='0' WHERE param = 'allow_new_users';
UPDATE piwigo_ldap_login_config SET value='0' WHERE param = 'ld_group_user_active';"
# Remove the public access
#REMOVEME? ynh_permission_update --permission="main" --remove="visitors"
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP FAIL2BAN
#=================================================
ynh_script_progression --message="Configuring Fail2Ban..." --weight=6
# Configure and activate log_failed_logins plugin
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "INSERT INTO plugins (id,state,version) VALUES ('log_failed_logins','active','1.2');"
ynh_mysql_connect_as --user=$db_name --password=$db_pwd --database=$db_name <<< "INSERT INTO config (param, value) VALUES ('logFailedLoginsFilename','/var/log/${app}FailedLogins.log');"
touch "/var/log/${app}FailedLogins.log"
chown $app: "/var/log/${app}FailedLogins.log"
ynh_add_fail2ban_config --logpath="/var/log/${app}FailedLogins.log" --failregex="ip=<HOST>" --max_retry=6
#=================================================
# SETUP SSOWAT
#=================================================
#REMOVEME? ynh_script_progression --message="Configuring permissions..."
# 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..."
#REMOVEME? ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed" --last