change_url 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. ynh_backup_if_checksum_is_different "$nginx_conf_path"
  47. # Replace locations starting with old_path
  48. # Look for every location possible patterns (see https://nginx.org/en/docs/http/ngx_http_core_module.html#location)
  49. ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $old_path" "location\1 $new_path" "$nginx_conf_path"
  50. # Replace path in "return" directives
  51. ynh_replace_string "return \([[:digit:]]\{3\}\) $old_path" "return \1 $new_path" "$nginx_conf_path"
  52. ynh_store_file_checksum "$nginx_conf_path"
  53. fi
  54. # Change the domain for nginx
  55. if [ $change_domain -eq 1 ]
  56. then
  57. mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf
  58. ynh_store_file_checksum "$nginx_conf_path"
  59. fi
  60. #=================================================
  61. # SPECIFIC MODIFICATIONS
  62. #=================================================
  63. # ...
  64. #=================================================
  65. #=================================================
  66. # GENERIC FINALISATION
  67. #=================================================
  68. # RELOAD NGINX
  69. #=================================================
  70. systemctl reload nginx