_common.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. #=================================================
  3. # COMMON VARIABLES
  4. #=================================================
  5. pihole_adminlte_version=5.18
  6. pihole_flt_version=5.20
  7. # This is hard-coded upstream...
  8. PI_HOLE_LOCAL_REPO="/etc/.pihole"
  9. PI_HOLE_INSTALL_DIR="/opt/pihole"
  10. PI_HOLE_CONFIG_DIR="/etc/pihole"
  11. PI_HOLE_BIN_DIR="/usr/local/bin"
  12. #=================================================
  13. # PERSONAL HELPERS
  14. #=================================================
  15. _configure_ports() {
  16. if [ "$port" -gt 4720 ]; then
  17. ynh_die --message="The ports 4711 to 4720 are already in use. Pi-hole can't work on another port. Please try to free one of these ports."
  18. fi
  19. # Disable the port 53 for upnp
  20. ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
  21. ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
  22. # Open the UDP port 67 for dhcp
  23. ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
  24. }
  25. _add_cron_jobs() {
  26. install -D -m 644 -T -o root -g root "$PI_HOLE_LOCAL_REPO/advanced/Templates/pihole.cron" /etc/cron.d/pihole
  27. # Randomize gravity update time
  28. ynh_replace_string --target_file="/etc/cron.d/pihole" \
  29. --match_string="59 1 " \
  30. --replace_string="$((1 + RANDOM % 58)) $((3 + RANDOM % 2)) "
  31. # Randomize update checker time
  32. ynh_replace_string --target_file="/etc/cron.d/pihole" \
  33. --match_string="59 17" \
  34. --replace_string="$((1 + RANDOM % 58)) $((12 + RANDOM % 8))"
  35. # Remove git usage for version. Which fails because we use here a release instead of master.
  36. ynh_replace_string --target_file="/etc/cron.d/pihole" \
  37. --match_string=".*updatechecker.*" \
  38. --replace_string="#&"
  39. }
  40. _add_sudoers_config() {
  41. install -m 0640 "$PI_HOLE_LOCAL_REPO/advanced/Templates/pihole.sudo" /etc/sudoers.d/pihole
  42. echo "$app ALL=NOPASSWD: ${PI_HOLE_BIN_DIR}/pihole" >> /etc/sudoers.d/pihole
  43. }
  44. _add_logrotate_config() {
  45. install -D -m 644 -T "${PI_HOLE_LOCAL_REPO}"/advanced/Templates/logrotate "$PI_HOLE_CONFIG_DIR/logrotate"
  46. sed -i "/# su #/d;" "$PI_HOLE_CONFIG_DIR/logrotate"
  47. }
  48. #=================================================
  49. # EXPERIMENTAL HELPERS
  50. #=================================================
  51. ynh_maintenance_mode_ON () {
  52. mkdir -p /var/www/html/
  53. # Create an html to serve as maintenance notice
  54. echo "<!DOCTYPE html>
  55. <html>
  56. <head>
  57. <meta http-equiv="refresh" content="3">
  58. <title>Your app $app is currently under maintenance!</title>
  59. <style>
  60. body {
  61. width: 70em;
  62. margin: 0 auto;
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. <h1>Your app $app is currently under maintenance!</h1>
  68. <p>This app has been put under maintenance by your administrator at $(date)</p>
  69. <p>Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.</p>
  70. </body>
  71. </html>" > "/var/www/html/maintenance.$app.html"
  72. # Create a new nginx config file to redirect all access to the app to the maintenance notice instead.
  73. echo "# All request to the app will be redirected to ${path}_maintenance and fall on the maintenance notice
  74. rewrite ^${path}/(.*)$ ${path}_maintenance/? redirect;
  75. # Use another location, to not be in conflict with the original config file
  76. location ${path}_maintenance/ {
  77. alias /var/www/html/ ;
  78. try_files maintenance.$app.html =503;
  79. # Include SSOWAT user panel.
  80. include conf.d/yunohost_panel.conf.inc;
  81. }" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  82. # The current config file will redirect all requests to the root of the app.
  83. # To keep the full path, we can use the following rewrite rule:
  84. # rewrite ^${path}/(.*)$ ${path}_maintenance/\$1? redirect;
  85. # The difference will be in the $1 at the end, which keep the following queries.
  86. # But, if it works perfectly for a html request, there's an issue with any php files.
  87. # This files are treated as simple files, and will be downloaded by the browser.
  88. # Would be really be nice to be able to fix that issue. So that, when the page is reloaded after the maintenance, the user will be redirected to the real page he was.
  89. systemctl reload nginx
  90. }
  91. ynh_maintenance_mode_OFF () {
  92. # Rewrite the nginx config file to redirect from ${path}_maintenance to the real url of the app.
  93. echo "rewrite ^${path}_maintenance/(.*)$ ${path}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  94. systemctl reload nginx
  95. # Sleep 4 seconds to let the browser reload the pages and redirect the user to the app.
  96. sleep 4
  97. # Then remove the temporary files used for the maintenance.
  98. rm "/var/www/html/maintenance.$app.html"
  99. rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
  100. systemctl reload nginx
  101. }
  102. #=================================================
  103. # FUTURE OFFICIAL HELPERS
  104. #=================================================