From d8bb515aa878eccec07511664bc4372ec2327641 Mon Sep 17 00:00:00 2001 From: Kayou Date: Wed, 13 Mar 2019 22:44:30 +0100 Subject: [PATCH] Migration from SPIP2 --- conf/spip2_migration | 10 ++ conf/spip2_post_migration.sh | 38 ++++++ scripts/_common.sh | 250 +++++++++++++++++++++++++++++++++++ scripts/upgrade | 32 ++++- 4 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 conf/spip2_migration create mode 100644 conf/spip2_post_migration.sh diff --git a/conf/spip2_migration b/conf/spip2_migration new file mode 100644 index 0000000..273d9a7 --- /dev/null +++ b/conf/spip2_migration @@ -0,0 +1,10 @@ +#!/bin/bash + +# Final path +/var/www/$app + +# Nginx config +/etc/nginx/conf.d/$domain.d/$app.conf + +# php-fpm config +/etc/php/7.0/fpm/pool.d/$app.conf diff --git a/conf/spip2_post_migration.sh b/conf/spip2_post_migration.sh new file mode 100644 index 0000000..34584fb --- /dev/null +++ b/conf/spip2_post_migration.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# Ending the migration process from Spip2 to Spip + +set -u + +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source /usr/share/yunohost/helpers + +#================================================= +# SET VARIABLES +#================================================= + +old_app="__OLD_APP__" +new_app="__NEW_APP__" +script_name="$0" + +#================================================= +# DELETE OLD APP'S SETTINGS +#================================================= + +ynh_secure_remove "/etc/yunohost/apps/$old_app" +yunohost app ssowatconf + +#================================================= +# REMOVE THE OLD USER +#================================================= + +ynh_system_user_delete $old_app + +#================================================= +# DELETE THIS SCRIPT +#================================================= + +echo "rm $script_name" | at now + 1 minutes \ No newline at end of file diff --git a/scripts/_common.sh b/scripts/_common.sh index 3a4393f..8e98c09 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,3 +1,5 @@ +#!/bin/bash + # Send an email to inform the administrator # # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type] @@ -131,4 +133,252 @@ __PRE_TAG1__$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/service # Send the email to the recipients cat mail_to_send | $mail_bin -a "Content-Type: $content_type; charset=UTF-8" -s "$mail_subject" "$recipients" +} + +# Make the main steps to migrate an app to its fork. +# +# This helper has to be used for an app which needs to migrate to a new name or a new fork +# (like owncloud to nextcloud or zerobin to privatebin). +# +# This helper will move the files of an app to its new name +# or recreate the things it can't move. +# +# To specify which files it has to move, you have to create a "migration file", stored in ../conf +# This file is a simple list of each file it has to move, +# except that file names must reference the $app variable instead of the real name of the app, +# and every instance-specific variables (like $domain). +# $app is especially important because it's this variable which will be used to identify the old place and the new one for each file. +# +# If a database exists for this app, it will be dumped and then imported in a newly created database, with a new name and new user. +# Don't forget you have to then apply these changes to application-specific settings (depends on the packaged application) +# +# Same things for an existing user, a new one will be created. +# But the old one can't be removed unless it's not used. See below. +# +# If you have some dependencies for your app, it's possible to change the fake debian package which manages them. +# You have to fill the $pkg_dependencies variable, and then a new fake package will be created and installed, +# and the old one will be removed. +# If you don't have a $pkg_dependencies variable, the helper can't know what the app dependencies are. +# +# The app settings.yml will be modified as follows: +# - finalpath will be changed according to the new name (but only if the existing $final_path contains the old app name) +# - The checksums of php-fpm and nginx config files will be updated too. +# - If there is a $db_name value, it will be changed. +# - And, of course, the ID will be changed to the new name too. +# +# Finally, the $app variable will take the value of the new name. +# The helper will set the $migration_process variable to 1 if a migration has been successfully handled. +# +# You have to handle by yourself all the migrations not done by this helper, like configuration or special values in settings.yml +# Also, at the end of the upgrade script, you have to add a post_migration script to handle all the things the helper can't do during YunoHost upgrade (mostly for permission reasons), +# especially remove the old user, move some hooks and remove the old configuration directory +# To launch this script, you have to move it elsewhere and start it after the upgrade script. +# `cp ../conf/$script_post_migration /tmp` +# `(cd /tmp; echo "/tmp/$script_post_migration" | at now + 2 minutes)` +# +# usage: ynh_handle_app_migration migration_id migration_list +# | arg: -i, --migration_id= - ID from which to migrate +# | arg: -l, --migration_list= - File specifying every file to move (one file per line) +ynh_handle_app_migration () { + #================================================= + # LOAD SETTINGS + #================================================= + + old_app=$YNH_APP_INSTANCE_NAME + local old_app_id=$YNH_APP_ID + local old_app_number=$YNH_APP_INSTANCE_NUMBER + + # Declare an array to define the options of this helper. + declare -Ar args_array=( [i]=migration_id= [l]=migration_list= ) + # Get the id from which to migrate + local migration_id + # And the file with the paths to move + local migration_list + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Get the new app id in the manifest + local new_app_id=$(grep \"id\": ../manifest.json | cut -d\" -f4) + if [ $old_app_number -eq 1 ]; then + local new_app=$new_app_id + else + local new_app=${new_app_id}__${old_app_number} + fi + + #================================================= + # CHECK IF IT HAS TO MIGRATE + #================================================= + + migration_process=0 + + if [ "$old_app_id" == "$new_app_id" ] + then + # If the 2 id are the same + # No migration to do. + echo 0 + return 0 + else + if [ "$old_app_id" != "$migration_id" ] + then + # If the new app is not the authorized id, fail. + ynh_die "Incompatible application for migration from $old_app_id to $new_app_id" + fi + + echo "Migrate from $old_app_id to $new_app_id" >&2 + + #================================================= + # CHECK IF THE MIGRATION CAN BE DONE + #================================================= + + # TODO Handle multi instance apps... + # Check that there is not already an app installed for this id. + (yunohost app list --installed -f "$new_app" | grep -q -w "$new_app") \ + && ynh_die "$new_app is already installed" + + #================================================= + # CHECK THE LIST OF FILES TO MOVE + #================================================= + + local temp_migration_list="$(tempfile)" + + # Build the list by removing blank lines and comment lines + sed '/^#.*\|^$/d' "../conf/$migration_list" > "$temp_migration_list" + + # Check if there is no file in the destination + local file_to_move="" + while read file_to_move + do + # Replace all occurences of $app by $new_app in each file to move. + local move_to_destination="${file_to_move//\$app/$new_app}" + test -e "$move_to_destination" && ynh_die "A file named $move_to_destination already exists." + done < "$temp_migration_list" + + #================================================= + # COPY YUNOHOST SETTINGS FOR THIS APP + #================================================= + + local settings_dir="/etc/yunohost/apps" + cp -a "$settings_dir/$old_app" "$settings_dir/$new_app" + + # Replace the old id by the new one + ynh_replace_string "\(^id: .*\)$old_app" "\1$new_app" "$settings_dir/$new_app/settings.yml" + # INFO: There a special behavior with yunohost app setting: + # if the id given in argument does not match with the id + # stored in the config file, the config file will be purged. + # That's why we use sed instead of app setting here. + # https://github.com/YunoHost/yunohost/blob/c6b5284be8da39cf2da4e1036a730eb5e0515096/src/yunohost/app.py#L1316-L1321 + + # Change the label if it's simply the name of the app + old_label=$(ynh_app_setting_get $new_app label) + if [ "${old_label,,}" == "$old_app_id" ] + then + # Build the new label from the id of the app. With the first character as upper case + new_label=$(echo $new_app_id | cut -c1 | tr [:lower:] [:upper:])$(echo $new_app_id | cut -c2-) + ynh_app_setting_set $new_app label $new_label + fi + + #================================================= + # MOVE FILES TO THE NEW DESTINATION + #================================================= + + while read file_to_move + do + # Replace all occurence of $app by $new_app in each file to move. + move_to_destination="$(eval echo "${file_to_move//\$app/$new_app}")" + local real_file_to_move="$(eval echo "${file_to_move//\$app/$old_app}")" + echo "Move file $real_file_to_move to $move_to_destination" >&2 + mv "$real_file_to_move" "$move_to_destination" + done < "$temp_migration_list" + + #================================================= + # UPDATE SETTINGS KNOWN ENTRIES + #================================================= + + # Replace nginx checksum + ynh_replace_string "\(^checksum__etc_nginx.*\)_$old_app" "\1_$new_app/" "$settings_dir/$new_app/settings.yml" + + # Replace php5-fpm checksums + ynh_replace_string "\(^checksum__etc_php5.*[-_]\)$old_app" "\1$new_app/" "$settings_dir/$new_app/settings.yml" + + # Replace final_path + ynh_replace_string "\(^final_path: .*\)$old_app" "\1$new_app" "$settings_dir/$new_app/settings.yml" + + #================================================= + # MOVE THE DATABASE + #================================================= + + db_pwd=$(ynh_app_setting_get $old_app mysqlpwd) + db_name=$(ynh_app_setting_get $old_app db_name) + + # Check if a database exists before trying to move it + local mysql_root_password=$(cat $MYSQL_ROOT_PWD_FILE) + if [ -n "$db_name" ] && mysqlshow -u root -p$mysql_root_password | grep -q "^| $db_name" + then + new_db_name=$(ynh_sanitize_dbid $new_app) + echo "Rename the database $db_name to $new_db_name" >&2 + + local sql_dump="/tmp/${db_name}-$(date '+%s').sql" + + # Dump the old database + ynh_mysql_dump_db "$db_name" > "$sql_dump" + + # Create a new database + ynh_mysql_setup_db $new_db_name $new_db_name $db_pwd + # Then restore the old one into the new one + ynh_mysql_connect_as $new_db_name $db_pwd $new_db_name < "$sql_dump" + + # Remove the old database + ynh_mysql_remove_db $db_name $db_name + # And the dump + ynh_secure_remove "$sql_dump" + + # Update the value of $db_name + db_name=$new_db_name + ynh_app_setting_set $new_app db_name $db_name + fi + + #================================================= + # CREATE A NEW USER + #================================================= + + # Check if the user exists on the system + if ynh_system_user_exists "$old_app" + then + echo "Create a new user $new_app to replace $old_app" >&2 + ynh_system_user_create $new_app + fi + + #================================================= + # CHANGE THE FAKE DEPENDENCIES PACKAGE + #================================================= + + # Check if a variable $pkg_dependencies exists + # If this variable doesn't exist, this part shall be managed in the upgrade script. + if [ -n "${pkg_dependencies:-}" ] + then + # Define the name of the package + local old_package_name="${old_app//_/-}-ynh-deps" + local new_package_name="${new_app//_/-}-ynh-deps" + + if ynh_package_is_installed "$old_package_name" + then + # Install a new fake package + app=$new_app + ynh_install_app_dependencies $pkg_dependencies + # Then remove the old one + app=$old_app + ynh_remove_app_dependencies + fi + fi + + #================================================= + # UPDATE THE ID OF THE APP + #================================================= + + app=$new_app + + + # Set migration_process to 1 to inform that an upgrade has been made + migration_process=1 + fi } \ No newline at end of file diff --git a/scripts/upgrade b/scripts/upgrade index e7c2eda..24a43e1 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -42,7 +42,7 @@ fi # If db_name doesn't exist, create it if [ -z $db_name ]; then - db_name=$app + db_name=$app ynh_app_setting_set $app db_name $db_name fi @@ -85,6 +85,19 @@ ynh_clean_setup () { # Exit if an error occurs during the execution of the script ynh_abort_if_errors +#================================================= +# HANDLE MIGRATION FROM SPIP2 +#================================================= + +ynh_handle_app_migration "spip2" "spip2_migration" +if [ $migration_process -eq 1 ] +then + # If a migration has been perform + # Reload some values changed by the migration process + final_path=$(ynh_app_setting_get $app final_path) + db_name=$(ynh_app_setting_get $app db_name) +fi + #================================================= # CHECK THE PATH #================================================= @@ -216,6 +229,23 @@ echo "To finish the upgrade, you may have to update the database by clicking on ynh_send_readme_to_admin --app_message="mail_to_send" --type="upgrade" +if [ $migration_process -eq 1 ] +then + ynh_print_info "Spip2 has been successfully migrated to Spip! \ +A last scheduled operation will run in a couple of minutes to finish the \ +migration in YunoHost side. Do not proceed any application operation while \ +you don't see Spip as installed." + + # Execute a post migration script after the end of this upgrade. + # Mainly for some cleaning + script_post_migration=spip2_post_migration.sh + cp ../conf/$script_post_migration /tmp + ynh_replace_string "__OLD_APP__" "$old_app" /tmp/$script_post_migration + ynh_replace_string "__NEW_APP__" "$app" /tmp/$script_post_migration + chmod +x /tmp/$script_post_migration + (cd /tmp; echo "/tmp/$script_post_migration > /tmp/$script_post_migration.log 2>&1" | at now + 2 minutes) +fi + #================================================= # END OF SCRIPT #=================================================