1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/zeronet_ynh.git synced 2024-09-03 17:46:12 +02:00

Use latest helpers

This commit is contained in:
anmol 2020-11-09 17:57:06 +05:30
parent 33e6d5c4a6
commit d0ef0cd3bb
15 changed files with 836 additions and 354 deletions

View file

@ -1,10 +1,10 @@
# ZeroNet for YunoHost # ZeroNet for YunoHost
[![Integration level](https://dash.yunohost.org/integration/zeronet.svg)](https://dash.yunohost.org/appci/app/zeronet) ![](https://ci-apps.yunohost.org/ci/badges/zeronet.status.svg) ![](https://ci-apps.yunohost.org/ci/badges/zeronet.maintain.svg) [![Integration level](https://dash.yunohost.org/integration/zeronet.svg)](https://dash.yunohost.org/appci/app/zeronet) ![](https://ci-apps.yunohost.org/ci/badges/zeronet.status.svg) ![](https://ci-apps.yunohost.org/ci/badges/zeronet.maintain.svg)
[![Install ZeroNet with YunoHost](https://install-app.yunohost.org/install-with-yunohost.png)](https://install-app.yunohost.org/?app=zeronet) [![Install ZeroNet with YunoHost](https://install-app.yunohost.org/install-with-yunohost.png)](https://install-app.yunohost.org/?app=zeronet)
*[Lire ce readme en français.](./README_fr.md)* *[Lire ce readme en français.](./README_fr.md)*
> *This package allows you to install REPLACEBYYOURAPP quickly and simply on a YunoHost server. > *This package allows you to install ZeroNet quickly and simply on a YunoHost server.
If you don't have YunoHost, please consult [the guide](https://yunohost.org/#/install) to learn how to install it.* If you don't have YunoHost, please consult [the guide](https://yunohost.org/#/install) to learn how to install it.*
## Overview ## Overview
@ -55,7 +55,7 @@ Can the app be used by multiple users?
* Other info you would like to add about this app. * Other info you would like to add about this app.
**More info on the documentation page:** **More info on the documentation page:**
https://yunohost.org/packaging_apps https://yunohost.org/packaging_apps

View file

@ -13,14 +13,14 @@
pkg_linter=1 pkg_linter=1
setup_sub_dir=0 setup_sub_dir=0
# setup_sub_dir=0 test disabled because app is always installed on / # setup_sub_dir=0 test disabled because app is always installed on /
setup_root=1 setup_root=1
setup_nourl=0 setup_nourl=0
setup_private=0 setup_private=0
# setup_private=0 test disabled because app is always installed as private # setup_private=0 test disabled because app is always installed as private
setup_public=0 setup_public=0
upgrade=1 upgrade=1
backup_restore=1 backup_restore=1
multi_instance=0 multi_instance=0
# multi_instance=1 test disabled because it requires installation on sub-path which is not available. successfully conducted test manually # multi_instance=1 test disabled because it requires installation on sub-path which is not available. successfully conducted test manually
wrong_user=1 wrong_user=1
wrong_path=0 wrong_path=0
@ -33,8 +33,8 @@
Level 1=auto Level 1=auto
Level 2=auto Level 2=auto
Level 3=auto Level 3=auto
Level 4=na Level 4=na
# level 4: the app does not directly use Yunohost user data # level 4: the app does not directly use Yunohost user data
Level 5=auto Level 5=auto
Level 6=auto Level 6=auto
Level 7=auto Level 7=auto

View file

@ -1,3 +1,7 @@
SOURCE_URL=https://github.com/HelloZeroNet/ZeroNet/archive/v0.7.1.tar.gz SOURCE_URL=https://github.com/HelloZeroNet/ZeroNet/archive/v0.7.1.tar.gz
SOURCE_SUM=78a27e1687d8e3699a854b77b516c95b30a8ba667f7ebbef0aabf7ec6ec7272d SOURCE_SUM=78a27e1687d8e3699a854b77b516c95b30a8ba667f7ebbef0aabf7ec6ec7272d
SOURCE_VERSION=0.7.1 SOURCE_SUM_PRG=sha256sum
SOURCE_FORMAT=tar.gz
SOURCE_IN_SUBDIR=true
SOURCE_FILENAME=0.7.1
SOURCE_EXTRACT=true

9
conf/nginx.conf Normal file
View file

@ -0,0 +1,9 @@
location / {
proxy_pass http://127.0.0.1:__PORT__;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}

View file

@ -1,14 +0,0 @@
location YNH_WWW_PATH {
proxy_pass http://127.0.0.1:YNH_LOCAL_PORT;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
# Include SSOWAT user panel.
include conf.d/yunohost_panel.conf.inc;
}

14
conf/systemd.service Normal file
View file

@ -0,0 +1,14 @@
[Unit]
Description=__APP__ service
After=network.target
[Service]
User=noreply
ExecStart=/usr/bin/python3 __FINALPATH__/zeronet.py --ui_port __PORT__ --ui_host __DOMAIN__ --fileserver_port __FS_PORT__ --data_dir __DATADIR__/data --log_dir __DATADIR__/log
WorkingDirectory=/var/www/ZeroNet-py3
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target

View file

@ -1,14 +0,0 @@
[Unit]
Description=ZeroNet for __USER__
After=network.target
[Service]
User=__USER__
ExecStart=/usr/bin/python2 ./zeronet.py --ui_port __UI_PORT__ --ui_host __UI_HOST__ --fileserver_port __FS_PORT__ --data_dir __DATA_DIR__ --log_dir __LOG_DIR__
WorkingDirectory=__WORKING_DIRECTORY__
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target

20
scripts/_common.sh Normal file
View file

@ -0,0 +1,20 @@
#!/bin/bash
#=================================================
# COMMON VARIABLES
#=================================================
# dependencies used by the app
pkg_dependencies="python-msgpack python-gevent"
#=================================================
# PERSONAL HELPERS
#=================================================
#=================================================
# EXPERIMENTAL HELPERS
#=================================================
#=================================================
# FUTURE OFFICIAL HELPERS
#=================================================

View file

@ -1,24 +1,68 @@
#!/usr/bin/env bash #!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
main() { #=================================================
ynh_abort_if_errors # MANAGE SCRIPT FAILURE
#=================================================
local app=$YNH_APP_INSTANCE_NAME ynh_clean_setup () {
local backup_dir=$YNH_APP_BACKUP_DIR ### Remove this function if there's nothing to clean before calling the remove script.
local deploy_path=$( ynh_app_setting_get $app deploy_path ) true
local nginx_config_file=$( ynh_app_setting_get $app nginx_config_file )
local systemd_service_name=$( ynh_app_setting_get $app systemd_service_name )
local systemd_service_file=$( ynh_app_setting_get $app systemd_service_file )
local user_zeronet_dir=$( ynh_app_setting_get $app user_zeronet_dir )
ynh_backup $deploy_path "sources"
mkdir --parent ./conf
ynh_backup $nginx_config_file "conf/nginx.conf"
ynh_backup $systemd_service_file "conf/${systemd_service_name}"
ynh_backup $user_zeronet_dir "conf/.zeronet"
} }
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
main #=================================================
# LOAD SETTINGS
#=================================================
ynh_print_info --message="Loading installation settings..."
app=$YNH_APP_INSTANCE_NAME
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
domain=$(ynh_app_setting_get --app=$app --key=domain)
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
#=================================================
# DECLARE DATA AND CONF FILES TO BACKUP
#=================================================
ynh_print_info --message="Declaring files to be backed up..."
### N.B. : the following 'ynh_backup' calls are only a *declaration* of what needs
### to be backuped and not an actual copy of any file. The actual backup that
### creates and fill the archive with the files happens in the core after this
### script is called. Hence ynh_backups calls takes basically 0 seconds to run.
#=================================================
# BACKUP THE APP MAIN DIR
#=================================================
ynh_backup --src_path="$final_path"
ynh_backup --src_path="$datadir"
#=================================================
# BACKUP THE NGINX CONFIGURATION
#=================================================
ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf"
#=================================================
# BACKUP SYSTEMD
#=================================================
ynh_backup --src_path="/etc/systemd/system/$app.service"
#=================================================
# END OF SCRIPT
#=================================================
ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)."

143
scripts/change_url Normal file
View file

@ -0,0 +1,143 @@
#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
old_domain=$YNH_APP_OLD_DOMAIN
old_path=$YNH_APP_OLD_PATH
new_domain=$YNH_APP_NEW_DOMAIN
new_path=$YNH_APP_NEW_PATH
app=$YNH_APP_INSTANCE_NAME
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..."
# Needed for helper "ynh_add_nginx_config"
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
port=$(ynh_app_setting_get --app=$app --key=port)
fs_port=$(ynh_app_setting_get --app=$app --key=fs_port)
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
# Add settings here as needed by your application
#db_name=$(ynh_app_setting_get --app=$app --key=db_name)
#db_user=$db_name
#db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd)
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before changing its url (may take a while)..."
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
# Remove the new domain config file, the remove script won't do it as it doesn't know yet its location.
ynh_secure_remove --file="/etc/nginx/conf.d/$new_domain.d/$app.conf"
# 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 WHICH PARTS SHOULD BE CHANGED
#=================================================
change_domain=0
if [ "$old_domain" != "$new_domain" ]
then
change_domain=1
fi
change_path=0
if [ "$old_path" != "$new_path" ]
then
change_path=1
fi
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..."
ynh_systemd_action --service_name=$app --action="stop" --log_path="$datadir/log/debug-last.log"
#=================================================
# MODIFY URL IN NGINX CONF
#=================================================
ynh_script_progression --message="Updating nginx web server configuration..."
nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf
# Change the path in the nginx config file
if [ $change_path -eq 1 ]
then
# Make a backup of the original nginx config file if modified
ynh_backup_if_checksum_is_different --file="$nginx_conf_path"
# Set global variables for nginx helper
domain="$old_domain"
path_url="$new_path"
# Create a dedicated nginx config
ynh_add_nginx_config
fi
# Change the domain for nginx
if [ $change_domain -eq 1 ]
then
# Delete file checksum for the old conf file location
ynh_delete_file_checksum --file="$nginx_conf_path"
mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf
# Store file checksum for the new config file location
ynh_store_file_checksum --file="/etc/nginx/conf.d/$new_domain.d/$app.conf"
fi
#=================================================
# SPECIFIC MODIFICATIONS
#=================================================
# ...
#=================================================
#=================================================
# GENERIC FINALISATION
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add $app --description "$app service" --others_var="$fs_port $datadir" --log "$datadir/log/debug-last.log" --needs_exposed_ports "$fs_port"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_systemd_action --service_name=$app --action="start" --log_path="$datadir/log/debug-last.log"
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Change of URL completed for $app" --time --last

View file

@ -1,65 +0,0 @@
#!/usr/bin/env bash
set -eu
install_dependencies() {
apt-get install --quiet --yes python-msgpack python-gevent
}
app_config_get() {
local app_config=$1
local attribute=$2
cat ${app_config} \
| grep ${attribute} \
| cut --delimiter== --fields=2
}
make_deploy_path() {
local app=$1
local source_version=$2
echo "/var/www/${app}-v${source_version}" # e.g. /var/www/zeronet__2-0.5.3
}
download_file() {
local url=$1
local output_document=$2
wget --no-verbose --output-document=${output_document} ${url}
}
check_file_integrity() {
local file=$1
local expected_checksum=$2
echo "${expected_checksum} ${file}" | sha256sum --check --status \
|| ynh_die "Corrupt source!"
}
extract_archive() {
local src_file=$1
local deploy_path=$2
mkdir --parents ${deploy_path}
tar --extract --file=${src_file} --directory=${deploy_path} --overwrite --strip-components 1
}
obtain_and_deploy_source() {
local app_config=$1
local deploy_path=$2
local symlink_to_deploy_path=$3
local user=$4
local src_url=$(app_config_get $app_config "SOURCE_URL")
local src_checksum=$(app_config_get $app_config "SOURCE_SUM")
local src_file="/tmp/source.tar.gz"
download_file $src_url $src_file
check_file_integrity $src_file $src_checksum
extract_archive $src_file $deploy_path
rm -f $symlink_to_deploy_path
ln --symbolic --force $deploy_path $symlink_to_deploy_path
chown $user: -LR $symlink_to_deploy_path
chown $user: -h $symlink_to_deploy_path
}

View file

@ -1,155 +1,246 @@
#!/usr/bin/env bash #!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
source functions.sh
nginx_config_file() { #=================================================
local app=$1 # MANAGE SCRIPT FAILURE
local domain=$2 #=================================================
mkdir --parents "/etc/nginx/conf.d/${domain}.d" ynh_clean_setup () {
### Remove this function if there's nothing to clean before calling the remove script.
echo "/etc/nginx/conf.d/${domain}.d/${app}.conf" true
} }
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
update_nginx_configuration() { #=================================================
local app=$1 # RETRIEVE ARGUMENTS FROM THE MANIFEST
local config_template=$2 #=================================================
local config_file=$3
local path=$4
local ui_port=$5
cp $config_template $config_file domain=$YNH_APP_ARG_DOMAIN
path_url="/"
admin=$YNH_APP_ARG_ADMIN
datadir="/home/yunohost.app/${app}"
sed --in-place "s@YNH_WWW_PATH@${path}@g" ${config_file} ### If it's a multi-instance app, meaning it can be installed several times independently
sed --in-place "s@YNH_LOCAL_PORT@${ui_port}/@g" ${config_file} ### 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", ...)
### The app instance name is available as $YNH_APP_INSTANCE_NAME
### - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample
### - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2
### - ynhexample__{N} for the subsequent installations, with N=3,4, ...
### The app instance name is probably what interests you most, since this is
### guaranteed to be unique. This is a good unique identifier to define installation path,
### db names, ...
app=$YNH_APP_INSTANCE_NAME
service nginx reload #=================================================
} # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
### About --weight and --time
### ynh_script_progression will show to your final users the progression of each scripts.
### In order to do that, --weight will represent the relative time of execution compared to the other steps in the script.
### --time is a packager option, it will show you the execution time since the previous call.
### This option should be removed before releasing your app.
### Use the execution time, given by --time, to estimate the weight of a step.
### A common way to do it is to set a weight equal to the execution time in second +1.
### The execution time is given for the duration since the previous call. So the weight should be applied to this previous call.
ynh_script_progression --message="Validating installation parameters..."
systemd_service_name() { ### If the app uses nginx as web server (written in HTML/PHP in most cases), the final path should be "/var/www/$app".
local app=$1 ### If the app provides an internal web server (or uses another application server such as uwsgi), the final path should be "/opt/yunohost/$app"
local user=$2 final_path=/var/www/$app
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
echo "ynh-${app}-${user}.service" # Register (book) web path
} ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
# #=================================================
# Creates service which runs zeronet on unique # STORE SETTINGS FROM MANIFEST
# UserInterace and FileServer port for the user #=================================================
# ZeroNet data is stored in the users home directory in ~/.zeronet ynh_script_progression --message="Storing installation settings..."
#
update_systemd_configuration() {
local app=$1
local service_template=$2
local service_name=$3
local service_file=$4
local user=$5
local ui_port=$6
local symlink_to_deploy_path=$7
local zeronet_dir=$8
local ui_host=$9
local fs_port=${10} # only works with bash
# create zeronet user data location ynh_app_setting_set --app=$app --key=domain --value=$domain
local data_dir=$zeronet_dir/data ynh_app_setting_set --app=$app --key=path --value=$path_url
local log_dir=$zeronet_dir/log ynh_app_setting_set --app=$app --key=admin --value=$admin
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
mkdir -p $data_dir
mkdir -p $log_dir
chown -R $user: $zeronet_dir
# configure systemd service #=================================================
cp $service_template $service_file # STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Configuring firewall..."
sed --in-place "s@__USER__@$user@g" $service_file ### Use these lines if you have to open a port for the application
sed --in-place "s@__UI_PORT__@$ui_port@g" $service_file ### `ynh_find_port` will find the first available port starting from the given port.
sed --in-place "s@__FS_PORT__@$fs_port@g" $service_file ### If you're not using these lines:
sed --in-place "s@__UI_HOST__@$ui_host@g" $service_file ### - Remove the section "CLOSE A PORT" in the remove script
sed --in-place "s@__APP_NAME__@$app@g" $service_file
sed --in-place "s@__WORKING_DIRECTORY__@$symlink_to_deploy_path@g" $service_file
sed --in-place "s@__DATA_DIR__@$data_dir@g" $service_file
sed --in-place "s@__LOG_DIR__@$log_dir@g" $service_file
systemctl daemon-reload # Find an available port
systemctl enable $service_name port=$(ynh_find_port --port=43110)
systemctl start $service_name fs_port=$(ynh_find_port --port=15441)
} ynh_app_setting_set --app=$app --key=port --value=$port
ynh_app_setting_set --app=$app --key=fs_port --value=$fs_port
# Source: https://github.com/YunoHost/yunohost/blob/901e3df9b604f542f2c460aad05bcc8efc9fd054/data/helpers.d/network # Optional: Expose this port publicly
# # (N.B. : you only need to do this if the app actually needs to expose the port publicly.
# Find a free port and return it # If you do this and the app doesn't actually need you are CREATING SECURITY HOLES IN THE SERVER !)
#
# example: port=$(ynh_find_port 8080)
#
# usage: ynh_find_port begin_port
# | arg: begin_port - port to start to search
ynh_find_port () {
port=$1
while netcat -z 127.0.0.1 $port # Check if the port is free # Open the port
do ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $fs_port
port=$((port+1)) # Else, pass to next port
done
echo $port
}
main() { #=================================================
ynh_abort_if_errors # INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..."
### `ynh_install_app_dependencies` allows you to add any "apt" dependencies to the package.
### Those deb packages will be installed as dependencies of this package.
### If you're not using this helper:
### - Remove the section "REMOVE DEPENDENCIES" in the remove script
### - Remove the variable "pkg_dependencies" in _common.sh
### - As well as the section "REINSTALL DEPENDENCIES" in the restore script
### - And the section "UPGRADE DEPENDENCIES" in the upgrade script
local app=$YNH_APP_INSTANCE_NAME ynh_install_app_dependencies $pkg_dependencies
local number=$YNH_APP_INSTANCE_NUMBER
local domain=$YNH_APP_ARG_DOMAIN
local user=$YNH_APP_ARG_ADMIN
local ui_port=$( ynh_find_port 43110 ) # the port zeronet ui listens on #=================================================
local fs_port=$( ynh_find_port 15441 ) # the port zeronet fileserver listens on # DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Setting up source files..."
local path="/" ### `ynh_setup_source` is used to install an app from a zip or tar.gz file,
local is_public=0 # by defaut access to zeronet is private ### downloaded from an upstream source, like a git repository.
local nginx_config_template=../conf/nginx.conf.template ### `ynh_setup_source` use the file conf/app.src
local systemd_service_template=../conf/systemd.service.template
local app_config=../conf/app.src
local systemd_service_dir=/etc/systemd/system
local url=$domain$path ynh_app_setting_set --app=$app --key=final_path --value=$final_path
local source_version=$( app_config_get $app_config "SOURCE_VERSION" ) # Download, check integrity, uncompress and patch the source from app.src
local systemd_service_name=$( systemd_service_name $app $user ) ynh_setup_source --dest_dir="$final_path"
local systemd_service_file="${systemd_service_dir}/${systemd_service_name}"
local nginx_config_file=$( nginx_config_file $app $domain )
local user_zeronet_dir="/home/${user}/.zeronet"
local deploy_path=$( make_deploy_path $app $source_version )
local symlink_to_deploy_path="/var/www/${app}"
ynh_webpath_available $domain $path #=================================================
ynh_webpath_register $app $domain $path # NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring nginx web server..."
ynh_user_exists $user ### `ynh_add_nginx_config` will use the file conf/nginx.conf
ynh_app_setting_set $app is_public $is_public # Create a dedicated nginx config
ynh_app_setting_set $app domain $domain ynh_add_nginx_config
ynh_app_setting_set $app path $path
ynh_app_setting_set $app url $url
ynh_app_setting_set $app admin $user
ynh_app_setting_set $app allowed_users $user
ynh_app_setting_set $app deploy_path $deploy_path
ynh_app_setting_set $app symlink_to_deploy_path $symlink_to_deploy_path
ynh_app_setting_set $app ui_port $ui_port
ynh_app_setting_set $app fs_port $fs_port
ynh_app_setting_set $app installed_version $source_version
ynh_app_setting_set $app nginx_config_file $nginx_config_file
ynh_app_setting_set $app user $user
ynh_app_setting_set $app user_zeronet_dir $user_zeronet_dir
ynh_app_setting_set $app systemd_service_name $systemd_service_name
ynh_app_setting_set $app systemd_service_file $systemd_service_file
install_dependencies #=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Configuring system user..."
obtain_and_deploy_source $app_config $deploy_path $symlink_to_deploy_path $user # Create a system user
ynh_system_user_create --username=$app
update_nginx_configuration $app $nginx_config_template $nginx_config_file $path $ui_port #=================================================
update_systemd_configuration $app $systemd_service_template $systemd_service_name $systemd_service_file \ # SETUP SYSTEMD
$user $ui_port $symlink_to_deploy_path $user_zeronet_dir $domain $fs_port #=================================================
} ynh_script_progression --message="Configuring a systemd service..."
### `ynh_systemd_config` is used to configure a systemd script for an app.
### It can be used for apps that use sysvinit (with adaptation) or systemd.
### Have a look at the app to be sure this app needs a systemd script.
### `ynh_systemd_config` will use the file conf/systemd.service
### If you're not using these lines:
### - You can remove those files in conf/.
### - Remove the section "BACKUP SYSTEMD" in the backup script
### - Remove also the section "STOP AND REMOVE SERVICE" in the remove script
### - As well as the section "RESTORE SYSTEMD" in the restore script
### - And the section "SETUP SYSTEMD" in the upgrade script
main # Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# SETUP APPLICATION WITH CURL
#=================================================
### Use these lines only if the app installation needs to be finalized through
### web forms. We generally don't want to ask the final user,
### so we're going to use curl to automatically fill the fields and submit the
### forms.
# Set right permissions for curl install
mkdir -p $datadir/data
mkdir -p $datadir/log
chown -R $app: $final_path
chown -R $app: $datadir
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
### `yunohost service add` integrates a service in YunoHost. It then gets
### displayed in the admin interface and through the others `yunohost service` commands.
### (N.B. : this line only makes sense if the app adds a service to the system!)
### If you're not using these lines:
### - You can remove these files in conf/.
### - Remove the section "REMOVE SERVICE INTEGRATION IN YUNOHOST" in the remove script
### - As well as the section "INTEGRATE SERVICE IN YUNOHOST" in the restore script
### - And the section "INTEGRATE SERVICE IN YUNOHOST" in the upgrade script
yunohost service add $app --description "$app service" --others_var="$fs_port $datadir" --log "$datadir/log/debug-last.log" --needs_exposed_ports "$fs_port"
### Additional options starting with 3.8:
###
### --needs_exposed_ports "$port" a list of ports that needs to be publicly exposed
### which will then be checked by YunoHost's diagnosis system
### (N.B. DO NOT USE THIS is the port is only internal !!!)
###
### --test_status "some command" a custom command to check the status of the service
### (only relevant if 'systemctl status' doesn't do a good job)
###
### --test_conf "some command" some command similar to "nginx -t" that validates the conf of the service
###
### Re-calling 'yunohost service add' during the upgrade script is the right way
### to proceed if you later realize that you need to enable some flags that
### weren't enabled on old installs (be careful it'll override the existing
### service though so you should re-provide all relevant flags when doing so)
###
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..."
### `ynh_systemd_action` is used to start a systemd service for an app.
### Only needed if you have configure a systemd service
### If you're not using these lines:
### - Remove the section "STOP SYSTEMD SERVICE" and "START SYSTEMD SERVICE" in the backup script
### - As well as the section "START SYSTEMD SERVICE" in the restore script
### - As well as the section"STOP SYSTEMD SERVICE" and "START SYSTEMD SERVICE" in the upgrade script
### - And the section "STOP SYSTEMD SERVICE" and "START SYSTEMD SERVICE" in the change_url script
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="$datadir/log/debug-last.log"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Configuring SSOwat..."
# Restrict access to admin only
yunohost app addaccess --users=$admin $app
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Installation of $app completed"

View file

@ -1,57 +1,95 @@
#!/usr/bin/env bash #!/bin/bash
set -u
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
remove_systemd_service_() { #=================================================
local service_name=$1 # LOAD SETTINGS
local service_file=$2 #=================================================
ynh_script_progression --message="Loading installation settings..."
systemctl stop $service_name app=$YNH_APP_INSTANCE_NAME
systemctl disable $service_name
rm $service_file
systemctl daemon-reload
systemctl reset-failed
}
remove_nginx_config() { domain=$(ynh_app_setting_get --app=$app --key=domain)
local nginx_config_file=$1 port=$(ynh_app_setting_get --app=$app --key=port)
fs_port=$(ynh_app_setting_get --app=$app --key=fs_port)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
ynh_secure_remove $nginx_config_file #=================================================
service nginx reload # STANDARD REMOVE
} #=================================================
# REMOVE SERVICE INTEGRATION IN YUNOHOST
#=================================================
remove_user_data() { # Remove the service from the list of services known by Yunohost (added from `yunohost service add`)
local user_zeronet_dir=$1 if ynh_exec_warn_less yunohost service status $app >/dev/null
then
ynh_script_progression --message="Removing $app service integration..."
yunohost service remove $app
fi
ynh_secure_remove $user_zeronet_dir #=================================================
} # STOP AND REMOVE SERVICE
#=================================================
ynh_script_progression --message="Stopping and removing the systemd service..."
remove_zeronet() { # Remove the dedicated systemd config
local deploy_path=$1 ynh_remove_systemd_config
local symlink_to_deploy_path=$2
ynh_secure_remove $deploy_path #=================================================
ynh_secure_remove $symlink_to_deploy_path # REMOVE DEPENDENCIES
} #=================================================
ynh_script_progression --message="Removing dependencies..."
main() { # Remove metapackage and its dependencies
local app=${YNH_APP_INSTANCE_NAME} ynh_remove_app_dependencies
local domain=$( ynh_app_setting_get $app domain )
local user=$( ynh_app_setting_get $app admin )
local nginx_config_file=$( ynh_app_setting_get $app nginx_config_file )
local systemd_service_name=$( ynh_app_setting_get $app systemd_service_name )
local systemd_service_file=$( ynh_app_setting_get $app systemd_service_file )
local deploy_path=$( ynh_app_setting_get $app deploy_path )
local symlink_to_deploy_path=$( ynh_app_setting_get $app symlink_to_deploy_path )
local user_zeronet_dir=$( ynh_app_setting_get $app user_zeronet_dir )
if [ -n $domain ]; then #=================================================
remove_nginx_config $nginx_config_file # REMOVE APP MAIN DIR
remove_user_data $user_zeronet_dir #=================================================
remove_systemd_service_ $systemd_service_name $systemd_service_file ynh_script_progression --message="Removing app main directory..."
remove_zeronet $deploy_path $symlink_to_deploy_path
fi
}
main # Remove the app directory securely
ynh_secure_remove --file="$final_path"
ynh_secure_remove --file="$datadir"
#=================================================
# REMOVE NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Removing nginx web server configuration..."
# Remove the dedicated nginx config
ynh_remove_nginx_config
#=================================================
# CLOSE A PORT
#=================================================
if yunohost firewall list | grep -q "\- $fs_port$"
then
ynh_script_progression --message="Closing port $port..."
ynh_exec_warn_less yunohost firewall disallow TCP $fs_port
fi
#=================================================
# GENERIC FINALIZATION
#=================================================
# REMOVE DEDICATED USER
#=================================================
ynh_script_progression --message="Removing the dedicated system user..."
# Delete a system user
ynh_system_user_delete --username=$app
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Removal of $app completed"

View file

@ -1,61 +1,134 @@
#!/usr/bin/env bash #!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# TODO: enable with fix of https://github.com/YunoHost/yunohost/pull/246 #=================================================
# source .hfunctions # MANAGE SCRIPT FAILURE
#=================================================
# TODO: remove with fix of https://github.com/YunoHost/yunohost/pull/246 ynh_clean_setup () {
install_dependencies() { #### Remove this function if there's nothing to clean before calling the remove script.
apt-get install --quiet --yes python-msgpack python-gevent true
} }
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
is_app_restorable() { #=================================================
local -r domain=$1 # LOAD SETTINGS
local -r path=$2 #=================================================
local -r deploy_path=$3 ynh_script_progression --message="Loading installation settings..."
ynh_webpath_available $domain $path \ app=$YNH_APP_INSTANCE_NAME
|| ynh_die "Path not available: ${domain}${path}"
test ! -d $deploy_path \ domain=$(ynh_app_setting_get --app=$app --key=domain)
|| ynh_die "There is already a directory: $deploy_path" path_url=$(ynh_app_setting_get --app=$app --key=path)
} admin=$(ynh_app_setting_get --app=$app --key=admin)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
port=$(ynh_app_setting_get --app=$app --key=port)
fs_port=$(ynh_app_setting_get --app=$app --key=fs_port)
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
main() { #=================================================
ynh_abort_if_errors # CHECK IF THE APP CAN BE RESTORED
#=================================================
ynh_script_progression --message="Validating restoration parameters..."
local app=$YNH_APP_INSTANCE_NAME ynh_webpath_available --domain=$domain --path_url=$path_url \
local domain=$(ynh_app_setting_get $app domain) || ynh_die --message="Path not available: ${domain}${path_url}"
local path=$(ynh_app_setting_get $app path) test ! -d $final_path \
local deploy_path=$(ynh_app_setting_get $app deploy_path) || ynh_die --message="There is already a directory: $final_path "
local symlink_to_deploy_path=$(ynh_app_setting_get $app symlink_to_deploy_path)
local nginx_config_file=$( ynh_app_setting_get $app nginx_config_file )
local systemd_service_name=$( ynh_app_setting_get $app systemd_service_name )
local systemd_service_file=$( ynh_app_setting_get $app systemd_service_file )
local user=$( ynh_app_setting_get $app user )
local user_zeronet_dir=$( ynh_app_setting_get $app user_zeronet_dir )
local url=$(ynh_app_setting_get $app url) #=================================================
# STANDARD RESTORATION STEPS
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
is_app_restorable $domain $path $deploy_path ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
install_dependencies #=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_script_progression --message="Restoring the app main directory..."
cp -a "./sources" $deploy_path ynh_restore_file --origin_path="$final_path"
ln --symbolic --force $deploy_path $symlink_to_deploy_path ynh_restore_file --origin_path="$datadir"
chown $user: -LR $symlink_to_deploy_path
chown $user: -h $symlink_to_deploy_path
ynh_secure_remove $user_zeronet_dir #=================================================
cp -aR "./conf/.zeronet" $user_zeronet_dir # RECREATE THE DEDICATED USER
chown -R $user: $user_zeronet_dir #=================================================
ynh_script_progression --message="Recreating the dedicated system user..."
cp -a "./conf/nginx.conf" $nginx_config_file
service nginx reload
cp -a "./conf/${systemd_service_name}" $systemd_service_file
systemctl restart $systemd_service_name
}
main # Create the dedicated user (if not existing)
ynh_system_user_create --username=$app
#=================================================
# RESTORE USER RIGHTS
#=================================================
# Restore permissions on app files
chown -R $app: $final_path
chown -R $app: $datadir
#=================================================
# SPECIFIC RESTORATION
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Reinstalling dependencies..."
# Define and install dependencies
ynh_install_app_dependencies $pkg_dependencies
#=================================================
# RESTORE SYSTEMD
#=================================================
ynh_script_progression --message="Restoring the systemd configuration..."
ynh_restore_file --origin_path="/etc/systemd/system/$app.service"
systemctl enable $app.service
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add $app --description "$app service" --others_var="$fs_port $datadir" --log "$datadir/log/debug-last.log" --needs_exposed_ports "$fs_port"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..."
ynh_systemd_action --service_name=$app --action="start" --log_path="$datadir/log/debug-last.log"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Upgrading SSOwat configuration..."
# Restrict access to admin only
yunohost app addaccess --users=$admin $app
#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server and php-fpm..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Restoration completed for $app"

View file

@ -1,33 +1,172 @@
#!/usr/bin/env bash #!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
source functions.sh
main() { #=================================================
ynh_abort_if_errors # LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..."
local app=${YNH_APP_INSTANCE_NAME} app=$YNH_APP_INSTANCE_NAME
local user=$( ynh_app_setting_get $app user )
local deploy_path=$( ynh_app_setting_get $app deploy_path )
local symlink_to_deploy_path=$( ynh_app_setting_get $app symlink_to_deploy_path )
local installed_version=$( ynh_app_setting_get $app installed_version )
local systemd_service_name=$( ynh_app_setting_get $app systemd_service_name )
local app_config=../conf/app.src
local source_version=$(app_config_get $app_config "SOURCE_VERSION")
local old_deploy_path=$deploy_path domain=$(ynh_app_setting_get --app=$app --key=domain)
local new_deploy_path=$( make_deploy_path $app $source_version ) path_url=$(ynh_app_setting_get --app=$app --key=path)
admin=$(ynh_app_setting_get --app=$app --key=admin)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
port=$(ynh_app_setting_get --app=$app --key=port)
fs_port=$(ynh_app_setting_get --app=$app --key=fs_port)
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
obtain_and_deploy_source $app_config $new_deploy_path $symlink_to_deploy_path $user #=================================================
# CHECK VERSION
#=================================================
systemctl restart $systemd_service_name ### This helper will compare the version of the currently installed app and the version of the upstream package.
### $upgrade_type can have 2 different values
### - UPGRADE_APP if the upstream app version has changed
### - UPGRADE_PACKAGE if only the YunoHost package has changed
### ynh_check_app_version_changed will stop the upgrade if the app is up to date.
### UPGRADE_APP should be used to upgrade the core app only if there's an upgrade to do.
upgrade_type=$(ynh_check_app_version_changed)
ynh_app_setting_set $app installed_version $source_version #=================================================
ynh_app_setting_set $app deploy_path $new_deploy_path # ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
if [ $new_deploy_path != $old_deploy_path ]; then # If final_path doesn't exist, create it
ynh_secure_remove $old_deploy_path if [ -z "$final_path" ]; then
fi final_path=/var/www/$app
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."
# 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
main #=================================================
# CHECK THE PATH
#=================================================
# Normalize the URL path syntax
# N.B. : this is for app installations before YunoHost 2.7
# where this value might be something like /foo/ or foo/
# instead of /foo ....
# If nobody installed your app before 2.7, then you may
# safely remove this line
path_url=$(ynh_normalize_url_path --path_url=$path_url)
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..."
ynh_systemd_action --service_name=$app --action="stop" --log_path="$datadir/log/debug-last.log"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..."
# Download, check integrity, uncompress and patch the source from app.src
ynh_secure_remove --file="$final_path"
ynh_setup_source --dest_dir="$final_path"
fi
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading nginx web server configuration..."
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..."
ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..."
# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..."
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set permissions on app files
chown -R $app: $final_path
chown -R $app: $datadir
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add $app --description "$app service" --others_var="$fs_port $datadir" --log "$datadir/log/debug-last.log" --needs_exposed_ports "$fs_port"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..."
ynh_systemd_action --service_name=$app --action="start" --log_path="$datadir/log/debug-last.log"
#=================================================
# SETUP SSOWAT
#=================================================
ynh_script_progression --message="Upgrading SSOwat configuration..."
# Restrict access to admin only
yunohost app addaccess --users=$admin $app
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed"