mirror of
https://github.com/YunoHost-Apps/distbin_ynh.git
synced 2024-09-03 18:26:10 +02:00
implement ynh_add_config
This commit is contained in:
parent
f44aa9c34e
commit
c6d1b249c3
5 changed files with 145 additions and 56 deletions
|
@ -8,7 +8,7 @@ PORT=__PORT__
|
||||||
DB_DIR=__FINALPATH__/distbin-db
|
DB_DIR=__FINALPATH__/distbin-db
|
||||||
|
|
||||||
# The external URL
|
# The external URL
|
||||||
EXTERNAL_URL=https://__DOMAIN_URI__/
|
EXTERNAL_URL=https://__DOMAIN____PATH__/
|
||||||
|
|
||||||
# The internal URL
|
# The internal URL
|
||||||
INTERNAL_URL=http://localhost:__PORT__/
|
INTERNAL_URL=http://localhost:__PORT__/
|
||||||
|
|
|
@ -113,22 +113,10 @@ fi
|
||||||
#=================================================
|
#=================================================
|
||||||
ynh_script_progression --message="Modifying config file..."
|
ynh_script_progression --message="Modifying config file..."
|
||||||
|
|
||||||
config="$final_path/.env"
|
domain="$new_domain"
|
||||||
ynh_backup_if_checksum_is_different --file="$config"
|
path_url="$new_path"
|
||||||
|
|
||||||
# Change the URL in the configuration file
|
ynh_add_config --template="../conf/.env" --destination="$final_path/.env"
|
||||||
|
|
||||||
if [ "$new_path" == "/" ]
|
|
||||||
then
|
|
||||||
new_domain_uri="$new_domain"
|
|
||||||
else
|
|
||||||
new_domain_uri="$new_domain$new_path"
|
|
||||||
fi
|
|
||||||
|
|
||||||
ynh_replace_string --match_string="EXTERNAL_URL=.*" --replace_string="EXTERNAL_URL=https://$new_domain_uri/" --target_file="$config"
|
|
||||||
|
|
||||||
# Recalculate and store the checksum of the file for the next upgrade.
|
|
||||||
ynh_store_file_checksum --file="$config"
|
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# GENERIC FINALISATION
|
# GENERIC FINALISATION
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#=================================================
|
#=================================================
|
||||||
|
|
||||||
source _common.sh
|
source _common.sh
|
||||||
|
source ynh_add_config
|
||||||
source /usr/share/yunohost/helpers
|
source /usr/share/yunohost/helpers
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
|
@ -150,28 +151,7 @@ ynh_add_systemd_config
|
||||||
#=================================================
|
#=================================================
|
||||||
ynh_script_progression --message="Modifying a config file..."
|
ynh_script_progression --message="Modifying a config file..."
|
||||||
|
|
||||||
config="$final_path/.env"
|
ynh_add_config --template="../conf/.env" --destination="$final_path/.env"
|
||||||
cp "../conf/.env" "$config"
|
|
||||||
|
|
||||||
if [ "$path_url" == "/" ]
|
|
||||||
then
|
|
||||||
domain_uri="$domain"
|
|
||||||
else
|
|
||||||
domain_uri="$domain$path_url"
|
|
||||||
fi
|
|
||||||
|
|
||||||
ynh_replace_string --match_string="__NODEJS_PATH__" --replace_string="$nodejs_path" --target_file="$config"
|
|
||||||
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$config"
|
|
||||||
ynh_replace_string --match_string="__DOMAIN_URI__" --replace_string="$domain_uri" --target_file="$config"
|
|
||||||
ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$config"
|
|
||||||
|
|
||||||
#=================================================
|
|
||||||
# STORE THE CONFIG FILE CHECKSUM
|
|
||||||
#=================================================
|
|
||||||
ynh_script_progression --message="Storing the config file checksum..."
|
|
||||||
|
|
||||||
# Calculate and store the config file checksum into the app settings
|
|
||||||
ynh_store_file_checksum --file="$config"
|
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# GENERIC FINALIZATION
|
# GENERIC FINALIZATION
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#=================================================
|
#=================================================
|
||||||
|
|
||||||
source _common.sh
|
source _common.sh
|
||||||
|
source ynh_add_config
|
||||||
source /usr/share/yunohost/helpers
|
source /usr/share/yunohost/helpers
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
|
@ -162,24 +163,7 @@ ynh_add_systemd_config
|
||||||
#=================================================
|
#=================================================
|
||||||
ynh_script_progression --message="Modifying config file..."
|
ynh_script_progression --message="Modifying config file..."
|
||||||
|
|
||||||
config="$final_path/.env"
|
ynh_add_config --template="../conf/.env" --destination="$final_path/.env"
|
||||||
ynh_backup_if_checksum_is_different --file="$config"
|
|
||||||
cp -f "../conf/.env" "$config"
|
|
||||||
|
|
||||||
if [ "$path_url" == "/" ]
|
|
||||||
then
|
|
||||||
domain_uri="$domain"
|
|
||||||
else
|
|
||||||
domain_uri="$domain$path_url"
|
|
||||||
fi
|
|
||||||
|
|
||||||
ynh_replace_string --match_string="__NODEJS_PATH__" --replace_string="$nodejs_path" --target_file="$config"
|
|
||||||
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$config"
|
|
||||||
ynh_replace_string --match_string="__DOMAIN_URI__" --replace_string="$domain_uri" --target_file="$config"
|
|
||||||
ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$config"
|
|
||||||
|
|
||||||
# Recalculate and store the checksum of the file for the next upgrade.
|
|
||||||
ynh_store_file_checksum --file="$config"
|
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# GENERIC FINALIZATION
|
# GENERIC FINALIZATION
|
||||||
|
|
137
scripts/ynh_add_config
Normal file
137
scripts/ynh_add_config
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Create a dedicated config file from a template
|
||||||
|
#
|
||||||
|
# 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"
|
||||||
|
#
|
||||||
|
# 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
|
||||||
|
# that should be defined before calling this helper :
|
||||||
|
# __PATH__ by $path_url
|
||||||
|
# __NAME__ by $app
|
||||||
|
# __NAMETOCHANGE__ by $app
|
||||||
|
# __USER__ by $app
|
||||||
|
# __FINALPATH__ by $final_path
|
||||||
|
# __PHPVERSION__ by $YNH_PHP_VERSION
|
||||||
|
#
|
||||||
|
# And any dynamic variables that should be defined before calling this helper like:
|
||||||
|
# __DOMAIN__ by $domain
|
||||||
|
# __APP__ by $app
|
||||||
|
# __VAR_1__ by $var_1
|
||||||
|
# __VAR_2__ by $var_2
|
||||||
|
#
|
||||||
|
# The helper will verify the checksum and backup the destination file
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
ynh_add_config () {
|
||||||
|
# Declare an array to define the options of this helper.
|
||||||
|
local legacy_args=tdv
|
||||||
|
local -A args_array=( [t]=template= [d]=destination= )
|
||||||
|
local template
|
||||||
|
local destination
|
||||||
|
# Manage arguments with getopts
|
||||||
|
ynh_handle_getopts_args "$@"
|
||||||
|
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
|
||||||
|
|
||||||
|
ynh_backup_if_checksum_is_different --file="$destination"
|
||||||
|
|
||||||
|
cp "$template_path" "$destination"
|
||||||
|
|
||||||
|
ynh_replace_vars --file="$destination"
|
||||||
|
|
||||||
|
ynh_store_file_checksum --file="$destination"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Replace variables in a file
|
||||||
|
#
|
||||||
|
# usage: ynh_replace_vars --file="file"
|
||||||
|
# | arg: -f, --file= - File where to replace variables
|
||||||
|
#
|
||||||
|
# The helper will replace the following keywords with global variables
|
||||||
|
# that should be defined before calling this helper :
|
||||||
|
# __PATH__ by $path_url
|
||||||
|
# __NAME__ by $app
|
||||||
|
# __NAMETOCHANGE__ by $app
|
||||||
|
# __USER__ by $app
|
||||||
|
# __FINALPATH__ by $final_path
|
||||||
|
# __PHPVERSION__ by $YNH_PHP_VERSION
|
||||||
|
#
|
||||||
|
# And any dynamic variables that should be defined before calling this helper like:
|
||||||
|
# __DOMAIN__ by $domain
|
||||||
|
# __APP__ by $app
|
||||||
|
# __VAR_1__ by $var_1
|
||||||
|
# __VAR_2__ by $var_2
|
||||||
|
#
|
||||||
|
#
|
||||||
|
ynh_replace_vars () {
|
||||||
|
# Declare an array to define the options of this helper.
|
||||||
|
local legacy_args=f
|
||||||
|
local -A args_array=( [f]=file= )
|
||||||
|
local file
|
||||||
|
# Manage arguments with getopts
|
||||||
|
ynh_handle_getopts_args "$@"
|
||||||
|
|
||||||
|
# Replace specific YunoHost variables
|
||||||
|
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%/}
|
||||||
|
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"
|
||||||
|
fi
|
||||||
|
if test -n "${app:-}"; then
|
||||||
|
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"
|
||||||
|
fi
|
||||||
|
if test -n "${final_path:-}"; then
|
||||||
|
ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$file"
|
||||||
|
fi
|
||||||
|
if test -n "${YNH_PHP_VERSION:-}"; then
|
||||||
|
ynh_replace_string --match_string="__PHPVERSION__" --replace_string="$YNH_PHP_VERSION" --target_file="$file"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Replace othes variables
|
||||||
|
|
||||||
|
# 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[@]}"
|
||||||
|
do
|
||||||
|
# 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"
|
||||||
|
done
|
||||||
|
}
|
Loading…
Reference in a new issue