From d3b202985184d475725cff72d75d79dc922926cb Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Fri, 13 Jul 2018 17:31:45 +0200 Subject: [PATCH] Upgrade helpers --- README.md | 9 +- README_fr.md | 9 +- manifest.json | 4 +- scripts/_common.sh | 692 +++++++++++++++++++++++++++++---------------- scripts/backup | 7 +- scripts/install | 2 +- scripts/restore | 9 +- scripts/upgrade | 22 +- 8 files changed, 487 insertions(+), 267 deletions(-) diff --git a/README.md b/README.md index 46eed6a..a5af61e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Archivist for YunoHost -[![Integration level](https://dash.yunohost.org/integration/archivist.svg)](https://ci-apps.yunohost.org/jenkins/job/archivist%20%28Community%29/lastBuild/consoleFull) +[![Integration level](https://dash.yunohost.org/integration/archivist.svg)](https://dash.yunohost.org/appci/app/archivist) [![Install archivist with YunoHost](https://install-app.yunohost.org/install-with-yunohost.png)](https://install-app.yunohost.org/?app=archivist) *[Lire ce readme en français.](./README_fr.md)* @@ -19,6 +19,10 @@ Archivist is automatically launched periodicaly to update your backups and send ## Screenshots +## Demo + +No demo available. + ## Configuration The configuration of archivist can be changed in the file /opt/yunohost/archivist/Backup_list.conf @@ -37,7 +41,8 @@ Not relevant. #### Supported architectures * x86-64b - [![Build Status](https://ci-apps.yunohost.org/jenkins/job/archivist%20(Community)/badge/icon)](https://ci-apps.yunohost.org/jenkins/job/archivist%20(Community)/) -* ARMv8-A - [![Build Status](https://ci-apps.yunohost.org/jenkins/job/archivist%20(Community)%20(%7EARM%7E)/badge/icon)](https://ci-apps.yunohost.org/jenkins/job/archivist%20(Community)%20(%7EARM%7E)/) +* ARMv8-A - [![Build Status](https://ci-apps-arm.yunohost.org/jenkins/job/archivist%20(Community)%20(%7EARM%7E)/badge/icon)](https://ci-apps-arm.yunohost.org/jenkins/job/archivist%20(Community)%20(%7EARM%7E)/) +* Jessie x86-64b - [![Build Status](https://ci-stretch.nohost.me/jenkins/job/archivist%20(Community)/badge/icon)](https://ci-stretch.nohost.me/jenkins/job/archivist%20(Community)/) ## Limitations diff --git a/README_fr.md b/README_fr.md index 7ab91c0..20a0b10 100644 --- a/README_fr.md +++ b/README_fr.md @@ -1,6 +1,6 @@ # Archivist pour YunoHost -[![Niveau d'intégration](https://dash.yunohost.org/integration/archivist.svg)](https://ci-apps.yunohost.org/jenkins/job/archivist%20%28Community%29/lastBuild/consoleFull) +[![Niveau d'intégration](https://dash.yunohost.org/integration/archivist.svg)](https://dash.yunohost.org/appci/app/archivist) [![Installer archivist avec YunoHost](https://install-app.yunohost.org/install-with-yunohost.png)](https://install-app.yunohost.org/?app=archivist) *[Read this readme in english.](./README.md)* @@ -19,6 +19,10 @@ Archivist est automatiquement lancé périodiquement pour mettre à jour vos sau ## Captures d'écran +## Démo + +Aucune démo pour cette application. + ## Configuration La configuration d'archivist peut être modifiée dans le fichier /opt/yunohost/archivist/Backup_list.conf @@ -37,7 +41,8 @@ Non applicable #### Architectures supportées. * x86-64b - [![Build Status](https://ci-apps.yunohost.org/jenkins/job/archivist%20(Community)/badge/icon)](https://ci-apps.yunohost.org/jenkins/job/archivist%20(Community)/) -* ARMv8-A - [![Build Status](https://ci-apps.yunohost.org/jenkins/job/archivist%20(Community)%20(%7EARM%7E)/badge/icon)](https://ci-apps.yunohost.org/jenkins/job/archivist%20(Community)%20(%7EARM%7E)/) +* ARMv8-A - [![Build Status](https://ci-apps-arm.yunohost.org/jenkins/job/archivist%20(Community)%20(%7EARM%7E)/badge/icon)](https://ci-apps-arm.yunohost.org/jenkins/job/archivist%20(Community)%20(%7EARM%7E)/) +* Jessie x86-64b - [![Build Status](https://ci-stretch.nohost.me/jenkins/job/archivist%20(Community)/badge/icon)](https://ci-stretch.nohost.me/jenkins/job/archivist%20(Community)/) ## Limitations diff --git a/manifest.json b/manifest.json index ddd13fc..59a4a70 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "Automatic backups.", "fr": "Sauvegardes automatiques." }, - "version": "0.6~ynh1", + "version": "0.6~ynh2", "url": "https://github.com/maniackcrudelis/archivist", "license": "GPL-3.0", "maintainer": { @@ -14,7 +14,7 @@ "email": "maniackc_dev@crudelis.fr" }, "requirements": { - "yunohost": ">= 2.7.2" + "yunohost": ">= 2.7.14" }, "multi_instance": true, "services": [], diff --git a/scripts/_common.sh b/scripts/_common.sh index 22b21c9..21234d0 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -26,186 +26,203 @@ CHECK_SIZE () { # Vérifie avant chaque backup que l'espace est suffisant # PACKAGE CHECK BYPASSING... #================================================= -IS_PACKAGE_CHECK () { # Détermine une exécution en conteneur (Non testé) - return $(uname -n | grep -c 'pchecker_lxc') +IS_PACKAGE_CHECK () { + return $(env | grep -c container=lxc) } #================================================= # EXPERIMENTAL HELPERS #================================================= -# INFOS -# n (Node version management) utilise la variable PATH pour stocker le path de la version de node à utiliser. -# C'est ainsi qu'il change de version -# ynh_install_nodejs installe la version de nodejs demandée en argument, avec n -# ynh_use_nodejs active une version de nodejs dans le script courant -# 3 variables sont mises à disposition, et 2 sont stockées dans la config de l'app -# - nodejs_path: Le chemin absolu de cette version de node -# Utilisé pour des appels directs à node. -# - nodejs_version: Simplement le numéro de version de nodejs pour cette application -# - nodejs_use_version: Un alias pour charger une version de node dans le shell courant. -# Utilisé pour démarrer un service ou un script qui utilise node ou npm -# Dans ce cas, c'est $PATH qui contient le chemin de la version de node. Il doit être propagé sur les autres shell si nécessaire. - -n_install_dir="/opt/node_n" -node_version_path="/opt/node_n/n/versions/node" -# N_PREFIX est le dossier de n, il doit être chargé dans les variables d'environnement pour n. -export N_PREFIX="$n_install_dir" - -ynh_install_n () { - echo "Installation of N - Node.js version management" >&2 - # Build an app.src for n - mkdir -p "../conf" - echo "SOURCE_URL=https://github.com/tj/n/archive/v2.1.7.tar.gz -SOURCE_SUM=2ba3c9d4dd3c7e38885b37e02337906a1ee91febe6d5c9159d89a9050f2eea8f" > "../conf/n.src" - # Download and extract n - ynh_setup_source "$n_install_dir/git" n - # Install n - (cd "$n_install_dir/git" - PREFIX=$N_PREFIX make install 2>&1) -} - -ynh_use_nodejs () { - nodejs_version=$(ynh_app_setting_get $app nodejs_version) - - load_n_path="[[ :$PATH: == *\":$n_install_dir/bin:\"* ]] || PATH=\"$n_install_dir/bin:$PATH\"; N_PREFIX="$n_install_dir"" - - nodejs_use_version="$n_install_dir/bin/n -q $nodejs_version" - - # "Load" a version of node - eval $load_n_path; $nodejs_use_version - - # Get the absolute path of this version of node - nodejs_path="$(n bin $nodejs_version)" - - # Make an alias for node use - ynh_node_exec="eval $load_n_path; n use $nodejs_version" -} - -ynh_install_nodejs () { - # Use n, https://github.com/tj/n to manage the nodejs versions - nodejs_version="$1" - local n_install_script="https://git.io/n-install" - - # Create $n_install_dir - mkdir -p "$n_install_dir" - - # Load n path in PATH - CLEAR_PATH="$n_install_dir/bin:$PATH" - # Remove /usr/local/bin in PATH in case of node has already setup. - PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') - - # Move an existing node binary, to avoid to block n. - test -x /usr/bin/node && mv /usr/bin/node /usr/bin/node_n - test -x /usr/bin/npm && mv /usr/bin/npm /usr/bin/npm_n - - # If n is not previously setup, install it - if ! test n --version > /dev/null 2>&1 +# Internal helper design to allow helpers to use getopts to manage their arguments +# +# example: function my_helper() +# { +# declare -Ar args_array=( [a]=arg1= [b]=arg2= [c]=arg3 ) +# local arg1 +# local arg2 +# local arg3 +# ynh_handle_getopts_args "$@" +# +# [...] +# } +# my_helper --arg1 "val1" -b val2 -c +# +# usage: ynh_handle_getopts_args "$@" +# | arg: $@ - Simply "$@" to tranfert all the positionnal arguments to the function +# +# This helper need an array, named "args_array" with all the arguments used by the helper +# that want to use ynh_handle_getopts_args +# Be carreful, this array has to be an associative array, as the following example: +# declare -Ar args_array=( [a]=arg1 [b]=arg2= [c]=arg3 ) +# Let's explain this array: +# a, b and c are short options, -a, -b and -c +# arg1, arg2 and arg3 are the long options associated to the previous short ones. --arg1, --arg2 and --arg3 +# For each option, a short and long version has to be defined. +# Let's see something more significant +# declare -Ar args_array=( [u]=user [f]=finalpath= [d]=database ) +# +# NB: Because we're using 'declare' without -g, the array will be declared as a local variable. +# +# Please keep in mind that the long option will be used as a variable to store the values for this option. +# For the previous example, that means that $finalpath will be fill with the value given as argument for this option. +# +# Also, in the previous example, finalpath has a '=' at the end. That means this option need a value. +# So, the helper has to be call with --finalpath /final/path, --finalpath=/final/path or -f /final/path, the variable $finalpath will get the value /final/path +# If there's many values for an option, -f /final /path, the value will be separated by a ';' $finalpath=/final;/path +# For an option without value, like --user in the example, the helper can be called only with --user or -u. $user will then get the value 1. +# +# To keep a retrocompatibility, a package can still call a helper, using getopts, with positional arguments. +# The "legacy mode" will manage the positional arguments and fill the variable in the same order than they are given in $args_array. +# e.g. for `my_helper "val1" val2`, arg1 will be filled with val1, and arg2 with val2. +ynh_handle_getopts_args () { + # Manage arguments only if there's some provided + set +x + if [ $# -ne 0 ] then - ynh_install_n + # Store arguments in an array to keep each argument separated + local arguments=("$@") + + # For each option in the array, reduce to short options for getopts (e.g. for [u]=user, --user will be -u) + # And built parameters string for getopts + # ${!args_array[@]} is the list of all keys in the array (A key is 'u' in [u]=user, user is a value) + local getopts_parameters="" + local key="" + for key in "${!args_array[@]}" + do + # Concatenate each keys of the array to build the string of arguments for getopts + # Will looks like 'abcd' for -a -b -c -d + # If the value of a key finish by =, it's an option with additionnal values. (e.g. --user bob or -u bob) + # Check the last character of the value associate to the key + if [ "${args_array[$key]: -1}" = "=" ] + then + # For an option with additionnal values, add a ':' after the letter for getopts. + getopts_parameters="${getopts_parameters}${key}:" + else + getopts_parameters="${getopts_parameters}${key}" + fi + # Check each argument given to the function + local arg="" + # ${#arguments[@]} is the size of the array + for arg in `seq 0 $(( ${#arguments[@]} - 1 ))` + do + # And replace long option (value of the key) by the short option, the key itself + # (e.g. for [u]=user, --user will be -u) + # Replace long option with = + arguments[arg]="${arguments[arg]//--${args_array[$key]}/-${key} }" + # And long option without = + arguments[arg]="${arguments[arg]//--${args_array[$key]%=}/-${key}}" + done + done + + # Read and parse all the arguments + # Use a function here, to use standart arguments $@ and be able to use shift. + parse_arg () { + # Read all arguments, until no arguments are left + while [ $# -ne 0 ] + do + # Initialize the index of getopts + OPTIND=1 + # Parse with getopts only if the argument begin by -, that means the argument is an option + # getopts will fill $parameter with the letter of the option it has read. + local parameter="" + getopts ":$getopts_parameters" parameter || true + + if [ "$parameter" = "?" ] + then + ynh_die "Invalid argument: -${OPTARG:-}" + elif [ "$parameter" = ":" ] + then + ynh_die "-$OPTARG parameter requires an argument." + else + local shift_value=1 + # Use the long option, corresponding to the short option read by getopts, as a variable + # (e.g. for [u]=user, 'user' will be used as a variable) + # Also, remove '=' at the end of the long option + # The variable name will be stored in 'option_var' + local option_var="${args_array[$parameter]%=}" + # If this option doesn't take values + # if there's a '=' at the end of the long option name, this option takes values + if [ "${args_array[$parameter]: -1}" != "=" ] + then + # 'eval ${option_var}' will use the content of 'option_var' + eval ${option_var}=1 + else + # Read all other arguments to find multiple value for this option. + # Load args in a array + local all_args=("$@") + + # If the first argument is longer than 2 characters, + # There's a value attached to the option, in the same array cell + if [ ${#all_args[0]} -gt 2 ]; then + # Remove the option and the space, so keep only the value itself. + all_args[0]="${all_args[0]#-${parameter} }" + # Reduce the value of shift, because the option has been removed manually + shift_value=$(( shift_value - 1 )) + fi + + # Then read the array value per value + for i in `seq 0 $(( ${#all_args[@]} - 1 ))` + do + # If this argument is an option, end here. + if [ "${all_args[$i]:0:1}" == "-" ] || [ -z "${all_args[$i]}" ] + then + # Ignore the first value of the array, which is the option itself + if [ "$i" -ne 0 ]; then + break + fi + else + # Declare the content of option_var as a variable. + eval ${option_var}="" + # Else, add this value to this option + # Each value will be separated by ';' + if [ -n "${!option_var}" ] + then + # If there's already another value for this option, add a ; before adding the new value + eval ${option_var}+="\;" + fi + eval ${option_var}+=\"${all_args[$i]}\" + shift_value=$(( shift_value + 1 )) + fi + done + fi + fi + + # Shift the parameter and its argument(s) + shift $shift_value + done + } + + # LEGACY MODE + # Check if there's getopts arguments + if [ "${arguments[0]:0:1}" != "-" ] + then + # If not, enter in legacy mode and manage the arguments as positionnal ones. + echo "! Helper used in legacy mode !" + for i in `seq 0 $(( ${#arguments[@]} -1 ))` + do + # Use getopts_parameters as a list of key of the array args_array + # Remove all ':' in getopts_parameters + getopts_parameters=${getopts_parameters//:} + # Get the key from getopts_parameters, by using the key according to the position of the argument. + key=${getopts_parameters:$i:1} + # Use the long option, corresponding to the key, as a variable + # (e.g. for [u]=user, 'user' will be used as a variable) + # Also, remove '=' at the end of the long option + # The variable name will be stored in 'option_var' + local option_var="${args_array[$key]%=}" + + # Store each value given as argument in the corresponding variable + # The values will be stored in the same order than $args_array + eval ${option_var}+=\"${arguments[$i]}\" + done + else + # END LEGACY MODE + # Call parse_arg and pass the modified list of args as an array of arguments. + parse_arg "${arguments[@]}" + fi fi - - # Modify the default N_PREFIX in n script - ynh_replace_string "^N_PREFIX=\${N_PREFIX-.*}$" "N_PREFIX=\${N_PREFIX-$N_PREFIX}" "$n_install_dir/bin/n" - - # Restore /usr/local/bin in PATH - PATH=$CLEAR_PATH - - # And replace the old node binary. - test -x /usr/bin/node_n && mv /usr/bin/node_n /usr/bin/node - test -x /usr/bin/npm_n && mv /usr/bin/npm_n /usr/bin/npm - - # Install the requested version of nodejs - n $nodejs_version - - # Find the last "real" version for this major version of node. - real_nodejs_version=$(find $node_version_path/$nodejs_version* -maxdepth 0 | sort --version-sort | tail --lines=1) - real_nodejs_version=$(basename $real_nodejs_version) - - # Create a symbolic link for this major version. If the file doesn't already exist - if [ ! -e "$node_version_path/$nodejs_version" ] - then - ln --symbolic --force --no-target-directory $node_version_path/$real_nodejs_version $node_version_path/$nodejs_version - fi - - # Store the ID of this app and the version of node requested for it - echo "$YNH_APP_ID:$nodejs_version" | tee --append "$n_install_dir/ynh_app_version" - - # Store nodejs_version into the config of this app - ynh_app_setting_set $app nodejs_version $nodejs_version - - # Build the update script and set the cronjob - ynh_cron_upgrade_node - - ynh_use_nodejs -} - -ynh_remove_nodejs () { - ynh_use_nodejs - - # Remove the line for this app - sed --in-place "/$YNH_APP_ID:$nodejs_version/d" "$n_install_dir/ynh_app_version" - - # If none another app uses this version of nodejs, remove it. - if ! grep --quiet "$nodejs_version" "$n_install_dir/ynh_app_version" - then - n rm $nodejs_version - fi - - # If none another app uses n, remove n - if [ ! -s "$n_install_dir/ynh_app_version" ] - then - ynh_secure_remove "$n_install_dir" - ynh_secure_remove "/usr/local/n" - sed --in-place "/N_PREFIX/d" /root/.bashrc - fi -} - -ynh_cron_upgrade_node () { - # Build the update script - cat > "$n_install_dir/node_update.sh" << EOF -#!/bin/bash - -version_path="$node_version_path" -n_install_dir="$n_install_dir" - -# Log the date -date - -# List all real installed version of node -all_real_version="\$(find \$version_path/* -maxdepth 0 -type d | sed "s@\$version_path/@@g")" - -# Keep only the major version number of each line -all_real_version=\$(echo "\$all_real_version" | sed 's/\..*\$//') - -# Remove double entries -all_real_version=\$(echo "\$all_real_version" | sort --unique) - -# Read each major version -while read version -do - echo "Update of the version \$version" - sudo \$n_install_dir/bin/n \$version - - # Find the last "real" version for this major version of node. - real_nodejs_version=\$(find \$version_path/\$version* -maxdepth 0 | sort --version-sort | tail --lines=1) - real_nodejs_version=\$(basename \$real_nodejs_version) - - # Update the symbolic link for this version - sudo ln --symbolic --force --no-target-directory \$version_path/\$real_nodejs_version \$version_path/\$version -done <<< "\$(echo "\$all_real_version")" -EOF - - chmod +x "$n_install_dir/node_update.sh" - - # Build the cronjob - cat > "/etc/cron.daily/node_update" << EOF -#!/bin/bash - -$n_install_dir/node_update.sh >> $n_install_dir/node_update.log -EOF - - chmod +x "/etc/cron.daily/node_update" + set -x } #================================================= @@ -214,36 +231,44 @@ EOF # # usage: ynh_check_starting "Line to match" [Log file] [Timeout] [Service name] # -# | arg: Line to match - The line to find in the log to attest the service have finished to boot. -# | arg: Log file - The log file to watch -# | arg: Service name -# /var/log/$app/$app.log will be used if no other log is defined. -# | arg: Timeout - The maximum time to wait before ending the watching. Defaut 300 seconds. -ynh_check_starting () { - local line_to_match="$1" - local service_name="${4:-$app}" - local app_log="${2:-/var/log/$service_name/$service_name.log}" - local timeout=${3:-300} +# | arg: -m, --line_to_match= - Line to match - The line to find in the log to attest the service have finished to boot. +# | arg: -l, --app_log= - Log file - The log file to watch; specify "systemd" to read systemd journal for specified service +# /var/log/$app/$app.log will be used if no other log is defined. +# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Defaut 300 seconds. +# | arg: -n, --service_name= - Service name - ynh_clean_check_starting () { - # Stop the execution of tail. - kill -s 15 $pid_tail 2>&1 - ynh_secure_remove "$templog" 2>&1 - } +ynh_check_starting () { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [m]=line_to_match= [l]=app_log= [t]=timeout= [n]=service_name= ) + local line_to_match + local app_log + local timeout + local service_name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + local app_log="${app_log:-/var/log/$service_name/$service_name.log}" + local timeout=${timeout:-300} + local service_name="${service_name:-$app}" echo "Starting of $service_name" >&2 systemctl stop $service_name local templog="$(mktemp)" # Following the starting of the app in its log - tail -F -n0 "$app_log" > "$templog" & - # Get the PID of the tail command + if [ "$app_log" == "systemd" ] ; then + # Read the systemd journal + journalctl -u $service_name -f --since=-45 > "$templog" & + else + # Read the specified log file + tail -F -n0 "$app_log" > "$templog" & + fi + # Get the PID of the last command local pid_tail=$! systemctl start $service_name local i=0 for i in `seq 1 $timeout` do - # Read the log until the sentence is found, that means the app finished to start. Or run until the timeout + # Read the log until the sentence is found, which means the app finished starting. Or run until the timeout. if grep --quiet "$line_to_match" "$templog" then echo "The service $service_name has correctly started." >&2 @@ -254,13 +279,23 @@ ynh_check_starting () { done if [ $i -eq $timeout ] then - echo "The service $service_name didn't fully started before the timeout." >&2 + echo "The service $service_name didn't fully start before the timeout." >&2 fi echo "" ynh_clean_check_starting } +# Clean temporary process and file used by ynh_check_starting +# (usually used in ynh_clean_setup scripts) +# +# usage: ynh_clean_check_starting +ynh_clean_check_starting () { + # Stop the execution of tail. + kill -s 15 $pid_tail 2>&1 + ynh_secure_remove "$templog" 2>&1 +} + #================================================= ynh_print_log () { @@ -369,9 +404,20 @@ ynh_print_ON () { # # usage: ynh_multimedia_build_main_dir ynh_multimedia_build_main_dir () { - wget -nv https://github.com/YunoHost-Apps/yunohost.multimedia/archive/master.zip 2>&1 - unzip -q master.zip - ./yunohost.multimedia-master/script/ynh_media_build.sh + local ynh_media_release="v1.0" + local checksum="4852c8607db820ad51f348da0dcf0c88" + + # Download yunohost.multimedia scripts + wget -nv https://github.com/YunoHost-Apps/yunohost.multimedia/archive/${ynh_media_release}.tar.gz + + # Check the control sum + echo "${checksum} ${ynh_media_release}.tar.gz" | md5sum -c --status \ + || ynh_die "Corrupt source" + + # Extract + mkdir yunohost.multimedia-master + tar -xf ${ynh_media_release}.tar.gz -C yunohost.multimedia-master --strip-components 1 + ./yunohost.multimedia-master/script/ynh_media_build.sh } # Add a directory in yunohost.multimedia @@ -379,11 +425,16 @@ ynh_multimedia_build_main_dir () { # # usage: ynh_multimedia_addfolder "Source directory" "Destination directory" # -# | arg: Source directory - The real directory which contains your medias. -# | arg: Destination directory - The name and the place of the symbolic link, relative to "/home/yunohost.multimedia" +# | arg: -s, --source_dir= - Source directory - The real directory which contains your medias. +# | arg: -d, --dest_dir= - Destination directory - The name and the place of the symbolic link, relative to "/home/yunohost.multimedia" ynh_multimedia_addfolder () { - local source_dir="$1" - local dest_dir="$2" + # Declare an array to define the options of this helper. + declare -Ar args_array=( [s]=source_dir= [d]=dest_dir= ) + local source_dir + local dest_dir + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + ./yunohost.multimedia-master/script/ynh_media_addfolder.sh --source="$source_dir" --dest="$dest_dir" } @@ -391,13 +442,18 @@ ynh_multimedia_addfolder () { # # usage: ynh_multimedia_movefolder "Source directory" "Destination directory" # -# | arg: Source directory - The real directory which contains your medias. +# | arg: -s, --source_dir= - Source directory - The real directory which contains your medias. # It will be moved to "Destination directory" # A symbolic link will replace it. -# | arg: Destination directory - The new name and place of the directory, relative to "/home/yunohost.multimedia" +# | arg: -d, --dest_dir= - Destination directory - The new name and place of the directory, relative to "/home/yunohost.multimedia" ynh_multimedia_movefolder () { - local source_dir="$1" - local dest_dir="$2" + # Declare an array to define the options of this helper. + declare -Ar args_array=( [s]=source_dir= [d]=dest_dir= ) + local source_dir + local dest_dir + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + ./yunohost.multimedia-master/script/ynh_media_addfolder.sh --inv --source="$source_dir" --dest="$dest_dir" } @@ -405,9 +461,14 @@ ynh_multimedia_movefolder () { # # usage: ynh_multimedia_addaccess user_name # -# | arg: user_name - The name of the user which gain this access. +# | arg: -u, --user_name= - The name of the user which gain this access. ynh_multimedia_addaccess () { - local user_name=$1 + # Declare an array to define the options of this helper. + declare -Ar args_array=( [u]=user_name=) + local user_name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + groupadd -f multimedia usermod -a -G multimedia $user_name } @@ -427,15 +488,15 @@ ynh_add_fail2ban_config () { failregex=$2 max_retry=${3:-3} ports=${4:-http,https} - + test -n "$logpath" || ynh_die "ynh_add_fail2ban_config expects a logfile path as first argument and received nothing." test -n "$failregex" || ynh_die "ynh_add_fail2ban_config expects a failure regex as second argument and received nothing." - + finalfail2banjailconf="/etc/fail2ban/jail.d/$app.conf" finalfail2banfilterconf="/etc/fail2ban/filter.d/$app.conf" ynh_backup_if_checksum_is_different "$finalfail2banjailconf" 1 ynh_backup_if_checksum_is_different "$finalfail2banfilterconf" 1 - + sudo tee $finalfail2banjailconf <~ynh +# For example : 4.3-2~ynh3 +# This include the number before ~ynh +# In the last example it return 4.3-2 # -# This helper should be used to avoid an upgrade of a package -# when it's not needed. +# usage: ynh_app_upstream_version +ynh_app_upstream_version () { + manifest_path="../manifest.json" + if [ ! -e "$manifest_path" ]; then + manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place + fi + version_key=$(ynh_read_manifest "$manifest_path" "version") + echo "${version_key/~ynh*/}" +} + + +# Read package version from the manifest +# The version number in the manifest is defined by ~ynh +# For example : 4.3-2~ynh3 +# This include the number after ~ynh +# In the last example it return 3 +# +# usage: ynh_app_package_version +ynh_app_package_version () { + manifest_path="../manifest.json" + if [ ! -e "$manifest_path" ]; then + manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place + fi + version_key=$(ynh_read_manifest "$manifest_path" "version") + echo "${version_key/*~ynh/}" +} + +# Checks the app version to upgrade with the existing app version and returns: +# - UPGRADE_APP if the upstream app version has changed +# - UPGRADE_PACKAGE if only the YunoHost package has changed +# +## It stops the current script without error if the package is up-to-date +# +# This helper should be used to avoid an upgrade of an app, or the upstream part +# of it, when it's not needed # # To force an upgrade, even if the package is up to date, # you have to set the variable YNH_FORCE_UPGRADE before. # example: sudo YNH_FORCE_UPGRADE=1 yunohost app upgrade MyApp -# -# usage: ynh_abort_if_up_to_date -ynh_abort_if_up_to_date () { - local force_upgrade=${YNH_FORCE_UPGRADE:-0} - local package_check=${PACKAGE_CHECK_EXEC:-0} - local version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0) - local last_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0) - if [ "$version" = "$last_version" ] - then - if [ "$force_upgrade" != "0" ] - then - echo "Upgrade forced by YNH_FORCE_UPGRADE." >&2 - unset YNH_FORCE_UPGRADE - elif [ "$package_check" != "0" ] - then - echo "Upgrade forced for package check." >&2 - else - ynh_die "Up-to-date, nothing to do" 0 - fi - fi +# usage: ynh_check_app_version_changed +ynh_check_app_version_changed () { + local force_upgrade=${YNH_FORCE_UPGRADE:-0} + local package_check=${PACKAGE_CHECK_EXEC:-0} + + # By default, upstream app version has changed + local return_value="UPGRADE_APP" + + local current_version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0) + local current_upstream_version="${current_version/~ynh*/}" + local update_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0) + local update_upstream_version="${update_version/~ynh*/}" + + if [ "$current_version" == "$update_version" ] ; then + # Complete versions are the same + if [ "$force_upgrade" != "0" ] + then + echo "Upgrade forced by YNH_FORCE_UPGRADE." >&2 + unset YNH_FORCE_UPGRADE + elif [ "$package_check" != "0" ] + then + echo "Upgrade forced for package check." >&2 + else + ynh_die "Up-to-date, nothing to do" 0 + fi + elif [ "$current_upstream_version" == "$update_upstream_version" ] ; then + # Upstream versions are the same, only YunoHost package versions differ + return_value="UPGRADE_PACKAGE" + fi + echo $return_value } #================================================= @@ -523,14 +631,20 @@ ynh_abort_if_up_to_date () { # Send an email to inform the administrator # # usage: ynh_send_readme_to_admin app_message [recipients] -# | arg: app_message - The message to send to the administrator. -# | arg: recipients - The recipients of this email. Use spaces to separate multiples recipients. - default: root +# | arg: -m --app_message= - The message to send to the administrator. +# | arg: -r, --recipients= - The recipients of this email. Use spaces to separate multiples recipients. - default: root # example: "root admin@domain" # If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you # example: "root admin@domain user1 user2" ynh_send_readme_to_admin() { - local app_message="${1:-...No specific information...}" - local recipients="${2:-root}" + # Declare an array to define the options of this helper. + declare -Ar args_array=( [m]=app_message= [r]=recipients= ) + local app_message + local recipients + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + local app_message="${app_message:-...No specific information...}" + local recipients="${recipients:-root}" # Retrieve the email of users find_mails () { @@ -569,8 +683,16 @@ Automatic diagnosis data from YunoHost $(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')" + # Define binary to use for mail command + if [ -e /usr/bin/bsd-mailx ] + then + local mail_bin=/usr/bin/bsd-mailx + else + local mail_bin=/usr/bin/mail.mailutils + fi + # Send the email to the recipients - echo "$mail_message" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients" + echo "$mail_message" | $mail_bin -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients" } #================================================= @@ -578,11 +700,16 @@ $(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')" # Reload (or other actions) a service and print a log in case of failure. # # usage: ynh_system_reload service_name [action] -# | arg: service_name - Name of the service to reload -# | arg: action - Action to perform with systemctl. Default: reload +# | arg: -n, --service_name= - Name of the service to reload +# | arg: -a, --action= - Action to perform with systemctl. Default: reload ynh_system_reload () { - local service_name=$1 - local action=${2:-reload} + # Declare an array to define the options of this helper. + declare -Ar args_array=( [n]=service_name= [a]=action= ) + local service_name + local action + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + local action=${action:-reload} # Reload, restart or start and print the log if the service fail to start or reload systemctl $action $service_name || ( journalctl --lines=20 -u $service_name >&2 && false) @@ -612,8 +739,6 @@ is_jessie () { fi } -#================================================= -#============= FUTURE YUNOHOST HELPER ============ #================================================= # Delete a file checksum from the app settings @@ -626,3 +751,82 @@ ynh_delete_file_checksum () { local checksum_setting_name=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_' ynh_app_setting_delete $app $checksum_setting_name } + +#================================================= + +ynh_maintenance_mode_ON () { + # Load value of $path_url and $domain from the config if their not set + if [ -z $path_url ]; then + path_url=$(ynh_app_setting_get $app path) + fi + if [ -z $domain ]; then + domain=$(ynh_app_setting_get $app domain) + fi + + # Create an html to serve as maintenance notice + echo " + + + +Your app $app is currently under maintenance! + + + +

Your app $app is currently under maintenance!

+

This app has been put under maintenance by your administrator at $(date)

+

Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.

+ + +" > "/var/www/html/maintenance.$app.html" + + # Create a new nginx config file to redirect all access to the app to the maintenance notice instead. + echo "# All request to the app will be redirected to ${path_url}_maintenance and fall on the maintenance notice +rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/? redirect; +# Use another location, to not be in conflict with the original config file +location ${path_url}_maintenance/ { +alias /var/www/html/ ; + +try_files maintenance.$app.html =503; + +# Include SSOWAT user panel. +include conf.d/yunohost_panel.conf.inc; +}" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf" + + # The current config file will redirect all requests to the root of the app. + # To keep the full path, we can use the following rewrite rule: + # rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/\$1? redirect; + # The difference will be in the $1 at the end, which keep the following queries. + # But, if it works perfectly for a html request, there's an issue with any php files. + # This files are treated as simple files, and will be downloaded by the browser. + # Would be really be nice to be able to fix that issue. So that, when the page is reloaded after the maintenance, the user will be redirected to the real page he was. + + systemctl reload nginx +} + +ynh_maintenance_mode_OFF () { + # Load value of $path_url and $domain from the config if their not set + if [ -z $path_url ]; then + path_url=$(ynh_app_setting_get $app path) + fi + if [ -z $domain ]; then + domain=$(ynh_app_setting_get $app domain) + fi + + # Rewrite the nginx config file to redirect from ${path_url}_maintenance to the real url of the app. + echo "rewrite ^${path_url}_maintenance/(.*)$ ${path_url}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf" + systemctl reload nginx + + # Sleep 4 seconds to let the browser reload the pages and redirect the user to the app. + sleep 4 + + # Then remove the temporary files used for the maintenance. + rm "/var/www/html/maintenance.$app.html" + rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf" + + systemctl reload nginx +} diff --git a/scripts/backup b/scripts/backup index dc38b76..87c7cec 100755 --- a/scripts/backup +++ b/scripts/backup @@ -6,12 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= -if [ ! -e _common.sh ]; then - # Get the _common.sh file if it's not in the current directory - cp ../settings/scripts/_common.sh ./_common.sh - chmod a+rx _common.sh -fi -source _common.sh +source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers #================================================= diff --git a/scripts/install b/scripts/install index 00b8d7d..d838f7e 100755 --- a/scripts/install +++ b/scripts/install @@ -194,5 +194,5 @@ Please read the documentation (https://github.com/maniackcrudelis/archivist/blob If you facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/archivist_ynh" -ynh_send_readme_to_admin "$message" "root" +ynh_send_readme_to_admin --app_message="$message" --recipients="root" ynh_print_ON diff --git a/scripts/restore b/scripts/restore index 103d56c..404cdee 100755 --- a/scripts/restore +++ b/scripts/restore @@ -6,12 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= -if [ ! -e _common.sh ]; then - # Get the _common.sh file if it's not in the current directory - cp ../settings/scripts/_common.sh ./_common.sh - chmod a+rx _common.sh -fi -source _common.sh +source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers #================================================= @@ -86,4 +81,4 @@ Please read the documentation (https://github.com/maniackcrudelis/archivist/blob If you facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/archivist_ynh" -ynh_send_readme_to_admin "$message" "root" +ynh_send_readme_to_admin --app_message="$message" --recipients="root" diff --git a/scripts/upgrade b/scripts/upgrade index 04793aa..1bcf178 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -29,7 +29,20 @@ frequency="$(ynh_app_setting_get $app frequency)" # CHECK VERSION #================================================= -ynh_abort_if_up_to_date +upgrade_type=$(ynh_check_app_version_changed) + +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= + +# Backup the current version of the app +ynh_backup_before_upgrade +ynh_clean_setup () { + # restore it if the upgrade fails + ynh_restore_upgradebackup +} +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors #================================================= # STANDARD UPGRADE STEPS @@ -37,8 +50,11 @@ ynh_abort_if_up_to_date # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= -# Download, check integrity, uncompress and patch the source from app.src -ynh_setup_source "$final_path" +if [ "$upgrade_type" == "UPGRADE_APP" ] +then + # Download, check integrity, uncompress and patch the source from app.src + ynh_setup_source "$final_path" +fi #================================================= # SPECIFIC UPGRADE