diff --git a/scripts/_common.sh b/scripts/_common.sh index ac4de90..0a1e55b 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -178,9 +178,61 @@ ynh_remove_app_dependencies () { ynh_package_autoremove ${dep_app}-ynh-deps # Remove the fake package and its dependencies if they not still used. } +# Check if a mysql user exists +# +# usage: ynh_mysql_user_exists user +# | arg: user - the user for which to check existence +function ynh_mysql_user_exists() +{ + local user=$1 + if [[ -z $(ynh_mysql_execute_as_root "SELECT User from mysql.user WHERE User = '$user';") ]] + then + return 1 + else + return 0 + fi +} -# Correct the name given in argument for mariadb -# Avoid invalid characters in your database name +# 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 "mysqlpwd" into the app settings. +# +# usage: ynh_mysql_setup_db user name +# | arg: user - Owner of the database +# | arg: name - Name of the database +ynh_mysql_setup_db () { + local db_user="$1" + local db_name="$2" + db_pwd=$(ynh_string_random) # Generate a random password + ynh_mysql_create_db "$db_name" "$db_user" "$db_pwd" # Create the database + ynh_app_setting_set $app mysqlpwd $db_pwd # Store the password in the app's config +} + +# Remove a database if it exists, and the associated user +# +# usage: ynh_mysql_remove_db user name +# | arg: user - Owner of the database +# | arg: name - Name of the database +ynh_mysql_remove_db () { + local db_user="$1" + local db_name="$2" + local mysql_root_password=$(sudo cat $MYSQL_ROOT_PWD_FILE) + if mysqlshow -u root -p$mysql_root_password | grep -q "^| $db_name"; then # Check if the database exists + echo "Removing database $db_name" >&2 + ynh_mysql_drop_db $db_name # Remove the database + else + echo "Database $db_name not found" >&2 + fi + + # Remove mysql user if it exists + if $(ynh_mysql_user_exists $db_user); then + ynh_mysql_drop_user $db_user + fi +} + +# Sanitize a string intended to be the name of a database +# (More specifically : replace - and . by _) # # Exemple: dbname=$(ynh_sanitize_dbid $app) # @@ -188,7 +240,7 @@ ynh_remove_app_dependencies () { # | arg: name - name to correct/sanitize # | ret: the corrected name ynh_sanitize_dbid () { - dbid=${1//[-.]/_} # Mariadb doesn't support - and . in the name of databases. It will be replace by _ + dbid=${1//[-.]/_} # We should avoid having - and . in the name of databases. They are replaced by _ echo $dbid } diff --git a/scripts/install b/scripts/install index 5ea0465..8947b94 100644 --- a/scripts/install +++ b/scripts/install @@ -60,14 +60,11 @@ db_user="$db_name" # Generate random DES key & password deskey=$(ynh_string_random 24) -dbpass=$(ynh_string_random) -ynh_app_setting_set "$app" mysqlpwd "$dbpass" ynh_app_setting_set "$app" deskey "$deskey" ynh_app_setting_set "$app" db_name "$db_name" # Initialize database -ynh_mysql_create_db "$db_name" "$db_user" "$dbpass" - +ynh_mysql_setup_db "$db_user" "$db_name" #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= @@ -92,7 +89,7 @@ wb_conf="${TMPDIR}/app/config/parameters.yml" cp ../conf/parameters.yml "$wb_conf" ynh_replace_string "{DBNAME}" "${db_name}" "$wb_conf" ynh_replace_string "{DBUSER}" "${db_user}" "$wb_conf" -ynh_replace_string "{DBPASS}" "${dbpass}" "$wb_conf" +ynh_replace_string "{DBPASS}" "${db_pwd}" "$wb_conf" ynh_replace_string "{DESKEY}" "${deskey}" "$wb_conf" # Install files and set permissions @@ -118,7 +115,7 @@ done exec_console $app "$final_path" fos:user:promote --super "$admin" # Configure Wallabag instance URL -ynh_mysql_connect_as "$db_name" "$dbpass" "$db_user" <<< "UPDATE craue_config_setting SET value = 'https://$domain$path_url' WHERE name = 'wallabag_url'" +ynh_mysql_connect_as "$db_name" "$db_pwd" "$db_user" <<< "UPDATE craue_config_setting SET value = 'https://$domain$path_url' WHERE name = 'wallabag_url'" #================================================= # NGINX CONFIGURATION diff --git a/scripts/remove b/scripts/remove index 1271ec1..8a942e1 100644 --- a/scripts/remove +++ b/scripts/remove @@ -33,8 +33,7 @@ ynh_remove_app_dependencies # REMOVE THE MYSQL DB #================================================= -ynh_mysql_drop_db "$db_name" -ynh_mysql_drop_user "$app" +ynh_mysql_remove_db "$app" "$db_name" #================================================= # REMOVE APP MAIN DIR diff --git a/scripts/upgrade b/scripts/upgrade index 6802efd..4cc5529 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -32,7 +32,7 @@ if [ -z "$path_url" ] ; then ynh_app_setting_set $app path_url "$path_url" fi path_url=$(ynh_normalize_url_path $path_url) -dbpass=$(ynh_app_setting_get "$app" mysqlpwd) +db_pwd=$(ynh_app_setting_get "$app" mysqlpwd) deskey=$(ynh_app_setting_get "$app" deskey) final_path=$(ynh_app_setting_get "$app" final_path) # Compatibility with previous version @@ -53,11 +53,15 @@ db_user="$db_name" # MANAGE SCRIPT FAILURE #================================================= -ynh_backup_before_upgrade # Backup the current version of the app -ynh_clean_setup () { - ynh_backup_after_failed_upgrade -} -ynh_abort_if_errors # Stop script if an error is detected +# Use prior backup and restore on error only if backup feature +# exists on installed instance +if [ -f "/etc/yunohost/apps/$app/scripts/backup" ] ; then + ynh_backup_before_upgrade # Backup the current version of the app + ynh_clean_setup () { + ynh_backup_after_failed_upgrade + } + ynh_abort_if_errors # Stop script if an error is detected +fi #================================================= # INSTALL DEPENDENCIES @@ -88,7 +92,7 @@ wb_conf="${TMPDIR}/app/config/parameters.yml" cp ../conf/parameters.yml "$wb_conf" ynh_replace_string "{DBNAME}" "${db_name}" "$wb_conf" ynh_replace_string "{DBUSER}" "${db_user}" "$wb_conf" -ynh_replace_string "{DBPASS}" "${dbpass}" "$wb_conf" +ynh_replace_string "{DBPASS}" "${db_pwd}" "$wb_conf" ynh_replace_string "{DESKEY}" "${deskey}" "$wb_conf" # Replace files and set permissions @@ -103,7 +107,7 @@ exec_console $app "${final_path}" doctrine:migrations:migrate exec_console $app "${final_path}" cache:clear # Configure Wallabag instance URL -ynh_mysql_connect_as "$db_name" "$dbpass" "$db_user" <<< "UPDATE craue_config_setting SET value = 'https://$domain$path_url' WHERE name = 'wallabag_url'" +ynh_mysql_connect_as "$db_name" "$db_pwd" "$db_user" <<< "UPDATE craue_config_setting SET value = 'https://$domain$path_url' WHERE name = 'wallabag_url'" #================================================= # NGINX CONFIGURATION