diff --git a/check_process b/check_process new file mode 100644 index 0000000..f44a27d --- /dev/null +++ b/check_process @@ -0,0 +1,43 @@ +# See here for more informations +# https://github.com/YunoHost/package_check#syntax-check_process-file + +# Move this file from check_process.default to check_process when you have filled it. + +;; Test complet + ; Manifest + domain="domain.tld" (DOMAIN) + path="/" (PATH) + admin="john" (USER) + language="fr" + is_public=1 (PUBLIC|public=1|private=0) + password="pass" + port="666" (PORT) + ; Checks + pkg_linter=1 + setup_sub_dir=0 + setup_root=1 + setup_nourl=0 + setup_private=1 + setup_public=1 + upgrade=1 + backup_restore=1 + multi_instance=1 + incorrect_path=1 + port_already_use=0 + change_url=0 +;;; Levels + Level 1=auto + Level 2=auto + Level 3=auto +# Level 4: + Level 4=0 +# Level 5: + Level 5=auto + Level 6=auto + Level 7=auto + Level 8=0 + Level 9=0 + Level 10=0 +;;; Options +Email= +Notification=none diff --git a/conf/app.src b/conf/app.src new file mode 100644 index 0000000..5b0660a --- /dev/null +++ b/conf/app.src @@ -0,0 +1,6 @@ +SOURCE_URL=https://github.com/seejohnrun/haste-server/archive/f19c5d10498e214b8323fff9e5eab77cbb7eebff.tar.gz +SOURCE_SUM=6e67fd09bc7782d23d612c209a2de39212682239018a1bd2618c120f9cc36a47 +SOURCE_SUM_PRG=sha256sum +SOURCE_FORMAT=tar.gz +SOURCE_IN_SUBDIR=true +SOURCE_FILENAME= diff --git a/conf/config.js b/conf/config.js index 00e98c5..226bf72 100644 --- a/conf/config.js +++ b/conf/config.js @@ -1,7 +1,7 @@ { "host": "127.0.0.1", - "port": 7777, + "port": __PORT__, "keyLength": 10, @@ -10,14 +10,14 @@ "staticMaxAge": 86400, "recompressStaticAssets": true, - + "logging": [ { - "level": "info", + "level": "verbose", "type": "Console", "colorize": true } - ], +], "keyGenerator": { "type": "phonetic" @@ -35,7 +35,7 @@ "storage": { "type": "file", "file": "YNH_DATA_PATH" - }, +}, "documents": { "about": "./about.md" diff --git a/conf/nginx.conf b/conf/nginx.conf index 8c68c6e..aa601d9 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,5 +1,5 @@ location / { - proxy_pass http://localhost:7777/; + proxy_pass http://localhost:__PORT__; proxy_set_header Host $host; proxy_buffering off; fastcgi_param REMOTE_USER $remote_user; diff --git a/conf/haste.service b/conf/systemd.service similarity index 51% rename from conf/haste.service rename to conf/systemd.service index 136d796..8d92c8b 100644 --- a/conf/haste.service +++ b/conf/systemd.service @@ -5,12 +5,13 @@ After=network.target [Service] Type=simple -User=haste -Group=haste -WorkingDirectory=/opt/haste -ExecStart=/usr/bin/npm start +User=__APP__ +Group=__APP__ +ExecStartPre=__NODEJS__ +WorkingDirectory=__FINALPATH__ +Environment="PATH=__ENV_PATH__" +ExecStart=/opt/node_n/n/versions/node/4/bin/npm start Restart=always -Environment=USER=haste HOME=/opt/haste [Install] WantedBy=multi-user.target diff --git a/manifest.json b/manifest.json index 188a1ff..306ddff 100644 --- a/manifest.json +++ b/manifest.json @@ -7,14 +7,14 @@ "fr": "Haste is an open-source pastebin software written in node.js" }, "url": "https://github.com/seejohnrun/haste-server", - "license": "free", + "license": "MIT", "maintainer": { "name": "mbugeia", "email": "maxime.bugeia@gmail.com", "url": "https://github.com/YunoHost-Apps/haste_ynh" }, "requirements": { - "yunohost": ">> 2.4.0" + "yunohost": ">= 2.7.2" }, "multi_instance": false, "services": [ diff --git a/scripts/_common.sh b/scripts/_common.sh index b199e56..3234d4a 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,107 +1,160 @@ -# -# Common variables -# +#!/bin/bash +# 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. -APPNAME="haste" -app=${YNH_APP_INSTANCE_NAME:-haste} +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" -# Haste version -VERSION="master" +ynh_use_nodejs () { + nodejs_version=$(ynh_app_setting_get $app nodejs_version) -# set globals variables -DESTDIR="/opt/"${app} -DATA_PATH="/home/yunohost.app/"$app + load_n_path="[[ :$PATH: == *\":$n_install_dir/bin:\"* ]] || PATH=\"$n_install_dir/bin:$PATH\"; N_PREFIX="$n_install_dir"" -# Remote URL to fetch Haste tarball -HASTE_URL="https://github.com/seejohnrun/haste-server/archive/"${VERSION}".zip" + nodejs_use_version="$n_install_dir/bin/n -q $nodejs_version" -# Source YunoHost helpers -source /usr/share/yunohost/helpers + # "Load" a version of node + eval $load_n_path; $nodejs_use_version -# -# Common helpers -# + # Get the absolute path of this version of node + nodejs_path="$(n bin $nodejs_version)" -check_or_install_npm() { - if ! dpkg -s npm | grep "installed" > /dev/null 2>&1 \ - || ! dpkg -s nodejs-legacy | grep "installed" > /dev/null 2>&1; then - sudo apt-get update - sudo apt-get install -y npm nodejs-legacy - fi + # Make an alias for node use + ynh_node_exec="eval $load_n_path; n use $nodejs_version" } -pre_inst_haste() { - # retrieve, extract, copy haste, add user if necessary - local TMPDIR=$(mktemp -d) - local HASTE_SOURCE=$1 +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" - haste_tarball="/tmp/haste.zip" - rm -f "$haste_tarball" - if [ "$HASTE_SOURCE" = "backup" ] - then - # Restore the app and data files - sudo cp -a ./www "$DESTDIR" - sudo cp -a ./data/. "$DATA_PATH" - # Restore directories and permissions - sudo chown -R "$app":"$app" "$DESTDIR" "$DATA_PATH" - else - wget -q -O "$haste_tarball" "$HASTE_URL" \ - || ynh_die "Unable to download haste" - unzip -q "$haste_tarball" -d "$TMPDIR" \ - || ynh_die "Unable to extract haste" - sudo rsync -a "$TMPDIR"/haste-server-master/* "$DESTDIR" - fi - rm -rf "$haste_tarball" "$TMPDIR" + # Create $n_install_dir + mkdir -p "$n_install_dir" - # Add user if not exist - id -g "$app" &>/dev/null || sudo addgroup "$app" --system --quiet - id -u "$app" &>/dev/null || sudo adduser "$app" \ - --ingroup "$app" --system --quiet --shell /bin/bash + # 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:@@') - # Configure init script - sudo cp ../conf/"$app".service /etc/systemd/system/ - sudo systemctl daemon-reload - sudo systemctl enable "$app".service - } + # 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 -# Download, extract and install Haste to the given directory -# usage: install_haste DESTDIR -install_haste() { - local DOMAIN=$1 - local YNH_PATH=$2 - local IS_PUBLIC=$3 + # If n is not previously setup, install it + n --version > /dev/null 2>&1 || \ + ( echo "Installation of N - Node.js version management" >&2; \ + curl -sL $n_install_script | N_PREFIX=$N_PREFIX bash -s -- -y - 2>&1 ) - check_or_install_npm - pre_inst_haste none + # 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" - # install haste - current_dir=$(pwd) - cd "$DESTDIR" - sudo npm install - cd $current_dir + # Restore /usr/local/bin in PATH + PATH=$CLEAR_PATH - sudo mkdir -p $DATA_PATH - sudo chown -R "$app":"$app" $DESTDIR $DATA_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 - # Configure haste with config.js file - sudo cp ../conf/config.js "$DESTDIR"/config.js - sudo sed -i "s@YNH_DATA_PATH@$DATA_PATH@g" "$DESTDIR"/config.js + # Install the requested version of nodejs + n $nodejs_version - # Modify Nginx configuration file and copy it to Nginx conf directory - sed -i "s@PATHTOCHANGE@${YNH_PATH%/}@g" ../conf/nginx.conf - sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/"$app".conf + # 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) - # If app is public, add url to SSOWat conf as skipped_uris - if [[ $IS_PUBLIC -eq 1 ]]; then - # unprotected_uris allows SSO credentials to be passed anyway. - ynh_app_setting_set "$app" unprotected_uris "/" - fi + # 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 - # Reload services - sudo systemctl reload nginx.service + # 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" - # install haste cli client - sed -i "s@YNH_HASTE_URL@${DOMAIN}${path%/}@g" ../conf/haste.sh - sudo cp ../conf/haste.sh /usr/bin/"$app" - sudo chmod +x /usr/bin/"$app" + # 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" } diff --git a/scripts/backup b/scripts/backup index e5071aa..ea25bb2 100755 --- a/scripts/backup +++ b/scripts/backup @@ -1,22 +1,61 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Load common variables and functions -source ./_common.sh +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 /usr/share/yunohost/helpers -domain=$(ynh_app_setting_get "$app" domain) +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= -# Copy the app source and data files -ynh_backup "$DESTDIR" "www" -ynh_backup "$DATA_PATH" "data" +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors -# Copy the conf files -mkdir ./conf -ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "conf/nginx.conf" -ynh_backup "/etc/systemd/system/${app}.service" "conf/systemd.service" -ynh_backup "/usr/bin/${app}" "conf/haste.sh" +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +final_path=$(ynh_app_setting_get $app final_path) +domain=$(ynh_app_setting_get $app domain) +db_name=$(ynh_app_setting_get $app db_name) + +#================================================= +# STANDARD BACKUP STEPS +#================================================= +# BACKUP THE APP MAIN DIR +#================================================= + +ynh_backup "$final_path" + +#================================================= +# BACKUP THE NGINX CONFIGURATION +#================================================= + +ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# SPECIFIC BACKUP +#================================================= +# BACKUP LOGROTATE +#================================================= + +ynh_backup "/etc/logrotate.d/$app" + +#================================================= +# BACKUP SYSTEMD +#================================================= + +ynh_backup "/etc/systemd/system/$app.service" -# Copy NGINX configuration -ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf" diff --git a/scripts/install b/scripts/install index 7c1953b..f1a3baf 100755 --- a/scripts/install +++ b/scripts/install @@ -1,33 +1,160 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Load common variables and functions -source ./_common.sh +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE FAILURE OF THE SCRIPT +#================================================= + +ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est détectée. + +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= -# Retrieve arguments domain=$YNH_APP_ARG_DOMAIN -path=$YNH_APP_ARG_PATH +path_url=$YNH_APP_ARG_PATH is_public=$YNH_APP_ARG_IS_PUBLIC -# Check domain/path availability -sudo yunohost app checkurl "${domain}${path}" -a "$app" \ - || ynh_die "Path not available: ${domain}${path}" +app=$YNH_APP_INSTANCE_NAME -if ! [ "$path" = "/" ] +#================================================= +# CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS +#================================================= + +final_path=/var/www/$app +test ! -e "$final_path" || ynh_die "This path already contains a folder" + +# Normalize the url path syntax +path_url=$(ynh_normalize_url_path $path_url) + +# Check web path availability +ynh_webpath_available $domain $path_url +# Register (book) web path +ynh_webpath_register $app $domain $path_url + +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= + +ynh_app_setting_set $app domain "$domain" +ynh_app_setting_set $app is_public "$is_public" +ynh_app_setting_set $app path_url "$path_url" + +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# FIND AND OPEN A PORT +#================================================= + +# Find a free port +port=$(ynh_find_port 7777) +# Open this port +yunohost firewall allow --no-upnp TCP $port 2>&1 +ynh_app_setting_set $app port $port +#================================================= +# INSTALL NODEJS +#================================================= + +ynh_install_nodejs 4 +#================================================= +# CREATE DEDICATED USER +#================================================= + +ynh_system_user_create $app + +#================================================= +# SPECIFIC SETUP +#================================================= +# HANDLE LOG FILES AND LOGROTATE +#================================================= + +# Créer le dossier de log +mkdir -p /var/log/$app +touch /var/log/$app/$app.log +chown $app -R /var/log/$app + +# Setup logrotate +ynh_use_logrotate + + +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +final_path=/var/www/$app +ynh_app_setting_set $app final_path $final_path +ynh_setup_source $final_path + +# Set files ownership during installation +chown $app: $final_path -R +chmod 755 $final_path -R + +#================================================= +# Modify Nginx configuration file and copy it to Nginx conf directory +#================================================= + +ynh_add_nginx_config + +#================================================= +# ADD SYSTEMD SERVICE +#================================================= + +ynh_replace_string "__NODE__" "$nodejs_path" "../conf/systemd.service" +ynh_replace_string "__NODEJS__" "$nodejs_use_version" "../conf/systemd.service" +ynh_replace_string "__ENV_PATH__" "$PATH" "../conf/systemd.service" +ynh_add_systemd_config + +#================================================= +# INSTALL HASTEBIN +#================================================= + +ynh_use_nodejs +script_dir="$PWD" +pushd "$final_path" +chown -R $app: $final_path +npm install +DATA_PATH="/home/yunohost.app/"$app +mkdir -p $DATA_PATH +chown -R "$app":"$app" $final_path $DATA_PATH + +#================================================= +# Configure haste with config.js file +#================================================= + + ynh_replace_string "YNH_DATA_PATH" "$DATA_PATH" "$final_path/config.js" + +#================================================= +# ENABLE SERVICE IN ADMIN PANEL +#================================================= + +# Ajoute le service au monitoring de Yunohost. +yunohost service add $app --log "/var/log/$app/$app.log" + +#================================================= +# START HASTEBIN IN BACKGROUND +#================================================= + +systemctl start $app + +#================================================= +# SETUP SSOWAT +#================================================= + +if [ $is_public -eq 1 ]; then - ynh_die "Path must be /" + ynh_app_setting_set "$app" unprotected_uris "/" fi -# Save app settings -ynh_app_setting_set "$app" is_public "$is_public" +#================================================= +# RELOAD NGINX +#================================================= -# Install the app -install_haste $domain $path $is_public - -# Start Haste -sudo systemctl start "$app".service - -# Add Haste to YunoHost's monitored services -sudo yunohost service add "$app" --log /var/log/"$app"/"$app".log +systemctl reload nginx diff --git a/scripts/remove b/scripts/remove index 7bf6d53..90f608e 100755 --- a/scripts/remove +++ b/scripts/remove @@ -1,33 +1,86 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Load common variables and functions -source ./_common.sh +source _common.sh +source /usr/share/yunohost/helpers -# Retrieve app settings -domain=$(ynh_app_setting_get "$app" domain) +#================================================= +# LOAD SETTINGS +#================================================= -# Stop haste -sudo systemctl stop "$app".service +app=$YNH_APP_INSTANCE_NAME -# Remove sources -sudo rm -rf "/opt/"${app} +domain=$(ynh_app_setting_get $app domain) +port=$(ynh_app_setting_get $app port) +db_name=$(ynh_app_setting_get $app db_name) +final_path=$(ynh_app_setting_get $app final_path) -# Remove nginx configuration file -[[ -n $domain ]] && sudo rm -f "/etc/nginx/conf.d/${domain}.d/${app}.conf" +#================================================= +# STANDARD REMOVE +#================================================= +# STOP AND REMOVE SERVICE +#================================================= -# Remove haste user and data -sudo userdel -r "$app" +# Remove the dedicated systemd config +ynh_remove_systemd_config -# Remove init script -sudo systemctl disable "$app".service -sudo rm -f /etc/systemd/system/"$app".service -sudo systemctl daemon-reload +#================================================= +# REMOVE SERVICE FROM ADMIN PANEL +#================================================= -# Remove monitor -sudo yunohost service remove "$app" +if yunohost service status | grep -q $app +then + echo "Remove $app service" + yunohost service remove $app +fi -# Reload nginx service -sudo systemctl reload nginx.service + +#================================================= +# REMOVE APP MAIN DIR +#================================================= + +# Remove the app directory securely +ynh_secure_remove "$final_path" + +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= + +# Remove the dedicated nginx config +ynh_remove_nginx_config + + +#================================================= +# REMOVE LOGROTATE CONFIGURATION +#================================================= + +# Remove the app-specific logrotate config +ynh_remove_logrotate + +#================================================= +# CLOSE A PORT +#================================================= + +if yunohost firewall list | grep -q "\- $port$" +then + echo "Close port $port" + yunohost firewall disallow TCP $port 2>&1 +fi + +#================================================= +# SPECIFIC REMOVE +#================================================= + +#================================================= +# GENERIC FINALIZATION +#================================================= +# REMOVE DEDICATED USER +#================================================= + +# Delete a system user +ynh_system_user_delete $app diff --git a/scripts/restore b/scripts/restore index 633d74b..f316133 100755 --- a/scripts/restore +++ b/scripts/restore @@ -1,39 +1,124 @@ + #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Load common variables and functions -source ./_common.sh +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 /usr/share/yunohost/helpers -# Retrieve old app settings -domain=$(ynh_app_setting_get "$app" domain) -path=$(ynh_app_setting_get "$app" path) -is_public=$(ynh_app_setting_get "$app" is_public) +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= -# Check domain/path availability -sudo yunohost app checkurl "${domain}${path}" -a "$app" \ - || ynh_die "Path not available: ${domain}${path}" +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors -# Check destination directory -[[ -d $DESTDIR ]] && ynh_die \ -"The destination directory '$DESTDIR' already exists.\ - You should safely delete it before restoring this app." +#================================================= +# LOAD SETTINGS +#================================================= - # Check configuration files - nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" - [[ -f $nginx_conf ]] && ynh_die \ - "The NGINX configuration already exists at '${nginx_conf}'. - You should safely delete it before restoring this app." +app=$YNH_APP_INSTANCE_NAME -pre_inst_haste backup +domain=$(ynh_app_setting_get $app domain) +path_url=$(ynh_app_setting_get $app path) +final_path=$(ynh_app_setting_get $app final_path) +db_name=$(ynh_app_setting_get $app db_name) -# Restore NGINX configuration -sudo cp -a ./nginx.conf "/etc/nginx/conf.d/${domain}.d/${app}.conf" +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= -# Restart webserver and app -sudo systemctl reload nginx.service -sudo systemctl start "$app".service +ynh_webpath_available $domain $path_url \ + || ynh_die "Path not available: ${domain}${path_url}" +test ! -d $final_path \ + || ynh_die "There is already a directory: $final_path " -# Add Haste to YunoHost's monitored services -sudo yunohost service add "$app" --log /var/log/"$app"/"$app".log +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= + +ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= + +ynh_restore_file "$final_path" + + +#================================================= +# RECREATE THE DEDICATED USER +#================================================= + +# Create the dedicated user (if not existing) +ynh_system_user_create $app + +#================================================= +# RESTORE USER RIGHTS +#================================================= + +# Restore permissions on app files +chown -R root: $final_path + + +#================================================= +# SPECIFIC RESTORATION +#================================================= +ynh_install_nodejs 4 +#================================================= +# INSTALL HASTEBIN +#================================================= + +ynh_use_nodejs +script_dir="$PWD" +pushd "$final_path" +chown -R $app: $final_path +npm install +DATA_PATH="/home/yunohost.app/"$app +mkdir -p $DATA_PATH +chown -R "$app":"$app" $final_path $DATA_PATH + + +#================================================= +# ADVERTISE SERVICE IN ADMIN PANEL +#================================================= + +yunohost service add $app --log "/var/log/$app/APP.log" + +#================================================= +# RESTORE SYSTEMD +#================================================= + +ynh_restore_file "/etc/systemd/system/$app.service" +systemctl enable $app.service + +#================================================= +# START HASTEBIN IN BACKGROUND +#================================================= + +systemctl start $app + +#================================================= +# RESTORE THE LOGROTATE CONFIGURATION +#================================================= + +ynh_restore_file "/etc/logrotate.d/$app" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX +#================================================= + +systemctl reload nginx diff --git a/scripts/upgrade b/scripts/upgrade index af5a271..ca651c0 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,18 +1,162 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Load common variables and functions -source ./_common.sh +source _common.sh +source /usr/share/yunohost/helpers -# Retrieve arguments -domain=$(ynh_app_setting_get "$app" domain) -path=$(ynh_app_setting_get "$app" path) -is_public=$(ynh_app_setting_get "$app" is_public) +#================================================= +# LOAD SETTINGS +#================================================= -# Install the app -install_haste $domain $path $is_public +app=$YNH_APP_INSTANCE_NAME -# Start Haste -sudo systemctl restart "$app".service +domain=$(ynh_app_setting_get $app domain) +path_url=$(ynh_app_setting_get $app path_url) +is_public=$(ynh_app_setting_get $app is_public) +final_path=$(ynh_app_setting_get $app final_path) +port=$(ynh_app_setting_get $app port) + +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= + +# Fix is_public as a boolean value +if [ "$is_public" = "Yes" ]; then + ynh_app_setting_set $app is_public 1 + is_public=1 +elif [ "$is_public" = "No" ]; then + ynh_app_setting_set $app is_public 0 + is_public=0 +fi + +# If db_name doesn't exist, create it +if [ -z $db_name ]; then + db_name=$(ynh_sanitize_dbid $app) + ynh_app_setting_set $app db_name $db_name +fi + +# If final_path doesn't exist, create it +if [ -z $final_path ]; then + final_path=/var/www/$app + ynh_app_setting_set $app final_path $final_path +fi + +#================================================= +# 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 + +#================================================= +# CHECK THE PATH +#================================================= + +# Normalize the URL path syntax +path_url=$(ynh_normalize_url_path $path_url) + +#================================================= +# INSTALL NODEJS +#================================================= + +ynh_install_nodejs 4 +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +# Download, check integrity, uncompress and patch the source from app.src +ynh_setup_source "$final_path" +# Set files ownership during installation +chown $app: $final_path -R +chmod 755 $final_path -R + +#================================================= +# NGINX CONFIGURATION +#================================================= + +# Create a dedicated nginx config +ynh_add_nginx_config + +#================================================= +# CREATE DEDICATED USER +#================================================= + +# Create a system user +ynh_system_user_create $app + +#================================================= +# ADD SYSTEMD SERVICE +#================================================= + +ynh_replace_string "__NODE__" "$nodejs_path" "../conf/systemd.service" +ynh_replace_string "__NODEJS__" "$nodejs_use_version" "../conf/systemd.service" +ynh_replace_string "__ENV_PATH__" "$PATH" "../conf/systemd.service" +ynh_add_systemd_config +#================================================= +# INSTALL HASTEBIN +#================================================= + +ynh_use_nodejs +script_dir="$PWD" +pushd "$final_path" +chown -R $app: $final_path +npm install +DATA_PATH="/home/yunohost.app/"$app +mkdir -p $DATA_PATH +chown -R "$app":"$app" $final_path $DATA_PATH + + +# Verify the checksum and backup the file if it's different +ynh_backup_if_checksum_is_different "$final_path/config.js" +# Recalculate and store the config file checksum into the app settings +ynh_store_file_checksum "$final_path/config.js" + +#================================================= +# SETUP LOGROTATE +#================================================= + +# Use logrotate to manage app-specific logfile(s) +ynh_use_logrotate + +#================================================= +# GENERIC FINALIZATION +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================= + +# Set right permissions for curl installation +chown -R root: $final_path + +#================================================= +# SETUP SSOWAT +#================================================= + +if [ $is_public -eq 0 ] +then # Remove the public access + ynh_app_setting_delete $app skipped_uris +fi +# Make app public if necessary +if [ $is_public -eq 1 ] +then + # unprotected_uris allows SSO credentials to be passed anyway + ynh_app_setting_set $app unprotected_uris "/" +fi + +#================================================= +# RELOAD NGINX +#================================================= + +systemctl reload nginx