reset_default_app 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. # Load common variables for all scripts.
  8. source scripts/_variables
  9. source scripts/_common.sh
  10. source /usr/share/yunohost/helpers
  11. #=================================================
  12. # MANAGE SCRIPT FAILURE
  13. #=================================================
  14. ynh_clean_setup () {
  15. # Clean installation remaining that are not handle by the remove script.
  16. ynh_clean_check_starting
  17. }
  18. # Exit if an error occurs during the execution of the script
  19. ynh_abort_if_errors
  20. #=================================================
  21. # RETRIEVE ARGUMENTS
  22. #=================================================
  23. app=$YNH_APP_INSTANCE_NAME
  24. path_url=$(ynh_app_setting_get --app=$app --key=path)
  25. domain=$(ynh_app_setting_get --app=$app --key=domain)
  26. final_path=$(ynh_app_setting_get --app=$app --key=final_path)
  27. #=================================================
  28. # SPECIFIC ACTION
  29. #=================================================
  30. # ACTIVATE MAINTENANCE MODE
  31. #=================================================
  32. ynh_script_progression --message="Activating maintenance mode..." --weight=1
  33. ynh_maintenance_mode_ON
  34. #=================================================
  35. # CREATE DEDICATED USER
  36. #=================================================
  37. ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
  38. # Create a dedicated user (if not existing)
  39. ynh_system_user_create --username=$app
  40. #=================================================
  41. # DOWNLOAD, CHECK AND UNPACK SOURCE
  42. #=================================================
  43. ynh_script_progression --message="Resetting source files..." --weight=1
  44. # Download, check integrity, uncompress and patch the source from app.src
  45. pihole_local_repo="/etc/.pihole"
  46. (
  47. cd scripts
  48. # Overwrite the last version available
  49. YNH_CWD=$PWD ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app
  50. # Overwrite admin dashboard
  51. YNH_CWD=$PWD ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard
  52. chown $app:www-data "$final_path"
  53. )
  54. #=================================================
  55. # NGINX CONFIGURATION
  56. #=================================================
  57. ynh_script_progression --message="Resetting NGINX web server configuration..." --weight=1
  58. # Create a dedicated nginx config
  59. yunohost app action run $app reset_default_nginx
  60. #=================================================
  61. # PHP-FPM CONFIGURATION
  62. #=================================================
  63. ynh_script_progression --message="Resetting PHP-FPM configuration..." --weight=1
  64. # Create a dedicated php-fpm config
  65. yunohost app action run $app reset_default_phpfpm
  66. #=================================================
  67. # RECREATE DIRECTORIES
  68. #=================================================
  69. ynh_script_progression --message="Recreating and populating directories..." --weight=1
  70. pihole_storage="/etc/pihole"
  71. mkdir -p "$pihole_storage"
  72. chown $app: -R "$pihole_storage"
  73. pihole_dir="/opt/pihole"
  74. mkdir -p "$pihole_dir"
  75. # Make a copy of Pi-Hole scripts
  76. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  77. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  78. # And copy this fucking COL_TABLE file...
  79. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  80. #=================================================
  81. # COPY PI-HOLE MAIN SCRIPT
  82. #=================================================
  83. ynh_script_progression --message="Copying Pi-Hole main script..."
  84. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  85. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  86. #=================================================
  87. # RECREATE LOG FILES
  88. #=================================================
  89. touch /var/log/{pihole,pihole-FTL}.log
  90. chmod 644 /var/log/{pihole,pihole-FTL}.log
  91. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  92. chown $dnsmasq_user:root /var/log/{pihole,pihole-FTL}.log
  93. #=================================================
  94. # RECREATE SUDOER FILE
  95. #=================================================
  96. # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more.
  97. cp "$pihole_local_repo/advanced/Templates/pihole.sudo" /etc/sudoers.d/pihole
  98. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  99. # echo "Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin" >> /etc/sudoers.d/pihole
  100. chmod 0440 /etc/sudoers.d/pihole
  101. #=================================================
  102. # REINSTALL LOGROTATE SCRIPT FOR PI-HOLE
  103. #=================================================
  104. cp "$pihole_local_repo/advanced/Templates/logrotate" "$pihole_storage/logrotate"
  105. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  106. #=================================================
  107. # REINSTALLATION OF PIHOLE-FTL
  108. #=================================================
  109. ynh_script_progression --message="Reinstalling PiHole-FTL..." --weight=30
  110. # Get the source of Pi-Hole-FTL
  111. FTL_temp_path=$(mktemp -d)
  112. # Install the last version available
  113. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL
  114. # Instead of downloading a binary file, we're going to compile it
  115. (
  116. cd "$FTL_temp_path"
  117. ynh_exec_warn_less make
  118. ynh_exec_warn_less make install
  119. )
  120. ynh_secure_remove --file="$FTL_temp_path"
  121. cp "../conf/dns-servers.conf" "$pihole_storage"
  122. # Restore the default pihole-FTL.conf
  123. yunohost app action run $app reset_default_ftl
  124. cp -a $pihole_local_repo/advanced/Templates/pihole-FTL.service /etc/init.d/pihole-FTL
  125. chmod +x /etc/init.d/pihole-FTL
  126. ynh_exec_warn_less systemctl enable pihole-FTL
  127. # Reload systemd config
  128. systemctl daemon-reload
  129. #=================================================
  130. # RESET THE VARIABLES FILE
  131. #=================================================
  132. # Restore the default setupVars.conf
  133. yunohost app action run $app reset_default_setupvars
  134. #=================================================
  135. # RESET DNSMASQ CONFIG
  136. #=================================================
  137. # Restore the default setupVars.conf
  138. yunohost app action run $app reset_default_dnsmasq
  139. #=================================================
  140. # REINSTALL CRON JOB
  141. #=================================================
  142. cp $pihole_local_repo/advanced/Templates/pihole.cron /etc/cron.d/pihole
  143. # Remove git usage for version. Which fails because we use here a release instead of master.
  144. ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole
  145. #=================================================
  146. # REINSTALL CONF_REGEN HOOK
  147. #=================================================
  148. (
  149. cd scripts
  150. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  151. ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  152. )
  153. #=================================================
  154. # RESTART PIHOLE-FTL
  155. #=================================================
  156. ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2
  157. ynh_systemd_action --action=restart --service_name=pihole-FTL
  158. #=================================================
  159. # RELOAD NGINX
  160. #=================================================
  161. ynh_script_progression --message="Reloading NGINX web server..." --weight=1
  162. ynh_systemd_action --service_name=nginx --action=reload
  163. #=================================================
  164. # DEACTIVE MAINTENANCE MODE
  165. #=================================================
  166. ynh_script_progression --message="Disabling maintenance mode..." --weight=1
  167. ynh_maintenance_mode_OFF
  168. #=================================================
  169. # END OF SCRIPT
  170. #=================================================
  171. ynh_script_progression --message="Execution completed" --last