1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/shaarli_ynh.git synced 2024-09-03 20:26:10 +02:00

Re-implement automatic post-installation

This commit is contained in:
tituspijean 2020-11-25 10:34:14 +01:00
parent a5a5ea5e64
commit eb7843947b
4 changed files with 235 additions and 18 deletions

View file

@ -8,7 +8,9 @@
"updates": "data\/updates.txt",
"log": "data\/log.txt",
"update_check": "data\/lastupdatecheck.txt",
"history": "data\/history.php",
"raintpl_tpl": "tpl\/",
"theme": "default",
"raintpl_tmp": "tmp\/",
"thumbnails_cache": "cache",
"page_cache": "pagecache"
@ -17,7 +19,12 @@
"ban_after": 4,
"ban_duration": 1800,
"session_protection_disabled": false,
"open_shaarli": false
"open_shaarli": false,
"allowed_protocols": [
"ftp",
"ftps",
"magnet"
]
},
"general": {
"header_link": "?",
@ -25,8 +32,10 @@
"enabled_plugins": [
"qrcode"
],
"timezone": "YNH_TIMEZONE",
"title": "YNH_TITLE"
"default_note_title": "Note: ",
"retrieve_description": false,
"timezone": "__TIMEZONE__",
"title": "__TITLE__"
},
"updates": {
"check_updates": true,
@ -35,26 +44,35 @@
},
"feed": {
"rss_permalinks": true,
"show_atom": false
"show_atom": true
},
"privacy": {
"default_private_links": YNH_PRIVATE_LINK_BY_DEFAULT,
"default_private_links": __DEFAULT_PRIVATE_LINKS__,
"hide_public_links": false,
"hide_timestamps": false
"force_login": false,
"hide_timestamps": false,
"remember_user_default": true
},
"thumbnail": {
"enable_thumbnails": true,
"enable_localcache": true
"thumbnails": {
"mode": "all",
"width": "125",
"height": "90"
},
"redirector": {
"url": "",
"encode_url": true
"translation": {
"language": "auto",
"mode": "php",
"extensions": []
},
"plugins": [],
"formatter": "markdown",
"credentials": {
"login": "YNH_ADMIN",
"salt": "YNH_SALT",
"hash": "YNH_HASH"
"login": "__ADMIN__",
"salt": "__SALT__",
"hash": "__HASH__"
},
"api": {
"enabled": true,
"secret": "__SECRET__"
}
}
*/ ?>

View file

@ -43,6 +43,34 @@
"example": "/shaarli",
"default": "/shaarli"
},
{
"name": "admin",
"type": "user",
"ask": {
"en": "Choose the admin user",
"fr": "Choisissez ladministrateur"
},
"example": "johndoe"
},
{
"name": "password",
"type": "password",
"ask": {
"en": "Choose the admin password",
"fr": "Choisissez le mot de passe de ladministrateur"
},
"example": "johndoe"
},
{
"name": "title",
"type": "string",
"ask": {
"en": "Choose a title for your Shaarli instance",
"fr": "Choisissez un titre pour votre instance Shaarli"
},
"example": "Shaarli",
"default": "Shaarli"
},
{
"name": "is_public",
"type": "boolean",
@ -51,11 +79,11 @@
"fr": "Est-ce un site Shaarli public ?"
},
"help": {
"en": "If set as public, Shaarli will be visible to anyone, including non-Yunohost users. However only Shaarli users can add bookmarks. Set this to public if you want people to see your (public) bookmarks",
"fr": "Si configuré en public, Shaarli sera visible par tout le monde, y compris des personnes sans compte sur votre Yunohost. Cependant seules les personnes avec un compte Shaarli pourrons ajouter des marques-pages. Activer la visibilité publique si vous voulez que les gens voient vos marques-pages (publiques)."
"en": "If set as public, Shaarli will be visible to anyone, including non-Yunohost users. However only Shaarli users can add bookmarks. Your links will be public by default, but you can change that in Shaarli's configuration page.",
"fr": "Si configuré en public, Shaarli sera visible par tout le monde, y compris des personnes sans compte sur votre Yunohost. Cependant seules les personnes avec un compte Shaarli pourrons ajouter des marques-pages. Vos nouveaux liens seront publics par défaut, mais vous pourrez changer ça dans la page de configuration de Shaarli."
},
"default": false
}
}
]
}
}

View file

@ -57,6 +57,146 @@ ynh_smart_mktemp () {
echo "$(mktemp --directory --tmpdir="$tmpdir")"
}
# 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
if test -n "${ynh_node_load_PATH:-}"; then
ynh_replace_string --match_string="__YNH_NODE_LOAD_PATH__" --replace_string="$ynh_node_load_PATH" --target_file="$file"
fi
# Replace others 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
}
#=================================================
# FUTURE OFFICIAL HELPERS
#=================================================

View file

@ -25,6 +25,9 @@ ynh_abort_if_errors
#=================================================
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
admin=$YNH_APP_ARG_ADMIN
password=$YNH_APP_ARG_PASSWORD
title=$YNH_APP_ARG_TITLE
is_public=$YNH_APP_ARG_IS_PUBLIC
app=$YNH_APP_INSTANCE_NAME
@ -104,6 +107,34 @@ ynh_script_progression --message="Configuring log rotation..."
touch "$final_path/data/log.txt"
ynh_use_logrotate "$final_path/data/log.txt"
#=================================================
# MODIFY A CONFIG FILE
#=================================================
ynh_script_progression --message="Configuring Shaarli..."
# Get the timezone
timezone=$(cat /etc/timezone)
# Generate the salt
salt=$(php${YNH_PHP_VERSION} -r 'echo sha1(uniqid("", true) ."_". mt_rand());')
# Generate the hash with the password
hash=$(php${YNH_PHP_VERSION} -r "echo sha1('${password}'.'${admin}'.'${salt}');")
# Generate the API secret
secret=$(php${YNH_PHP_VERSION} -r "echo str_shuffle(substr(hash_hmac('sha512', uniqid('${salt}'), '${admin}'), 10, 12));")
# Set default_private_links. By default, make them public if the app is public.
if [ $is_public -eq 1 ]
then
default_private_links=false
else
default_private_links=true
fi
# Installing the config file and replace the placeholders
ynh_add_config --template="../conf/config.json.php" --destination="$final_path/data/config.json.php"
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================