1
0
mirror of https://github.com/kenzok8/small-package synced 2025-09-18 18:51:16 +08:00
Files
small-package/luci-app-easyupdate/root/usr/bin/easyupdate.sh

165 lines
4.9 KiB
Bash
Raw Normal View History

2021-12-23 20:33:16 +08:00
#!/bin/bash
# https://github.com/sundaqiang/openwrt-packages
# EasyUpdate for Openwrt
function checkEnv() {
if !type sysupgrade >/dev/null 2>&1; then
2021-12-29 20:33:04 +08:00
writeLog 'Your firmware does not contain sysupgrade and does not support automatic updates(您的固件未包含sysupgrade,暂不支持自动更新)'
2021-12-23 20:33:16 +08:00
exit
fi
}
2021-12-29 20:33:04 +08:00
function writeLog() {
now_time='['$(date +"%Y-%m-%d %H:%M:%S")']'
echo ${now_time} $1 | tee -a '/tmp/easyupdatemain.log'
}
2021-12-23 20:33:16 +08:00
function shellHelp() {
checkEnv
cat <<EOF
Openwrt-EasyUpdate Script by sundaqiang
Your firmware already includes Sysupgrade and supports automatic updates(您的固件已包含sysupgrade,支持自动更新)
参数:
2021-12-29 20:33:04 +08:00
-c Get the cloud firmware version(获取云端固件版本)
-d Download cloud Firmware(下载云端固件)
-f filename Flash firmware(刷写固件)
-u One-click firmware update(一键更新固件)
2021-12-23 20:33:16 +08:00
EOF
}
function getCloudVer() {
checkEnv
github=$(uci get easyupdate.main.github)
github=(${github//// })
2021-12-29 20:33:04 +08:00
uclient-fetch -qO- "https://api.github.com/repos/${github[2]}/${github[3]}/releases/latest" | jsonfilter -e '@.tag_name' | sed -e 's/OpenWrt_//'
2021-12-23 20:33:16 +08:00
}
function downCloudVer() {
checkEnv
2021-12-29 20:33:04 +08:00
writeLog 'Get github project address(读取github项目地址)'
2021-12-23 20:33:16 +08:00
github=$(uci get easyupdate.main.github)
2021-12-29 20:33:04 +08:00
writeLog "Github project address(github项目地址):$github"
2021-12-23 20:33:16 +08:00
github=(${github//// })
2021-12-29 20:33:04 +08:00
writeLog 'Check whether EFI firmware is available(判断是否EFI固件)'
2021-12-23 20:33:16 +08:00
if [ -d "/sys/firmware/efi/" ]; then
suffix="combined-efi.img.gz"
else
suffix="combined.img.gz"
fi
2021-12-29 20:33:04 +08:00
writeLog "Whether EFI firmware is available(是否EFI固件):$suffix"
writeLog 'Get the cloud firmware link(获取云端固件链接)'
2021-12-23 20:33:16 +08:00
url=$(uclient-fetch -qO- "https://api.github.com/repos/${github[2]}/${github[3]}/releases/latest" | jsonfilter -e '@.assets[*].browser_download_url' | sed -n "/$suffix/p")
2021-12-29 20:33:04 +08:00
writeLog "Cloud firmware link(云端固件链接):$url"
writeLog 'Get whether to use Chinese mirror(读取是否使用中国镜像)'
2021-12-23 20:33:16 +08:00
proxy=$(uci get easyupdate.main.proxy)
if [ $proxy -eq 1 ]; then
proxy='https://ghproxy.com/'
res='yes'
else
proxy=''
res='no'
fi
2021-12-29 20:33:04 +08:00
writeLog "Whether to use Chinese mirror(是否使用中国镜像):$res"
writeLog 'Start downloading firmware, log output in /tmp/easyupdate.log(开始下载固件,日志输出在/tmp/easyupdate.log)'
2021-12-23 20:33:16 +08:00
fileName=(${url//// })
uclient-fetch -O "/tmp/${fileName[7]}" "$proxy$url" >/tmp/easyupdate.log 2>&1 &
}
function flashFirmware() {
checkEnv
if [[ -z "$file" ]]; then
2021-12-29 20:33:04 +08:00
writeLog 'Please specify the file name(请指定文件名)'
2021-12-23 20:33:16 +08:00
else
2021-12-29 20:33:04 +08:00
writeLog 'Get whether to save the configuration(读取是否保存配置)'
2021-12-23 20:33:16 +08:00
keepconfig=$(uci get easyupdate.main.keepconfig)
if [ $keepconfig -eq 1 ]; then
2021-12-28 20:32:26 +08:00
keepconfig=' '
2021-12-23 20:33:16 +08:00
res='yes'
else
keepconfig='-n '
res='no'
fi
2021-12-29 20:33:04 +08:00
writeLog "Whether to save the configuration(读取是否保存配置):$res"
writeLog 'Start flash firmware, log output in /tmp/easyupdate.log(开始刷写固件,日志输出在/tmp/easyupdate.log)'
2021-12-28 20:32:26 +08:00
sysupgrade $keepconfig$file >/tmp/easyupdate.log 2>&1 &
2021-12-23 20:33:16 +08:00
fi
}
function updateCloud() {
checkEnv
2021-12-29 20:33:04 +08:00
writeLog 'Get the local firmware version(获取本地固件版本)'
lFirVer=$(cat /etc/openwrt_release | sed -n "s/DISTRIB_VERSIONS='\(.*\)'/\1/p")
writeLog "Local firmware version(本地固件版本):$lFirVer"
writeLog 'Get the cloud firmware version(获取云端固件版本)'
cFirVer=$(getCloudVer)
writeLog "Cloud firmware version(云端固件版本):$cFirVer"
lFirVer=$(date -d "${lFirVer:0:4}-${lFirVer:4:2}-${lFirVer:6:2} ${lFirVer:9:2}:${lFirVer:11:2}:${lFirVer:13:2}" +%s)
cFirVer=$(date -d "${cFirVer:0:4}-${cFirVer:4:2}-${cFirVer:6:2} ${cFirVer:9:2}:${cFirVer:11:2}:${cFirVer:13:2}" +%s)
if [ $cFirVer -gt $lFirVer ]; then
writeLog 'Need to be updated(需要更新)'
downCloudVer
i=0
while [ $i -le 100 ]; do
log=$(cat /tmp/easyupdate.log)
str=' error'
if [[ $log =~ $str ]]; then
writeLog 'Download error(下载出错)'
i=101
break
else
str='Connection reset'
if [[ $log =~ $str ]]; then
writeLog 'Download error(下载出错)'
i=101
break
else
str='Download completed'
if [[ $log =~ $str ]]; then
writeLog 'Download completes(下载完成)'
i=100
break
else
echo $log | sed -n '$p'
if [[ $i -eq 99 ]]; then
writeLog 'Download the timeout(下载超时)'
break
fi
fi
fi
fi
i++
sleep 3
done
if [[ $i -eq 100 ]]; then
writeLog 'Prepare flash firmware(准备刷写固件)'
file=$(cat /tmp/easyupdate.log | sed -n "s/Writing to '\(.*\)'/\1/p")
flashFirmware
fi
else
writeLog "Is the latest(已是最新)"
fi
2021-12-23 20:33:16 +08:00
}
if [[ -z "$1" ]]; then
shellHelp
else
case $1 in
-c)
getCloudVer
;;
-d)
downCloudVer
;;
-f)
file=$2
flashFirmware
;;
-u)
updateCloud
;;
*)
shellHelp
;;
esac
2021-12-29 20:33:04 +08:00
fi