From 58e96aa627d59a17cc1b247e9bc5d8267a4d7389 Mon Sep 17 00:00:00 2001 From: anmol26s Date: Sat, 1 Sep 2018 18:02:40 +0530 Subject: [PATCH] Fix for Yunohost 3 --- conf/cron | 2 +- conf/php-fpm.conf | 2 +- scripts/_common.sh | 99 +++++++++++---------- scripts/backup | 20 ++--- scripts/install | 28 +++--- scripts/psql.sh | 216 ++++++++++++++++++++++++++------------------- scripts/remove | 12 +-- scripts/restore | 26 ++---- 8 files changed, 212 insertions(+), 193 deletions(-) diff --git a/conf/cron b/conf/cron index 1608a25..6027f3d 100644 --- a/conf/cron +++ b/conf/cron @@ -1 +1 @@ -* * * * * /usr/bin/php7.1 YNH_DOMAIN/admin/cli/cron.php >/dev/null +* * * * * /usr/bin/php YNH_DOMAIN/admin/cli/cron.php >/dev/null diff --git a/conf/php-fpm.conf b/conf/php-fpm.conf index 98cb11a..2aec4a2 100644 --- a/conf/php-fpm.conf +++ b/conf/php-fpm.conf @@ -30,7 +30,7 @@ group = __USER__ ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. -listen = /var/run/php7.1-fpm-__NAMETOCHANGE__.sock +listen = /var/run/php5-fpm-__NAMETOCHANGE__.sock ; Set listen(2) backlog. A value of '-1' means unlimited. ; Default Value: 128 (-1 on FreeBSD and OpenBSD) diff --git a/scripts/_common.sh b/scripts/_common.sh index 07c27b7..c726850 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -12,58 +12,59 @@ ynh_delete_file_checksum () { ynh_app_setting_delete $app $checksum_setting_name } -ynh_install_php7 () { - - ynh_package_update - ynh_package_install apt-transport-https --no-install-recommends - - wget -q -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg - echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.list - - ynh_package_update - ynh_install_app_dependencies php7.1 php7.1-zip php7.1-fpm php7.1-mysql php7.1-xml php7.1-intl php7.1-mbstring php7.1-gd php7.1-curl php7.1-soap php7.1-pgsql php7.1-xmlrpc postgresql-9.4 - sudo update-alternatives --install /usr/bin/php php /usr/bin/php5 70 -} - -ynh_remove_php7 () { - sudo rm -f /etc/apt/sources.list.d/php7.list - sudo apt-key del 4096R/89DF5277 - sudo apt-key del 2048R/11A06851 - ynh_remove_app_dependencies php7.1 php7.1-zip php7.1-fpm php7.1-mysql php7.1-xml php7.1-intl php7.1-mbstring php7.1-gd php7.1-curl php7.1-soap php7.1-pgsql php7.1-xmlrpc postgresql-9.4 -} - - -# Create a dedicated php-fpm config for php7.1 +# Send an email to inform the administrator # -# usage: ynh_add_fpm_config -ynh_add_php7.1-fpm_config () { - finalphpconf="/etc/php/7.1/fpm/pool.d/$app.conf" - ynh_backup_if_checksum_is_different "$finalphpconf" - sudo cp ../conf/php-fpm.conf "$finalphpconf" - ynh_replace_string "__NAMETOCHANGE__" "$app" "$finalphpconf" - ynh_replace_string "__FINALPATH__" "$final_path" "$finalphpconf" - ynh_replace_string "__USER__" "$app" "$finalphpconf" - sudo chown root: "$finalphpconf" - ynh_store_file_checksum "$finalphpconf" +# usage: ynh_send_readme_to_admin app_message [recipients] +# | arg: app_message - The message to send to the administrator. +# | arg: recipients - The recipients of this email. Use spaces to separate multiples recipients. - default: root +# example: "root admin@domain" +# If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you +# example: "root admin@domain user1 user2" +ynh_send_readme_to_admin() { + local app_message="${1:-...No specific information...}" + local recipients="${2:-root}" - if [ -e "../conf/php-fpm.ini" ] + # Retrieve the email of users + find_mails () { + local list_mails="$1" + local mail + local recipients=" " + # Read each mail in argument + for mail in $list_mails + do + # Keep root or a real email address as it is + if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@" + then + recipients="$recipients $mail" + else + # But replace an user name without a domain after by its email + if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null) + then + recipients="$recipients $mail" + fi + fi + done + echo "$recipients" + } + recipients=$(find_mails "$recipients") + + local mail_subject="☁️🆈🅽🅷☁️: \`$app\` has important message for you" + + local mail_message="This is an automated message from your beloved YunoHost server. +Specific information for the application $app. +$app_message +--- +Automatic diagnosis data from YunoHost +$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')" + + # Define binary to use for mail command + if [ -e /usr/bin/bsd-mailx ] then - finalphpini="/etc/php/7.1/fpm/conf.d/20-$app.ini" - ynh_backup_if_checksum_is_different "$finalphpini" - sudo cp ../conf/php-fpm.ini "$finalphpini" - sudo chown root: "$finalphpini" - ynh_store_file_checksum "$finalphpini" + local mail_bin=/usr/bin/bsd-mailx + else + local mail_bin=/usr/bin/mail.mailutils fi - sudo systemctl reload php7.1-fpm -} - - -# Remove the dedicated php-fpm config for php7.1 -# -# usage: ynh_remove_fpm_config -ynh_remove_php7.1-fpm_config () { - ynh_secure_remove "/etc/php/7.1/fpm/pool.d/$app.conf" - ynh_secure_remove "/etc/php/7.1/fpm/conf.d/20-$app.ini" 2>&1 - sudo systemctl reload php7.1-fpm + # Send the email to the recipients + echo "$mail_message" | $mail_bin -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients" } diff --git a/scripts/backup b/scripts/backup index 92b2cdf..4e4230b 100644 --- a/scripts/backup +++ b/scripts/backup @@ -6,11 +6,6 @@ # IMPORT GENERIC HELPERS #================================================= -if [ ! -e _common.sh ]; then - # Get the _common.sh file if it's not in the current directory - cp ../settings/scripts/_common.sh ./_common.sh - chmod a+rx _common.sh -fi source _common.sh source psql.sh source /usr/share/yunohost/helpers @@ -19,6 +14,10 @@ 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 @@ -52,8 +51,8 @@ ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" # BACKUP THE PHP-FPM CONFIGURATION #================================================= -ynh_backup "/etc/php/7.1/fpm/pool.d/$app.conf" -ynh_backup "/etc/php/7.1/fpm/conf.d/20-$app.ini" +ynh_backup "/etc/php5/fpm/pool.d/$app.conf" +ynh_backup "/etc/php5/fpm/conf.d/20-$app.ini" #================================================= # BACKUP THE PSQL DATABASE @@ -62,15 +61,8 @@ ynh_backup "/etc/php/7.1/fpm/conf.d/20-$app.ini" ynh_psql_dump_db "$db_name" > db.sql ynh_backup "db.sql" - #================================================= # SPECIFIC BACKUP -#================================================= -# BACKUP LOGROTATE -#================================================= - -ynh_backup "/etc/logrotate.d/$app" - #================================================= # BACKUP THE CRON FILE #================================================= diff --git a/scripts/install b/scripts/install index 51a8e79..a93949a 100644 --- a/scripts/install +++ b/scripts/install @@ -14,6 +14,10 @@ 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 @@ -67,7 +71,7 @@ ynh_app_setting_set $app is_public $is_public #================================================= # INSTALL DEPENDENCIES #================================================= -ynh_install_php7 +ynh_install_app_dependencies php-zip php-mysql php-xml php-intl php-mbstring php-gd php-curl php-soap php-pgsql php-xmlrpc postgresql-9.4 #================================================= # Create postgresql database #================================================= @@ -107,8 +111,19 @@ ynh_system_user_create $app # PHP-FPM CONFIGURATION #================================================= +### `ynh_add_fpm_config` is used to set up a PHP config. +### You can remove it if your app doesn't use PHP. +### `ynh_add_fpm_config` will use the files conf/php-fpm.conf and conf/php-fpm.ini +### If you're not using these lines: +### - You can remove these files in conf/. +### - Remove the section "BACKUP THE PHP-FPM CONFIGURATION" in the backup script +### - Remove also the section "REMOVE PHP-FPM CONFIGURATION" in the remove script +### - As well as the section "RESTORE THE PHP-FPM CONFIGURATION" in the restore script +### With the reload at the end of the script. +### - And the section "PHP-FPM CONFIGURATION" in the upgrade script + # Create a dedicated php-fpm config -ynh_add_php7.1-fpm_config +ynh_add_fpm_config #================================================= # SPECIFIC SETUP @@ -133,7 +148,7 @@ ynh_store_file_checksum "$final_path/config.php" # Create the home directory mkdir -p $var_root -chown -R $app:root $var_root +chown -R $app: $var_root chmod -R 700 $var_root #================================================= @@ -155,13 +170,6 @@ systemctl reload nginx # Set permissions to app files chown root: $final_path/config.php -#================================================= -# SETUP LOGROTATE -#================================================= - -# Use logrotate to manage application logfile(s) -ynh_use_logrotate - # Set up poller sudo cp ../conf/cron /etc/cron.d/$app diff --git a/scripts/psql.sh b/scripts/psql.sh index 419d300..d4fc2c3 100644 --- a/scripts/psql.sh +++ b/scripts/psql.sh @@ -1,99 +1,16 @@ #!/bin/bash #================================================= +# # POSTGRES HELPERS +# +# Point of contact : Jean-Baptiste Holcroft #================================================= -# Open a connection as a user +# Create a master password and set up global settings +# Please always call this script in install and restore scripts # -# example: ynh_psql_connect_as 'user' 'pass' <<< "UPDATE ...;" -# example: ynh_psql_connect_as 'user' 'pass' < /path/to/file.sql -# -# usage: ynh_psql_connect_as user pwd [db] -# | arg: user - the user name to connect as -# | arg: pwd - the user password -# | arg: db - the database to connect to -ynh_psql_connect_as() { - ynh_die "ynh_psql_connect_as is not yet implemented" -} - -# # Execute a command as root user -# -# usage: ynh_psql_execute_as_root sql [db] -# | arg: sql - the SQL command to execute -# | arg: db - the database to connect to -ynh_psql_execute_as_root () { - sudo su -c "psql" - postgres <<< ${1} -#TODO support db argument ? -} - -# Execute a command from a file as root user -# -# usage: ynh_psql_execute_file_as_root file [db] -# | arg: file - the file containing SQL commands -# | arg: db - the database to connect to -ynh_psql_execute_file_as_root() { - file="$1" - db="$2" - su -c "psql $db" postgres < "$file" -} - -# Create a database and grant optionnaly privilegies to a user -# -# usage: ynh_psql_create_db db [user [pwd]] -# | arg: db - the database name to create -# | arg: user - the user to grant privilegies -# | arg: pwd - the password to identify user by -ynh_psql_create_db() { - db=$1 - # grant all privilegies to user - if [[ $# -gt 1 ]]; then - ynh_psql_create_user ${2} "${3}" - sudo su -c "createdb -O ${2} $db" - postgres - else - sudo su -c "createdb $db" - postgres - fi - -} - -# Drop a database -# -# usage: ynh_psql_drop_db db -# | arg: db - the database name to drop -ynh_psql_drop_db() { - sudo su -c "dropdb ${1}" - postgres -} - -# Dump a database -# -# example: ynh_psql_dump_db 'roundcube' > ./dump.sql -# -# usage: ynh_psql_dump_db db -# | arg: db - the database name to dump -# | ret: the psqldump output -ynh_psql_dump_db() { - db="$1" -su --command="pg_dump \"${db}\"" postgres -} - - -# Create a user -# -# usage: ynh_psql_create_user user pwd [host] -# | arg: user - the user name to create -# | arg: pwd - the password to identify user by -ynh_psql_create_user() { - ynh_psql_execute_as_root \ - "CREATE USER ${1} WITH PASSWORD '${2}';" -} - -# Drop a user -# -# usage: ynh_psql_drop_user user -# | arg: user - the user name to drop -ynh_psql_drop_user() { - sudo su -c "dropuser ${1}" - postgres -} +# usage: ynh_psql_test_if_first_run ynh_psql_test_if_first_run() { if [ -f /etc/yunohost/psql ]; @@ -115,11 +32,128 @@ ynh_psql_test_if_first_run() { fi systemctl start postgresql - su --command="psql -c\"ALTER user postgres WITH PASSWORD '${pgsql}'\"" postgres - # we can't use peer since YunoHost create users with nologin + sudo --login --user=postgres psql -c"ALTER user postgres WITH PASSWORD '$pgsql'" postgres + + # force all user to connect to local database using passwords + # https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html#EXAMPLE-PG-HBA.CONF + # Note: we can't use peer since YunoHost create users with nologin + # See: https://github.com/YunoHost/yunohost/blob/unstable/data/helpers.d/user sed -i '/local\s*all\s*all\s*peer/i \ local all all password' "$pg_hba" systemctl enable postgresql systemctl reload postgresql fi } + +# Open a connection as a user +# +# example: ynh_psql_connect_as 'user' 'pass' <<< "UPDATE ...;" +# example: ynh_psql_connect_as 'user' 'pass' < /path/to/file.sql +# +# usage: ynh_psql_connect_as user pwd [db] +# | arg: user - the user name to connect as +# | arg: pwd - the user password +# | arg: db - the database to connect to +ynh_psql_connect_as() { + user="$1" + pwd="$2" + db="$3" + sudo --login --user=postgres PGUSER="$user" PGPASSWORD="$pwd" psql "$db" +} + +# # Execute a command as root user +# +# usage: ynh_psql_execute_as_root sql [db] +# | arg: sql - the SQL command to execute +# | arg: db - the database to connect to +ynh_psql_execute_as_root () { + sql="$1" + sudo --login --user=postgres psql <<< "$sql" +} + +# Execute a command from a file as root user +# +# usage: ynh_psql_execute_file_as_root file [db] +# | arg: file - the file containing SQL commands +# | arg: db - the database to connect to +ynh_psql_execute_file_as_root() { + file="$1" + db="$2" + sudo --login --user=postgres psql "$db" < "$file" +} + +# Create a database, an user and its password. Then store the password in the app's config +# +# After executing this helper, the password of the created database will be available in $db_pwd +# It will also be stored as "psqlpwd" into the app settings. +# +# usage: ynh_psql_setup_db user name [pwd] +# | arg: user - Owner of the database +# | arg: name - Name of the database +# | arg: pwd - Password of the database. If not given, a password will be generated +ynh_psql_setup_db () { + db_user="$1" + db_name="$2" + new_db_pwd=$(ynh_string_random) # Generate a random password + # If $3 is not given, use new_db_pwd instead for db_pwd. + db_pwd="${3:-$new_db_pwd}" + ynh_psql_create_db "$db_name" "$db_user" "$db_pwd" # Create the database + ynh_app_setting_set "$app" psqlpwd "$db_pwd" # Store the password in the app's config +} + +# Create a database and grant privilegies to a user +# +# usage: ynh_psql_create_db db [user [pwd]] +# | arg: db - the database name to create +# | arg: user - the user to grant privilegies +# | arg: pwd - the user password +ynh_psql_create_db() { + db="$1" + user="$2" + pwd="$3" + ynh_psql_create_user "$user" "$pwd" + sudo --login --user=postgres createdb --owner="$user" "$db" +} + +# Drop a database +# +# usage: ynh_psql_drop_db db +# | arg: db - the database name to drop +# | arg: user - the user to drop +ynh_psql_remove_db() { + db="$1" + user="$2" + sudo --login --user=postgres dropdb "$db" + ynh_psql_drop_user "$user" +} + +# Dump a database +# +# example: ynh_psql_dump_db 'roundcube' > ./dump.sql +# +# usage: ynh_psql_dump_db db +# | arg: db - the database name to dump +# | ret: the psqldump output +ynh_psql_dump_db() { + db="$1" + sudo --login --user=postgres pg_dump "$db" +} + + +# Create a user +# +# usage: ynh_psql_create_user user pwd [host] +# | arg: user - the user name to create +ynh_psql_create_user() { + user="$1" + pwd="$2" + sudo --login --user=postgres psql -c"CREATE USER $user WITH PASSWORD '$pwd'" postgres +} + +# Drop a user +# +# usage: ynh_psql_drop_user user +# | arg: user - the user name to drop +ynh_psql_drop_user() { + user="$1" + sudo --login --user=postgres dropuser "$user" diff --git a/scripts/remove b/scripts/remove index 120587b..ae57c7e 100644 --- a/scripts/remove +++ b/scripts/remove @@ -29,7 +29,6 @@ final_path=$(ynh_app_setting_get $app final_path) # Remove metapackage and its dependencies ynh_remove_app_dependencies -ynh_remove_php7 #================================================= # REMOVE THE psql DATABASE #================================================= @@ -53,18 +52,11 @@ ynh_secure_remove "$final_path" ynh_remove_nginx_config #================================================= -# REMOVE PHP-FPM CONFIGURATION for php7.1 +# REMOVE PHP-FPM CONFIGURATION #================================================= # Remove the dedicated php-fpm config -ynh_remove_php7.1-fpm_config - -#================================================= -# REMOVE LOGROTATE CONFIGURATION -#================================================= - -# Remove the app-specific logrotate config -ynh_remove_logrotate +ynh_remove_fpm_config #================================================= # SPECIFIC REMOVE diff --git a/scripts/restore b/scripts/restore index 8f236c5..fadd254 100644 --- a/scripts/restore +++ b/scripts/restore @@ -5,12 +5,6 @@ #================================================= # IMPORT GENERIC HELPERS #================================================= - -if [ ! -e _common.sh ]; then - # Get the _common.sh file if it's not in the current directory - cp ../settings/scripts/_common.sh ./_common.sh - chmod a+rx _common.sh -fi source _common.sh source psql.sh source /usr/share/yunohost/helpers @@ -19,6 +13,10 @@ 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 @@ -82,15 +80,15 @@ ynh_system_user_create $app chown -R $app: $final_path chown -R root: $final_path/config.php mkdir -p $var_root -chown -R $app:root $var_root +chown -R $app: $var_root chmod -R 700 $var_root #================================================= # RESTORE THE PHP-FPM CONFIGURATION #================================================= -ynh_restore_file "/etc/php/7.1/pool.d/$app.conf" -ynh_restore_file "/etc/php/7.1/conf.d/20-$app.ini" +ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf" +ynh_restore_file "/etc/php5/fpm/conf.d/20-$app.ini" #================================================= # SPECIFIC RESTORATION @@ -99,7 +97,7 @@ ynh_restore_file "/etc/php/7.1/conf.d/20-$app.ini" #================================================= # Define and install dependencies -ynh_install_php7 +ynh_install_app_dependencies php-zip php-mysql php-xml php-intl php-mbstring php-gd php-curl php-soap php-pgsql php-xmlrpc postgresql-9.4 #================================================= # RESTORE THE CRON FILE @@ -107,17 +105,11 @@ ynh_install_php7 ynh_restore_file "/etc/cron.d/$app" -#================================================= -# RESTORE THE LOGROTATE CONFIGURATION -#================================================= - -ynh_restore_file "/etc/logrotate.d/$app" - #================================================= # GENERIC FINALIZATION #================================================= # RELOAD NGINX AND PHP-FPM #================================================= -systemctl reload php7.1-fpm +systemctl reload php5-fpm systemctl reload nginx