upgrade 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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?rules_preset=template" \
  67. | grep -Po 'name="sid" value="\K([a-z0-9]{16})(?=")' )
  68. importState=$(curl -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
  69. --form "config=1" \
  70. --form "import_file=@$localpath" \
  71. --form rules[groups][createMissing]=1 \
  72. --form rules[templates][updateExisting]=1 \
  73. --form rules[templates][createMissing]=1 \
  74. --form rules[templateScreens][updateExisting]=1 \
  75. --form rules[templateScreens][createMissing]=1 \
  76. --form rules[templateLinkage][createMissing]=1 \
  77. --form rules[applications][createMissing]=1 \
  78. --form rules[items][updateExisting]=1 \
  79. --form rules[items][createMissing]=1 \
  80. --form rules[discoveryRules][updateExisting]=1 \
  81. --form rules[discoveryRules][createMissing]=1 \
  82. --form rules[triggers][updateExisting]=1 \
  83. --form rules[triggers][createMissing]=1 \
  84. --form rules[graphs][updateExisting]=1 \
  85. --form rules[graphs][createMissing]=1 \
  86. --form rules[httptests][updateExisting]=1 \
  87. --form rules[httptests][createMissing]=1 \
  88. --form rules[valueMaps][createMissing]=1 \
  89. --form "import=Import" \
  90. --form "backurl=templates.php" \
  91. --form "form_refresh=1" \
  92. --form "sid=${sid}" \ \
  93. "${zabbixFullpath}/conf.import.php?rules_preset=template" \
  94. | grep -c "Imported successfully")
  95. if [ "$importState" -eq "1" ];then
  96. ynh_print_info "Template Yunohost imported !"
  97. else
  98. ynh_print_warn "Template Yunohost not imported !"
  99. fi
  100. #apply template to host
  101. tokenapi=$(curl -s --header "Content-Type: application/json" --request POST --data '{ "jsonrpc": "2.0","method": "user.login","params": {"user": "Admin","password": "zabbix"},"id": 1,"auth": null}' ${zabbixFullpath}/api_jsonrpc.php | jq -r '.result')
  102. zabbixHostID=$(curl -s --header "Content-Type: application/json" --request POST --data '{"jsonrpc":"2.0","method":"host.get","params":{"filter":{"host":["Zabbix server"]}},"auth":"'$tokenapi'","id":1}' ${zabbixFullpath}/api_jsonrpc.php | jq -r '.result[0].hostid')
  103. zabbixTemplateID=$(curl -s --header "Content-Type: application/json" --request POST --data '{"jsonrpc":"2.0","method":"template.get","params":{"filter":{"host":["Template Yunohost"]}},"auth":"'$tokenapi'","id":1}' ${zabbixFullpath}/api_jsonrpc.php | jq -r '.result[0].templateid')
  104. applyTemplate=$(curl -s --header "Content-Type: application/json" --request POST --data '{"jsonrpc":"2.0","method":"host.massadd","params":{"hosts":[{"hostid":"'$zabbixHostID'"}],"templates":[{"templateid":"'$zabbixTemplateID'"}]},"auth":"'$tokenapi'","id":1}' http://mike.test/zabbix/api_jsonrpc.php | jq -r '.result.hostids[]')
  105. if [ "$applyTemplate" -eq "$zabbixHostID" ];then
  106. ynh_print_info "Template Yunohost linked to Zabbix server !"
  107. else
  108. ynh_print_warn "Template Yunohost no linked to Zabbix server !"
  109. fi
  110. #=================================================
  111. # Disable default admin for security issue
  112. #=================================================
  113. 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")
  114. if [ "$haveDefaultAdminDisabled" -eq 0 ] ;then
  115. ynh_print_info "Disable default admin"
  116. #disable default admin
  117. lastid=$(mysql -u$db_user -p$db_pwd $db_name -BN -e "SELECT max(id) from \`users_groups\`")
  118. lastid=$(($lastid + 1 ))
  119. mysql -u$db_user -p$db_pwd $db_name -e "INSERT INTO \`users_groups\` (\`id\` , \`usrgrpid\`, \`userid\`) VALUES ($lastid ,9, 1);"
  120. else
  121. ynh_print_info "default admin already disabled"
  122. fi
  123. #=================================================
  124. # CHECK THE PATH
  125. #=================================================
  126. # Normalize the URL path syntax
  127. path_url=$(ynh_normalize_url_path $path_url)
  128. #=================================================
  129. # STANDARD UPGRADE STEPS
  130. #=================================================
  131. ynh_package_update
  132. zabbixServerInstalledVersion=$(apt-cache policy zabbix-server-mysql | grep -Po "Installed: \K(.*)")
  133. zabbixServerCandidateVersion=$(apt-cache policy zabbix-server-mysql | grep -Po "Candidate: \K(.*)")
  134. zabbixFrontendInstalledVersion=$(apt-cache policy zabbix-frontend-php | grep -Po "Installed: \K(.*)")
  135. zabbixFrontendCandidateVersion=$(apt-cache policy zabbix-frontend-php | grep -Po "Candidate: \K(.*)")
  136. zabbixagentInstalledVersion=$(apt-cache policy zabbix-agent | grep -Po "Installed: \K(.*)")
  137. zabbixagentCandidateVersion=$(apt-cache policy zabbix-agent | grep -Po "Candidate: \K(.*)")
  138. if [ "$trustedversion" == "$zabbixServerCandidateVersion" ]
  139. then
  140. if [ "$zabbixServerInstalledVersion" != "$zabbixServerCandidateVersion" -o "$zabbixFrontendInstalledVersion" != "$zabbixFrontendCandidateVersion" -o "$zabbixagentInstalledVersion" != "$zabbixagentCandidateVersion" ]
  141. then
  142. #=================================================
  143. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  144. #=================================================
  145. # Backup the current version of the app
  146. ynh_backup_before_upgrade
  147. ynh_clean_setup () {
  148. # restore it if the upgrade fails
  149. ynh_restore_upgradebackup
  150. }
  151. # Exit if an error occurs during the execution of the script
  152. ynh_abort_if_errors
  153. cp -rp /etc/zabbix /tmp/
  154. cp -p /usr/share/zabbix/conf/zabbix.conf.php /tmp/
  155. DEBIAN_FRONTEND=noninteractive apt-mark unhold zabbix-server-mysql zabbix-frontend-php
  156. DEBIAN_FRONTEND=noninteractive apt-get -y download zabbix-frontend-php
  157. ar x *.deb
  158. tar xzf control.tar.gz
  159. sed -i 's/apache2 | httpd, //' control
  160. tar --ignore-failed-read -cvzf control.tar.gz {post,pre}{inst,rm} md5sums control
  161. ar rcs zabbix-frontend-php+stretch_all-noapache2.deb debian-binary control.tar.gz data.tar.xz
  162. dpkg -i zabbix-frontend-php+stretch_all-noapache2.deb
  163. rm -fr zabbix-*.deb
  164. apt-get -y --only-upgrade install zabbix-server-mysql zabbix-agent
  165. DEBIAN_FRONTEND=noninteractive apt-mark hold zabbix-server-mysql zabbix-frontend-php
  166. rm /usr/share/zabbix/conf/zabbix.conf.php
  167. cp -rpf /tmp/zabbix /etc/
  168. cp -pf /tmp/zabbix.conf.php /usr/share/zabbix/conf/
  169. rm -fr /tmp/zabbix*
  170. systemctl reload nginx
  171. else
  172. ynh_print_info "No update from repo ! (Already up to date)"
  173. fi
  174. else
  175. ynh_print_info "No update from repo ! (Trusted version)"
  176. fi