From 6eda4149953219e711aaf6d5874b621603f12d65 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Thu, 18 May 2017 19:14:23 +0200 Subject: [PATCH] Fix install et upgrade & last helpers --- check_process | 2 +- conf/app.src | 1 + scripts/_common.sh | 303 +++++++++++++++++++++++++++++++-------------- scripts/install | 4 +- scripts/upgrade | 7 +- 5 files changed, 223 insertions(+), 94 deletions(-) diff --git a/check_process b/check_process index 660e369..138af08 100644 --- a/check_process +++ b/check_process @@ -18,7 +18,7 @@ backup_restore=1 multi_instance=0 incorrect_path=0 - port_already_use=0 + port_already_use=1 (9001) ;; Test abiword auto_remove=1 ; Manifest diff --git a/conf/app.src b/conf/app.src index 2098747..f59ae38 100644 --- a/conf/app.src +++ b/conf/app.src @@ -1,3 +1,4 @@ SOURCE_URL=https://github.com/ether/etherpad-lite/archive/1.6.0.tar.gz SOURCE_SUM=9ff1ef760e8285a1cd117d9bb576398b ARCH_FORMAT=tar.gz +SOURCE_SUM_PRG=md5sum diff --git a/scripts/_common.sh b/scripts/_common.sh index d0565c8..4ba1440 100755 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -6,79 +6,7 @@ #================================================= #================================================= -# Remove a file or a directory securely -# -# usage: ynh_secure_remove path_to_remove -# | arg: path_to_remove - File or directory to remove -ynh_secure_remove () { - path_to_remove=$1 - forbidden_path=" \ - /var/www \ - /home/yunohost.app" - - if [[ "$forbidden_path" =~ "$path_to_remove" \ - # Match all path or subpath in $forbidden_path - || "$path_to_remove" =~ ^/[[:alnum:]]+$ \ - # Match all first level path from / (Like /var, /root, etc...) - || "${path_to_remove:${#path_to_remove}-1}" = "/" ]] - # Match if the path finish by /. Because it's seems there is an empty variable - then - echo "Avoid deleting of $path_to_remove." >&2 - else - if [ -e "$path_to_remove" ] - then - sudo rm -R "$path_to_remove" - else - echo "$path_to_remove doesn't deleted because it's not exist." >&2 - fi - fi -} - -ynh_setup_source () { - src_url=$(cat ../conf/app.src | grep SOURCE_URL | cut -d= -f2-) - src_checksum=$(cat ../conf/app.src | grep SOURCE_SUM | cut -d= -f2-) - arch_format=$(cat ../conf/app.src | grep ARCH_FORMAT | cut -d= -f2-) - local_source="/opt/yunohost-apps-src/$YNH_APP_ID/source.$arch_format" - - if test -e "$local_source" - then # Use the local source file if it is present - cp $local_source source.$arch_format - else # If not, download the source - wget -nv -O source.$arch_format $src_url - fi - - # Check the control sum - echo "$src_checksum source.$arch_format" \ - | md5sum -c --status || ynh_die "Corrupt source" - - # Extract source into the app dir - sudo mkdir -p "$final_path" - if [ $(echo "$arch_format" | tr '[:upper:]' '[:lower:]') = "zip" ] - then # Zip format - # Using of a temp directory, because unzip doesn't manage --strip-components - temp_dir=$(mktemp -d) - unzip -quo source.zip -d "$temp_dir" - sudo cp -a $temp_dir/*/. "$final_path" - ynh_secure_remove "$temp_dir" - elif [ $(echo "$arch_format" | tr '[:upper:]' '[:lower:]') = "tar.gz" ]; then - sudo tar -x -f source.tar.gz -C "$final_path" --strip-components 1 - else - ynh_die "Format d'archive non reconnu." - fi - - # Apply patches - if test -f ../sources/patches/*.patch; then - (cd "$DEST" \ - && for p in ${PKG_DIR}/patches/*.patch; do \ - sudo patch -p1 < $p; done) \ - || ynh_die "Unable to apply patches" - fi - - # Add supplementary files - if test -e "../sources/extra_files"; then - sudo cp -a ../sources/extra_files/. "$final_path" - fi -} +YNH_EXECUTION_DIR="." ynh_backup_abstract () { # A intégrer à ynh_backup directement. @@ -437,33 +365,56 @@ ynh_normalize_url_path () { echo $path_url } +# Check if a mysql user exists +# +# usage: ynh_mysql_user_exists user +# | arg: user - the user for which to check existence +function ynh_mysql_user_exists() +{ + local user=$1 + if [[ -z $(ynh_mysql_execute_as_root "SELECT User from mysql.user WHERE User = '$user';") ]] + then + return 1 + else + return 0 + fi +} + # Create a database, an user and its password. Then store the password in the app's config # -# User of database will be store in db_user's variable. -# Name of database will be store in db_name's variable. -# And password in db_pwd's variable. +# After executing this helper, the password of the created database will be available in $db_pwd +# It will also be stored as "mysqlpwd" into the app settings. # -# usage: ynh_mysql_generate_db user name +# usage: ynh_mysql_setup_db user name # | arg: user - Owner of the database # | arg: name - Name of the database -ynh_mysql_generate_db () { +ynh_mysql_setup_db () { + local db_user="$1" + local db_name="$2" db_pwd=$(ynh_string_random) # Generate a random password - ynh_mysql_create_db "$2" "$1" "$db_pwd" # Create the database + ynh_mysql_create_db "$db_name" "$db_user" "$db_pwd" # Create the database ynh_app_setting_set $app mysqlpwd $db_pwd # Store the password in the app's config } -# Remove a database if it exist and the associated user +# Remove a database if it exists, and the associated user # # usage: ynh_mysql_remove_db user name -# | arg: user - Proprietary of the database +# | arg: user - Owner of the database # | arg: name - Name of the database ynh_mysql_remove_db () { - if mysqlshow -u root -p$(sudo cat $MYSQL_ROOT_PWD_FILE) | grep -q "^| $2"; then # Check if the database exist - echo "Remove database $2" >&2 - ynh_mysql_drop_db $2 # Remove the database - ynh_mysql_drop_user $1 # Remove the associated user to database + local db_user="$1" + local db_name="$2" + local mysql_root_password=$(sudo cat $MYSQL_ROOT_PWD_FILE) + if mysqlshow -u root -p$mysql_root_password | grep -q "^| $db_name"; then # Check if the database exists + echo "Removing database $db_name" >&2 + ynh_mysql_drop_db $db_name # Remove the database else - echo "Database $2 not found" >&2 + echo "Database $db_name not found" >&2 + fi + + # Remove mysql user if it exists + if $(ynh_mysql_user_exists $db_user); then + ynh_mysql_drop_user $db_user fi } @@ -536,7 +487,7 @@ ynh_install_app_dependencies () { if [ ! -e "$manifest_path" ]; then manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place fi - version=$(sudo python3 -c "import sys, json;print(json.load(open(\"$manifest_path\"))['version'])") # Retrieve the version number in the manifest file. + version=$(grep '\"version\": ' "$manifest_path" | cut -d '"' -f 4) # Retrieve the version number in the manifest file. dep_app=${app//_/-} # Replace all '_' by '-' if ynh_package_is_installed "${dep_app}-ynh-deps"; then @@ -690,11 +641,14 @@ ynh_local_curl () { do POST_data="${POST_data}${arg}&" done - # (Remove the last character, which is an unecessary '&') - POST_data=${POST_data::-1} + if [ -n "$POST_data" ] + then + # Add --data arg and remove the last character, which is an unecessary '&' + POST_data="--data \"${POST_data::-1}\"" + fi # Curl the URL - curl -kL -H "Host: $domain" --resolve $domain:443:127.0.0.1 --data "$POST_data" "$full_page_url" 2>&1 + curl --silent --show-error -kL -H "Host: $domain" --resolve $domain:443:127.0.0.1 $POST_data "$full_page_url" } # Substitute/replace a string by another in a file @@ -711,3 +665,172 @@ ynh_replace_string () { sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$workfile" } + +# Remove a file or a directory securely +# +# usage: ynh_secure_remove path_to_remove +# | arg: path_to_remove - File or directory to remove +ynh_secure_remove () { + path_to_remove=$1 + forbidden_path=" \ + /var/www \ + /home/yunohost.app" + + if [[ "$forbidden_path" =~ "$path_to_remove" \ + # Match all paths or subpaths in $forbidden_path + || "$path_to_remove" =~ ^/[[:alnum:]]+$ \ + # Match all first level paths from / (Like /var, /root, etc...) + || "${path_to_remove:${#path_to_remove}-1}" = "/" ]] + # Match if the path finishes by /. Because it seems there is an empty variable + then + echo "Avoid deleting $path_to_remove." >&2 + else + if [ -e "$path_to_remove" ] + then + sudo rm -R "$path_to_remove" + else + echo "$path_to_remove wasn't deleted because it doesn't exist." >&2 + fi + fi +} + +# Download, check integrity, uncompress and patch the source from app.src +# +# The file conf/app.src need to contains: +# +# SOURCE_URL=Address to download the app archive +# SOURCE_SUM=Control sum +# # (Optional) Programm to check the integrity (sha256sum, md5sum$YNH_EXECUTION_DIR/...) +# # default: sha256 +# SOURCE_SUM_PRG=sha256 +# # (Optional) Archive format +# # default: tar.gz +# SOURCE_FORMAT=tar.gz +# # (Optional) Put false if source are directly in the archive root +# # default: true +# SOURCE_IN_SUBDIR=false +# # (Optionnal) Name of the local archive (offline setup support) +# # default: ${src_id}.${src_format} +# SOURCE_FILENAME=example.tar.gz +# +# Details: +# This helper download sources from SOURCE_URL if there is no local source +# archive in /opt/yunohost-apps-src/APP_ID/SOURCE_FILENAME +# +# Next, it check the integrity with "SOURCE_SUM_PRG -c --status" command. +# +# If it's ok, the source archive will be uncompress in $dest_dir. If the +# SOURCE_IN_SUBDIR is true, the first level directory of the archive will be +# removed. +# +# Finally, patches named sources/patches/${src_id}-*.patch and extra files in +# sources/extra_files/$src_id will be applyed to dest_dir +# +# +# usage: ynh_setup_source dest_dir [source_id] +# | arg: dest_dir - Directory where to setup sources +# | arg: source_id - Name of the app, if the package contains more than one app +ynh_setup_source () { + local dest_dir=$1 + local src_id=${2:-app} # If the argument is not given, source_id equal "app" + + # Load value from configuration file (see above for a small doc about this file + # format) + local src_url=$(grep 'SOURCE_URL=' "$YNH_EXECUTION_DIR/../conf/${src_id}.src" | cut -d= -f2-) + local src_sum=$(grep 'SOURCE_SUM=' "$YNH_EXECUTION_DIR/../conf/${src_id}.src" | cut -d= -f2-) + local src_sumprg=$(grep 'SOURCE_SUM_PRG=' "$YNH_EXECUTION_DIR/../conf/${src_id}.src" | cut -d= -f2-) + local src_format=$(grep 'SOURCE_FORMAT=' "$YNH_EXECUTION_DIR/../conf/${src_id}.src" | cut -d= -f2-) + local src_in_subdir=$(grep 'SOURCE_IN_SUBDIR=' "$YNH_EXECUTION_DIR/../conf/${src_id}.src" | cut -d= -f2-) + local src_filename=$(grep 'SOURCE_FILENAME=' "$YNH_EXECUTION_DIR/../conf/${src_id}.src" | cut -d= -f2-) + + # Default value + src_sumprg=${src_sumprg:-sha256sum} + src_in_subdir=${src_in_subdir:-true} + src_format=${src_format:-tar.gz} + src_format=$(echo "$src_format" | tr '[:upper:]' '[:lower:]') + if [ "$src_filename" = "" ] ; then + src_filename="${src_id}.${src_format}" + fi + local local_src="/opt/yunohost-apps-src/${YNH_APP_ID}/${src_filename}" + + if test -e "$local_src" + then # Use the local source file if it is present + sudo cp $local_src $src_filename + else # If not, download the source + wget -nv -O $src_filename $src_url + fi + + # Check the control sum + echo "${src_sum} ${src_filename}" | ${src_sumprg} -c --status \ + || ynh_die "Corrupt source" + + # Extract source into the app dir + sudo mkdir -p "$dest_dir" + if [ "$src_format" = "zip" ] + then + # Zip format + # Using of a temp directory, because unzip doesn't manage --strip-components + if $src_in_subdir ; then + local tmp_dir=$(mktemp -d) + unzip -quo $src_filename -d "$tmp_dir" + sudo cp -a $tmp_dir/*/. "$dest_dir" + ynh_secure_remove "$tmp_dir" + else + sudo unzip -quo $src_filename -d "$dest_dir" + fi + else + local strip="" + if $src_in_subdir ; then + strip="--strip-components 1" + fi + if [[ "$src_format" =~ ^tar.gz|tar.bz2|tar.xz$ ]] ; then + sudo tar -xf $src_filename -C "$dest_dir" $strip + else + ynh_die "Archive format unrecognized." + fi + fi + + # Apply patches + if (( $(find $YNH_EXECUTION_DIR/../sources/patches/ -type f -name "${src_id}-*.patch" 2> /dev/null | wc -l) > "0" )); then + local old_dir=$(pwd) + (cd "$dest_dir" \ + && for p in $YNH_EXECUTION_DIR/../sources/patches/${src_id}-*.patch; do \ + patch -p1 < $p; done) \ + || ynh_die "Unable to apply patches" + cd $old_dir + fi + + # Add supplementary files + if test -e "$YNH_EXECUTION_DIR/../sources/extra_files/${src_id}"; then + cp -a $YNH_EXECUTION_DIR/../sources/extra_files/$src_id/. "$dest_dir" + fi + +} + +# Check availability of a web path +# +# example: ynh_webpath_available some.domain.tld /coffee +# +# usage: ynh_webpath_available domain path +# | arg: domain - the domain/host of the url +# | arg: path - the web path to check the availability of +ynh_webpath_available () { + local domain=$1 + local path=$2 + sudo yunohost domain url-available $domain $path +} + +# Register/book a web path for an app +# +# example: ynh_webpath_register wordpress some.domain.tld /coffee +# +# usage: ynh_webpath_register app domain path +# | arg: app - the app for which the domain should be registered +# | arg: domain - the domain/host of the web path +# | arg: path - the web path to be registered +ynh_webpath_register () { + local app=$1 + local domain=$2 + local path=$3 + sudo yunohost app register-url $app $domain $path +} diff --git a/scripts/install b/scripts/install index 529ca3d..7613908 100644 --- a/scripts/install +++ b/scripts/install @@ -99,14 +99,14 @@ ynh_install_nodejs 0.10 db_name=$(ynh_make_valid_dbid $app) ynh_app_setting_set $app db_name $db_name -ynh_mysql_generate_db $db_name $db_name +ynh_mysql_setup_db $db_name $db_name #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= ynh_app_setting_set $app final_path $final_path -ynh_setup_source # Télécharge la source, décompresse et copie dans $final_path +ynh_setup_source $final_path # Télécharge la source, décompresse et copie dans $final_path #================================================= # NGINX CONFIGURATION diff --git a/scripts/upgrade b/scripts/upgrade index f1f6d19..cb8cedb 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -41,6 +41,11 @@ if [ -z $db_name ]; then # Si db_name n'est pas renseigné dans app setting ynh_app_setting_set $app db_name $db_name fi +if [ -z $abiword ]; then # Si abiword n'est pas renseigné dans app setting + abiword=0 + ynh_app_setting_set $app abiword $abiword +fi + #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= @@ -57,7 +62,7 @@ ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est dét # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= -ynh_setup_source # Télécharge la source, décompresse et copie dans $final_path +ynh_setup_source $final_path # Télécharge la source, décompresse et copie dans $final_path #================================================= # NGINX CONFIGURATION