upgrade 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. # Load common variables for all scripts.
  10. source _variables
  11. #=================================================
  12. # LOAD SETTINGS
  13. #=================================================
  14. app=$YNH_APP_INSTANCE_NAME
  15. domain=$(ynh_app_setting_get $app domain)
  16. path_url=$(ynh_app_setting_get $app path)
  17. admin=$(ynh_app_setting_get $app admin)
  18. query_logging=$(ynh_app_setting_get $app query_logging)
  19. final_path=$(ynh_app_setting_get $app final_path)
  20. port=$(ynh_app_setting_get $app port)
  21. overwrite_setupvars=$(ynh_app_setting_get $app overwrite_setupvars)
  22. overwrite_ftl=$(ynh_app_setting_get $app overwrite_ftl)
  23. overwrite_nginx=$(ynh_app_setting_get $app overwrite_nginx)
  24. overwrite_phpfpm=$(ynh_app_setting_get $app overwrite_phpfpm)
  25. #=================================================
  26. # CHECK VERSION
  27. #=================================================
  28. upgrade_type=$(ynh_check_app_version_changed)
  29. #=================================================
  30. # ENSURE DOWNWARD COMPATIBILITY
  31. #=================================================
  32. # If overwrite_setupvars doesn't exist, create it
  33. if [ -z "$overwrite_setupvars" ]; then
  34. overwrite_setupvars=1
  35. ynh_app_setting_set $app overwrite_setupvars $overwrite_setupvars
  36. fi
  37. # If overwrite_ftl doesn't exist, create it
  38. if [ -z "$overwrite_ftl" ]; then
  39. overwrite_ftl=1
  40. ynh_app_setting_set $app overwrite_ftl $overwrite_ftl
  41. fi
  42. # If overwrite_nginx doesn't exist, create it
  43. if [ -z "$overwrite_nginx" ]; then
  44. overwrite_nginx=1
  45. ynh_app_setting_set $app overwrite_nginx $overwrite_nginx
  46. fi
  47. # If overwrite_phpfpm doesn't exist, create it
  48. if [ -z "$overwrite_phpfpm" ]; then
  49. overwrite_phpfpm=1
  50. ynh_app_setting_set $app overwrite_phpfpm $overwrite_phpfpm
  51. fi
  52. #=================================================
  53. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  54. #=================================================
  55. # Backup the current version of the app
  56. ynh_backup_before_upgrade
  57. ynh_clean_setup () {
  58. # restore it if the upgrade fails
  59. ynh_restore_upgradebackup
  60. }
  61. # Exit if an error occurs during the execution of the script
  62. ynh_abort_if_errors
  63. #=================================================
  64. # CHECK THE PATH
  65. #=================================================
  66. path_url=$(ynh_normalize_url_path $path_url) # Vérifie et corrige la syntaxe du path.
  67. #=================================================
  68. # ACTIVATE MAINTENANCE MODE
  69. #=================================================
  70. ynh_maintenance_mode_ON
  71. #=================================================
  72. # STANDARD UPGRADE STEPS
  73. #=================================================
  74. # INSTALL DEPENDENCIES
  75. #=================================================
  76. ynh_install_app_dependencies $app_depencencies
  77. #=================================================
  78. # DOWNLOAD, CHECK AND UNPACK SOURCE
  79. #=================================================
  80. pihole_local_repo="/etc/.pihole"
  81. if [ "$upgrade_type" == "UPGRADE_APP" ]
  82. then
  83. # Update la copie du repo de pihole (nécessaire pour Gravity)
  84. ynh_setup_source "$pihole_local_repo"
  85. # Update le dashboard admin
  86. ynh_setup_source "$final_path" admin_dashboard
  87. fi
  88. #=================================================
  89. # NGINX CONFIGURATION
  90. #=================================================
  91. # Overwrite the nginx configuration only if it's allowed
  92. if [ $overwrite_nginx -eq 1 ]
  93. then
  94. ynh_add_nginx_config
  95. fi
  96. #=================================================
  97. # CREATE DEDICATED USER
  98. #=================================================
  99. ynh_system_user_create $app # Create the dedicated user, if not exist
  100. #=================================================
  101. # PHP-FPM CONFIGURATION
  102. #=================================================
  103. # Overwrite the php-fpm configuration only if it's allowed
  104. if [ $overwrite_phpfpm -eq 1 ]
  105. then
  106. ynh_add_fpm_config # Créer le fichier de configuration du pool php-fpm et le configure.
  107. fi
  108. #=================================================
  109. # SPECIFIC UPGRADE
  110. #=================================================
  111. # UPDATE PI-HOLE SCRIPTS
  112. #=================================================
  113. # Update les scripts de Pi-hole
  114. pihole_dir="/opt/pihole"
  115. cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
  116. cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
  117. # And copy this fucking COL_TABLE file...
  118. cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
  119. #=================================================
  120. # Copy the Pi-hole main script
  121. #=================================================
  122. cp -a "$pihole_local_repo/pihole" /usr/local/bin/
  123. cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
  124. #=================================================
  125. # CREATE SUDOER FILE
  126. #=================================================
  127. # Cette configuration sudoers autorise pihole à exécuter /usr/local/bin/pihole en root sans mot de passe. Pas plus.
  128. cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
  129. echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
  130. chmod 0440 /etc/sudoers.d/pihole
  131. #=================================================
  132. # UPDATE LOGROTATE SCRIPT FOR PI-HOLE
  133. #=================================================
  134. pihole_storage="/etc/pihole"
  135. cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
  136. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  137. sed -i "/# su #/d;" "$pihole_storage/logrotate"
  138. #=================================================
  139. # UPDATE OF PIHOLE-FTL
  140. #=================================================
  141. ynh_systemd_action --action=stop --service_name=pihole-FTL
  142. if [ "$upgrade_type" == "UPGRADE_APP" ]
  143. then
  144. # Get the source of Pi-Hole-FTL
  145. FTL_temp_path=$(mktemp -d)
  146. ynh_setup_source "$FTL_temp_path" FTL
  147. # Plutôt que télécharger le binaire C, on le compile nous-même.
  148. ( cd "$FTL_temp_path"
  149. ynh_exec_warn_less make
  150. ynh_exec_warn_less make install )
  151. ynh_secure_remove "$FTL_temp_path"
  152. fi
  153. # Overwrite the pihole-FTL config file only if it's allowed
  154. if [ $overwrite_ftl -eq 1 ]
  155. then
  156. ynh_backup_if_checksum_is_different "$pihole_storage/pihole-FTL.conf" # Créé un backup du fichier de config si il a été modifié.
  157. cp "../conf/pihole-FTL.conf" "$pihole_storage"
  158. ynh_store_file_checksum "$pihole_storage/pihole-FTL.conf" # Enregistre la somme de contrôle du fichier de config
  159. fi
  160. cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
  161. chmod +x /etc/init.d/pihole-FTL
  162. ynh_exec_warn_less systemctl enable pihole-FTL
  163. #=================================================
  164. # BUILD THE VARIABLES FILE
  165. #=================================================
  166. setupVars="$pihole_storage/setupVars.conf"
  167. # Overwrite the setupVars config file only if it's allowed
  168. if [ $overwrite_setupvars -eq 1 ]
  169. then
  170. ynh_backup_if_checksum_is_different "$setupVars" # Créé un backup du fichier de config si il a été modifié.
  171. # Trouve l'interface réseau par défaut
  172. main_iface=$(ip route | grep default | awk '{print $5;}')
  173. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  174. echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
  175. echo "IPV6_ADDRESS=" >> $setupVars
  176. echo "PIHOLE_DNS_1=" >> $setupVars
  177. echo "PIHOLE_DNS_2=" >> $setupVars
  178. if [ $query_logging -eq 1 ]; then
  179. query_logging=true
  180. else
  181. query_logging=false
  182. fi
  183. echo "QUERY_LOGGING=$query_logging" >> $setupVars
  184. echo "INSTALL_WEB=true" >> $setupVars
  185. ynh_store_file_checksum "$setupVars" # Enregistre la somme de contrôle du fichier de config
  186. fi
  187. #=================================================
  188. # UPDATE THE CRON JOB
  189. #=================================================
  190. cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
  191. # Remove git usage for version. Which fails because we use here a release instead of master.
  192. ynh_replace_string ".*updatechecker.*" "#&" /etc/cron.d/pihole
  193. #=================================================
  194. # START PIHOLE-FTL
  195. #=================================================
  196. ynh_systemd_action --action=restart --service_name=pihole-FTL
  197. #=================================================
  198. # UPDATE THE CONF_REGEN HOOK
  199. #=================================================
  200. cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
  201. #=================================================
  202. # RELOAD NGINX
  203. #=================================================
  204. ynh_systemd_action --action=reload --service_name=nginx
  205. #=================================================
  206. # DEACTIVE MAINTENANCE MODE
  207. #=================================================
  208. ynh_maintenance_mode_OFF