reset_default_app 7.8 KB

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