Skip to content

Commit

Permalink
Merge pull request YunoHost#80 from YunoHost/new_helpers
Browse files Browse the repository at this point in the history
Use new 3.5 helpers
  • Loading branch information
maniackcrudelis authored May 1, 2019
2 parents 7e7e342 + 197269f commit 46d318c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 70 deletions.
24 changes: 16 additions & 8 deletions scripts/backup
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
ynh_print_info --message="Loading installation settings..."
ynh_script_progression --message="Loading installation settings..." --time --weight=1

app=$YNH_APP_INSTANCE_NAME

Expand All @@ -37,44 +37,52 @@ db_name=$(ynh_app_setting_get --app=$app --key=db_name)
#=================================================
# BACKUP THE APP MAIN DIR
#=================================================
ynh_print_info --message="Backing up the main app directory..."
ynh_script_progression --message="Backing up the main app directory..." --time --weight=1

ynh_backup --src_path="$final_path"

#=================================================
# BACKUP THE NGINX CONFIGURATION
#=================================================
ynh_print_info --message="Backing up nginx web server configuration..."
ynh_script_progression --message="Backing up nginx web server configuration..." --time --weight=1

ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf"

#=================================================
# BACKUP THE PHP-FPM CONFIGURATION
#=================================================
ynh_print_info --message="Backing up php-fpm configuration..."
ynh_script_progression --message="Backing up php-fpm configuration..." --time --weight=1

ynh_backup --src_path="/etc/php/7.0/fpm/pool.d/$app.conf"

#=================================================
# BACKUP THE MYSQL DATABASE
#=================================================
ynh_print_info --message="Backing up the MySQL database..."
ynh_script_progression --message="Backing up the MySQL database..." --time --weight=1

ynh_mysql_dump_db --database="$db_name" > db.sql

#=================================================
# BACKUP FAIL2BAN CONFIGURATION
#=================================================
ynh_script_progression --message="Backing up fail2ban configuration" --time --weight=1

ynh_backup "/etc/fail2ban/jail.d/$app.conf"
ynh_backup "/etc/fail2ban/filter.d/$app.conf"

#=================================================
# SPECIFIC BACKUP
#=================================================
# BACKUP LOGROTATE
#=================================================
ynh_print_info --message="Backing up logrotate configuration..."
ynh_script_progression --message="Backing up logrotate configuration..." --time --weight=1

ynh_backup --src_path="/etc/logrotate.d/$app"

#=================================================
# BACKUP SYSTEMD
#=================================================
ynh_print_info --message="Backing up systemd configuration..."
ynh_script_progression --message="Backing up systemd configuration..." --time --weight=1

ynh_backup --src_path="/etc/systemd/system/$app.service"

Expand All @@ -88,4 +96,4 @@ ynh_backup --src_path="/etc/cron.d/$app"
# END OF SCRIPT
#=================================================

ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)."
ynh_script_progression --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)." --time --last
10 changes: 5 additions & 5 deletions scripts/change_url
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ app=$YNH_APP_INSTANCE_NAME
#=================================================
# LOAD SETTINGS
#=================================================
ynh_print_info --message="Loading installation settings..."
ynh_script_progression --message="Loading installation settings..." --time --weight=1

# Needed for helper "ynh_add_nginx_config"
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
Expand Down Expand Up @@ -54,7 +54,7 @@ fi
#=================================================
# MODIFY URL IN NGINX CONF
#=================================================
ynh_print_info --message="Updating nginx web server configuration..."
ynh_script_progression --message="Updating nginx web server configuration..." --time --weight=1

nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf

Expand Down Expand Up @@ -91,12 +91,12 @@ fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_print_info --message="Reloading nginx web server..."
ynh_script_progression --message="Reloading nginx web server..." --time --weight=1

systemctl reload nginx
ynh_systemd_action --service_name=nginx --action=reload

#=================================================
# END OF SCRIPT
#=================================================

ynh_print_info --message="Change of URL completed for $app"
ynh_script_progression --message="Change of URL completed for $app" --time --last
52 changes: 34 additions & 18 deletions scripts/install
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_print_info --message="Validating installation parameters..."
### 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..." --time --weight=1

### 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"
Expand All @@ -59,7 +67,7 @@ ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_print_info --message="Storing installation settings..."
ynh_script_progression --message="Storing installation settings..." --time --weight=1

ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
Expand All @@ -72,7 +80,7 @@ ynh_app_setting_set --app=$app --key=language --value=$language
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_print_info --message="Configuring firewall..."
ynh_script_progression --message="Configuring firewall..." --time --weight=1

### 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.
Expand All @@ -88,7 +96,7 @@ ynh_app_setting_set --app=$app --key=port --value=$port
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_print_info --message="Installing dependencies..."
ynh_script_progression --message="Installing dependencies..." --time --weight=1

### `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.
Expand All @@ -103,7 +111,7 @@ ynh_install_app_dependencies $pkg_dependencies
#=================================================
# CREATE A MYSQL DATABASE
#=================================================
ynh_print_info --message="Creating a MySQL database..."
ynh_script_progression --message="Creating a MySQL database..." --time --weight=1

### Use these lines if you need a database for the application.
### `ynh_mysql_setup_db` will create a database, an associated user and a ramdom password.
Expand All @@ -121,7 +129,7 @@ ynh_mysql_setup_db --db_user=$db_name --db_name=$db_name
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_print_info --message="Setting up source files..."
ynh_script_progression --message="Setting up source files..." --time --weight=1

### `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.
Expand All @@ -134,7 +142,7 @@ ynh_setup_source --dest_dir="$final_path"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_print_info --message="Configuring nginx web server..."
ynh_script_progression --message="Configuring nginx web server..." --time --weight=1

### `ynh_add_nginx_config` will use the file conf/nginx.conf

Expand All @@ -144,15 +152,15 @@ ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_print_info --message="Configuring system user..."
ynh_script_progression --message="Configuring system user..." --time --weight=1

# Create a system user
ynh_system_user_create --username=$app

#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_print_info --message="Configuring php-fpm..."
ynh_script_progression --message="Configuring php-fpm..." --time --weight=1

### `ynh_add_fpm_config` is used to set up a PHP config.
### You can remove it if your app doesn't use PHP.
Expand All @@ -177,7 +185,7 @@ ynh_add_fpm_config
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_print_info --message="Configuring a systemd service..."
ynh_script_progression --message="Configuring a systemd service..." --time --weight=1

### `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.
Expand Down Expand Up @@ -206,16 +214,16 @@ ynh_add_systemd_config
chown -R $app: $final_path

# Set the app as temporarily public for curl call
ynh_print_info --message="Configuring SSOwat..."
ynh_script_progression --message="Configuring SSOwat..." --time --weight=1
ynh_app_setting_set --app=$app --key=skipped_uris --value="/"
# Reload SSOwat config
yunohost app ssowatconf

# Reload Nginx
systemctl reload nginx
ynh_systemd_action --service_name=nginx --action=reload

# Installation with curl
ynh_print_info --message="Finalizing installation..."
ynh_script_progression --message="Finalizing installation..." --time --weight=1
ynh_local_curl "/INSTALL_PATH" "key1=value1" "key2=value2" "key3=value3"

# Remove the public access
Expand Down Expand Up @@ -260,7 +268,7 @@ chown -R root: $final_path
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_print_info --message="Configuring log rotation..."
ynh_script_progression --message="Configuring log rotation..." --time --weight=1

### `ynh_use_logrotate` is used to configure a logrotate configuration for the logs of this app.
### Use this helper only if there is effectively a log file for this app.
Expand Down Expand Up @@ -289,10 +297,18 @@ yunohost service add $app --log "/var/log/$app/$app.log"
# if using yunohost version 3.2 or more in the 'manifest.json', a description can be added
#yunohost service add $app --description "$app daemon for XXX" --log "/var/log/$app/$app.log"

#=================================================
# SETUP FAIL2BAN
#=================================================
ynh_script_progression --message="Configuring fail2ban..." --time --weight=1

# Create a dedicated fail2ban config
ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failregex="Regex to match into the log for a failed login"

#=================================================
# SETUP SSOWAT
#=================================================
ynh_print_info --message="Configuring SSOwat..."
ynh_script_progression --message="Configuring SSOwat..." --time --weight=1

# Make app public if necessary
if [ $is_public -eq 1 ]
Expand All @@ -304,12 +320,12 @@ fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_print_info --message="Reloading nginx web server..."
ynh_script_progression --message="Reloading nginx web server..." --time --weight=1

systemctl reload nginx
ynh_systemd_action --service_name=nginx --action=reload

#=================================================
# END OF SCRIPT
#=================================================

ynh_print_info --message="Installation of $app completed"
ynh_script_progression --message="Installation of $app completed" --time --last
32 changes: 20 additions & 12 deletions scripts/remove
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_print_info --message="Loading installation settings..."
ynh_script_progression --message="Loading installation settings..." --time --weight=1

app=$YNH_APP_INSTANCE_NAME

Expand All @@ -31,62 +31,62 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path)
# Remove a service from the admin panel, added by `yunohost service add`
if yunohost service status $app >/dev/null 2>&1
then
ynh_print_info --message="Removing $app service"
ynh_script_progression --message="Removing $app service" --time --weight=1
yunohost service remove $app
fi

#=================================================
# STOP AND REMOVE SERVICE
#=================================================
ynh_print_info --message="Stopping and removing the systemd service"
ynh_script_progression --message="Stopping and removing the systemd service" --time --weight=1

# Remove the dedicated systemd config
ynh_remove_systemd_config

#=================================================
# REMOVE THE MYSQL DATABASE
#=================================================
ynh_print_info --message="Removing the MySQL database"
ynh_script_progression --message="Removing the MySQL database" --time --weight=1

# Remove a database if it exists, along with the associated user
ynh_mysql_remove_db --db_user=$db_user --db_name=$db_name

#=================================================
# REMOVE DEPENDENCIES
#=================================================
ynh_print_info --message="Removing dependencies"
ynh_script_progression --message="Removing dependencies" --time --weight=1

# Remove metapackage and its dependencies
ynh_remove_app_dependencies

#=================================================
# REMOVE APP MAIN DIR
#=================================================
ynh_print_info --message="Removing app main directory"
ynh_script_progression --message="Removing app main directory" --time --weight=1

# Remove the app directory securely
ynh_secure_remove --file="$final_path"

#=================================================
# REMOVE NGINX CONFIGURATION
#=================================================
ynh_print_info --message="Removing nginx web server configuration"
ynh_script_progression --message="Removing nginx web server configuration" --time --weight=1

# Remove the dedicated nginx config
ynh_remove_nginx_config

#=================================================
# REMOVE PHP-FPM CONFIGURATION
#=================================================
ynh_print_info --message="Removing php-fpm configuration"
ynh_script_progression --message="Removing php-fpm configuration" --time --weight=1

# Remove the dedicated php-fpm config
ynh_remove_fpm_config

#=================================================
# REMOVE LOGROTATE CONFIGURATION
#=================================================
ynh_print_info --message="Removing logrotate configuration"
ynh_script_progression --message="Removing logrotate configuration" --time --weight=1

# Remove the app-specific logrotate config
ynh_remove_logrotate
Expand All @@ -97,10 +97,18 @@ ynh_remove_logrotate

if yunohost firewall list | grep -q "\- $port$"
then
ynh_print_info --message="Closing port $port"
ynh_script_progression --message="Closing port $port"
ynh_exec_warn_less yunohost firewall disallow TCP $port
fi

#=================================================
# REMOVE FAIL2BAN CONFIGURATION
#=================================================
ynh_script_progression --message="Removing fail2ban configuration..." --time --weight=1

# Remove the dedicated fail2ban config
ynh_remove_fail2ban_config

#=================================================
# SPECIFIC REMOVE
#=================================================
Expand All @@ -121,7 +129,7 @@ ynh_secure_remove --file="/var/log/$app/"
#=================================================
# REMOVE DEDICATED USER
#=================================================
ynh_print_info --message="Removing the dedicated system user"
ynh_script_progression --message="Removing the dedicated system user" --time --weight=1

# Delete a system user
ynh_system_user_delete --username=$app
Expand All @@ -130,4 +138,4 @@ ynh_system_user_delete --username=$app
# END OF SCRIPT
#=================================================

ynh_print_info --message="Removal of $app completed"
ynh_script_progression --message="Removal of $app completed" --time --last
Loading

0 comments on commit 46d318c

Please sign in to comment.