upgrade 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC START
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source _common.sh
  8. source /usr/share/yunohost/helpers
  9. #=================================================
  10. # LOAD SETTINGS
  11. #=================================================
  12. app=$YNH_APP_INSTANCE_NAME
  13. trustedversion="4.0.3-1+stretch"
  14. domain=$(ynh_app_setting_get $app domain)
  15. path_url=$(ynh_app_setting_get $app path)
  16. admin=$(ynh_app_setting_get $app admin)
  17. is_public=$(ynh_app_setting_get $app is_public)
  18. final_path=$(ynh_app_setting_get $app final_path)
  19. language=$(ynh_app_setting_get $app language)
  20. db_name=$(ynh_app_setting_get $app db_name)
  21. db_user=$(ynh_app_setting_get $app db_user)
  22. db_pwd=$(ynh_app_setting_get $app mysqlpwd)
  23. #=================================================
  24. # ENSURE DOWNWARD COMPATIBILITY
  25. #=================================================
  26. # Fix is_public as a boolean value
  27. if [ "$is_public" = "Yes" ]; then
  28. ynh_app_setting_set $app is_public 1
  29. is_public=1
  30. elif [ "$is_public" = "No" ]; then
  31. ynh_app_setting_set $app is_public 0
  32. is_public=0
  33. fi
  34. # If db_name doesn't exist, create it
  35. if [ -z $db_name ]; then
  36. db_name=$(ynh_sanitize_dbid $app)
  37. ynh_app_setting_set $app db_name $db_name
  38. fi
  39. # If final_path doesn't exist, create it
  40. if [ -z $final_path ]; then
  41. final_path=/var/www/$app
  42. ynh_app_setting_set $app final_path $final_path
  43. fi
  44. #=================================================
  45. # Enable default admin temporaly
  46. #=================================================
  47. haveDefaultAdminDisabled=$(mysql -BN -u$db_user -p$db_pwd $db_name -BN -e "SELECT count(id) from \`users_groups\` where userid=1 and usrgrpid=9")
  48. if [ "$haveDefaultAdminDisabled" -eq 1 ] ;then
  49. echo "Enable default admin"
  50. #enable default admin temporaly
  51. mysql -u$db_user -p$db_pwd $db_name -e "DELETE FROM users_groups where usrgrpid=9 and userid=1;"
  52. else
  53. echo "default admin already enabled"
  54. fi
  55. #=================================================
  56. # Import Yunohost template
  57. #=================================================
  58. zabbixFullpath=https://$domain/$path_url
  59. localpath=$(find /var/cache/yunohost/ -name "Template_Yunohost.xml")
  60. curl -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
  61. --form "enter=Sign+in" \
  62. --form "name=Admin" \
  63. --form "password=zabbix" \
  64. "$zabbixFullpath/index.php"
  65. sid=$(curl -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
  66. "$zabbixFullpath/conf.import.php" \
  67. |grep sid |head -n 1 \
  68. |awk -Fsid= '{print $2}' \
  69. |awk -F\" '{print $1}' \
  70. |cut -c -16 )
  71. curl -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
  72. --form "config=1" \
  73. --form "import_file=@$localpath" \
  74. --form rules[groups][createMissing]=1 \
  75. --form rules[templates][updateExisting]=1 \
  76. --form rules[templates][createMissing]=1 \
  77. --form rules[templateScreens][updateExisting]=1 \
  78. --form rules[templateScreens][createMissing]=1 \
  79. --form rules[templateLinkage][createMissing]=1 \
  80. --form rules[applications][createMissing]=1 \
  81. --form rules[items][updateExisting]=1 \
  82. --form rules[items][createMissing]=1 \
  83. --form rules[discoveryRules][updateExisting]=1 \
  84. --form rules[discoveryRules][createMissing]=1 \
  85. --form rules[triggers][updateExisting]=1 \
  86. --form rules[triggers][createMissing]=1 \
  87. --form rules[graphs][updateExisting]=1 \
  88. --form rules[graphs][createMissing]=1 \
  89. --form rules[httptests][updateExisting]=1 \
  90. --form rules[httptests][createMissing]=1 \
  91. --form rules[valueMaps][createMissing]=1 \
  92. --form "import=Import" \
  93. --form "backurl=templates.php" \
  94. --form "form_refresh=1" \
  95. --form "sid=${sid}" \ \
  96. 'https://$zabbixFullpath/conf.import.php?rules_preset=template'
  97. #=================================================
  98. # Disable default admin for security issue
  99. #=================================================
  100. haveDefaultAdminDisabled=$(mysql -BN -u$db_user -p$db_pwd $db_name -BN -e "SELECT count(id) from \`users_groups\` where userid=1 and usrgrpid=9")
  101. if [ "$haveDefaultAdminDisabled" -eq 0 ] ;then
  102. ynh_print_info "Disable default admin"
  103. #disable default admin
  104. lastid=$(mysql -u$db_user -p$db_pwd $db_name -BN -e "SELECT max(id) from \`users_groups\`")
  105. lastid=$(($lastid + 1 ))
  106. mysql -u$db_user -p$db_pwd $db_name -e "INSERT INTO \`users_groups\` (\`id\` , \`usrgrpid\`, \`userid\`) VALUES ($lastid ,9, 1);"
  107. else
  108. ynh_print_info "default admin already disabled"
  109. fi
  110. #=================================================
  111. # CHECK THE PATH
  112. #=================================================
  113. # Normalize the URL path syntax
  114. path_url=$(ynh_normalize_url_path $path_url)
  115. #=================================================
  116. # STANDARD UPGRADE STEPS
  117. #=================================================
  118. ynh_package_update
  119. zabbixServerInstalledVersion=$(apt-cache policy zabbix-server-mysql | grep -Po "Installed: \K(.*)")
  120. zabbixServerCandidateVersion=$(apt-cache policy zabbix-server-mysql | grep -Po "Candidate: \K(.*)")
  121. zabbixFrontendInstalledVersion=$(apt-cache policy zabbix-frontend-php | grep -Po "Installed: \K(.*)")
  122. zabbixFrontendCandidateVersion=$(apt-cache policy zabbix-frontend-php | grep -Po "Candidate: \K(.*)")
  123. zabbixagentInstalledVersion=$(apt-cache policy zabbix-agent | grep -Po "Installed: \K(.*)")
  124. zabbixagentCandidateVersion=$(apt-cache policy zabbix-agent | grep -Po "Candidate: \K(.*)")
  125. if [ "$trustedversion" == "$zabbixServerCandidateVersion" ]
  126. then
  127. if [ "$zabbixServerInstalledVersion" != "$zabbixServerCandidateVersion" -o "$zabbixFrontendInstalledVersion" != "$zabbixFrontendCandidateVersion" -o "$zabbixagentInstalledVersion" != "$zabbixagentCandidateVersion" ]
  128. then
  129. #=================================================
  130. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  131. #=================================================
  132. # Backup the current version of the app
  133. ynh_backup_before_upgrade
  134. ynh_clean_setup () {
  135. # restore it if the upgrade fails
  136. ynh_restore_upgradebackup
  137. }
  138. # Exit if an error occurs during the execution of the script
  139. ynh_abort_if_errors
  140. cp -rp /etc/zabbix /tmp/
  141. cp -p /usr/share/zabbix/conf/zabbix.conf.php /tmp/
  142. DEBIAN_FRONTEND=noninteractive apt-mark unhold zabbix-server-mysql zabbix-frontend-php
  143. DEBIAN_FRONTEND=noninteractive apt-get -y download zabbix-frontend-php
  144. ar x *.deb
  145. tar xzf control.tar.gz
  146. sed -i 's/apache2 | httpd, //' control
  147. tar --ignore-failed-read -cvzf control.tar.gz {post,pre}{inst,rm} md5sums control
  148. ar rcs zabbix-frontend-php+stretch_all-noapache2.deb debian-binary control.tar.gz data.tar.xz
  149. dpkg -i zabbix-frontend-php+stretch_all-noapache2.deb
  150. rm -fr zabbix-*.deb
  151. apt-get -y --only-upgrade install zabbix-server-mysql zabbix-agent
  152. DEBIAN_FRONTEND=noninteractive apt-mark hold zabbix-server-mysql zabbix-frontend-php
  153. rm /usr/share/zabbix/conf/zabbix.conf.php
  154. cp -rpf /tmp/zabbix /etc/
  155. cp -pf /tmp/zabbix.conf.php /usr/share/zabbix/conf/
  156. rm -fr /tmp/zabbix*
  157. systemctl reload nginx
  158. else
  159. ynh_print_info "No update from repo ! (Already up to date)"
  160. fi
  161. else
  162. ynh_print_info "No update from repo ! (Trusted version)"
  163. fi