diff --git a/README.md b/README.md index 0d07035..29c173d 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,23 @@ # Work In Progress : do not install in production -## TODO -- find a way to let user edit the hook and not overwrite it -- better check if gogs is installed and on the repo format -- backup/restore +# Haste for YunoHost -# Gogs web hosting for YunoHost +Haste is an open-source pastebin software written in node.js. +A publicly available version can be found at hastebin.com -This Yunohost App take an existing gogs repository and serve it with nginx - -Gogs is a self-hosted Git service written in Go. Alternative to Github. -- [Gogs website](http://gogs.io) -- [Gogs package for YunoHost](https://github.com/YunoHost-Apps/gogs_ynh) ## Requirements - Functionnal instance of [YunoHost](https://yunohost.org) -- [Gogs package must](https://github.com/YunoHost-Apps/gogs_ynh) be installed -- The repository that you want to serve must exist in Gogs ## Installation From command line: -`sudo yunohost app install -l MySite https://github.com/YunoHost-Apps/gogs_webhost_ynh` +`sudo yunohost app install -l Haste https://github.com/YunoHost-Apps/haste_ynh` ## Upgrade From command line: -`sudo yunohost app upgrade -u https://github.com/YunoHost-Apps/gogs_webhost_ynh gogswebhost` +`sudo yunohost app upgrade -u https://github.com/YunoHost-Apps/haste_ynh haste` ## Infos diff --git a/conf/haste.sh b/conf/haste.sh new file mode 100644 index 0000000..1f38713 --- /dev/null +++ b/conf/haste.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +set -eu + +PASTE_URL="https://YNH_HASTE_URL" + +haste () { + local output returnfile contents + if (( $# == 0 )) && [[ $(printf "%s" "$0" | wc -c) > 0 ]] + then + contents=$0 + elif (( $# != 1 )) || [[ $1 =~ ^(-h|--help)$ ]] + then + echo "Usage: $0 FILE" + echo "Upload contents of plaintext document to hastebin." + echo "\nInvocation with no arguments takes input from stdin or pipe." + echo "Terminate stdin by EOF (Ctrl-D)." + return 1 + elif [[ -e $1 && ! -f $1 ]] + then + echo "Error: Not a regular file." + return 1 + elif [[ ! -e $1 ]] + then + echo "Error: No such file." + return 1 + elif (( $(stat -c %s $1) > (512*1024**1) )) + then + echo "Error: File must be smaller than 512 KiB." + return 1 + fi + if [[ -n "$contents" ]] || [[ $(printf "%s" "$contents" | wc -c) < 1 ]] + then + contents=$(cat $1) + fi + output=$(curl -# -f -XPOST "http://"${PASTE_URL}"/documents" -d"$contents") + if (( $? == 0 )) && [[ $output =~ \"key\" ]] + then + returnfile=$(sed 's/^.*"key":"/http:\/\/'${PASTE_URL}'\//;s/".*$//' <<< "$output") + if [[ -n $returnfile ]] + then + echo "$returnfile" + return 0 + fi + fi + echo "Upload failed." + return 1 +} + +haste diff --git a/manifest.json b/manifest.json index c165399..027dd2b 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "Haste is an open-source pastebin software written in node.js", "fr": "Haste is an open-source pastebin software written in node.js" }, - "url": "https://example.com", + "url": "https://github.com/seejohnrun/haste-server", "license": "free", "maintainer": { "name": "mbugeia", diff --git a/scripts/_common.sh b/scripts/_common.sh index 3e02b90..97fadab 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -3,36 +3,121 @@ # APPNAME="haste" +app=${YNH_APP_INSTANCE_NAME:-haste} -# Gogs version +# Haste version VERSION="master" +# set globals variables +DESTDIR="/opt/"${app} +DATA_PATH="/home/yunohost.app/"$app + # Remote URL to fetch Haste tarball HASTE_URL="https://github.com/seejohnrun/haste-server/archive/"${VERSION}".zip" +# Source YunoHost helpers +source /usr/share/yunohost/helpers + # # Common helpers # +check_or_install_npm() { + if ! dpkg -s npm | grep "installed" > /dev/null 2>&1; then + sudo apt-get update + sudo apt-get install -y npm + fi +} + +pre_inst_haste() { + # retrieve, extract, copy haste, add user if necessary + local TMPDIR=$(mktemp -d) + local HASTE_SOURCE=$1 + + # Check destination directory + [[ -d $DESTDIR ]] && ynh_die \ + "The destination directory '$DESTDIR' already exists.\ + You should safely delete it before restoring this app." + + # 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." + + 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" + + # 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 + + # Configure init script + sudo cp ../conf/"$app".service /etc/systemd/system/ + sudo systemctl daemon-reload + sudo systemctl enable "$app".service + } + # Download, extract and install Haste to the given directory # usage: install_haste DESTDIR install_haste() { - local DESTDIR=$1 - local TMPDIR=$(mktemp -d) + local DOMAIN=$1 + local PATH=$2 + local IS_PUBLIC=$3 - # retrieve, extract, install haste - haste_tarball="/tmp/haste.zip" - rm -f "$haste_tarball" - 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" - rm -rf "$haste_tarball" "$TMPDIR" + check_or_install_npm + pre_inst_haste # install haste current_dir=$(pwd) cd "$DESTDIR" sudo npm install cd $current_dir + + sudo mkdir -p $DATA_PATH + sudo chown -R "$app":"$app" $DESTDIR $DATA_PATH + + # 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 + + # Modify Nginx configuration file and copy it to Nginx conf directory + sed -i "s@PATHTOCHANGE@${path%/}@g" ../conf/nginx.conf + if [ "$path" = "/" ] + then + sed -i "s@COMMENT_IF_ROOT@#@g" ../conf/nginx.conf + else + sed -i "s@COMMENT_IF_ROOT@@g" ../conf/nginx.conf + fi + sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/"$app".conf + + # 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 + + # Reload services + sudo systemctl reload nginx.service + + # 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" } diff --git a/scripts/backup b/scripts/backup new file mode 100755 index 0000000..e5071aa --- /dev/null +++ b/scripts/backup @@ -0,0 +1,22 @@ +#!/bin/bash + +# Exit on command errors and treat unset variables as an error +set -eu + +# Load common variables and functions +source ./_common.sh + +domain=$(ynh_app_setting_get "$app" domain) + +# Copy the app source and data files +ynh_backup "$DESTDIR" "www" +ynh_backup "$DATA_PATH" "data" + +# 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" + +# Copy NGINX configuration +ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf" diff --git a/scripts/install b/scripts/install index eadf898..35de8c2 100755 --- a/scripts/install +++ b/scripts/install @@ -3,69 +3,26 @@ # Exit on command errors and treat unset variables as an error set -eu -app=${YNH_APP_INSTANCE_NAME:-haste} +# Load common variables and functions +source ./_common.sh # Retrieve arguments domain=$YNH_APP_ARG_DOMAIN path=$YNH_APP_ARG_PATH is_public=$YNH_APP_ARG_IS_PUBLIC -DATA_PATH="/home/yunohost.app/"$app - -# Load common variables -source ./_common.sh - -# Source YunoHost helpers -source /usr/share/yunohost/helpers - -# Save app settings -ynh_app_setting_set "$app" is_public "$is_public" - # Check domain/path availability sudo yunohost app checkurl "${domain}${path}" -a "$app" \ || ynh_die "Path not available: ${domain}${path}" -# Add user -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 +# Save app settings +ynh_app_setting_set "$app" is_public "$is_public" # Install the app -DESTDIR="/opt/"${app} -install_haste $DESTDIR -sudo mkdir -p $DATA_PATH -sudo chown -R "$app":"$app" $DESTDIR $DATA_PATH - -# 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 - -# Configure init script -sudo cp ../conf/haste.service /etc/systemd/system/ -sudo systemctl daemon-reload -sudo systemctl enable "$app".service +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 - -# Modify Nginx configuration file and copy it to Nginx conf directory -sed -i "s@PATHTOCHANGE@${path%/}@g" ../conf/nginx.conf -if [ "$path" = "/" ] -then - sed -i "s@COMMENT_IF_ROOT@#@g" ../conf/nginx.conf -else - sed -i "s@COMMENT_IF_ROOT@@g" ../conf/nginx.conf -fi -sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/"$app".conf - -# 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 - -# Reload services -sudo systemctl reload nginx.service diff --git a/scripts/remove b/scripts/remove index 107c5c8..7bf6d53 100755 --- a/scripts/remove +++ b/scripts/remove @@ -1,10 +1,10 @@ #!/bin/bash -# See comments in install script -app=${YNH_APP_INSTANCE_NAME:-haste} +# Exit on command errors and treat unset variables as an error +set -eu -# Source YunoHost helpers -source /usr/share/yunohost/helpers +# Load common variables and functions +source ./_common.sh # Retrieve app settings domain=$(ynh_app_setting_get "$app" domain) @@ -15,10 +15,6 @@ sudo systemctl stop "$app".service # Remove sources sudo rm -rf "/opt/"${app} -# Remove post-receive hook -repo_path="/home/gogs/repositories/"$gogsrepo".git" -sudo rm -f $repo_path"/hooks/post-receive" - # Remove nginx configuration file [[ -n $domain ]] && sudo rm -f "/etc/nginx/conf.d/${domain}.d/${app}.conf" diff --git a/scripts/restore b/scripts/restore new file mode 100755 index 0000000..2b4fada --- /dev/null +++ b/scripts/restore @@ -0,0 +1,28 @@ +#!/bin/bash + +# Exit on command errors and treat unset variables as an error +set -eu + +# Load common variables and functions +source ./_common.sh + +# 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) + +# Check domain/path availability +sudo yunohost app checkurl "${domain}${path}" -a "$app" \ + || ynh_die "Path not available: ${domain}${path}" + +pre_inst_haste backup + +# Restore NGINX configuration +sudo cp -a ./nginx.conf "/etc/nginx/conf.d/${domain}.d/${app}.conf" + +# Restart webserver and app +sudo systemctl reload nginx.service +sudo systemctl start "$app".service + +# Add Haste to YunoHost's monitored services +sudo yunohost service add "$app" --log /var/log/"$app"/"$app".log diff --git a/scripts/upgrade b/scripts/upgrade new file mode 100755 index 0000000..af5a271 --- /dev/null +++ b/scripts/upgrade @@ -0,0 +1,18 @@ +#!/bin/bash + +# Exit on command errors and treat unset variables as an error +set -eu + +# Load common variables and functions +source ./_common.sh + +# 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) + +# Install the app +install_haste $domain $path $is_public + +# Start Haste +sudo systemctl restart "$app".service