update_core.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/bash
  2. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  3. touch /var/run/update_core
  4. binpath=$(uci get AdGuardHome.AdGuardHome.binpath)
  5. if [ -z "$binpath" ]; then
  6. uci get AdGuardHome.AdGuardHome.binpath="/tmp/AdGuardHome/AdGuardHome"
  7. binpath="/tmp/AdGuardHome/AdGuardHome"
  8. fi
  9. mkdir -p ${binpath%/*}
  10. configpath=$(uci get AdGuardHome.AdGuardHome.configpath)
  11. if [ -z "$configpath" ]; then
  12. uci get AdGuardHome.AdGuardHome.configpath="/etc/AdGuardHome.yaml"
  13. configpath="/etc/AdGuardHome.yaml"
  14. fi
  15. mkdir -p ${configpath%/*}
  16. check_if_already_running(){
  17. running_tasks="$(ps |grep "AdGuardHome" |grep "update_core" |grep -v "grep" |awk '{print $1}' |wc -l)"
  18. [ "${running_tasks}" -gt "2" ] && echo -e "\nA task is already running." >>/tmp/AdGuardHome_update.log && rm /var/run/update_core && exit 2
  19. }
  20. clean_log(){
  21. echo "" > /tmp/AdGuardHome_update.log
  22. }
  23. check_latest_version(){
  24. latest_ver="$(wget -O- https://api.github.com/repos/AdguardTeam/AdGuardHome/releases/latest 2>/dev/null|grep -E 'tag_name' |grep -E 'v[0-9.]+' -o 2>/dev/null)"
  25. [ -z "${latest_ver}" ] && echo -e "\nFailed to check latest version, please try again later." >>/tmp/AdGuardHome_update.log && exit 1
  26. if [ -f "$configpath" ]; then
  27. now_ver="$($binpath -c $configpath --check-config 2>&1| grep -E 'v[0-9.]+' -o)"
  28. else
  29. now_ver=$(uci get AdGuardHome.AdGuardHome.version)
  30. fi
  31. if [ "${latest_ver}"x != "${now_ver}"x ]; then
  32. clean_log
  33. echo -e "Local version: ${now_ver}., cloud version: ${latest_ver}." >>/tmp/AdGuardHome_update.log
  34. doupdate_core
  35. else
  36. echo -e "\nLocal version: ${now_ver}, cloud version: ${latest_ver}." >>/tmp/AdGuardHome_update.log
  37. echo -e "You're already using the latest version." >>/tmp/AdGuardHome_update.log
  38. uci set AdGuardHome.AdGuardHome.version="${latest_ver}"
  39. rm /var/run/update_core
  40. exit 3
  41. fi
  42. }
  43. doupdate_core(){
  44. echo -e "Updating core..." >>/tmp/AdGuardHome_update.log
  45. mkdir -p "/tmp/AdGuardHome/update" >/dev/null 2>&1
  46. rm -rf /tmp/AdGuardHome/update/* >/dev/null 2>&1
  47. Archt="$(opkg info kernel | grep Architecture | awk -F "[ _]" '{print($2)}')"
  48. case $Archt in
  49. "i386")
  50. Arch="386"
  51. ;;
  52. "x86")
  53. Arch="amd64"
  54. ;;
  55. "mipsel")
  56. Arch="mipsle"
  57. ;;
  58. "mips")
  59. Arch="mips"
  60. ;;
  61. "arm")
  62. Arch="arm"
  63. ;;
  64. "ram64")
  65. Arch="arm64"
  66. ;;
  67. "aarch64")
  68. Arch="arm64"
  69. ;;
  70. *)
  71. echo -e "error not support $Archt" >>/tmp/AdGuardHome_update.log
  72. rm /var/run/update_core
  73. exit 1
  74. ;;
  75. esac
  76. wget-ssl --no-check-certificate -t 1 -T 10 -O /tmp/AdGuardHome/update/AdGuardHome_linux_${Arch}.tar.gz "https://github.com/AdguardTeam/AdGuardHome/releases/download/${latest_ver}/AdGuardHome_linux_${Arch}.tar.gz" >/dev/null 2>&1
  77. tar -zxf "/tmp/AdGuardHome/update/AdGuardHome_linux_${Arch}.tar.gz" -C "/tmp/AdGuardHome/update/" >/dev/null 2>&1
  78. if [ ! -e "/tmp/AdGuardHome/update/AdGuardHome" ]; then
  79. echo -e "Failed to download core." >>/tmp/AdGuardHome_update.log
  80. rm -rf "/tmp/AdGuardHome/update" >/dev/null 2>&1
  81. rm /var/run/update_core
  82. exit 1
  83. else
  84. if [ "$(uci get AdGuardHome.AdGuardHome.lessspace)"x != "1"x ]; then
  85. cp -f /tmp/AdGuardHome/update/AdGuardHome/AdGuardHome "$binpath"
  86. if [ "$?" == "1" ]; then
  87. echo cp failed maybe not enough space try to kill and cp
  88. /etc/init.d/AdGuardHome stop
  89. cp -f /tmp/AdGuardHome/update/AdGuardHome/AdGuardHome "$binpath"
  90. if [ "$?" == "0" ]; then
  91. uci set AdGuardHome.AdGuardHome.lessspace="1"
  92. else
  93. echo "cp failed" >>/tmp/AdGuardHome_update.log
  94. rm /var/run/update_core
  95. exit 1
  96. fi
  97. fi
  98. else
  99. /etc/init.d/AdGuardHome stop
  100. cp -f /tmp/AdGuardHome/update/AdGuardHome/AdGuardHome "$binpath"
  101. if [ "$?" != "0" ]; then
  102. echo "cp failed" >>/tmp/AdGuardHome_update.log
  103. rm /var/run/update_core
  104. exit 1
  105. fi
  106. fi
  107. [ "${luci_update}" == "y" ] && touch "/tmp/AdGuardHome/update_successfully"
  108. /etc/init.d/AdGuardHome restart
  109. fi
  110. rm -rf "/tmp/AdGuardHome/update" >/dev/null 2>&1
  111. echo -e "Succeeded in updating core." >>/tmp/AdGuardHome_update.log
  112. uci set AdGuardHome.AdGuardHome.version="${latest_ver}"
  113. echo -e "Local version: ${now_ver}, cloud version: ${latest_ver}.\n" >>/tmp/AdGuardHome_update.log
  114. rm /var/run/update_core
  115. }
  116. main(){
  117. check_if_already_running
  118. check_latest_version
  119. }
  120. main