helpers2.1: use positional args for file checksum helpers

This commit is contained in:
Alexandre Aubin 2024-06-20 13:44:09 +02:00
parent e12d79390f
commit d9b9aa1884
8 changed files with 39 additions and 64 deletions

View file

@ -62,7 +62,7 @@ ynh_apt_install_dependencies() {
if [[ -f "$old_php_finalphpconf" ]] if [[ -f "$old_php_finalphpconf" ]]
then then
ynh_backup_if_checksum_is_different --file="$old_php_finalphpconf" ynh_backup_if_checksum_is_different "$old_php_finalphpconf"
ynh_remove_fpm_config ynh_remove_fpm_config
fi fi
fi fi

View file

@ -201,34 +201,19 @@ ynh_restore_everything() {
done done
} }
_ynh_file_checksum_exists() {
local file=$1
local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_'
[[ -n "$(ynh_app_setting_get --key=$checksum_setting_name)" ]]
}
# Calculate and store a file checksum into the app settings # Calculate and store a file checksum into the app settings
# #
# usage: ynh_store_file_checksum --file=file # usage: ynh_store_file_checksum /path/to/file
# | arg: -f, --file= - The file on which the checksum will performed, then stored.
#
# $app should be defined when calling this helper
#
# Requires YunoHost version 2.6.4 or higher.
ynh_store_file_checksum() { ynh_store_file_checksum() {
# ============ Argument parsing ============= local file=$1
local -A args_array=([f]=file= [u]=update_only)
local file
local update_only
update_only="${update_only:-0}"
ynh_handle_getopts_args "$@"
# ===========================================
local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_' local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_'
# If update only, we don't save the new checksum if no old checksum exist
if [ $update_only -eq 1 ]; then
local checksum_value=$(ynh_app_setting_get --key=$checksum_setting_name)
if [ -z "${checksum_value}" ]; then
unset backup_file_checksum
return 0
fi
fi
ynh_app_setting_set --key=$checksum_setting_name --value=$(md5sum "$file" | cut --delimiter=' ' --fields=1) ynh_app_setting_set --key=$checksum_setting_name --value=$(md5sum "$file" | cut --delimiter=' ' --fields=1)
if [ ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]; then if [ ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]; then
@ -250,21 +235,12 @@ ynh_store_file_checksum() {
# Verify the checksum and backup the file if it's different # Verify the checksum and backup the file if it's different
# #
# usage: ynh_backup_if_checksum_is_different --file=file # usage: ynh_backup_if_checksum_is_different /path/to/file
# | arg: -f, --file= - The file on which the checksum test will be perfomed.
# | ret: the name of a backup file, or nothing
# #
# This helper is primarily meant to allow to easily backup personalised/manually # This helper is primarily meant to allow to easily backup personalised/manually
# modified config files. # modified config files.
#
# Requires YunoHost version 2.6.4 or higher.
ynh_backup_if_checksum_is_different() { ynh_backup_if_checksum_is_different() {
# ============ Argument parsing ============= local file=$1
local -A args_array=([f]=file=)
local file
ynh_handle_getopts_args "$@"
# ===========================================
local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_' local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_'
local checksum_value=$(ynh_app_setting_get --key=$checksum_setting_name) local checksum_value=$(ynh_app_setting_get --key=$checksum_setting_name)
# backup_file_checksum isn't declare as local, so it can be reuse by ynh_store_file_checksum # backup_file_checksum isn't declare as local, so it can be reuse by ynh_store_file_checksum
@ -291,19 +267,9 @@ ynh_backup_if_checksum_is_different() {
# Delete a file checksum from the app settings # Delete a file checksum from the app settings
# #
# usage: ynh_delete_file_checksum --file=file # usage: ynh_delete_file_checksum /path/to/file
# | arg: -f, --file= - The file for which the checksum will be deleted
#
# $app should be defined when calling this helper
#
# Requires YunoHost version 3.3.1 or higher.
ynh_delete_file_checksum() { ynh_delete_file_checksum() {
# ============ Argument parsing ============= local file=$1
local -A args_array=([f]=file=)
local file
ynh_handle_getopts_args "$@"
# ===========================================
local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_' local checksum_setting_name=checksum_${file//[\/ ]/_} # Replace all '/' and ' ' by '_'
ynh_app_setting_delete --key=$checksum_setting_name ynh_app_setting_delete --key=$checksum_setting_name
} }

View file

@ -75,16 +75,19 @@ _ynh_app_config_apply_one() {
fi fi
local bind_file="$(echo "$bind" | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)" local bind_file="$(echo "$bind" | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)"
if [[ "${!short_setting}" == "" ]]; then if [[ "${!short_setting}" == "" ]]; then
ynh_backup_if_checksum_is_different --file="$bind_file" ynh_backup_if_checksum_is_different "$bind_file"
ynh_safe_rm "$bind_file" ynh_safe_rm "$bind_file"
ynh_delete_file_checksum --file="$bind_file" ynh_delete_file_checksum "$bind_file"
ynh_print_info "File '$bind_file' removed" ynh_print_info "File '$bind_file' removed"
else else
ynh_backup_if_checksum_is_different --file="$bind_file" ynh_backup_if_checksum_is_different "$bind_file"
if [[ "${!short_setting}" != "$bind_file" ]]; then if [[ "${!short_setting}" != "$bind_file" ]]; then
cp "${!short_setting}" "$bind_file" cp "${!short_setting}" "$bind_file"
fi fi
ynh_store_file_checksum --file="$bind_file" --update_only if _ynh_file_checksum_exists "$bind_file"
then
ynh_store_file_checksum "$bind_file"
fi
ynh_print_info "File '$bind_file' overwritten with ${!short_setting}" ynh_print_info "File '$bind_file' overwritten with ${!short_setting}"
fi fi
@ -99,9 +102,12 @@ _ynh_app_config_apply_one() {
ynh_die "For technical reasons, multiline text '${short_setting}' can't be stored automatically in a variable file, you have to create custom getter/setter" ynh_die "For technical reasons, multiline text '${short_setting}' can't be stored automatically in a variable file, you have to create custom getter/setter"
fi fi
local bind_file="$(echo "$bind" | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)" local bind_file="$(echo "$bind" | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)"
ynh_backup_if_checksum_is_different --file="$bind_file" ynh_backup_if_checksum_is_different "$bind_file"
echo "${!short_setting}" >"$bind_file" echo "${!short_setting}" >"$bind_file"
ynh_store_file_checksum --file="$bind_file" --update_only if _ynh_file_checksum_exists "$bind_file"
then
ynh_store_file_checksum "$bind_file"
fi
ynh_print_info "File '$bind_file' overwritten with the content provided in question '${short_setting}'" ynh_print_info "File '$bind_file' overwritten with the content provided in question '${short_setting}'"
# Set value into a kind of key/value file # Set value into a kind of key/value file
@ -115,9 +121,12 @@ _ynh_app_config_apply_one() {
bind_key_=${bind_key_:-$short_setting} bind_key_=${bind_key_:-$short_setting}
local bind_file="$(echo "$bind" | cut -d: -f2 | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)" local bind_file="$(echo "$bind" | cut -d: -f2 | sed s@__INSTALL_DIR__@${install_dir:-}@ | sed s/__APP__/$app/)"
ynh_backup_if_checksum_is_different --file="$bind_file" ynh_backup_if_checksum_is_different "$bind_file"
ynh_write_var_in_file --file="${bind_file}" --key="${bind_key_}" --value="${!short_setting}" --after="${bind_after}" ynh_write_var_in_file --file="${bind_file}" --key="${bind_key_}" --value="${!short_setting}" --after="${bind_after}"
ynh_store_file_checksum --file="$bind_file" --update_only if _ynh_file_checksum_exists "$bind_file"
then
ynh_store_file_checksum "$bind_file"
fi
# We stored the info in settings in order to be able to upgrade the app # We stored the info in settings in order to be able to upgrade the app
ynh_app_setting_set --key=$short_setting --value="${!short_setting}" ynh_app_setting_set --key=$short_setting --value="${!short_setting}"

View file

@ -191,7 +191,7 @@ _ynh_go_cleanup () {
then then
# Remove goenv environment configuration # Remove goenv environment configuration
ynh_print_info "Removing of goenv" ynh_print_info "Removing of goenv"
ynh_secure_remove --file="$goenv_install_dir" ynh_safe_rm "$goenv_install_dir"
ynh_secure_remove --file="/etc/profile.d/goenv.sh" ynh_safe_rm "/etc/profile.d/goenv.sh"
fi fi
} }

View file

@ -28,7 +28,7 @@ ynh_config_add_nginx() {
ynh_replace --match="^#root_path_only" --replace="" --file="$finalnginxconf" ynh_replace --match="^#root_path_only" --replace="" --file="$finalnginxconf"
fi fi
ynh_store_file_checksum --file="$finalnginxconf" ynh_store_file_checksum "$finalnginxconf"
ynh_systemd_action --service=nginx --action=reload ynh_systemd_action --service=nginx --action=reload
} }
@ -56,8 +56,8 @@ ynh_config_change_url_nginx() {
# ynh_config_add inside ynh_config_add_nginx because the path may have # ynh_config_add inside ynh_config_add_nginx because the path may have
# changed if we're changing the domain too...) # changed if we're changing the domain too...)
local old_nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf local old_nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf
ynh_backup_if_checksum_is_different --file="$old_nginx_conf_path" ynh_backup_if_checksum_is_different "$old_nginx_conf_path"
ynh_delete_file_checksum --file="$old_nginx_conf_path" ynh_delete_file_checksum "$old_nginx_conf_path"
ynh_safe_rm "$old_nginx_conf_path" ynh_safe_rm "$old_nginx_conf_path"
# Regen the nginx conf # Regen the nginx conf

View file

@ -76,7 +76,7 @@ ynh_config_add_phpfpm() {
if [[ -f "$old_php_finalphpconf" ]] if [[ -f "$old_php_finalphpconf" ]]
then then
ynh_backup_if_checksum_is_different --file="$old_php_finalphpconf" ynh_backup_if_checksum_is_different "$old_php_finalphpconf"
ynh_remove_fpm_config ynh_remove_fpm_config
fi fi
fi fi

View file

@ -91,7 +91,7 @@ ynh_config_add() {
ynh_die "The provided template $template doesn't exist" ynh_die "The provided template $template doesn't exist"
fi fi
ynh_backup_if_checksum_is_different --file="$destination" ynh_backup_if_checksum_is_different "$destination"
# Make sure to set the permissions before we copy the file # Make sure to set the permissions before we copy the file
# This is to cover a case where an attacker could have # This is to cover a case where an attacker could have
@ -113,7 +113,7 @@ ynh_config_add() {
ynh_replace_vars --file="$destination" ynh_replace_vars --file="$destination"
fi fi
ynh_store_file_checksum --file="$destination" ynh_store_file_checksum "$destination"
} }
# Replace variables in a file # Replace variables in a file

View file

@ -422,7 +422,7 @@ _acceptable_path_to_delete() {
fi fi
} }
# Remove a file or a directory securely # Remove a file or a directory, checking beforehand that it's not a disastrous location to rm such as entire /var or /home
# #
# usage: ynh_safe_rm path_to_remove # usage: ynh_safe_rm path_to_remove
# #