1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/wekan_ynh.git synced 2024-09-03 20:36:09 +02:00
wekan_ynh/scripts/ynh_add_config

141 lines
5.4 KiB
Text
Raw Normal View History

2020-06-15 03:40:49 +02:00
#!/bin/bash
2020-08-08 20:27:34 +02:00
# Create a dedicated config file from a template
2020-06-15 03:40:49 +02:00
#
2020-08-08 20:27:34 +02:00
# examples:
# ynh_add_config --template=".env" --destination="$final_path/.env"
# ynh_add_config --template="../conf/.env" --destination="$final_path/.env"
# ynh_add_config --template="/etc/nginx/sites-available/default" --destination="etc/nginx/sites-available/mydomain.conf"
2020-06-15 03:40:49 +02:00
#
2020-08-08 20:27:34 +02:00
# usage: ynh_add_config --template="template" --destination="destination"
# | arg: -t, --template= - Template config file to use
# | arg: -d, --destination= - Destination of the config file
#
# The template can be by default the name of a file in the conf directory
# of a YunoHost Package, a relative path or an absolute path
# The helper will use the template $template to generate a config file
# $destination by replacing the following keywords with global variables
2020-06-15 03:40:49 +02:00
# that should be defined before calling this helper :
2020-08-08 20:27:34 +02:00
# __PATH__ by $path_url
# __NAME__ by $app
2020-06-15 03:40:49 +02:00
# __NAMETOCHANGE__ by $app
2020-08-08 20:27:34 +02:00
# __USER__ by $app
# __FINALPATH__ by $final_path
# __PHPVERSION__ by $YNH_PHP_VERSION
2020-06-15 03:40:49 +02:00
#
2020-08-08 20:27:34 +02:00
# And any dynamic variables that should be defined before calling this helper like:
# __DOMAIN__ by $domain
# __APP__ by $app
2020-06-15 03:40:49 +02:00
# __VAR_1__ by $var_1
# __VAR_2__ by $var_2
#
# The helper will verify the checksum and backup the destination file
2020-08-08 20:27:34 +02:00
# if it's different before applying the new template.
# And it will calculate and store the destination file checksum
# into the app settings when configuration is done.
#
2020-06-15 03:40:49 +02:00
#
ynh_add_config () {
# Declare an array to define the options of this helper.
2020-08-08 20:27:34 +02:00
local legacy_args=tdv
local -A args_array=( [t]=template= [d]=destination= )
local template
2020-06-15 03:40:49 +02:00
local destination
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-08-08 20:27:34 +02:00
local template_path
if [ -f "../conf/$template" ]; then
template_path="../conf/$template"
elif [ -f "../settings/conf/$template" ]; then
template_path="../settings/conf/$template"
elif [ -f "$template" ]; then
template_path=$template
else
ynh_die --message="The provided template $template doesn't exist"
fi
2020-06-15 03:40:49 +02:00
ynh_backup_if_checksum_is_different --file="$destination"
2020-08-08 20:27:34 +02:00
cp "$template_path" "$destination"
2020-06-16 06:13:34 +02:00
2020-08-08 20:27:34 +02:00
ynh_replace_vars --file="$destination"
2020-06-16 06:13:34 +02:00
2020-08-08 20:27:34 +02:00
ynh_store_file_checksum --file="$destination"
2020-06-16 06:13:34 +02:00
}
# Replace variables in a file
#
2020-08-08 20:27:34 +02:00
# usage: ynh_replace_vars --file="file"
# | arg: -f, --file= - File where to replace variables
2020-06-16 06:13:34 +02:00
#
2020-08-08 20:27:34 +02:00
# The helper will replace the following keywords with global variables
2020-06-16 06:13:34 +02:00
# that should be defined before calling this helper :
2020-08-08 20:27:34 +02:00
# __PATH__ by $path_url
# __NAME__ by $app
2020-06-16 06:13:34 +02:00
# __NAMETOCHANGE__ by $app
2020-08-08 20:27:34 +02:00
# __USER__ by $app
# __FINALPATH__ by $final_path
# __PHPVERSION__ by $YNH_PHP_VERSION
2020-06-16 06:13:34 +02:00
#
2020-08-08 20:27:34 +02:00
# And any dynamic variables that should be defined before calling this helper like:
# __DOMAIN__ by $domain
# __APP__ by $app
2020-06-16 06:13:34 +02:00
# __VAR_1__ by $var_1
# __VAR_2__ by $var_2
#
#
ynh_replace_vars () {
# Declare an array to define the options of this helper.
2020-08-08 20:27:34 +02:00
local legacy_args=f
local -A args_array=( [f]=file= )
2020-06-16 06:13:34 +02:00
local file
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-08-08 20:27:34 +02:00
# Replace specific YunoHost variables
2020-06-15 03:40:49 +02:00
if test -n "${path_url:-}"
then
# path_url_slash_less is path_url, or a blank value if path_url is only '/'
local path_url_slash_less=${path_url%/}
2020-06-16 06:13:34 +02:00
ynh_replace_string --match_string="__PATH__/" --replace_string="$path_url_slash_less/" --target_file="$file"
ynh_replace_string --match_string="__PATH__" --replace_string="$path_url" --target_file="$file"
2020-06-15 03:40:49 +02:00
fi
if test -n "${app:-}"; then
2020-06-16 06:13:34 +02:00
ynh_replace_string --match_string="__NAME__" --replace_string="$app" --target_file="$file"
ynh_replace_string --match_string="__NAMETOCHANGE__" --replace_string="$app" --target_file="$file"
ynh_replace_string --match_string="__USER__" --replace_string="$app" --target_file="$file"
2020-06-15 03:40:49 +02:00
fi
if test -n "${final_path:-}"; then
2020-06-16 06:13:34 +02:00
ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$file"
2020-06-15 03:40:49 +02:00
fi
if test -n "${YNH_PHP_VERSION:-}"; then
2020-06-16 06:13:34 +02:00
ynh_replace_string --match_string="__PHPVERSION__" --replace_string="$YNH_PHP_VERSION" --target_file="$file"
2020-06-15 03:40:49 +02:00
fi
2020-08-08 21:02:38 +02:00
if test -n "${ynh_node_load_PATH:-}"; then
2020-08-08 21:46:47 +02:00
ynh_replace_string --match_string="__YNH_NODE_LOAD_PATH__" --replace_string="$ynh_node_load_PATH" --target_file="$file"
2020-08-08 21:02:38 +02:00
fi
2020-06-15 03:40:49 +02:00
2020-08-08 21:46:47 +02:00
# Replace othes variables
2020-08-08 20:27:34 +02:00
# List other unique (__ __) variables in $file
local uniques_vars=( $(grep -o '__[A-Z0-9_]*__' $file | sort --unique | sed "s@__\([^.]*\)__@\L\1@g" ))
# Do the replacement
local delimit=@
for one_var in "${uniques_vars[@]}"
2020-06-15 03:40:49 +02:00
do
2020-08-08 20:27:34 +02:00
# Validate that one_var is indeed defined
test -n "${!one_var:-}" || ynh_die --message="\$$one_var wasn't initialized when trying to replace __${one_var^^}__ in $file"
# Escape delimiter in match/replace string
match_string="__${one_var^^}__"
match_string=${match_string//${delimit}/"\\${delimit}"}
replace_string="${!one_var}"
replace_string=${replace_string//${delimit}/"\\${delimit}"}
# Actually replace (sed is used instead of ynh_replace_string to avoid triggering an epic amount of debug logs)
sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$file"
2020-06-15 03:40:49 +02:00
done
2020-08-08 20:27:34 +02:00
}