upgrade 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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="1:4.0.4-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) #not used
  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) #not used
  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. export mysqlconn="mysql -u $db_user -p $db_pwd $db_name"
  45. #=================================================
  46. # Enable default admin temporaly
  47. #=================================================
  48. enable_admin_user
  49. #=================================================
  50. # Import Yunohost template
  51. #=================================================
  52. ynh_print_info "Import Yunohost template"
  53. #disable sso temporaly
  54. ynh_app_setting_set "$app" unprotected_uris "/"
  55. systemctl reload nginx
  56. yunohost app ssowatconf
  57. zabbixFullpath=https://$domain$path_url
  58. localpath=$(find /var/cache/yunohost/ -name "Template_Yunohost.xml")
  59. sudoUserPpath=$(find /var/cache/yunohost/ -name "etc_sudoers.d_zabbix")
  60. confUserPpath=$(find /var/cache/yunohost/ -name "etc_zabbix_zabbix_agentd.d_userP_yunohost.conf")
  61. bashUserPpath=$(find /var/cache/yunohost/ -name "etc_zabbix_zabbix_agentd.d_yunohost.sh")
  62. cp "$sudoUserPpath" /etc/sudoers.d/zabbix
  63. cp "$confUserPpath" /etc/zabbix/zabbix_agentd.d/userP_yunohost.conf
  64. cp "$bashUserPpath" /etc/zabbix/zabbix_agentd.d/yunohost.sh
  65. chmod a+x /etc/zabbix/zabbix_agentd.d/yunohost.sh
  66. systemctl restart zabbix-agent
  67. curl -L -k -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
  68. --form "enter=Sign+in" \
  69. --form "name=Admin" \
  70. --form "password=zabbix" \
  71. "$zabbixFullpath/index.php"
  72. sid=$(curl -k -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
  73. "$zabbixFullpath/conf.import.php?rules_preset=template" \
  74. | grep -Po 'name="sid" value="\K([a-z0-9]{16})(?=")' )
  75. importState=$(curl -k -s --cookie cookiejar.txt --cookie-jar cookiejar.txt \
  76. --form "config=1" \
  77. --form "import_file=@$localpath" \
  78. --form "rules[groups][createMissing]=1" \
  79. --form "rules[templates][updateExisting]=1" \
  80. --form "rules[templates][createMissing]=1" \
  81. --form "rules[templateScreens][updateExisting]=1" \
  82. --form "rules[templateScreens][createMissing]=1" \
  83. --form "rules[templateLinkage][createMissing]=1" \
  84. --form "rules[applications][createMissing]=1" \
  85. --form "rules[items][updateExisting]=1" \
  86. --form "rules[items][createMissing]=1" \
  87. --form "rules[discoveryRules][updateExisting]=1" \
  88. --form "rules[discoveryRules][createMissing]=1" \
  89. --form "rules[triggers][updateExisting]=1" \
  90. --form "rules[triggers][createMissing]=1" \
  91. --form "rules[graphs][updateExisting]=1" \
  92. --form "rules[graphs][createMissing]=1" \
  93. --form "rules[httptests][updateExisting]=1" \
  94. --form "rules[httptests][createMissing]=1" \
  95. --form "rules[valueMaps][createMissing]=1" \
  96. --form "import=Import" \
  97. --form "backurl=templates.php" \
  98. --form "form_refresh=1" \
  99. --form "sid=${sid}" \ \
  100. "${zabbixFullpath}/conf.import.php?rules_preset=template" \
  101. | grep -c "Imported successfully")
  102. if [ "$importState" -eq "1" ];then
  103. ynh_print_info "Template Yunohost imported !"
  104. else
  105. ynh_print_warn "Template Yunohost not imported !"
  106. fi
  107. #apply template to host
  108. tokenapi=$(curl -k -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')
  109. zabbixHostID=$(curl -k -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')
  110. zabbixTemplateID=$(curl -k -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')
  111. applyTemplate=$(curl -k -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}' "${zabbixFullpath}/api_jsonrpc.php" | jq -r '.result.hostids[]')
  112. if [ "$applyTemplate" -eq "$zabbixHostID" ];then
  113. ynh_print_info "Template Yunohost linked to Zabbix server !"
  114. else
  115. ynh_print_warn "Template Yunohost no linked to Zabbix server !"
  116. fi
  117. #=================================================
  118. # Disable default admin for security issue
  119. #=================================================
  120. ynh_print_info "disable default admin"
  121. disable_admin_user
  122. #=================================================
  123. # Disable default guest for security issue
  124. #=================================================
  125. ynh_print_info "disable default guest"
  126. disable_guest_user
  127. #=================================================
  128. # CHECK THE PATH
  129. #=================================================
  130. # Normalize the URL path syntax
  131. path_url=$(ynh_normalize_url_path "$path_url")
  132. #=================================================
  133. # STANDARD UPGRADE STEPS
  134. #=================================================
  135. ynh_print_info "Check if new zabbix version is available on repo"
  136. ynh_package_update
  137. #REMOVE DUPLICATE LOG ENTRY IN LOGROTATE PATCH IF NEEDED
  138. ynh_remove_logrotate
  139. zabbixServerInstalledVersion=$(apt-cache policy zabbix-server-mysql | grep -Po "Installed: \K(.*)")
  140. zabbixServerCandidateVersion=$(apt-cache policy zabbix-server-mysql | grep -Po "Candidate: \K(.*)")
  141. zabbixFrontendInstalledVersion=$(apt-cache policy zabbix-frontend-php | grep -Po "Installed: \K(.*)")
  142. zabbixFrontendCandidateVersion=$(apt-cache policy zabbix-frontend-php | grep -Po "Candidate: \K(.*)")
  143. zabbixagentInstalledVersion=$(apt-cache policy zabbix-agent | grep -Po "Installed: \K(.*)")
  144. zabbixagentCandidateVersion=$(apt-cache policy zabbix-agent | grep -Po "Candidate: \K(.*)")
  145. if [ "$trustedversion" == "$zabbixServerCandidateVersion" ]
  146. then
  147. if [ "$zabbixServerInstalledVersion" != "$zabbixServerCandidateVersion" ] || [ "$zabbixFrontendInstalledVersion" != "$zabbixFrontendCandidateVersion" ] || [ "$zabbixagentInstalledVersion" != "$zabbixagentCandidateVersion" ]
  148. then
  149. #=================================================
  150. # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
  151. #=================================================
  152. # Backup the current version of the app
  153. ynh_backup_before_upgrade
  154. ynh_clean_setup () {
  155. # restore it if the upgrade fails
  156. ynh_restore_upgradebackup
  157. }
  158. # Exit if an error occurs during the execution of the script
  159. ynh_abort_if_errors
  160. cp -rp /etc/zabbix /tmp/
  161. cp -p /usr/share/zabbix/conf/zabbix.conf.php /tmp/
  162. DEBIAN_FRONTEND=noninteractive apt-mark unhold zabbix-server-mysql zabbix-frontend-php
  163. DEBIAN_FRONTEND=noninteractive apt-get -y download zabbix-frontend-php
  164. ar x ./*.deb
  165. tar xzf control.tar.gz
  166. ynh_replace_string "apache2 | httpd, " "" control
  167. tar --ignore-failed-read -cvzf control.tar.gz {post,pre}{inst,rm} md5sums control
  168. ar rcs zabbix-frontend-php+stretch_all-noapache2.deb debian-binary control.tar.gz data.tar.xz
  169. dpkg -i zabbix-frontend-php+stretch_all-noapache2.deb
  170. rm -fr zabbix-*.deb
  171. apt-get -y --only-upgrade install zabbix-server-mysql zabbix-agent
  172. DEBIAN_FRONTEND=noninteractive apt-mark hold zabbix-server-mysql zabbix-frontend-php
  173. rm /usr/share/zabbix/conf/zabbix.conf.php
  174. cp -rpf /tmp/zabbix /etc/
  175. cp -pf /tmp/zabbix.conf.php /usr/share/zabbix/conf/
  176. rm -fr /tmp/zabbix*
  177. else
  178. ynh_print_info "No update from repo ! (Already up to date)"
  179. fi
  180. else
  181. ynh_print_info "No update from repo ! (Trusted version)"
  182. fi
  183. #=================================================
  184. # RE-ENABLE SSOWAT
  185. #=================================================
  186. ynh_print_info "re-enable SSOWAT"
  187. # Make app private if necessary
  188. if [ $is_public -eq 0 ]
  189. then
  190. # unprotected_uris allows SSO credentials to be passed anyway.
  191. ynh_app_setting_delete "$app" unprotected_uris
  192. else
  193. ynh_app_setting_set "$app" unprotected_uris "/"
  194. fi
  195. systemctl reload nginx
  196. yunohost app ssowatconf