upgrade 11 KB

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