diff --git a/README.md b/README.md index 1dc8d7d..84227ef 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # 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) *[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.* ## Overview @@ -55,7 +55,7 @@ Can the app be used by multiple users? * 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 diff --git a/check_process b/check_process index 9f2a7ae..d966a49 100644 --- a/check_process +++ b/check_process @@ -13,14 +13,14 @@ pkg_linter=1 setup_sub_dir=0 # setup_sub_dir=0 test disabled because app is always installed on / - setup_root=1 + setup_root=1 setup_nourl=0 setup_private=0 # setup_private=0 test disabled because app is always installed as private setup_public=0 upgrade=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 wrong_user=1 wrong_path=0 @@ -33,8 +33,8 @@ Level 1=auto Level 2=auto Level 3=auto - Level 4=na -# level 4: the app does not directly use Yunohost user data + Level 4=na +# level 4: the app does not directly use Yunohost user data Level 5=auto Level 6=auto Level 7=auto diff --git a/conf/app.src b/conf/app.src index aa97514..0117e6c 100644 --- a/conf/app.src +++ b/conf/app.src @@ -1,3 +1,7 @@ SOURCE_URL=https://github.com/HelloZeroNet/ZeroNet/archive/v0.7.1.tar.gz 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 diff --git a/conf/nginx.conf b/conf/nginx.conf new file mode 100644 index 0000000..ba502fb --- /dev/null +++ b/conf/nginx.conf @@ -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; + } \ No newline at end of file diff --git a/conf/nginx.conf.template b/conf/nginx.conf.template deleted file mode 100644 index c8fb620..0000000 --- a/conf/nginx.conf.template +++ /dev/null @@ -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; -} diff --git a/conf/systemd.service b/conf/systemd.service new file mode 100644 index 0000000..3cc5fc5 --- /dev/null +++ b/conf/systemd.service @@ -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 diff --git a/conf/systemd.service.template b/conf/systemd.service.template deleted file mode 100644 index 4e6f674..0000000 --- a/conf/systemd.service.template +++ /dev/null @@ -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 diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..b7bf9c2 --- /dev/null +++ b/scripts/_common.sh @@ -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 +#================================================= diff --git a/scripts/backup b/scripts/backup index 348f844..62c9f8b 100755 --- a/scripts/backup +++ b/scripts/backup @@ -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 -main() { - ynh_abort_if_errors +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= - local app=$YNH_APP_INSTANCE_NAME - local backup_dir=$YNH_APP_BACKUP_DIR - local deploy_path=$( ynh_app_setting_get $app 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_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" +ynh_clean_setup () { + ### Remove this function if there's nothing to clean before calling the remove script. + true } +# 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)." \ No newline at end of file diff --git a/scripts/change_url b/scripts/change_url new file mode 100644 index 0000000..d0ef915 --- /dev/null +++ b/scripts/change_url @@ -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 diff --git a/scripts/functions.sh b/scripts/functions.sh deleted file mode 100644 index f975fa2..0000000 --- a/scripts/functions.sh +++ /dev/null @@ -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 -} diff --git a/scripts/install b/scripts/install index 8ad4ff9..6f65378 100755 --- a/scripts/install +++ b/scripts/install @@ -1,155 +1,246 @@ -#!/usr/bin/env bash +#!/bin/bash +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh source /usr/share/yunohost/helpers -source functions.sh -nginx_config_file() { - local app=$1 - local domain=$2 +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= - mkdir --parents "/etc/nginx/conf.d/${domain}.d" - - echo "/etc/nginx/conf.d/${domain}.d/${app}.conf" +ynh_clean_setup () { + ### Remove this function if there's nothing to clean before calling the remove script. + true } +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors -update_nginx_configuration() { - local app=$1 - local config_template=$2 - local config_file=$3 - local path=$4 - local ui_port=$5 +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= - 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} - sed --in-place "s@YNH_LOCAL_PORT@${ui_port}/@g" ${config_file} +### If it's a multi-instance app, meaning it can be installed several times independently +### 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() { - local app=$1 - local user=$2 +### If the app uses nginx as web server (written in HTML/PHP in most cases), the final path should be "/var/www/$app". +### If the app provides an internal web server (or uses another application server such as uwsgi), the final path should be "/opt/yunohost/$app" +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 -# UserInterace and FileServer port for the user -# ZeroNet data is stored in the users home directory in ~/.zeronet -# -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 +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_script_progression --message="Storing installation settings..." - # create zeronet user data location - local data_dir=$zeronet_dir/data - local log_dir=$zeronet_dir/log +ynh_app_setting_set --app=$app --key=domain --value=$domain +ynh_app_setting_set --app=$app --key=path --value=$path_url +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 - sed --in-place "s@__UI_PORT__@$ui_port@g" $service_file - sed --in-place "s@__FS_PORT__@$fs_port@g" $service_file - sed --in-place "s@__UI_HOST__@$ui_host@g" $service_file - 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 +### Use these lines if you have to open a port for the application +### `ynh_find_port` will find the first available port starting from the given port. +### If you're not using these lines: +### - Remove the section "CLOSE A PORT" in the remove script - systemctl daemon-reload - systemctl enable $service_name - systemctl start $service_name -} +# Find an available port +port=$(ynh_find_port --port=43110) +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 -# -# Find a free port and return it -# -# 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 +# Optional: Expose this port publicly +# (N.B. : you only need to do this if the app actually needs to expose the port publicly. +# If you do this and the app doesn't actually need you are CREATING SECURITY HOLES IN THE SERVER !) - while netcat -z 127.0.0.1 $port # Check if the port is free - do - port=$((port+1)) # Else, pass to next port - done - echo $port -} +# Open the port +ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $fs_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 - local number=$YNH_APP_INSTANCE_NUMBER - local domain=$YNH_APP_ARG_DOMAIN - local user=$YNH_APP_ARG_ADMIN +ynh_install_app_dependencies $pkg_dependencies - 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="/" - local is_public=0 # by defaut access to zeronet is private - local nginx_config_template=../conf/nginx.conf.template - local systemd_service_template=../conf/systemd.service.template - local app_config=../conf/app.src - local systemd_service_dir=/etc/systemd/system +### `ynh_setup_source` is used to install an app from a zip or tar.gz file, +### downloaded from an upstream source, like a git repository. +### `ynh_setup_source` use the file conf/app.src - local url=$domain$path - local source_version=$( app_config_get $app_config "SOURCE_VERSION" ) - local systemd_service_name=$( systemd_service_name $app $user ) - 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_app_setting_set --app=$app --key=final_path --value=$final_path +# Download, check integrity, uncompress and patch the source from app.src +ynh_setup_source --dest_dir="$final_path" - 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 - ynh_app_setting_set $app domain $domain - 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 +# Create a dedicated nginx config +ynh_add_nginx_config - 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 \ - $user $ui_port $symlink_to_deploy_path $user_zeronet_dir $domain $fs_port -} +#================================================= +# SETUP SYSTEMD +#================================================= +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" diff --git a/scripts/remove b/scripts/remove index fb79174..6fe8ad5 100755 --- a/scripts/remove +++ b/scripts/remove @@ -1,57 +1,95 @@ -#!/usr/bin/env bash -set -u +#!/bin/bash +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh source /usr/share/yunohost/helpers -remove_systemd_service_() { - local service_name=$1 - local service_file=$2 +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." - systemctl stop $service_name - systemctl disable $service_name - rm $service_file - systemctl daemon-reload - systemctl reset-failed -} +app=$YNH_APP_INSTANCE_NAME -remove_nginx_config() { - local nginx_config_file=$1 +domain=$(ynh_app_setting_get --app=$app --key=domain) +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() { - local user_zeronet_dir=$1 +# Remove the service from the list of services known by Yunohost (added from `yunohost service add`) +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() { - local deploy_path=$1 - local symlink_to_deploy_path=$2 +# Remove the dedicated systemd config +ynh_remove_systemd_config - ynh_secure_remove $deploy_path - ynh_secure_remove $symlink_to_deploy_path -} +#================================================= +# REMOVE DEPENDENCIES +#================================================= +ynh_script_progression --message="Removing dependencies..." -main() { - local app=${YNH_APP_INSTANCE_NAME} - 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 ) +# Remove metapackage and its dependencies +ynh_remove_app_dependencies - if [ -n $domain ]; then - remove_nginx_config $nginx_config_file - remove_user_data $user_zeronet_dir - remove_systemd_service_ $systemd_service_name $systemd_service_file - remove_zeronet $deploy_path $symlink_to_deploy_path - fi -} +#================================================= +# REMOVE APP MAIN DIR +#================================================= +ynh_script_progression --message="Removing app main directory..." -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" diff --git a/scripts/restore b/scripts/restore index 314471d..d4afe49 100755 --- a/scripts/restore +++ b/scripts/restore @@ -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 -# 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 -install_dependencies() { - apt-get install --quiet --yes python-msgpack python-gevent +ynh_clean_setup () { + #### Remove this function if there's nothing to clean before calling the remove script. + true } +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors -is_app_restorable() { - local -r domain=$1 - local -r path=$2 - local -r deploy_path=$3 +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." - ynh_webpath_available $domain $path \ - || ynh_die "Path not available: ${domain}${path}" +app=$YNH_APP_INSTANCE_NAME - test ! -d $deploy_path \ - || ynh_die "There is already a directory: $deploy_path" -} +domain=$(ynh_app_setting_get --app=$app --key=domain) +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 - local domain=$(ynh_app_setting_get $app domain) - local path=$(ynh_app_setting_get $app path) - 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 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 ) +ynh_webpath_available --domain=$domain --path_url=$path_url \ + || ynh_die --message="Path not available: ${domain}${path_url}" +test ! -d $final_path \ + || ynh_die --message="There is already a directory: $final_path " - 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 - ln --symbolic --force $deploy_path $symlink_to_deploy_path - chown $user: -LR $symlink_to_deploy_path - chown $user: -h $symlink_to_deploy_path +ynh_restore_file --origin_path="$final_path" +ynh_restore_file --origin_path="$datadir" - ynh_secure_remove $user_zeronet_dir - cp -aR "./conf/.zeronet" $user_zeronet_dir - chown -R $user: $user_zeronet_dir - - 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 -} +#================================================= +# RECREATE THE DEDICATED USER +#================================================= +ynh_script_progression --message="Recreating the dedicated system user..." -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" \ No newline at end of file diff --git a/scripts/upgrade b/scripts/upgrade index 3b09cd2..54891e5 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,33 +1,172 @@ -#!/usr/bin/env bash +#!/bin/bash +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh 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} - 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") +app=$YNH_APP_INSTANCE_NAME - local old_deploy_path=$deploy_path - local new_deploy_path=$( make_deploy_path $app $source_version ) +domain=$(ynh_app_setting_get --app=$app --key=domain) +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 - ynh_secure_remove $old_deploy_path - fi +# If final_path doesn't exist, create it +if [ -z "$final_path" ]; then + 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"