diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index bdb0cab..0000000 --- a/.gitattributes +++ /dev/null @@ -1,17 +0,0 @@ -# Auto detect text files and perform LF normalization -* text=auto - -# Custom for Visual Studio -*.cs diff=csharp - -# Standard to msysgit -*.doc diff=astextplain -*.DOC diff=astextplain -*.docx diff=astextplain -*.DOCX diff=astextplain -*.dot diff=astextplain -*.DOT diff=astextplain -*.pdf diff=astextplain -*.PDF diff=astextplain -*.rtf diff=astextplain -*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 96374c4..0000000 --- a/.gitignore +++ /dev/null @@ -1,43 +0,0 @@ -# Windows image file caches -Thumbs.db -ehthumbs.db - -# Folder config file -Desktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Windows Installer files -*.cab -*.msi -*.msm -*.msp - -# Windows shortcuts -*.lnk - -# ========================= -# Operating System Files -# ========================= - -# OSX -# ========================= - -.DS_Store -.AppleDouble -.LSOverride - -# Thumbnails -._* - -# Files that might appear on external disk -.Spotlight-V100 -.Trashes - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items -.apdisk diff --git a/conf/nginx.conf b/conf/nginx.conf index e3a589f..d3513a7 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,23 +1,32 @@ +#sub_path_only rewrite ^__PATH__$ __PATH__/ permanent; location __PATH__/ { - alias __FINALPATH__/; + # Path to source + alias __FINALPATH__/ ; + + # Force usage of https + if ($scheme = http) { + rewrite ^ https://$server_name$request_uri? permanent; + } + + index index.php; + + # Common parameter to increase upload size limit in conjunction with dedicated php-fpm file + #client_max_body_size 50M; - index index.html index.php ; try_files $uri $uri/ index.php; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; - fastcgi_pass unix:/var/run/php7-fpm-__NAME__.sock; + fastcgi_pass unix:/var/run/php/php7.0-fpm-__NAME__.sock; + fastcgi_index index.php; include fastcgi_params; - fastcgi_param REMOTE_USER $remote_user; - fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param REMOTE_USER $remote_user; + fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $request_filename; } - # Do not include SSOWAT user panel. - # include conf.d/yunohost_panel.conf.inc; - - location ~ /(conf|history)(/|$) { - deny all; - } + # Include SSOWAT user panel. + include conf.d/yunohost_panel.conf.inc; } + diff --git a/manifest.json b/manifest.json index 79979ff..00bf72f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,23 +1,24 @@ { "name": "Minchat", "id": "minchat", + "packaging_format": 1, "description": { "en": "A minimalist web chat", "fr": "Un web chat minimaliste" }, - "packaging_format": 1, - "requirements": { - "yunohost": ">> 2.3.15" - }, + "version": "1.0~ynh1", + "url": "https://github.com/wojtek77/chat", "license": "MIT", "maintainer": { "name": "chtixof" }, - "url": "https://github.com/wojtek77/chat", + "requirements": { + "yunohost": ">> 3.8.1" + }, "multi_instance": true, "services": [ "nginx", - "php5-fpm" + "php7.0-fpm" ], "license": "MIT", "arguments": { @@ -26,8 +27,8 @@ "name": "domain", "type": "domain", "ask": { - "en": "Choose a domain for minchat", - "fr": "Choisissez un domaine pour minchat" + "en": "Choose a domain for Minchat", + "fr": "Choisissez un domaine pour Minchat" }, "example": "domain.org" }, @@ -35,8 +36,8 @@ "name": "path", "type": "path", "ask": { - "en": "Choose a path for minchat", - "fr": "Choisissez un chemin pour minchat" + "en": "Choose a path for Minchat", + "fr": "Choisissez un chemin pour Minchat" }, "example": "/minchat", "default": "/minchat" @@ -45,10 +46,10 @@ "name": "is_public", "type": "boolean", "ask": { - "en": "Is it a public minchat site ?", + "en": "Is it a public site?", "fr": "Est-ce un site public ?" }, - "default": "true" + "default": true } ] } diff --git a/scripts/_common.sh b/scripts/_common.sh index 35f580d..b0cbf30 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -5,9 +5,9 @@ #================================================= # dependencies used by the app -pkg_dependencies="deb1 deb2 php$YNH_DEFAULT_PHP_VERSION-deb1 php$YNH_DEFAULT_PHP_VERSION-deb2" +#pkg_dependencies="" -YNH_PHP_VERSION="7.3" +#YNH_PHP_VERSION="7.3" #================================================= # PERSONAL HELPERS @@ -20,321 +20,3 @@ YNH_PHP_VERSION="7.3" #================================================= # FUTURE OFFICIAL HELPERS #================================================= - - - - - -QUIET () { # Redirige la sortie standard dans /dev/null - $@ > /dev/null -} - -ALL_QUIET () { # Redirige la sortie standard et d'erreur dans /dev/null - $@ > /dev/null 2>&1 -} - -CHECK_VAR () { # Check variable is not empty -# $1 = Checking variable -# $2 = Text to display on error - test -n "$1" || (echo "$2" >&2 && false) -} - -CHECK_PATH () { # Checks / at the beginning of the path. And his absence at the end. - if [ "${path:0:1}" != "/" ]; then # If the first character is not / - path="/$path" # Add / at the beginning of path - fi - if [ "${path:${#path}-1}" == "/" ] && [ ${#path} -gt 1 ]; then # If the last character is a / and it is not the only character. - path="${path:0:${#path}-1}" # Delete last character - fi -} - -CHECK_DOMAINPATH () { # Checks the availability of the path and domain. - sudo yunohost app checkurl $domain$path -a $app -} - -CHECK_FINALPATH () { # Checks that the destination folder is not already in use. - final_path=/var/www/$app - if [ -e "$final_path" ] - then - echo "This path already contains a folder" >&2 - false - fi -} - -# Substitute a string by another in a file -# -# usage: ynh_substitute_char string_to_find replace_string file_to_analyse -# | arg: string_to_find - String to replace in the file -# | arg: replace_string - New string that will replace -# | arg: file_to_analyse - File where the string will be replaced. -ynh_substitute_char () { - delimit=@ - match_char=${1//${delimit}/"\\${delimit}"} # Escape the delimiter if it's in the string. - replace_char=${2//${delimit}/"\\${delimit}"} - workfile=$3 - - sudo sed --in-place "s${delimit}${match_char}${delimit}${replace_char}${delimit}g" "$workfile" -} - -ynh_store_checksum_config () { - config_file_checksum=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_' - ynh_app_setting_set $app $config_file_checksum $(sudo md5sum "$1" | cut -d' ' -f1) -} - -extract_source() { - local DESTDIR=$1 - - # retrieve and extract Minichat tarball - rc_tarball="${DESTDIR}/master.tar.gz" - sudo wget -q -O "$rc_tarball" "$MINCHAT_SOURCE_URL" \ - || ynh_die "Unable to download source tarball" - echo "$MINCHAT_SOURCE_SHA256 $rc_tarball" | sha256sum -c >/dev/null \ - || ynh_die "Invalid checksum of downloaded tarball" - sudo tar xf "$rc_tarball" -C "$DESTDIR" --strip-components 1 \ - || ynh_die "Unable to extract source tarball" - sudo rm "$rc_tarball" -} - -ynh_nginx_config () { - finalnginxconf="/etc/nginx/conf.d/$domain.d/$app.conf" - ynh_compare_checksum_config "$finalnginxconf" 1 - sudo cp ../conf/nginx.conf "$finalnginxconf" - - # To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable. - # Substitute in a nginx config file only if the variable is not empty - if test -n "${path:-}"; then - ynh_substitute_char "__PATH__" "$path" "$finalnginxconf" - fi - if test -n "${domain:-}"; then - ynh_substitute_char "__DOMAIN__" "$domain" "$finalnginxconf" - fi - if test -n "${port:-}"; then - ynh_substitute_char "__PORT__" "$port" "$finalnginxconf" - fi - if test -n "${app:-}"; then - ynh_substitute_char "__NAME__" "$app" "$finalnginxconf" - fi - if test -n "${final_path:-}"; then - ynh_substitute_char "__FINALPATH__" "$final_path" "$finalnginxconf" - fi - ynh_store_checksum_config "$finalnginxconf" - - sudo systemctl reload nginx -} - -ynh_store_checksum_config () { - config_file_checksum=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_' - ynh_app_setting_set $app $config_file_checksum $(sudo md5sum "$1" | cut -d' ' -f1) -} - -ynh_remove_nginx_config () { - ynh_secure_remove "/etc/nginx/conf.d/$domain.d/$app.conf" - sudo systemctl reload nginx -} - -ynh_fpm_config () { - finalphpconf="/etc/php5/fpm/pool.d/$app.conf" - ynh_compare_checksum_config "$finalphpconf" 1 - sudo cp ../conf/php-fpm.conf "$finalphpconf" - ynh_substitute_char "__NAMETOCHANGE__" "$app" "$finalphpconf" - ynh_substitute_char "__FINALPATH__" "$final_path" "$finalphpconf" - ynh_substitute_char "__USER__" "$app" "$finalphpconf" - sudo chown root: "$finalphpconf" - ynh_store_checksum_config "$finalphpconf" - - if [ -e "../conf/php-fpm.ini" ] - then - finalphpini="/etc/php5/fpm/conf.d/20-$app.ini" - ynh_compare_checksum_config "$finalphpini" 1 - sudo cp ../conf/php-fpm.ini "$finalphpini" - sudo chown root: "$finalphpini" - ynh_store_checksum_config "$finalphpini" - fi - - sudo systemctl reload php5-fpm -} - -ynh_remove_fpm_config () { - ynh_secure_remove "/etc/php5/fpm/pool.d/$app.conf" - ynh_secure_remove "/etc/php5/fpm/conf.d/20-$app.ini" - sudo systemctl reload php5-fpm -} - -# 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 -} - -# Create a system user -# -# usage: ynh_system_user_create user_name [home_dir] -# | arg: user_name - Name of the system user that will be create -# | arg: home_dir - Path of the home dir for the user. Usually the final path of the app. If this argument is omitted, the user will be created without home -ynh_system_user_create () { - if ! ynh_system_user_exists "$1" # Check if the user exists on the system - then # If the user doesn't exist - if [ $# -ge 2 ]; then # If a home dir is mentioned - user_home_dir="-d $2" - else - user_home_dir="--no-create-home" - fi - sudo useradd $user_home_dir --system --user-group $1 --shell /usr/sbin/nologin || ynh_die "Unable to create $1 system account" - fi -} - -# Delete a system user -# -# usage: ynh_system_user_delete user_name -# | arg: user_name - Name of the system user that will be create -ynh_system_user_delete () { - if ynh_system_user_exists "$1" # Check if the user exists on the system - then - echo "Remove the user $1" >&2 - sudo userdel $1 - else - echo "The user $1 was not found" >&2 - fi -} - -ynh_compare_checksum_config () { - current_config_file=$1 - compress_backup=${2:-0} # If $2 is empty, compress_backup will set at 0 - config_file_checksum=checksum_${current_config_file//[\/ ]/_} # Replace all '/' and ' ' by '_' - checksum_value=$(ynh_app_setting_get $app $config_file_checksum) - if [ -n "$checksum_value" ] - then # Proceed only if a value was stocked into the app config - if ! echo "$checksum_value $current_config_file" | md5sum -c --status - then # If the checksum is now different - backup_config_file="$current_config_file.backup.$(date '+%d.%m.%y_%Hh%M,%Ss')" - if [ compress_backup -eq 1 ] - then - sudo tar --create --gzip --file "$backup_config_file.tar.gz" "$current_config_file" # Backup the current config file and compress - backup_config_file="$backup_config_file.tar.gz" - else - sudo cp -a "$current_config_file" "$backup_config_file" # Backup the current config file - fi - echo "Config file $current_config_file has been manually modified since the installation or last upgrade. So it has been duplicated in $backup_config_file" >&2 - echo "$backup_config_file" # Return the name of the backup file - fi - fi -} - -ynh_backup_fail_upgrade () { - WARNING echo "Upgrade failed." - app_bck=${app//_/-} # Replace all '_' by '-' - if sudo yunohost backup list | grep -q $app_bck-pre-upgrade$backup_number; then # Vérifie l'existence de l'archive avant de supprimer l'application et de restaurer - sudo yunohost app remove $app # Supprime l'application avant de la restaurer. - sudo yunohost backup restore --ignore-hooks $app_bck-pre-upgrade$backup_number --apps $app --force # Restore the backup if upgrade failed - ynh_die "The app was restored to the way it was before the failed upgrade." - fi -} - -ynh_backup_before_upgrade () { # Backup the current version of the app, restore it if the upgrade fails - backup_number=1 - old_backup_number=2 - app_bck=${app//_/-} # Replace all '_' by '-' - if sudo yunohost backup list | grep -q $app_bck-pre-upgrade1; then # Vérifie l'existence d'une archive déjà numéroté à 1. - backup_number=2 # Et passe le numéro de l'archive à 2 - old_backup_number=1 - fi - - sudo yunohost backup create --ignore-hooks --apps $app --name $app_bck-pre-upgrade$backup_number # Créer un backup différent de celui existant. - if [ "$?" -eq 0 ]; then # Si le backup est un succès, supprime l'archive précédente. - if sudo yunohost backup list | grep -q $app_bck-pre-upgrade$old_backup_number; then # Vérifie l'existence de l'ancienne archive avant de la supprimer, pour éviter une erreur. - QUIET sudo yunohost backup delete $app_bck-pre-upgrade$old_backup_number - fi - else # Si le backup a échoué - ynh_die "Backup failed, the upgrade process was aborted." - fi -} - -# Manage a fail of the script -# -# Print a warning to inform that the script was failed -# Execute the ynh_clean_setup function if used in the app script -# -# usage of ynh_clean_setup function -# This function provide a way to clean some residual of installation that not managed by remove script. -# To use it, simply add in your script: -# ynh_clean_setup () { -# instructions... -# } -# This function is optionnal. -# -# Usage: ynh_exit_properly is used only by the helper ynh_check_error. -# You must not use it directly. -ynh_exit_properly () { - exit_code=$? - if [ "$exit_code" -eq 0 ]; then - ynh_die # Exit without error if the script ended correctly - fi - - trap '' EXIT # Ignore new exit signals - set +eu # Do not exit anymore if a command fail or if a variable is empty - - echo -e "!!\n $app's script has encountered an error. Its execution was cancelled.\n!!" >&2 - - if type -t ynh_clean_setup > /dev/null; then # Check if the function exist in the app script. - ynh_clean_setup # Call the function to do specific cleaning for the app. - fi - - ynh_die # Exit with error status -} - -# Exit if an error occurs during the execution of the script. -# -# Stop immediatly the execution if an error occured or if a empty variable is used. -# The execution of the script is derivate to ynh_exit_properly function before exit. -# -# Usage: ynh_abort_if_errors -ynh_abort_if_errors () { - set -eu # Exit if a command fail, and if a variable is used unset. - trap ynh_exit_properly EXIT # Capturing exit signals on shell script -} - -# Normalize the url path syntax -# Handle the slash at the beginning of path and its absence at ending -# Return a normalized url path -# -# example: url_path=$(ynh_normalize_url_path $url_path) -# ynh_normalize_url_path example -> /example -# ynh_normalize_url_path /example -> /example -# ynh_normalize_url_path /example/ -> /example -# ynh_normalize_url_path / -> / -# -# usage: ynh_normalize_url_path path_to_normalize -# | arg: url_path_to_normalize - URL path to normalize before using it -ynh_normalize_url_path () { - path_url=$1 - test -n "$path_url" || ynh_die "ynh_normalize_url_path expect a URL path as first argument and received nothing." - if [ "${path_url:0:1}" != "/" ]; then # If the first character is not a / - path_url="/$path_url" # Add / at begin of path variable - fi - if [ "${path_url:${#path_url}-1}" == "/" ] && [ ${#path_url} -gt 1 ]; then # If the last character is a / and that not the only character. - path_url="${path_url:0:${#path_url}-1}" # Delete the last character - fi - echo $path_url -} \ No newline at end of file diff --git a/scripts/upgrade b/scripts/upgrade index 1f35f5e..0e484d0 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,64 +1,117 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -if [ ! -e _common ]; then - # Get file fonction if not been to the current directory - sudo cp ../settings/scripts/_common ./_common - sudo chmod a+rx _common -fi -# Source app helpers -source ./_common +source _common.sh source /usr/share/yunohost/helpers +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --weight=1 + app=$YNH_APP_INSTANCE_NAME -domain=$(ynh_app_setting_get "$app" domain) -path=$(ynh_app_setting_get "$app" path) -is_public=$(ynh_app_setting_get $app is_public) +domain=$(ynh_app_setting_get --app=$app --key=domain) +path_url=$(ynh_app_setting_get --app=$app --key=path) +is_public=$(ynh_app_setting_get --app=$app --key=is_public) +final_path=$(ynh_app_setting_get --app=$app --key=final_path) -ynh_backup_before_upgrade # Backup the current version of the app -ynh_clean_setup () { - ynh_backup_fail_upgrade # restore it if the upgrade fails -} -ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est détectée. +#================================================= +# CHECK VERSION +#================================================= -CHECK_PATH # Checks and corrects the syntax of the path. +upgrade_type=$(ynh_check_app_version_changed) -# Create system user dedicace for this app -ynh_system_user_create $app +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= +ynh_script_progression --message="Ensuring downward compatibility..." --weight=1 -# Init final_path, if ever it got deleted somehow -final_path=/var/www/$app - -# Clean all files and directory except the data directory -ynh_secure_remove $final_path - -# Create directory -sudo mkdir -p $final_path - -# Copy files to the right place -extract_source $final_path - -# Fix owner -sudo chown -R $app: $final_path - -# Modify Nginx configuration file and copy it to Nginx conf directory -ynh_nginx_config - -# Create the php-fpm pool config -ynh_fpm_config - -# Set ssowat config +# Fix is_public as a boolean value if [ "$is_public" = "Yes" ]; then - ynh_app_setting_set $app is_public 1 # Fixe is_public en booléen + ynh_app_setting_set --app=$app --key=is_public --value=1 is_public=1 elif [ "$is_public" = "No" ]; then - ynh_app_setting_set $app is_public 0 - is_public=0 + ynh_app_setting_set --app=$app --key=is_public --value=0 + is_public=0 fi -# Reload Nginx -sudo systemctl reload nginx -sudo yunohost app ssowatconf +# If final_path doesn't exist, create it +if [ -z "$final_path" ]; then + final_path=/var/www/$app + ynh_app_setting_set --app=$app --key=final_path --value=$final_path +fi + +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= +ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=5 + +# 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 + +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +if [ "$upgrade_type" == "UPGRADE_APP" ] +then + ynh_script_progression --message="Upgrading source files..." --weight=5 + + # Download, check integrity, uncompress and patch the source from app.src + ynh_setup_source --dest_dir="$final_path" +fi + +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1 + +# Create a dedicated nginx config +ynh_add_nginx_config + +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1 + +# Create a dedicated user (if not existing) +ynh_system_user_create --username=$app + +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================= + +# Set permissions to app files +chown -R $app $final_path + +#================================================= +# PHP-FPM CONFIGURATION +#================================================= +ynh_script_progression --message="Upgrading PHP-fpm configuration..." --weight=1 + +# Create a dedicated php-fpm config +ynh_add_fpm_config #--package="$extra_php_dependencies" + +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." --weight=1 + +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= +ynh_script_progression --message="Upgrade of $app completed" --last