1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/paheko_ynh.git synced 2024-09-03 19:56:22 +02:00

release of the source experimental_helpers

This commit is contained in:
rodinux 2023-01-19 09:43:18 +01:00
parent 0f9f95802a
commit 1e2ef97983
2 changed files with 64 additions and 60 deletions

View file

@ -1,15 +1,4 @@
# Execute a command as another user #!/bin/bash
# usage: exec_as USER COMMAND [ARG ...]
exec_as() {
local USER=$1
shift 1
if [[ $USER = $(whoami) ]]; then
eval "$@"
else
sudo -u "$USER" "$@"
fi
}
# Need also the helper https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/ynh_handle_getopts_args/ynh_handle_getopts_args # Need also the helper https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/ynh_handle_getopts_args/ynh_handle_getopts_args
@ -102,10 +91,10 @@ ynh_handle_app_migration () {
if [ "$old_app_id" != "$migration_id" ] if [ "$old_app_id" != "$migration_id" ]
then then
# If the new app is not the authorized id, fail. # If the new app is not the authorized id, fail.
ynh_die --message "Incompatible application for migration from $old_app_id to $new_app_id" ynh_die --message="Incompatible application for migration from $old_app_id to $new_app_id"
fi fi
echo "Migrate from $old_app_id to $new_app_id" >&2 ynh_print_info --message="Migrate from $old_app_id to $new_app_id" >&2
#================================================= #=================================================
# CHECK IF THE MIGRATION CAN BE DONE # CHECK IF THE MIGRATION CAN BE DONE
@ -113,14 +102,14 @@ ynh_handle_app_migration () {
# TODO Handle multi instance apps... # TODO Handle multi instance apps...
# Check that there is not already an app installed for this id. # Check that there is not already an app installed for this id.
(yunohost app list --installed -f "$new_app" | grep -q id) \ (yunohost app list | grep -q -w "id: $new_app") \
&& ynh_die "$new_app is already installed" && ynh_die --message="$new_app is already installed"
#================================================= #=================================================
# CHECK THE LIST OF FILES TO MOVE # CHECK THE LIST OF FILES TO MOVE
#================================================= #=================================================
local temp_migration_list="$(mktemp)" local temp_migration_list="$(tempfile)"
# Build the list by removing blank lines and comment lines # Build the list by removing blank lines and comment lines
sed '/^#.*\|^$/d' "../conf/$migration_list" > "$temp_migration_list" sed '/^#.*\|^$/d' "../conf/$migration_list" > "$temp_migration_list"
@ -131,7 +120,7 @@ ynh_handle_app_migration () {
do do
# Replace all occurences of $app by $new_app in each file to move. # Replace all occurences of $app by $new_app in each file to move.
local move_to_destination="${file_to_move//\$app/$new_app}" 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." test -e "$move_to_destination" && ynh_die --message="A file named $move_to_destination already exists."
done < "$temp_migration_list" done < "$temp_migration_list"
#================================================= #=================================================
@ -140,10 +129,10 @@ ynh_handle_app_migration () {
local settings_dir="/etc/yunohost/apps" local settings_dir="/etc/yunohost/apps"
cp -a "$settings_dir/$old_app" "$settings_dir/$new_app" cp -a "$settings_dir/$old_app" "$settings_dir/$new_app"
cp -a ../{scripts,conf} "$settings_dir/$new_app" cp -a ../{scripts,conf,manifest.json} "$settings_dir/$new_app"
# Replace the old id by the new one # Replace the old id by the new one
ynh_replace_string "\(^id: .*\)$old_app" "\1$new_app" "$settings_dir/$new_app/settings.yml" ynh_replace_string --match_string="\(^id: .*\)$old_app" --replace_string="\1$new_app" --target_file="$settings_dir/$new_app/settings.yml"
# INFO: There a special behavior with yunohost app setting: # INFO: There a special behavior with yunohost app setting:
# if the id given in argument does not match with the id # if the id given in argument does not match with the id
# stored in the config file, the config file will be purged. # stored in the config file, the config file will be purged.
@ -151,16 +140,21 @@ ynh_handle_app_migration () {
# https://github.com/YunoHost/yunohost/blob/c6b5284be8da39cf2da4e1036a730eb5e0515096/src/yunohost/app.py#L1316-L1321 # 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 # Change the label if it's simply the name of the app
old_label=$(ynh_app_setting_get $new_app label) old_label=$(ynh_app_setting_get --app=$new_app --key=label)
if [ "${old_label,,}" == "$old_app_id" ] if [ "${old_label,,}" == "$old_app_id" ]
then then
# Build the new label from the id of the app. With the first character as upper case # 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-) 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 ynh_app_setting_set --app=$new_app --key=label --value=$new_label
fi fi
yunohost tools shell -c "from yunohost.permission import permission_delete; permission_delete('$old_app.main', force=True, sync_perm=False)" permissions_name=$(yunohost user permission list $old_app --short --output-as plain)
yunohost tools shell -c "from yunohost.permission import permission_create; permission_create('$new_app.main', url='/' , sync_perm=True)" for permission_name in $permissions_name
do
yunohost tools shell -c "from yunohost.permission import permission_delete; permission_delete('$permission_name', force=True, sync_perm=False)"
done
yunohost tools shell -c "from yunohost.permission import permission_create; permission_create('$new_app.main', url='/' , show_tile=True , sync_perm=True)"
#================================================= #=================================================
# MOVE FILES TO THE NEW DESTINATION # MOVE FILES TO THE NEW DESTINATION
@ -171,7 +165,7 @@ ynh_handle_app_migration () {
# Replace all occurence of $app by $new_app in each file to move. # Replace all occurence of $app by $new_app in each file to move.
move_to_destination="$(eval echo "${file_to_move//\$app/$new_app}")" move_to_destination="$(eval echo "${file_to_move//\$app/$new_app}")"
local real_file_to_move="$(eval echo "${file_to_move//\$app/$old_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 ynh_print_info --message="Move file $real_file_to_move to $move_to_destination" >&2
mv "$real_file_to_move" "$move_to_destination" mv "$real_file_to_move" "$move_to_destination"
done < "$temp_migration_list" done < "$temp_migration_list"
@ -180,47 +174,62 @@ ynh_handle_app_migration () {
#================================================= #=================================================
# Replace nginx checksum # Replace nginx checksum
ynh_replace_string "\(^checksum__etc_nginx.*\)_$old_app" "\1_$new_app/" "$settings_dir/$new_app/settings.yml" ynh_replace_string --match_string="\(^checksum__etc_nginx.*\)_$old_app" --replace_string="\1_$new_app" --target_file="$settings_dir/$new_app/settings.yml"
# Replace php5-fpm checksums # Replace php-fpm checksums
ynh_replace_string "\(^checksum__etc_php8.O.*[-_]\)$old_app" "\1$new_app/" "$settings_dir/$new_app/settings.yml" ynh_replace_string --match_string="\(^checksum__etc_php.*[-_]\)$old_app" --replace_string="\1$new_app" --target_file="$settings_dir/$new_app/settings.yml"
# Replace final_path # Replace final_path
ynh_replace_string "\(^final_path: .*\)$old_app" "\1$new_app" "$settings_dir/$new_app/settings.yml" ynh_replace_string --match_string="\(^final_path: .*\)$old_app" --replace_string="\1$new_app" --target_file="$settings_dir/$new_app/settings.yml"
# Replace fail2ban_filter
ynh_replace_string --match_string="\(^checksum__etc_fail2ban_filter.*\)_$old_app" --replace_string="\1_$new_app" --target_file="$settings_dir/$new_app/settings.yml"
# Replace fail2ban_jail
ynh_replace_string --match_string="\(^checksum__etc_fail2ban_jail.*\)_$old_app" --replace_string="\1_$new_app" --target_file="$settings_dir/$new_app/settings.yml"
# Replace systemd
ynh_replace_string --match_string="\(^checksum__etc_systemd_system.*\)_$old_app" --replace_string="\1_$new_app" --target_file="$settings_dir/$new_app/settings.yml"
#================================================= #=================================================
# MOVE THE DATABASE # MOVE THE MYSQL DATABASE
#================================================= #=================================================
# db_pwd=$(ynh_app_setting_get $old_app mysqlpwd) old_db_name=$(ynh_app_setting_get --app=$old_app --key=db_name)
# db_name=$dbname
# # Check if a database exists before trying to move it # Check if a database exists before trying to move it
# local mysql_root_password=$(cat $MYSQL_ROOT_PWD_FILE) local mysql_root_password=$(cat $MYSQL_ROOT_PWD_FILE)
# if [ -n "$db_name" ] && mysqlshow -u root -p$mysql_root_password | grep -q "^| $db_name" if [ -n "$old_db_name" ] && mysqlshow -u root -p$mysql_root_password | grep -q "^| $old_db_name"
# then then
# new_db_name=$(ynh_sanitize_dbid $new_app) old_db_user=$old_db_name
# echo "Rename the database $db_name to $new_db_name" >&2 db_pwd=$(ynh_app_setting_get --app=$old_app --key=mysqlpwd)
# local sql_dump="/tmp/${db_name}-$(date '+%s').sql" new_db_name=$(ynh_sanitize_dbid --db_name=$new_app)
new_db_user=$new_db_name
ynh_print_info --message="Rename the database $db_name to $new_db_name" >&2
# # Dump the old database local sql_dump="/tmp/${db_name}-$(date '+%s').sql"
# ynh_mysql_dump_db "$db_name" > "$sql_dump"
# # Create a new database # Dump the old database
# ynh_mysql_setup_db $new_db_name $new_db_name $db_pwd ynh_mysql_dump_db --database="$old_db_name" > "$sql_dump"
# # 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 # Create a new database
# ynh_mysql_remove_db $db_name $db_name ynh_mysql_setup_db --db_user=$new_db_user --db_name=$new_db_name --db_pwd=$db_pwd
# # And the dump
# ynh_secure_remove --file="$sql_dump"
# # Update the value of $db_name # Then restore the old one into the new one
# db_name=$new_db_name ynh_mysql_connect_as --user=$new_db_user --password=$db_pwd --database=$new_db_name < "$sql_dump"
# ynh_app_setting_set $new_app db_name $db_name
# fi # Remove the old database
ynh_mysql_remove_db --db_user=$old_db_user --db_name=$old_db_name
# And the dump
ynh_secure_remove --file="$sql_dump"
# Update the value of $db_name
db_name=$new_db_name
db_user=$new_db_user
ynh_app_setting_set --app=$new_app --key=db_name --value=$db_name
fi
#================================================= #=================================================
# CHANGE THE FAKE DEPENDENCIES PACKAGE # CHANGE THE FAKE DEPENDENCIES PACKAGE
@ -234,7 +243,7 @@ ynh_handle_app_migration () {
local old_package_name="${old_app//_/-}-ynh-deps" local old_package_name="${old_app//_/-}-ynh-deps"
local new_package_name="${new_app//_/-}-ynh-deps" local new_package_name="${new_app//_/-}-ynh-deps"
if ynh_package_is_installed "$old_package_name" if ynh_package_is_installed --package="$old_package_name"
then then
# Install a new fake package # Install a new fake package
app=$new_app app=$new_app

View file

@ -81,11 +81,6 @@ if [[ $migration_process -eq 1 ]]; then
# Replace the user # Replace the user
ynh_system_user_delete $old_app ynh_system_user_delete $old_app
ynh_system_user_create --username=$app --home_dir="$final_path" ynh_system_user_create --username=$app --home_dir="$final_path"
# Ensuring the user has the right home dir
if [ ~$app != "$final_path" ]; then
usermod -d "$final_path" $app
fi
fi fi
#================================================= #=================================================