reset_default_app 8.8 KB

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