1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/haste_ynh.git synced 2024-09-03 20:36:28 +02:00

Major refactor + add upgrade, backup, restore

This commit is contained in:
mbugeia 2016-10-25 23:34:42 +02:00
parent 71c3650f51
commit 9fea11f7c6
9 changed files with 230 additions and 83 deletions

View file

@ -1,32 +1,23 @@
# Work In Progress : do not install in production # 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 ## Requirements
- Functionnal instance of [YunoHost](https://yunohost.org) - 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 ## Installation
From command line: 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 ## Upgrade
From command line: 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 ## Infos

50
conf/haste.sh Normal file
View file

@ -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

View file

@ -6,7 +6,7 @@
"en": "Haste is an open-source pastebin software written in node.js", "en": "Haste is an open-source pastebin software written in node.js",
"fr": "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", "license": "free",
"maintainer": { "maintainer": {
"name": "mbugeia", "name": "mbugeia",

View file

@ -3,36 +3,121 @@
# #
APPNAME="haste" APPNAME="haste"
app=${YNH_APP_INSTANCE_NAME:-haste}
# Gogs version # Haste version
VERSION="master" VERSION="master"
# set globals variables
DESTDIR="/opt/"${app}
DATA_PATH="/home/yunohost.app/"$app
# Remote URL to fetch Haste tarball # Remote URL to fetch Haste tarball
HASTE_URL="https://github.com/seejohnrun/haste-server/archive/"${VERSION}".zip" HASTE_URL="https://github.com/seejohnrun/haste-server/archive/"${VERSION}".zip"
# Source YunoHost helpers
source /usr/share/yunohost/helpers
# #
# Common helpers # Common helpers
# #
# Download, extract and install Haste to the given directory check_or_install_npm() {
# usage: install_haste DESTDIR if ! dpkg -s npm | grep "installed" > /dev/null 2>&1; then
install_haste() { sudo apt-get update
local DESTDIR=$1 sudo apt-get install -y npm
local TMPDIR=$(mktemp -d) 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."
# retrieve, extract, install haste
haste_tarball="/tmp/haste.zip" haste_tarball="/tmp/haste.zip"
rm -f "$haste_tarball" 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" \ wget -q -O "$haste_tarball" "$HASTE_URL" \
|| ynh_die "Unable to download haste" || ynh_die "Unable to download haste"
unzip -q "$haste_tarball" -d "$TMPDIR" \ unzip -q "$haste_tarball" -d "$TMPDIR" \
|| ynh_die "Unable to extract haste" || ynh_die "Unable to extract haste"
sudo rsync -a "$TMPDIR"/haste-server-master/* "$DESTDIR" sudo rsync -a "$TMPDIR"/haste-server-master/* "$DESTDIR"
fi
rm -rf "$haste_tarball" "$TMPDIR" 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 DOMAIN=$1
local PATH=$2
local IS_PUBLIC=$3
check_or_install_npm
pre_inst_haste
# install haste # install haste
current_dir=$(pwd) current_dir=$(pwd)
cd "$DESTDIR" cd "$DESTDIR"
sudo npm install sudo npm install
cd $current_dir 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"
} }

22
scripts/backup Executable file
View file

@ -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"

View file

@ -3,69 +3,26 @@
# Exit on command errors and treat unset variables as an error # Exit on command errors and treat unset variables as an error
set -eu set -eu
app=${YNH_APP_INSTANCE_NAME:-haste} # Load common variables and functions
source ./_common.sh
# Retrieve arguments # Retrieve arguments
domain=$YNH_APP_ARG_DOMAIN domain=$YNH_APP_ARG_DOMAIN
path=$YNH_APP_ARG_PATH path=$YNH_APP_ARG_PATH
is_public=$YNH_APP_ARG_IS_PUBLIC 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 # Check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" \ sudo yunohost app checkurl "${domain}${path}" -a "$app" \
|| ynh_die "Path not available: ${domain}${path}" || ynh_die "Path not available: ${domain}${path}"
# Add user # Save app settings
id -g "$app" &>/dev/null || sudo addgroup "$app" --system --quiet ynh_app_setting_set "$app" is_public "$is_public"
id -u "$app" &>/dev/null || sudo adduser "$app" \
--ingroup "$app" --system --quiet --shell /bin/bash
# Install the app # Install the app
DESTDIR="/opt/"${app} install_haste $domain $path $is_public
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
# Start Haste # Start Haste
sudo systemctl start "$app".service sudo systemctl start "$app".service
# Add Haste to YunoHost's monitored services # Add Haste to YunoHost's monitored services
sudo yunohost service add "$app" --log /var/log/"$app"/"$app".log 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

View file

@ -1,10 +1,10 @@
#!/bin/bash #!/bin/bash
# See comments in install script # Exit on command errors and treat unset variables as an error
app=${YNH_APP_INSTANCE_NAME:-haste} set -eu
# Source YunoHost helpers # Load common variables and functions
source /usr/share/yunohost/helpers source ./_common.sh
# Retrieve app settings # Retrieve app settings
domain=$(ynh_app_setting_get "$app" domain) domain=$(ynh_app_setting_get "$app" domain)
@ -15,10 +15,6 @@ sudo systemctl stop "$app".service
# Remove sources # Remove sources
sudo rm -rf "/opt/"${app} 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 # Remove nginx configuration file
[[ -n $domain ]] && sudo rm -f "/etc/nginx/conf.d/${domain}.d/${app}.conf" [[ -n $domain ]] && sudo rm -f "/etc/nginx/conf.d/${domain}.d/${app}.conf"

28
scripts/restore Executable file
View file

@ -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

18
scripts/upgrade Executable file
View file

@ -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