1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/bozon_ynh.git synced 2024-09-03 18:16:09 +02:00

Add change_url script & remove sudo

This commit is contained in:
ewilly 2017-10-11 21:21:48 +02:00
parent 2e554e5fb0
commit 3094be110b
3 changed files with 86 additions and 28 deletions

View file

@ -26,6 +26,7 @@
fail_download_source=1 fail_download_source=1
port_already_use=0 port_already_use=0
final_path_already_use=1 final_path_already_use=1
change_url=1
;;; Levels ;;; Levels
Level 1=auto Level 1=auto
Level 2=auto Level 2=auto

View file

@ -14,14 +14,14 @@ myynh_check_path () {
# Create directory only if not already exists (path in argument) # Create directory only if not already exists (path in argument)
myynh_create_dir () { myynh_create_dir () {
[ -z "$1" ] && ynh_die "No argument supplied" [ -z "$1" ] && ynh_die "No argument supplied"
[ -d "$1" ] || sudo mkdir -p "$1" [ -d "$1" ] || mkdir -p "$1"
} }
# Check if enough disk space available on backup storage # Check if enough disk space available on backup storage
myynh_check_disk_space () { myynh_check_disk_space () {
file_to_analyse=$1 file_to_analyse=$1
backup_size=$(sudo du --summarize "$1" | cut -f1) backup_size=$(du --summarize "$1" | cut -f1)
free_space=$(sudo df --output=avail "/home/yunohost.backup" | sed 1d) free_space=$(df --output=avail "/home/yunohost.backup" | sed 1d)
if [ $free_space -le $backup_size ] if [ $free_space -le $backup_size ]
then then
WARNING echo "Not enough backup disk space for: $1" WARNING echo "Not enough backup disk space for: $1"
@ -39,46 +39,53 @@ myynh_clean_source () {
# Create a dedicated nginx config # Create a dedicated nginx config
myynh_add_nginx_config () { myynh_add_nginx_config () {
ynh_backup_if_checksum_is_different "$nginx_conf" 1 ynh_backup_if_checksum_is_different "$nginx_conf" 1
sudo cp ../conf/nginx.conf "$nginx_conf" cp ../conf/nginx.conf "$nginx_conf"
# To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable. # To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable.
# Substitute in a nginx config file only if the variable is not empty # Substitute in a nginx config file only if the variable is not empty
if test -n "${path_url:-}"; then [ -n "${path_url:-}" ] && ynh_replace_string "__PATH__" "$path_url" "$nginx_conf"
ynh_replace_string "__PATH__" "$path_url" "$nginx_conf" [ -n "${final_path:-}" ] && ynh_replace_string "__FINALPATH__" "$final_path" "$nginx_conf"
fi [ -n "${app:-}" ] && ynh_replace_string "__NAME__" "$app" "$nginx_conf"
if test -n "${final_path:-}"; then [ -n "${filesize:-}" ] && ynh_replace_string "__FILESIZE__" "$filesize" "$nginx_conf"
ynh_replace_string "__FINALPATH__" "$final_path" "$nginx_conf"
fi
if test -n "${app:-}"; then
ynh_replace_string "__NAME__" "$app" "$nginx_conf"
fi
if test -n "${filesize:-}"; then
ynh_replace_string "__FILESIZE__" "$filesize" "$nginx_conf"
fi
ynh_store_file_checksum "$nginx_conf" ynh_store_file_checksum "$nginx_conf"
sudo systemctl reload nginx systemctl reload nginx
} }
# Create a dedicated php-fpm config # Create a dedicated php-fpm config
myynh_add_fpm_config () { myynh_add_fpm_config () {
ynh_backup_if_checksum_is_different "$phpfpm_conf" 1 ynh_backup_if_checksum_is_different "$phpfpm_conf" 1
sudo cp ../conf/php-fpm.conf "$phpfpm_conf" cp ../conf/php-fpm.conf "$phpfpm_conf"
postsize=${filesize%?}.1${filesize: -1} postsize=${filesize%?}.1${filesize: -1}
ynh_replace_string "__FINALPATH__" "$final_path" "$phpfpm_conf" ynh_replace_string "__FINALPATH__" "$final_path" "$phpfpm_conf"
ynh_replace_string "__NAME__" "$app" "$phpfpm_conf" ynh_replace_string "__NAME__" "$app" "$phpfpm_conf"
ynh_replace_string "__FILESIZE__" "$filesize" "$phpfpm_conf" ynh_replace_string "__FILESIZE__" "$filesize" "$phpfpm_conf"
ynh_replace_string "__POSTSIZE__" "$postsize" "$phpfpm_conf" ynh_replace_string "__POSTSIZE__" "$postsize" "$phpfpm_conf"
sudo chown root: "$phpfpm_conf" chown root: "$phpfpm_conf"
ynh_store_file_checksum "$phpfpm_conf" ynh_store_file_checksum "$phpfpm_conf"
sudo systemctl reload php5-fpm systemctl reload php5-fpm
} }
myynh_set_permissions () { myynh_set_permissions () {
[ $(sudo find "$final_path" -type f | wc -l) -gt 0 ] && sudo find "$final_path" -type f | xargs sudo chmod 0644 [ $(find "$final_path" -type f | wc -l) -gt 0 ] && find "$final_path" -type f | xargs chmod 0644
[ $(sudo find "$final_path" -type d | wc -l) -gt 0 ] && sudo find "$final_path" -type d | xargs sudo chmod 0755 [ $(find "$final_path" -type d | wc -l) -gt 0 ] && find "$final_path" -type d | xargs chmod 0755
[ $(sudo find "$data_path" -type f | wc -l) -gt 0 ] && sudo find "$data_path" -type f | xargs sudo chmod 0644 [ $(find "$data_path" -type f | wc -l) -gt 0 ] && find "$data_path" -type f | xargs chmod 0644
[ $(sudo find "$data_path" -type d | wc -l) -gt 0 ] && sudo find "$data_path" -type d | xargs sudo chmod 0755 [ $(find "$data_path" -type d | wc -l) -gt 0 ] && find "$data_path" -type d | xargs chmod 0755
sudo chown -R root:"$app" "$final_path" chown -R root:"$app" "$final_path"
sudo chown -R "$app": "$final_path/private" chown -R "$app": "$final_path/private"
sudo chown -R "$app": "$data_path" chown -R "$app": "$data_path"
sudo chown root: "$data_path" chown root: "$data_path"
}
#=================================================
# FUTURE YUNOHOST HELPERS - TO BE REMOVED LATER
#=================================================
# Delete a file checksum from the app settings
#
# $app should be defined when calling this helper
#
# usage: ynh_remove_file_checksum file
# | arg: file - The file for which the checksum will be deleted
ynh_delete_file_checksum () {
local checksum_setting_name=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_'
ynh_app_setting_delete $app $checksum_setting_name
} }

50
scripts/change_url Normal file
View file

@ -0,0 +1,50 @@
#!/bin/bash
set -eu
source _common.sh
source /usr/share/yunohost/helpers
# manage script failure
ynh_abort_if_errors
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME
domain_old=$YNH_APP_OLD_DOMAIN
domain_new=$YNH_APP_NEW_DOMAIN
path_url_old=$YNH_APP_OLD_PATH
path_url_new=$YNH_APP_NEW_PATH
# check the syntax
[ -n "$path_url_old" ] || path_url_old="/"
[ -n "$path_url_new" ] || path_url_new="/"
path_url_new=$(ynh_normalize_url_path "$path_url_new")
path_url_old=$(ynh_normalize_url_path "$path_url_old")
# definie useful vars
nginx_conf_old="/etc/nginx/conf.d/$domain_old.d/$app.conf"
nginx_conf_new="/etc/nginx/conf.d/$domain_new.d/$app.conf"
change_domain=0
change_path_url=0
# check what should be changed
[ "$domain_old" != "$domain_new" ] && change_domain=1
[ "$path_url_old" != "$path_url_new" ] && change_path_url=1
# change the path in the nginx config file
if [ $change_path_url -eq 1 ]
then
ynh_backup_if_checksum_is_different "$nginx_conf_old"
ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $path_url_old" "location\1 $path_url_new" "$nginx_conf_old"
ynh_replace_string "return \([[:digit:]]\{3\}\) $path_url_old" "return \1 $path_url_new" "$nginx_conf_old"
ynh_store_file_checksum "$nginx_conf_old"
fi
# change the domain for nginx
if [ $change_domain -eq 1 ]
then
ynh_delete_file_checksum "$nginx_conf_old"
mv $nginx_conf_old "$nginx_conf_new"
ynh_store_file_checksum "$nginx_conf_new"
fi
systemctl reload nginx