reset_default_app 7.7 KB

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