From 3094be110bd2107f8fa1f802db5c5f96bf311bf7 Mon Sep 17 00:00:00 2001 From: ewilly Date: Wed, 11 Oct 2017 21:21:48 +0200 Subject: [PATCH] Add change_url script & remove sudo --- check_process | 1 + scripts/_common.sh | 63 +++++++++++++++++++++++++--------------------- scripts/change_url | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 scripts/change_url diff --git a/check_process b/check_process index 65232b0..008d203 100644 --- a/check_process +++ b/check_process @@ -26,6 +26,7 @@ fail_download_source=1 port_already_use=0 final_path_already_use=1 + change_url=1 ;;; Levels Level 1=auto Level 2=auto diff --git a/scripts/_common.sh b/scripts/_common.sh index 82cfe10..d7bb99c 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -14,14 +14,14 @@ myynh_check_path () { # Create directory only if not already exists (path in argument) myynh_create_dir () { [ -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 myynh_check_disk_space () { file_to_analyse=$1 - backup_size=$(sudo du --summarize "$1" | cut -f1) - free_space=$(sudo df --output=avail "/home/yunohost.backup" | sed 1d) + backup_size=$(du --summarize "$1" | cut -f1) + free_space=$(df --output=avail "/home/yunohost.backup" | sed 1d) if [ $free_space -le $backup_size ] then WARNING echo "Not enough backup disk space for: $1" @@ -39,46 +39,53 @@ myynh_clean_source () { # Create a dedicated nginx config myynh_add_nginx_config () { 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. # Substitute in a nginx config file only if the variable is not empty - if test -n "${path_url:-}"; then - ynh_replace_string "__PATH__" "$path_url" "$nginx_conf" - fi - if test -n "${final_path:-}"; then - 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 + [ -n "${path_url:-}" ] && ynh_replace_string "__PATH__" "$path_url" "$nginx_conf" + [ -n "${final_path:-}" ] && ynh_replace_string "__FINALPATH__" "$final_path" "$nginx_conf" + [ -n "${app:-}" ] && ynh_replace_string "__NAME__" "$app" "$nginx_conf" + [ -n "${filesize:-}" ] && ynh_replace_string "__FILESIZE__" "$filesize" "$nginx_conf" ynh_store_file_checksum "$nginx_conf" - sudo systemctl reload nginx + systemctl reload nginx } # Create a dedicated php-fpm config myynh_add_fpm_config () { 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} ynh_replace_string "__FINALPATH__" "$final_path" "$phpfpm_conf" ynh_replace_string "__NAME__" "$app" "$phpfpm_conf" ynh_replace_string "__FILESIZE__" "$filesize" "$phpfpm_conf" ynh_replace_string "__POSTSIZE__" "$postsize" "$phpfpm_conf" - sudo chown root: "$phpfpm_conf" + chown root: "$phpfpm_conf" ynh_store_file_checksum "$phpfpm_conf" - sudo systemctl reload php5-fpm + systemctl reload php5-fpm } myynh_set_permissions () { - [ $(sudo find "$final_path" -type f | wc -l) -gt 0 ] && sudo find "$final_path" -type f | xargs sudo chmod 0644 - [ $(sudo find "$final_path" -type d | wc -l) -gt 0 ] && sudo find "$final_path" -type d | xargs sudo chmod 0755 - [ $(sudo find "$data_path" -type f | wc -l) -gt 0 ] && sudo find "$data_path" -type f | xargs sudo chmod 0644 - [ $(sudo find "$data_path" -type d | wc -l) -gt 0 ] && sudo find "$data_path" -type d | xargs sudo chmod 0755 - sudo chown -R root:"$app" "$final_path" - sudo chown -R "$app": "$final_path/private" - sudo chown -R "$app": "$data_path" - sudo chown root: "$data_path" + [ $(find "$final_path" -type f | wc -l) -gt 0 ] && find "$final_path" -type f | xargs chmod 0644 + [ $(find "$final_path" -type d | wc -l) -gt 0 ] && find "$final_path" -type d | xargs chmod 0755 + [ $(find "$data_path" -type f | wc -l) -gt 0 ] && find "$data_path" -type f | xargs chmod 0644 + [ $(find "$data_path" -type d | wc -l) -gt 0 ] && find "$data_path" -type d | xargs chmod 0755 + chown -R root:"$app" "$final_path" + chown -R "$app": "$final_path/private" + chown -R "$app": "$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 } diff --git a/scripts/change_url b/scripts/change_url new file mode 100644 index 0000000..f7d1b8e --- /dev/null +++ b/scripts/change_url @@ -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