change_url 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. #=================================================
  10. # RETRIEVE ARGUMENTS
  11. #=================================================
  12. old_domain=$YNH_APP_OLD_DOMAIN
  13. old_path=$YNH_APP_OLD_PATH
  14. new_domain=$YNH_APP_NEW_DOMAIN
  15. new_path=$YNH_APP_NEW_PATH
  16. app=$YNH_APP_INSTANCE_NAME
  17. #=================================================
  18. # CHECK THE SYNTAX OF THE PATHS
  19. #=================================================
  20. test -n "$old_path" || old_path="/"
  21. test -n "$new_path" || new_path="/"
  22. new_path=$(ynh_normalize_url_path $new_path)
  23. old_path=$(ynh_normalize_url_path $old_path)
  24. #=================================================
  25. # CHECK WHICH PARTS SHOULD BE CHANGED
  26. #=================================================
  27. change_domain=0
  28. if [ "$old_domain" != "$new_domain" ]
  29. then
  30. change_domain=1
  31. fi
  32. change_path=0
  33. if [ "$old_path" != "$new_path" ]
  34. then
  35. change_path=1
  36. fi
  37. #=================================================
  38. # STANDARD MODIFICATIONS
  39. #=================================================
  40. # MODIFY URL IN NGINX CONF
  41. #=================================================
  42. nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf
  43. # Change the path in the nginx config file
  44. if [ $change_path -eq 1 ]
  45. then
  46. # Make a backup of the original nginx config file if modified
  47. ynh_backup_if_checksum_is_different "$nginx_conf_path"
  48. # Replace locations starting with old_path
  49. # Look for every location possible patterns (see https://nginx.org/en/docs/http/ngx_http_core_module.html#location)
  50. ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $old_path" "location\1 $new_path" "$nginx_conf_path"
  51. # Replace path in "return" directives
  52. ynh_replace_string "return \([[:digit:]]\{3\}\) $old_path" "return \1 $new_path" "$nginx_conf_path"
  53. # Calculate and store the nginx config file checksum
  54. ynh_store_file_checksum "$nginx_conf_path"
  55. fi
  56. # Change the domain for nginx
  57. if [ $change_domain -eq 1 ]
  58. then
  59. # Delete file checksum for the old conf file location
  60. ynh_delete_file_checksum "$nginx_conf_path"
  61. mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf
  62. # Store file checksum for the new config file location
  63. ynh_store_file_checksum "/etc/nginx/conf.d/$new_domain.d/$app.conf"
  64. fi
  65. #=================================================
  66. # SPECIFIC MODIFICATIONS
  67. #=================================================
  68. # ...
  69. #=================================================
  70. #=================================================
  71. # GENERIC FINALISATION
  72. #=================================================
  73. # RELOAD NGINX
  74. #=================================================
  75. systemctl reload nginx