diff --git a/conf/config.ini b/conf/config.ini index cf164d0..feccda9 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -17,7 +17,7 @@ port = 3306 [app] site_name = __SITENAME__ site_description = __SITEDESCRIPTION__ -host = https://your-writefreely-url-instance.com +host = __DOMAIN_URL__ theme = write disable_js = false webfonts = true diff --git a/conf/nginx.conf b/conf/nginx.conf index 4899948..19cfa5b 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,6 +1,5 @@ #sub_path_only rewrite ^__PATH__$ __PATH__/ permanent; - location ~ ^/.well-known/(webfinger|nodeinfo|host-meta) { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; @@ -15,8 +14,6 @@ location ~ ^/(css|img|js|fonts)/ { # expires 12M; } - - location __PATH__/ { proxy_pass http://localhost:__PORT__/; diff --git a/conf/systemd.service b/conf/systemd.service index e480739..b2cdfdf 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -7,7 +7,7 @@ Type=simple User=__APP__ Group=__APP__ WorkingDirectory=__FINALPATH__/ -ExecStart=__FINALPATH__/writefreely >> /var/log/__APP__/__APP__.log 2>&1 +ExecStart=/bin/sh -c '__FINALPATH__/writefreely >> /var/log/__APP__/__APP__.log 2>&1' [Install] WantedBy=multi-user.target diff --git a/scripts/backup b/scripts/backup index 96a872c..a565a30 100755 --- a/scripts/backup +++ b/scripts/backup @@ -8,6 +8,8 @@ source ../settings/scripts/detect_arch source ../settings/scripts/_common.sh +source ../settings/scripts/detect_arch +source ../settings/scripts/ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -25,7 +27,7 @@ fi ynh_clean_setup () { ### Remove this function if there's nothing to clean before calling the remove script. - true + ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors diff --git a/scripts/change_url b/scripts/change_url index 1f71445..ff6fa1b 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -100,18 +100,27 @@ fi #================================================= # SPECIFIC MODIFICATIONS #================================================= -# ... +# STOP SERVICE #================================================= -# Stop writefreely for modification -yunohost service stop "$app" +ynh_systemd_action --action=stop --service_name=$app + +#================================================= +# MODIFY A CONFIG FILE +#================================================= ynh_replace_string "host =.*" "host =https://$new_domain$new_path" "$final_path/config.ini" -# Start writefreely after modification -yunohost service start $app +#================================================= +# START SERVICE +#================================================= -### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. +ynh_systemd_action --action=start --service_name=$app + +#================================================= +# STORE THE CONFIG FILE CHECKSUM +#================================================= +fy the checksum of a file, stored by `ynh_store_file_checksum` in the install script. ### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it. ynh_backup_if_checksum_is_different "$final_path/config.ini" # Recalculate and store the checksum of the file for the next upgrade. diff --git a/scripts/install b/scripts/install index c82e337..cbf201e 100755 --- a/scripts/install +++ b/scripts/install @@ -8,6 +8,8 @@ source detect_arch source _common.sh +source detect_arch +source ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -25,7 +27,7 @@ fi ynh_clean_setup () { ### Remove this function if there's nothing to clean before calling the remove script. - true + ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -57,7 +59,6 @@ then admin="test" fi - ### If it's a multi-instance app, meaning it can be installed several times independently ### The id of the app as stated in the manifest is available as $YNH_APP_ID ### The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...) @@ -76,7 +77,7 @@ app=$YNH_APP_INSTANCE_NAME ### If the app uses nginx as web server (written in HTML/PHP in most cases), the final path should be "/var/www/$app". ### If the app provides an internal web server (or uses another application server such as uwsgi), the final path should be "/opt/yunohost/$app" -final_path=/var/www/$app +final_path="/var/www/$app" test ! -e "$final_path" || ynh_die "This path already contains a folder" # Normalize the url path syntax @@ -250,7 +251,6 @@ ynh_add_systemd_config # ynh_app_setting_delete $app skipped_uris #fi - #================================================= # MODIFY A CONFIG FILE #================================================= @@ -259,13 +259,13 @@ ynh_add_systemd_config ### (It's compatible with sed regular expressions syntax) # setup application config -sudo cp ../conf/config.ini $final_path/config.ini +sudo cp "../conf/config.ini" "$final_path/config.ini" ynh_replace_string "__DBNAME__" "$db_name" "$final_path/config.ini" ynh_replace_string "__DBUSER__" "$db_name" "$final_path/config.ini" ynh_replace_string "__DBPWD__" "$db_pwd" "$final_path/config.ini" ynh_replace_string "__PORT__" "$port" "$final_path/config.ini" -ynh_replace_string "https://your-writefreely-url-instance.com" "https://$domain$path_url" "$final_path/config.ini" +ynh_replace_string "__DOMAIN_URL__" "https://$domain$path_url" "$final_path/config.ini" ynh_replace_string "__SINGLEUSER__" "$single_user" "$final_path/config.ini" ynh_replace_string "__SITENAME__" "$site_name" "$final_path/config.ini" ynh_replace_string "__SITEDESCRIPTION__" "$site_description" "$final_path/config.ini" @@ -290,13 +290,23 @@ ynh_store_file_checksum "$final_path/config.ini" #================================================= # SPECIFIC SETUP #================================================= -# ... +# CREATE LOG FOLDER #================================================= -( cd $final_path && $final_path/writefreely --init-db ) -( cd $final_path && $final_path/writefreely --gen-keys ) -( cd $final_path && $final_path/writefreely --create-admin $admin:$password ) +mkdir -p "/var/log/$app" +chown -R "$app":"$app" "/var/log/$app" +#================================================= +# MAKE SETUP +#================================================= + +chown -R "$app":"$app" "$final_path" + +pushd $final_path + sudo -u $app ./writefreely --init-db + sudo -u $app ./writefreely --gen-keys + sudo -u $app ./writefreely --create-admin "$admin":"$password" +popd #================================================= # GENERIC FINALIZATION @@ -325,7 +335,6 @@ chown -R "$app":"$app" "$final_path" # Use logrotate to manage application logfile(s) ynh_use_logrotate -chown -R $app: /var/log/$app/ #================================================= # ADVERTISE SERVICE IN ADMIN PANEL @@ -344,7 +353,6 @@ chown -R $app: /var/log/$app/ # if using yunohost version 3.2 or more in the 'manifest.json', a description can be added yunohost service add $app --description "$app daemon for WriteFreely" --log "/var/log/$app/$app.log" - #================================================= # SETUP SSOWAT #================================================= @@ -366,4 +374,4 @@ systemctl reload nginx # START SERVICE #================================================= -yunohost service start $app +ynh_systemd_action --action=start --service_name=$app diff --git a/scripts/remove b/scripts/remove index 7d890b0..e2cca67 100755 --- a/scripts/remove +++ b/scripts/remove @@ -8,6 +8,7 @@ source detect_arch source _common.sh +source ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -28,7 +29,6 @@ app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get $app domain) port=$(ynh_app_setting_get $app port) db_name=$(ynh_app_setting_get $app db_name) -db_user=$db_name final_path=$(ynh_app_setting_get $app final_path) #================================================= @@ -63,7 +63,7 @@ ynh_remove_systemd_config #================================================= # Remove a database if it exists, along with the associated user -ynh_mysql_remove_db $db_user $db_name +ynh_mysql_remove_db $db_name $db_name #================================================= # REMOVE APP MAIN DIR diff --git a/scripts/restore b/scripts/restore index 24978a3..f4bb76a 100755 --- a/scripts/restore +++ b/scripts/restore @@ -8,6 +8,8 @@ source ../settings/scripts/detect_arch source ../settings/scripts/_common.sh +source ../settings/scripts/detect_arch +source ../settings/scripts/ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -25,7 +27,7 @@ fi ynh_clean_setup () { #### Remove this function if there's nothing to clean before calling the remove script. - true + ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -107,6 +109,7 @@ chown -R "$app":"$app" "$final_path" ynh_restore_file "/etc/systemd/system/$app.service" systemctl enable $app.service +systemctl daemon-reload #================================================= # ADVERTISE SERVICE IN ADMIN PANEL @@ -135,8 +138,15 @@ ynh_restore_file "/etc/logrotate.d/$app" #systemctl reload php5-fpm systemctl reload nginx +#================================================= +# CREATE LOG FOLDER +#================================================= + +mkdir -p "/var/log/$app" +chown -R "$app":"$app" "/var/log/$app" + #================================================= # START SERVICE #================================================= -yunohost service start $app +ynh_systemd_action --action=start --service_name=$app diff --git a/scripts/upgrade b/scripts/upgrade index d27f63d..efe6305 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -8,6 +8,9 @@ source detect_arch source _common.sh +source detect_arch +source ynh_systemd_action +source ynh_check_app_version_changed source /usr/share/yunohost/helpers #================================================= @@ -34,6 +37,12 @@ final_path=$(ynh_app_setting_get $app final_path) db_name=$(ynh_app_setting_get $app db_name) port=$(ynh_app_setting_get $app port) +#================================================= +# CHECK VERSION +#================================================= + +upgrade_type=$(ynh_check_app_version_changed) + #================================================= # ENSURE DOWNWARD COMPATIBILITY #================================================= @@ -68,12 +77,16 @@ ynh_backup_before_upgrade ynh_clean_setup () { # restore it if the upgrade fails ynh_restore_upgradebackup + ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors -# Stop writefreely for upgrade -yunohost service stop "$app" +#================================================= +# STOP SERVICE +#================================================= + +ynh_systemd_action --action=stop --service_name=$app #================================================= # CHECK THE PATH @@ -88,8 +101,11 @@ path_url=$(ynh_normalize_url_path $path_url) # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= +if [ "$upgrade_type" == "UPGRADE_APP" ] +then # Download, check integrity, uncompress and patch the source from app.src ynh_setup_source "$final_path" +fi #================================================= # NGINX CONFIGURATION @@ -124,7 +140,11 @@ ynh_system_user_create "$app" "$final_path" # MAKE UPGRADE #================================================= -( cd $final_path && $final_path/writefreely --migrate ) +chown -R "$app":"$app" "$final_path" + +pushd $final_path + sudo -u $app ./writefreely --migrate +popd ### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. ### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it. @@ -132,7 +152,6 @@ ynh_backup_if_checksum_is_different "$final_path/config.ini" # Recalculate and store the checksum of the file for the next upgrade. ynh_store_file_checksum "$final_path/config.ini" - #================================================= # CLOSE A PORT #================================================= @@ -143,7 +162,6 @@ then yunohost firewall disallow TCP $port 2>&1 fi - #================================================= # SETUP LOGROTATE #================================================= @@ -188,4 +206,4 @@ systemctl reload nginx # START SERVICE #================================================= -yunohost service start $app +ynh_systemd_action --action=start --service_name=$app diff --git a/scripts/ynh_check_app_version_changed b/scripts/ynh_check_app_version_changed new file mode 100644 index 0000000..641815c --- /dev/null +++ b/scripts/ynh_check_app_version_changed @@ -0,0 +1,46 @@ +#!/bin/bash + +# 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_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 +} diff --git a/scripts/ynh_systemd_action b/scripts/ynh_systemd_action new file mode 100644 index 0000000..6bed6be --- /dev/null +++ b/scripts/ynh_systemd_action @@ -0,0 +1,89 @@ +#!/bin/bash + +# Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started +# +# usage: ynh_systemd_action [-n service_name] [-a action] [ [-l "line to match"] [-p log_path] [-t timeout] [-e length] ] +# | arg: -n, --service_name= - Name of the service to reload. Default : $app +# | arg: -a, --action= - Action to perform with systemctl. Default: start +# | arg: -l, --line_match= - Line to match - The line to find in the log to attest the service have finished to boot. +# If not defined it don't wait until the service is completely started. +# | arg: -p, --log_path= - Log file - Path to the log file. Default : /var/log/$app/$app.log +# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Default : 300 seconds. +# | arg: -e, --length= - Length of the error log : Default : 20 +ynh_systemd_action() { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [n]=service_name= [a]=action= [l]=line_match= [p]=log_path= [t]=timeout= [e]=length= ) + local service_name + local action + local line_match + local length + local log_path + local timeout + + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + local service_name="${service_name:-$app}" + local action=${action:-start} + local log_path="${log_path:-/var/log/$service_name/$service_name.log}" + local length=${length:-20} + local timeout=${timeout:-300} + + # Start to read the log + if [[ -n "${line_match:-}" ]] + then + local templog="$(mktemp)" + # Following the starting of the app in its log + if [ "$log_path" == "systemd" ] ; then + # Read the systemd journal + journalctl -u $service_name -f --since=-45 > "$templog" & + else + # Read the specified log file + tail -F -n0 "$log_path" > "$templog" & + fi + # Get the PID of the tail command + local pid_tail=$! + fi + + echo "${action^} the service $service_name" >&2 + systemctl $action $service_name \ + || ( journalctl --lines=$length -u $service_name >&2 \ + ; test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 \ + ; false ) + + # Start the timeout and try to find line_match + if [[ -n "${line_match:-}" ]] + then + 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 + if grep --quiet "$line_match" "$templog" + then + echo "The service $service_name has correctly started." >&2 + break + fi + echo -n "." >&2 + sleep 1 + done + if [ $i -eq $timeout ] + then + echo "The service $service_name didn't fully started before the timeout." >&2 + journalctl --lines=$length -u $service_name >&2 + test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 + fi + + echo "" + ynh_clean_check_starting + fi +} + +# 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 +}