diff --git a/README.md b/README.md index 6afb824..b993fcb 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ An open source, modern and powerful wiki app built on Node.js, Git and Markdown **Wiki.js is still under development, all features not already implemented.** -**Shipped version:** 2.0.0-beta.11 +**Shipped version:** 2.0.0-beta.13 ## Licence diff --git a/conf/app.src b/conf/app.src index 9eaaaf7..e4dd89f 100644 --- a/conf/app.src +++ b/conf/app.src @@ -1,4 +1,4 @@ -SOURCE_URL=https://github.com/Requarks/wiki/releases/download/2.0.0-beta.11/wiki-js.tar.gz +SOURCE_URL=url of app's source SOURCE_SUM=sha256 checksum SOURCE_SUM_PRG=sha256sum SOURCE_FORMAT=tar.gz diff --git a/conf/systemd.service b/conf/systemd.service index 35c2d16..eb1c958 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -7,7 +7,7 @@ Type=simple User=__APP__ Group=__APP__ WorkingDirectory=__FINALPATH__/ -ExecStart=__NODEJS_PATH__/node __FINALPATH__/server >> /var/log/__APP__/__APP__.log 2>&1 +ExecStart=/bin/sh -c '__NODEJS_PATH__/node __FINALPATH__/server >> /var/log/__APP__/__APP__.log 2>&1' [Install] WantedBy=multi-user.target diff --git a/manifest.json b/manifest.json index 1aa8d07..a58ba38 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "An open source, modern and powerful wiki app built on Node.js, Git and Markdown for YunoHost.", "fr": "Un wiki moderne et open source propulsé Node.js, Git et Markdown pour YunoHost." }, - "version": "2.0.0-beta.11~ynh1", + "version": "2.0.0-beta.13~ynh1", "url": "https://wiki.js.org/", "license": "AGPL-3.0-or-later", "maintainer": { diff --git a/scripts/backup b/scripts/backup index 4a8b7f0..93302d7 100644 --- a/scripts/backup +++ b/scripts/backup @@ -7,6 +7,7 @@ #================================================= source ../settings/scripts/_common.sh +source ../settings/scripts/ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -15,7 +16,7 @@ source /usr/share/yunohost/helpers 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/install b/scripts/install index c45ea91..38032f0 100644 --- a/scripts/install +++ b/scripts/install @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -15,7 +16,7 @@ source /usr/share/yunohost/helpers 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 @@ -96,9 +97,13 @@ ynh_app_setting_set $app port $port ### - As well as the section "REINSTALL DEPENDENCIES" in the restore script ### - And the section "UPGRADE DEPENDENCIES" in the upgrade script -ynh_install_app_dependencies redis-server postgresql postgresql-contrib +curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - +echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list + ynh_install_nodejs 10 +ynh_install_app_dependencies redis-server postgresql postgresql-contrib yarn + #================================================= # CREATE A POSTGRESQL DATABASE #================================================= @@ -132,6 +137,10 @@ ynh_app_setting_set $app final_path $final_path #ynh_setup_source "$final_path" git clone https://github.com/Requarks/wiki "$final_path" +pushd $final_path + git checkout "tags/2.0.0-beta.13" --quiet +popd + #================================================= # NGINX CONFIGURATION #================================================= @@ -169,20 +178,22 @@ ynh_system_user_create "$app" "$final_path" #================================================= # SPECIFIC SETUP #================================================= -# ... +# CREATE LOG FOLDER +#================================================= + +mkdir -p "/var/log/$app" +chown -R "$app":"$app" "/var/log/$app" + +#================================================= +# MAKE SETUP #================================================= chown -R "$app":"$app" "$final_path" pushd $final_path -ynh_use_nodejs -sudo -u $app env PATH=$PATH:$nodejs_path npm install "graphql@^0.13.1" -sudo -u $app env PATH=$PATH:$nodejs_path npm install "acorn@^6.0.0" -sudo -u $app env PATH=$PATH:$nodejs_path npm install "babel-core@>=6.0.20" -sudo -u $app env PATH=$PATH:$nodejs_path npm install "eslint@>=5.0.0" -sudo -u $app env PATH=$PATH:$nodejs_path npm install -sudo -u $app env PATH=$PATH:$nodejs_path npm audit fix -sudo -u $app env PATH=$PATH:$nodejs_path npm run build + ynh_use_nodejs + sudo -u $app env PATH=$PATH:$nodejs_path yarn install + sudo -u $app env PATH=$PATH:$nodejs_path npm run build popd @@ -317,6 +328,11 @@ fi #================================================= # RELOAD NGINX #================================================= -systemctl start $app + systemctl reload nginx -sleep 30 + +#================================================= +# START SERVICE +#================================================= + +ynh_systemd_action --action=start --service_name=$app --line_match="Redis connection:" diff --git a/scripts/remove b/scripts/remove index 39d1815..2b6f6a4 100644 --- a/scripts/remove +++ b/scripts/remove @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -57,12 +58,13 @@ ynh_remove_app_dependencies ynh_use_nodejs ynh_remove_nodejs +rm -rf "/etc/apt/sources.list.d/yarn.list" + #================================================= # REMOVE APP MAIN DIR #================================================= # remove tmp files -sudo -u $app rm -rf /tmp/* ynh_secure_remove "/var/tmp/*" # Remove the app directory securely diff --git a/scripts/restore b/scripts/restore index b6fb475..9541981 100644 --- a/scripts/restore +++ b/scripts/restore @@ -7,6 +7,7 @@ #================================================= source ../settings/scripts/_common.sh +source ../settings/scripts/ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -15,7 +16,7 @@ source /usr/share/yunohost/helpers 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 @@ -91,16 +92,21 @@ chown -R "$app":"$app" "$final_path" #================================================= # Define and install dependencies -ynh_install_app_dependencies redis-server postgresql postgresql-contrib + +curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - +echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list + ynh_install_nodejs 10 +ynh_install_app_dependencies redis-server postgresql postgresql-contrib yarn + #================================================= # RESTORE SYSTEMD #================================================= ynh_restore_file "/etc/systemd/system/$app.service" systemctl enable $app.service -systemctl start $app +systemctl daemon-reload #================================================= # ADVERTISE SERVICE IN ADMIN PANEL @@ -128,4 +134,16 @@ ynh_restore_file "/etc/logrotate.d/$app" #systemctl reload php5-fpm systemctl reload nginx -sleep 30 + +#================================================= +# CREATE LOG FOLDER +#================================================= + +mkdir -p "/var/log/$app" +chown -R "$app":"$app" "/var/log/$app" + +#================================================= +# START SERVICE +#================================================= + +ynh_systemd_action --action=start --service_name=$app --line_match="Redis connection:" diff --git a/scripts/upgrade b/scripts/upgrade index d304d57..bbb5706 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -22,6 +23,7 @@ is_public=$(ynh_app_setting_get $app is_public) final_path=$(ynh_app_setting_get $app final_path) #language=$(ynh_app_setting_get $app language) db_name=$(ynh_app_setting_get $app db_name) +db_pwd=$(ynh_app_setting_get $app db_pwd) port=$(ynh_app_setting_get $app port) #================================================= @@ -58,10 +60,17 @@ 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 SERVICE +#================================================= + +ynh_systemd_action --action=stop --service_name=$app + #================================================= # CHECK THE PATH #================================================= @@ -77,7 +86,11 @@ path_url=$(ynh_normalize_url_path $path_url) # Download, check integrity, uncompress and patch the source from app.src #ynh_setup_source "$final_path" -( cd $final_path && git pull ) + +pushd $final_path + git fetch origin + git checkout "tags/2.0.0-beta.13" --quiet +popd #================================================= # NGINX CONFIGURATION @@ -90,9 +103,13 @@ ynh_add_nginx_config # UPGRADE DEPENDENCIES #================================================= -ynh_install_app_dependencies redis-server postgresql postgresql-contrib +curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - +echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list + ynh_install_nodejs 10 +ynh_install_app_dependencies redis-server postgresql postgresql-contrib yarn + #================================================= # CREATE DEDICATED USER #================================================= @@ -116,16 +133,26 @@ ynh_system_user_create "$app" "$final_path" chown -R "$app":"$app" "$final_path" pushd $final_path -ynh_use_nodejs -sudo -u $app env PATH=$PATH:$nodejs_path npm install "graphql@^0.13.1" -sudo -u $app env PATH=$PATH:$nodejs_path npm install "acorn@^6.0.0" -sudo -u $app env PATH=$PATH:$nodejs_path npm install "babel-core@>=6.0.20" -sudo -u $app env PATH=$PATH:$nodejs_path npm install "eslint@>=5.0.0" -sudo -u $app env PATH=$PATH:$nodejs_path npm install -sudo -u $app env PATH=$PATH:$nodejs_path npm audit fix -sudo -u $app env PATH=$PATH:$nodejs_path npm run build + ynh_use_nodejs + sudo -u $app env PATH=$PATH:$nodejs_path yarn install + sudo -u $app env PATH=$PATH:$nodejs_path npm run build popd + +#================================================= +# MODIFY A CONFIG FILE +#================================================= + +cp -f ../conf/config.sample.yml "$final_path/config.yml" + +### `ynh_replace_string` is used to replace a string in a file. +### (It's compatible with sed regular expressions syntax) + +#ynh_replace_string "match_string" "replace_string" "$final_path/config.yml" +ynh_replace_string "__PORT__" "$port" "$final_path/config.yml" +ynh_replace_string "__DB_PWD__" "$db_pwd" "$final_path/config.yml" +ynh_replace_string "__DB_NAME__" "$db_name" "$final_path/config.yml" + ### 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. ynh_backup_if_checksum_is_different "$final_path/config.yml" @@ -172,4 +199,9 @@ fi #================================================= systemctl reload nginx -sleep 30 + +#================================================= +# START SERVICE +#================================================= + +ynh_systemd_action --action=start --service_name=$app --line_match="Redis connection:" 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 +}