reset_default_app 8.7 KB

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