reset_default_app 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. pihole_version="$(ynh_app_setting_get --app=$app --key=pihole_version)"
  28. #=================================================
  29. # SPECIFIC ACTION
  30. #=================================================
  31. # ACTIVATE MAINTENANCE MODE
  32. #=================================================
  33. ynh_script_progression --message="Activating maintenance mode..." --weight=1
  34. ynh_maintenance_mode_ON
  35. #=================================================
  36. # CREATE DEDICATED USER
  37. #=================================================
  38. ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
  39. # Create a dedicated user (if not existing)
  40. ynh_system_user_create --username=$app
  41. #=================================================
  42. # DOWNLOAD, CHECK AND UNPACK SOURCE
  43. #=================================================
  44. ynh_script_progression --message="Resetting source files..." --weight=1
  45. # Download, check integrity, uncompress and patch the source from app.src
  46. pihole_local_repo="/etc/.pihole"
  47. (cd scripts
  48. if [ "$pihole_version" == "Last 3.X" ]
  49. then
  50. # Overwrite the version 3.3.1
  51. YNH_CWD=$PWD ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app_3
  52. # Overwrite admin dashboard
  53. YNH_CWD=$PWD ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard_3
  54. else
  55. # Overwrite the last version available
  56. YNH_CWD=$PWD ynh_setup_source --dest_dir="$pihole_local_repo" --source_id=app_last
  57. # Overwrite admin dashboard
  58. YNH_CWD=$PWD ynh_setup_source --dest_dir="$final_path" --source_id=admin_dashboard_last
  59. fi
  60. chown $app:www-data "$final_path"
  61. )
  62. #=================================================
  63. # NGINX CONFIGURATION
  64. #=================================================
  65. ynh_script_progression --message="Resetting NGINX web server configuration..." --weight=1
  66. # Create a dedicated nginx config
  67. yunohost app action run $app reset_default_nginx
  68. #=================================================
  69. # PHP-FPM CONFIGURATION
  70. #=================================================
  71. ynh_script_progression --message="Resetting PHP-FPM configuration..." --weight=1
  72. # Create a dedicated php-fpm config
  73. yunohost app action run $app reset_default_phpfpm
  74. #=================================================
  75. # RECREATE DIRECTORIES
  76. #=================================================
  77. ynh_script_progression --message="Recreating and populating directories..." --weight=1
  78. pihole_storage="/etc/pihole"
  79. mkdir -p "$pihole_storage"
  80. chown $app: -R "$pihole_storage"
  81. pihole_dir="/opt/pihole"
  82. mkdir -p "$pihole_dir"
  83. # Make a copy of Pi-Hole scripts
  84. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  85. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  86. # And copy this fucking COL_TABLE file...
  87. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  88. #=================================================
  89. # COPY PI-HOLE MAIN SCRIPT
  90. #=================================================
  91. ynh_script_progression --message="Copying Pi-Hole main script..."
  92. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  93. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  94. #=================================================
  95. # RECREATE LOG FILES
  96. #=================================================
  97. touch /var/log/{pihole,pihole-FTL}.log
  98. chmod 644 /var/log/{pihole,pihole-FTL}.log
  99. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  100. chown $dnsmasq_user:root /var/log/{pihole,pihole-FTL}.log
  101. #=================================================
  102. # RECREATE SUDOER FILE
  103. #=================================================
  104. # This sudoers config allow pihole to execute /usr/local/bin/pihole as root without password. Nothing more.
  105. if [ "$pihole_version" == "Last 3.X" ]
  106. then
  107. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  108. else
  109. cp "$pihole_local_repo/advanced/Templates/pihole.sudo" /etc/sudoers.d/pihole
  110. fi
  111. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  112. # echo "Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin" >> /etc/sudoers.d/pihole
  113. chmod 0440 /etc/sudoers.d/pihole
  114. #=================================================
  115. # REINSTALL LOGROTATE SCRIPT FOR PI-HOLE
  116. #=================================================
  117. if [ "$pihole_version" == "Last 3.X" ]
  118. then
  119. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  120. else
  121. cp "$pihole_local_repo/advanced/Templates/logrotate" "$pihole_storage/logrotate"
  122. fi
  123. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  124. #=================================================
  125. # REINSTALLATION OF PIHOLE-FTL
  126. #=================================================
  127. ynh_script_progression --message="Reinstalling PiHole-FTL..." --weight=30
  128. # Get the source of Pi-Hole-FTL
  129. FTL_temp_path=$(mktemp -d)
  130. if [ "$pihole_version" == "Last 3.X" ]
  131. then
  132. # Install the version 3.3.1
  133. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL_3
  134. else
  135. # Install the last version available
  136. ynh_setup_source --dest_dir="$FTL_temp_path" --source_id=FTL_last
  137. fi
  138. # Instead of downloading a binary file, we're going to compile it
  139. ( cd "$FTL_temp_path"
  140. ynh_exec_warn_less make
  141. ynh_exec_warn_less make install )
  142. ynh_secure_remove --file="$FTL_temp_path"
  143. cp "../conf/dns-servers.conf" "$pihole_storage"
  144. # Restore the default pihole-FTL.conf
  145. yunohost app action run $app reset_default_ftl
  146. if [ "$pihole_version" == "Last 3.X" ]
  147. then
  148. # Version 3.3.1
  149. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  150. chmod +x /etc/init.d/pihole-FTL
  151. ynh_exec_warn_less systemctl enable pihole-FTL
  152. else
  153. cp -a $pihole_local_repo/advanced/Templates/pihole-FTL.service /etc/init.d/pihole-FTL
  154. chmod +x /etc/init.d/pihole-FTL
  155. ynh_exec_warn_less systemctl enable pihole-FTL
  156. # Reload systemd config
  157. systemctl daemon-reload
  158. fi
  159. #=================================================
  160. # RESET THE VARIABLES FILE
  161. #=================================================
  162. # Restore the default setupVars.conf
  163. yunohost app action run $app reset_default_setupvars
  164. #=================================================
  165. # RESET DNSMASQ CONFIG
  166. #=================================================
  167. # Restore the default setupVars.conf
  168. yunohost app action run $app reset_default_dnsmasq
  169. #=================================================
  170. # REINSTALL CRON JOB
  171. #=================================================
  172. if [ "$pihole_version" == "Last 3.X" ]
  173. then
  174. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  175. else
  176. cp $pihole_local_repo/advanced/Templates/pihole.cron /etc/cron.d/pihole
  177. fi
  178. # Remove git usage for version. Which fails because we use here a release instead of master.
  179. ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole
  180. #=================================================
  181. # REINSTALL CONF_REGEN HOOK
  182. #=================================================
  183. (
  184. cd scripts
  185. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  186. ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  187. )
  188. #=================================================
  189. # RESTART PIHOLE-FTL
  190. #=================================================
  191. ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2
  192. ynh_systemd_action --action=restart --service_name=pihole-FTL
  193. #=================================================
  194. # RELOAD NGINX
  195. #=================================================
  196. ynh_script_progression --message="Reloading NGINX web server..." --weight=1
  197. ynh_systemd_action --service_name=nginx --action=reload
  198. #=================================================
  199. # DEACTIVE MAINTENANCE MODE
  200. #=================================================
  201. ynh_script_progression --message="Disabling maintenance mode..." --weight=1
  202. ynh_maintenance_mode_OFF
  203. #=================================================
  204. # END OF SCRIPT
  205. #=================================================
  206. ynh_script_progression --message="Execution completed" --last