commit c2e191c8bd07fb6f7d0ab1233d40d89b38c60ed0 Author: lujinlin <694419720@qq.com> Date: Tue Dec 17 18:07:41 2024 +0800 feat:首次提交 diff --git a/lsy-loader/build.sh b/lsy-loader/build.sh new file mode 100755 index 0000000..3548546 --- /dev/null +++ b/lsy-loader/build.sh @@ -0,0 +1,5 @@ +dpkg-deb --build lsyloader +ver=$(dpkg-deb --show lsyloader.deb|awk '{print $2}') +echo `sha256sum lsyloader.deb` >> "lsyloader$ver.debSign" +mv lsyloader.deb lsyloader$ver.deb +chmod 777 *.deb \ No newline at end of file diff --git a/lsy-loader/install.sh b/lsy-loader/install.sh new file mode 100755 index 0000000..b9696a8 --- /dev/null +++ b/lsy-loader/install.sh @@ -0,0 +1,11 @@ + +cur_dir=$(dirname "$(realpath "$0")") + +# sudo dpkg -i lsyloader1.0.0.deb + +cp *.deb /tmp/ + +# deb必须放在/tmp这种有自由权限的目录 +# apt会自动处理依赖关系 +sudo apt install -y --allow-downgrades "/tmp/lsyloader1.0.0.deb" + diff --git a/lsy-loader/lsyloader/DEBIAN/control b/lsy-loader/lsyloader/DEBIAN/control new file mode 100644 index 0000000..71c91a3 --- /dev/null +++ b/lsy-loader/lsyloader/DEBIAN/control @@ -0,0 +1,8 @@ +Package: lsyloader +Version: 1.0.1 +Section: utils +Priority: optional +Architecture: amd64 +Maintainer: lsy@mail.com +Depends: systemd (>= 240), libc6 (>= 2.28) +Description: lsy udpate. diff --git a/lsy-loader/lsyloader/DEBIAN/postinst b/lsy-loader/lsyloader/DEBIAN/postinst new file mode 100755 index 0000000..43aa9b3 --- /dev/null +++ b/lsy-loader/lsyloader/DEBIAN/postinst @@ -0,0 +1,11 @@ +#!/bin/bash +#set -e + +# Reload systemd configurations +systemctl daemon-reload + +# Enable the service to start on boot +systemctl enable lsyloader.service + +# Start the service +systemctl start lsyloader.service diff --git a/lsy-loader/lsyloader/DEBIAN/postrm b/lsy-loader/lsyloader/DEBIAN/postrm new file mode 100755 index 0000000..5969671 --- /dev/null +++ b/lsy-loader/lsyloader/DEBIAN/postrm @@ -0,0 +1,9 @@ +#!/bin/bash +#set -e + + +systemctl disable lsyloader.service >/dev/null 2>&1 || true +rm -f /lib/systemd/system/lsyloader.service +rm -f /data/lsyloader/lsyloader.log +systemctl daemon-reload + diff --git a/lsy-loader/lsyloader/DEBIAN/prerm b/lsy-loader/lsyloader/DEBIAN/prerm new file mode 100755 index 0000000..bb8da09 --- /dev/null +++ b/lsy-loader/lsyloader/DEBIAN/prerm @@ -0,0 +1,10 @@ +#!/bin/bash +#set -e + +# Stop the service if running +if systemctl is-active --quiet lsyloader.service; then + systemctl stop lsyloader.service +fi + +# Disable the service +systemctl disable lsyloader.service diff --git a/lsy-loader/lsyloader/data/lsyloader/lsydeb.sh b/lsy-loader/lsyloader/data/lsyloader/lsydeb.sh new file mode 100755 index 0000000..13bf027 --- /dev/null +++ b/lsy-loader/lsyloader/data/lsyloader/lsydeb.sh @@ -0,0 +1,259 @@ +#!/bin/bash + +jsonIn="" +#### jsonIn=version接口 +# { +# "version": "1.0.5", +# "name": "lsyloader", +# "path": "http://192.168.200.128:5212/versionFile?filename=lsyloader1.0.5.deb", +# "sign": "a0d47ccb8edaa029e2c1262a8351674846e4c055c798ad63c55851fa5dff7059" +# } +cur_dir=$(dirname "$(realpath "$0")") +mkdir -p "/tmp/log" + +logFile="/tmp/log/lsyloader.log" +maxLogSize=$((1024*1024*3)) +versionLocal="1.0.0" +url="https://deb.jxm.cool/deb/version.ini" + + +# 只处理 +pkgNameArrOnly=("lsyupdate") +# 除了这些都处理,一般是自己, pkgNameArrOnly存在时,pkgNameArrExcept无效 +pkgNameArrExcept=("lsyloader") + + +pkgName="" +pkgUrl="" +pkgVersion="" +pkgSign="" +outPath="$cur_dir/out.deb" + +function initLog() { + [ ! -e "$logFile" ] && return + local size=`ls -al "$logFile" |awk '{print $5}'` + echo $size + [ $size -gt $maxLogSize ] && rm -rf "$logFile" +} + +function freshNow() { + nowtime=`date +"%Y-%m-%d %H:%M:%S"` +} + +function get_json_value() { + awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{ + foundKeyCount = 0 + while (length(json) > 0) { + # pos = index(json, "\""key"\""); ## 这行更快一些,但是如果有value是字符串,且刚好与要查找的key相同,会被误认为是key而导致值获取错误 + pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*"); + if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;} + + ++foundKeyCount; + start = 0; stop = 0; layer = 0; + for (i = pos + length(key) + 1; i <= length(json); ++i) { + lastChar = substr(json, i - 1, 1) + currChar = substr(json, i, 1) + + if (start <= 0) { + if (lastChar == ":") { + start = currChar == " " ? i + 1: i; + if (currChar == "{" || currChar == "[") { + layer = 1; + } + } + } else { + if (currChar == "{" || currChar == "[") { + ++layer; + } + if (currChar == "}" || currChar == "]") { + --layer; + } + if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) { + stop = currChar == "," ? i : i + 1 + layer; + break; + } + } + } + + if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) { + if (foundKeyCount == 0) {print defaultValue;} exit 0; + } else { + print substr(json, start, stop - start); + } + + json = substr(json, stop + 1, length(json) - stop) + } + }' +} + +# split_str "${tmp_data}" "|" +split_str_return_list=() +function split_str() { + local str_data=$1 + local str_pattern=$2 + + str=$str_data + delimiter=$str_pattern + + array=() + while [ "$str" ]; do + substring="${str%%"$delimiter"*}" + [ -z "$substring" ] && str="${str#"$delimiter"}" && continue + array+=( "$substring" ) + str="${str:${#substring}}" + [ "$str" == "$delimiter" ] && break + done + #declare -p array + + split_str_return_list=() + for var in "${array[@]}"; do + #echo "[v]=${var}" + split_str_return_list+=( "${var}" ) + done +} + +function trimL() { + local str=$1 + local toTrim=$2 + local dst=${str#$toTrim} + echo $dst +} +function trimR() { + local str=$1 + local toTrim=$2 + local dst=${str%$toTrim} + echo $dst +} +function trim() { + local str=$1 + local toTrim=$2 + str=${str#$toTrim} + local dst=${str%$toTrim} + echo $dst +} + +function hasVersion() { + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] hasVersion start" >> $logFile + + local str_ack=$jsonIn + + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] hasVersion done,$str_ack" >> $logFile + + pkgUrl=$(get_json_value "$str_ack" "path" "") + pkgVersion=$(get_json_value "$str_ack" "version" "-1") + pkgSign=$(get_json_value "$str_ack" "sign" "") + pkgName=$(get_json_value "$str_ack" "name" "") + + pkgUrl=$(trim "$pkgUrl" "\"") + pkgSign=$(trim "$pkgSign" "\"") + pkgName=$(trim "$pkgName" "\"") + [ -z "$pkgName" ] && echo "pkgName is empty skip" >> $logFile && return 0 + # pkgNameArrOnly + local found=false + for item in "${pkgNameArrOnly[@]}"; do + if [[ "$item" == "$pkgName" ]]; then + found=true + break + fi + done + [ ${#pkgNameArrOnly[@]} -ne 0 ] && [ "$found" == false ] && echo "pkgName not in $pkgNameArrOnly skip" >> $logFile && return 0 + + # pkgNameArrExcept + for item in "${pkgNameArrExcept[@]}"; do + if [[ "$item" == "$pkgName" ]]; then + echo "pkgName in $pkgNameArrExcept skip" >> $logFile && return 0 + fi + done + + versionLocal=$(apt show "$pkgName"|grep "Version:"|head -n 1|awk '{print $2}') + [ "$versionLocal" == "" ] && versionLocal="1.0.0" + outPath="$cur_dir/$pkgName.deb" + + + version=$(trim "$pkgVersion" "\"") + split_str "${version}" "." + len=${#split_str_return_list[@]} + if [ $len -lt 3 ];then + return 0 + fi + local v1=${split_str_return_list[0]} + local v2=${split_str_return_list[1]} + local v3=${split_str_return_list[2]} + + split_str "${versionLocal}" "." + local lv1=${split_str_return_list[0]} + local lv2=${split_str_return_list[1]} + local lv3=${split_str_return_list[2]} + + echo "[$nowtime] pkgVersion=$pkgVersion" >> $logFile + echo "[$nowtime] pkgUrl=$pkgUrl" >> $logFile + echo "[$nowtime] pkgSign=$pkgSign" >> $logFile + echo "[$nowtime] pkgName=$pkgName" >> $logFile + + [ -z "$pkgUrl" ] && echo "[$nowtime] pkgurl empty skip" >> $logFile && return 0 + [ -z "$pkgSign" ] && echo "[$nowtime] pkgSign empty skip" >> $logFile && return 0 + + echo "[$nowtime] v=$v1.$v2.$v3" >> $logFile + echo "[$nowtime] lv=$lv1.$lv2.$lv3" >> $logFile + + [ "$v1" -gt "$lv1" ] && return 1 + [ "$v2" -gt "$lv2" ] && return 1 + [ "$v3" -gt "$lv3" ] && return 1 + + return 0 +} + +function runInstall() { + + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] start..." >> $logFile + + hasVersion + local bUpdate=$? + if [ $bUpdate -eq 0 ]; then + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] no need to update" >> $logFile + return + fi + echo "[$nowtime] need update" >> $logFile + + freshNow + rm -rf "$outPath" + str_ack=`wget -O "$outPath" $pkgUrl` + [ ! -e "$outPath" ] && echo "[$nowtime] $outPath not exist" >> $logFile && return + + freshNow + local fileSign=`sha256sum "$outPath"|awk '{print $1}'` + echo "[$nowtime] get sign,fileSign=$fileSign,$outPath" >> $logFile + + [ "$fileSign" != "$pkgSign" ] && echo "[$nowtime] $fileSign!=$pkgSign,$outPath skip" >> $logFile && return + + chmod +x "$outPath" + echo "[$nowtime] start uninstall $outPath" >> $logFile + bash $cur_dir/uninstall.sh "$pkgName" >> $logFile + echo "[$nowtime] start install $outPath" >> $logFile + apt install -y --allow-downgrades "$outPath" >> $logFile + + freshNow + echo "[$nowtime] finish" >> $logFile +} + +function main() { + initLog + str_ack=$( + curl --request GET \ + --url "$url" \ + --header 'content-type: application/json' \ + --data "" + ) + apps=($(echo "$str_ack" | jq -c '.[]')) + for item in "${apps[@]}"; do + jsonIn=$item + runInstall + sleep 2 + done +} +main + diff --git a/lsy-loader/lsyloader/data/lsyloader/lsyloader.sh b/lsy-loader/lsyloader/data/lsyloader/lsyloader.sh new file mode 100755 index 0000000..8ffce8d --- /dev/null +++ b/lsy-loader/lsyloader/data/lsyloader/lsyloader.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +cur_dir=$(dirname "$(realpath "$0")") +while true; do + bash "$cur_dir/lsydeb.sh" + sleep 20 +done diff --git a/lsy-loader/lsyloader/data/lsyloader/test.sh b/lsy-loader/lsyloader/data/lsyloader/test.sh new file mode 100755 index 0000000..56afa49 --- /dev/null +++ b/lsy-loader/lsyloader/data/lsyloader/test.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +json_data=$(cat "/home/lyn/work/deb-pkg/lsy-loader/lsyloader/data/lsyloader/version.ini") +apps=($(echo "$json_data" | jq -c '.[]')) + +# 输出每个元素 +for item in "${apps[@]}"; do + echo "apps: $item" +done + diff --git a/lsy-loader/lsyloader/data/lsyloader/uninstall.sh b/lsy-loader/lsyloader/data/lsyloader/uninstall.sh new file mode 100755 index 0000000..4642301 --- /dev/null +++ b/lsy-loader/lsyloader/data/lsyloader/uninstall.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# "$appname" +appname=$1 + +[ -z "$appname" ] && echo "appname is empty skip" && exit 0 + +apt remove -y "$appname" + +sudo systemctl stop "$appname".service +sudo systemctl disable "$appname".service +sudo dpkg -r "$appname" +sudo dpkg --purge "$appname" +sudo rm -rf /var/lib/dpkg/info/"$appname".* + +systemctl disable "$appname".service >/dev/null 2>&1 || true +rm -f /lib/systemd/system/"$appname".service +#rm -f /data/"$appname".log +systemctl daemon-reload +dpkg --remove --force-remove-reinstreq "$appname" \ No newline at end of file diff --git a/lsy-loader/lsyloader/data/lsyloader/version.ini b/lsy-loader/lsyloader/data/lsyloader/version.ini new file mode 100644 index 0000000..1b34713 --- /dev/null +++ b/lsy-loader/lsyloader/data/lsyloader/version.ini @@ -0,0 +1,13 @@ +[{ + "version": "1.0.1", + "name": "lsyloader", + "path": "http://192.168.200.128:5212/versionFile?filename=lsyloader1.0.1.deb", + "sign": "3973118446a2c81db53803633ca84b287fe80197a707f5d7b8a6cfd712f30c0a" + }, + { + "version": "1.0.3", + "name": "lsyupdate", + "path": "http://192.168.200.128:5212/versionFile?filename=lsyupdate1.0.3.deb", + "sign": "b990a391dc7b8fd2bce1f22c80be674a764d725f87bd706a81f6dd1225210010" + } +] \ No newline at end of file diff --git a/lsy-loader/lsyloader/lib/systemd/system/lsyloader.service b/lsy-loader/lsyloader/lib/systemd/system/lsyloader.service new file mode 100644 index 0000000..d388788 --- /dev/null +++ b/lsy-loader/lsyloader/lib/systemd/system/lsyloader.service @@ -0,0 +1,12 @@ +[Unit] +Description=lsy update Service +After=network.target + +[Service] +ExecStart=/data/lsyloader/lsyloader.sh +Restart=always +User=root +Group=root + +[Install] +WantedBy=multi-user.target diff --git a/lsy-loader/lsyloader1.0.0.deb b/lsy-loader/lsyloader1.0.0.deb new file mode 100755 index 0000000..3a1c3ff Binary files /dev/null and b/lsy-loader/lsyloader1.0.0.deb differ diff --git a/lsy-loader/lsyloader1.0.0.debSign b/lsy-loader/lsyloader1.0.0.debSign new file mode 100644 index 0000000..faf4692 --- /dev/null +++ b/lsy-loader/lsyloader1.0.0.debSign @@ -0,0 +1 @@ +bc41b7e875a1303f8cb4bdf14b5352a56537aaf01dc265e5f929f6b71a8c722f lsyloader.deb diff --git a/lsy-loader/lsyloader1.0.1.deb b/lsy-loader/lsyloader1.0.1.deb new file mode 100755 index 0000000..24827f7 Binary files /dev/null and b/lsy-loader/lsyloader1.0.1.deb differ diff --git a/lsy-loader/lsyloader1.0.1.debSign b/lsy-loader/lsyloader1.0.1.debSign new file mode 100644 index 0000000..4749114 --- /dev/null +++ b/lsy-loader/lsyloader1.0.1.debSign @@ -0,0 +1 @@ +24708d46126cb647a0bd1fdf1b0beb49aee9091558c023ea0eed555005ca0e76 lsyloader.deb diff --git a/lsy-loader/readme.md b/lsy-loader/readme.md new file mode 100644 index 0000000..8514c83 --- /dev/null +++ b/lsy-loader/readme.md @@ -0,0 +1,6 @@ + +[lsyloader] + only lsyupdate's update +[lsyupdate] + no lsyupdate, all others's update + diff --git a/lsy-loader/showVersion.sh b/lsy-loader/showVersion.sh new file mode 100755 index 0000000..8cb7d17 --- /dev/null +++ b/lsy-loader/showVersion.sh @@ -0,0 +1,2 @@ +apt show lsyloader |grep Version + diff --git a/lsy-loader/uninstallAll.sh b/lsy-loader/uninstallAll.sh new file mode 100755 index 0000000..3403477 --- /dev/null +++ b/lsy-loader/uninstallAll.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# "$appname" +appname=$1 + +[ -z "$appname" ] && appname="lsyloader" +[ -z "$appname" ] && echo "appname is empty skip" && exit 0 + +apt remove -y "$appname" + +sudo systemctl stop "$appname".service +sudo systemctl disable "$appname".service +sudo dpkg -r "$appname" +sudo dpkg --purge "$appname" +sudo rm -rf /var/lib/dpkg/info/"$appname".* + +systemctl disable "$appname".service >/dev/null 2>&1 || true +rm -f /lib/systemd/system/"$appname".service +rm -f /data/"$appname".log +systemctl daemon-reload +dpkg --remove --force-remove-reinstreq "$appname" \ No newline at end of file diff --git a/lsy-update/build.sh b/lsy-update/build.sh new file mode 100755 index 0000000..a2d010f --- /dev/null +++ b/lsy-update/build.sh @@ -0,0 +1,5 @@ +dpkg-deb --build lsyupdate +ver=$(dpkg-deb --show lsyupdate.deb|awk '{print $2}') +mv lsyupdate.deb lsyupdate$ver.deb +echo `sha256sum "lsyupdate$ver.deb"`>>"lsyupdate$ver.debSign" +chmod 777 *.deb \ No newline at end of file diff --git a/lsy-update/install.sh b/lsy-update/install.sh new file mode 100755 index 0000000..e63c944 --- /dev/null +++ b/lsy-update/install.sh @@ -0,0 +1,11 @@ + +cur_dir=$(dirname "$(realpath "$0")") + +# sudo dpkg -i lsyupdate1.0.0.deb + +cp *.deb /tmp/ + +# deb必须放在/tmp这种有自由权限的目录 +# apt会自动处理依赖关系 +sudo apt install -y --allow-downgrades "/tmp/lsyupdate1.0.0.deb" + diff --git a/lsy-update/lsy_update-bak.sh b/lsy-update/lsy_update-bak.sh new file mode 100755 index 0000000..73773e0 --- /dev/null +++ b/lsy-update/lsy_update-bak.sh @@ -0,0 +1,217 @@ +#!/bin/bash + + +#### version接口 +# { +# "version": "1.2.3", +# "path": "http://192.168.200.128:5212/versionFile?filename=app1.2.3", +# "sign": "2d28d9af69d48c964abafaff97f6e9958ecf40a9ca6d62a7ae69e00b279c22f2" +# } + +mkdir -p "/data" + +logFile="/data/lsyudpate.log" +maxLogSize=$((1024*1024*3)) +url="http://192.168.200.128:5212/version" +versionLocal="1.0.1" + +pkgUrl="" +pkgVersion="" +pkgSign="" +outPath="out.deb" + + +function initLog() { + [ ! -e "$logFile" ] && return + local size=`ls -al "$logFile" |awk '{print $5}'` + echo $size + [ $size -gt $maxLogSize ] && rm -rf "$logFile" +} + +function freshNow() { + nowtime=`date +"%Y-%m-%d %H:%M:%S"` +} + +function get_json_value() { + awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{ + foundKeyCount = 0 + while (length(json) > 0) { + # pos = index(json, "\""key"\""); ## 这行更快一些,但是如果有value是字符串,且刚好与要查找的key相同,会被误认为是key而导致值获取错误 + pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*"); + if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;} + + ++foundKeyCount; + start = 0; stop = 0; layer = 0; + for (i = pos + length(key) + 1; i <= length(json); ++i) { + lastChar = substr(json, i - 1, 1) + currChar = substr(json, i, 1) + + if (start <= 0) { + if (lastChar == ":") { + start = currChar == " " ? i + 1: i; + if (currChar == "{" || currChar == "[") { + layer = 1; + } + } + } else { + if (currChar == "{" || currChar == "[") { + ++layer; + } + if (currChar == "}" || currChar == "]") { + --layer; + } + if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) { + stop = currChar == "," ? i : i + 1 + layer; + break; + } + } + } + + if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) { + if (foundKeyCount == 0) {print defaultValue;} exit 0; + } else { + print substr(json, start, stop - start); + } + + json = substr(json, stop + 1, length(json) - stop) + } + }' +} + +# split_str "${tmp_data}" "|" +split_str_return_list=() +function split_str() { + local str_data=$1 + local str_pattern=$2 + + str=$str_data + delimiter=$str_pattern + + array=() + while [ "$str" ]; do + substring="${str%%"$delimiter"*}" + [ -z "$substring" ] && str="${str#"$delimiter"}" && continue + array+=( "$substring" ) + str="${str:${#substring}}" + [ "$str" == "$delimiter" ] && break + done + #declare -p array + + split_str_return_list=() + for var in "${array[@]}"; do + #echo "[v]=${var}" + split_str_return_list+=( "${var}" ) + done +} + +function trimL() { + local str=$1 + local toTrim=$2 + local dst=${str#$toTrim} + echo $dst +} +function trimR() { + local str=$1 + local toTrim=$2 + local dst=${str%$toTrim} + echo $dst +} +function trim() { + local str=$1 + local toTrim=$2 + str=${str#$toTrim} + local dst=${str%$toTrim} + echo $dst +} + + +function hasVersion() { + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] hasVersion start" >> $logFile + + local str_ack=$( + curl --request GET \ + --url "$url" \ + --header 'content-type: application/json' \ + --data "" + ) + + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] hasVersion done,$str_ack" >> $logFile + + local version=$(get_json_value "$str_ack" "version" "-1") + version=$(trim "$version" "\"") + split_str "${version}" "." + len=${#split_str_return_list[@]} + if [ ! $len -eq 3 ];then + return 0 + fi + local v1=${split_str_return_list[0]} + local v2=${split_str_return_list[1]} + local v3=${split_str_return_list[2]} + + split_str "${versionLocal}" "." + local lv1=${split_str_return_list[0]} + local lv2=${split_str_return_list[1]} + local lv3=${split_str_return_list[2]} + + pkgUrl=$(get_json_value "$str_ack" "path" "") + pkgVersion=$version + pkgSign=$(get_json_value "$str_ack" "sign" "") + + pkgUrl=$(trim "$pkgUrl" "\"") + pkgSign=$(trim "$pkgSign" "\"") + + echo "[$nowtime] pkgVersion=$pkgVersion" >> $logFile + echo "[$nowtime] pkgUrl=$pkgUrl" >> $logFile + echo "[$nowtime] pkgSign=$pkgSign" >> $logFile + + [ -z "$pkgUrl" ] && echo "[$nowtime] pkgurl empty skip" >> $logFile && return 0 + [ -z "$pkgSign" ] && echo "[$nowtime] pkgSign empty skip" >> $logFile && return 0 + + echo "[$nowtime] v=$v1.$v2.$v3" >> $logFile + echo "[$nowtime] lv=$lv1.$lv2.$lv3" >> $logFile + + [ $v1 -gt $lv1 ] && return 1 + [ $v2 -gt $lv2 ] && return 1 + [ $v3 -gt $lv3 ] && return 1 + + return 0 +} + +function main() { + initLog + + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] start..." >> $logFile + + hasVersion + local bUpdate=$? + if [ $bUpdate -eq 0 ]; then + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] no need to update" >> $logFile + return + fi + echo "[$nowtime] need update" >> $logFile + + freshNow + rm -rf "$outPath" + str_ack=`wget -O "$outPath" $pkgUrl` + [ ! -e "$outPath" ] && echo "[$nowtime] $outPath not exist" >> $logFile && return + + freshNow + local fileSign=`sha256sum "$outPath"|awk '{print $1}'` + echo "[$nowtime] get sign,fileSign=$fileSign,$outPath" >> $logFile + + [ "$fileSign" != "$pkgSign" ] && echo "[$nowtime] $fileSign!=$pkgSign,$outPath skip" >> $logFile && return + + chmod +x "$outPath" + echo "[$nowtime] start run $outPath" >> $logFile + bash -c "./$outPath" >> $logFile + rm -rf "$outPath" + + freshNow + echo "[$nowtime] finish" >> $logFile +} + +main \ No newline at end of file diff --git a/lsy-update/lsyupdate/DEBIAN/control b/lsy-update/lsyupdate/DEBIAN/control new file mode 100644 index 0000000..d5b249b --- /dev/null +++ b/lsy-update/lsyupdate/DEBIAN/control @@ -0,0 +1,8 @@ +Package: lsyupdate +Version: 1.0.1 +Section: utils +Priority: optional +Architecture: amd64 +Maintainer: lsy@mail.com +Depends: systemd (>= 240), libc6 (>= 2.28) +Description: lsy udpate. diff --git a/lsy-update/lsyupdate/DEBIAN/postinst b/lsy-update/lsyupdate/DEBIAN/postinst new file mode 100755 index 0000000..b46a22c --- /dev/null +++ b/lsy-update/lsyupdate/DEBIAN/postinst @@ -0,0 +1,11 @@ +#!/bin/bash +#set -e + +# Reload systemd configurations +systemctl daemon-reload + +# Enable the service to start on boot +systemctl enable lsyupdate.service + +# Start the service +systemctl start lsyupdate.service diff --git a/lsy-update/lsyupdate/DEBIAN/postrm b/lsy-update/lsyupdate/DEBIAN/postrm new file mode 100755 index 0000000..eb9e541 --- /dev/null +++ b/lsy-update/lsyupdate/DEBIAN/postrm @@ -0,0 +1,9 @@ +#!/bin/bash +#set -e + + +systemctl disable lsyupdate.service >/dev/null 2>&1 || true +rm -f /lib/systemd/system/lsyupdate.service +rm -f /data/lsyupdate/lsyupdate.log +systemctl daemon-reload + diff --git a/lsy-update/lsyupdate/DEBIAN/prerm b/lsy-update/lsyupdate/DEBIAN/prerm new file mode 100755 index 0000000..80c0435 --- /dev/null +++ b/lsy-update/lsyupdate/DEBIAN/prerm @@ -0,0 +1,10 @@ +#!/bin/bash +#set -e + +# Stop the service if running +if systemctl is-active --quiet lsyupdate.service; then + systemctl stop lsyupdate.service +fi + +# Disable the service +systemctl disable lsyupdate.service diff --git a/lsy-update/lsyupdate/data/lsyupdate/lsydeb.sh b/lsy-update/lsyupdate/data/lsyupdate/lsydeb.sh new file mode 100755 index 0000000..1319ee5 --- /dev/null +++ b/lsy-update/lsyupdate/data/lsyupdate/lsydeb.sh @@ -0,0 +1,259 @@ +#!/bin/bash + +jsonIn="" +#### jsonIn=version接口 +# { +# "version": "1.0.5", +# "name": "lsyupdate", +# "path": "http://192.168.200.128:5212/versionFile?filename=lsyupdate1.0.5.deb", +# "sign": "a0d47ccb8edaa029e2c1262a8351674846e4c055c798ad63c55851fa5dff7059" +# } +cur_dir=$(dirname "$(realpath "$0")") +mkdir -p "/tmp/log" + +logFile="/tmp/log/lsyupdate.log" +maxLogSize=$((1024*1024*3)) +versionLocal="1.0.0" +url="https://deb.jxm.cool/deb/version.ini" + + +# 只处理 +pkgNameArrOnly=() +# 除了这些都处理,一般是自己, pkgNameArrOnly存在时,pkgNameArrExcept无效 +pkgNameArrExcept=("lsyupdate") + + +pkgName="" +pkgUrl="" +pkgVersion="" +pkgSign="" +outPath="$cur_dir/out.deb" + +function initLog() { + [ ! -e "$logFile" ] && return + local size=`ls -al "$logFile" |awk '{print $5}'` + echo $size + [ $size -gt $maxLogSize ] && rm -rf "$logFile" +} + +function freshNow() { + nowtime=`date +"%Y-%m-%d %H:%M:%S"` +} + +function get_json_value() { + awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{ + foundKeyCount = 0 + while (length(json) > 0) { + # pos = index(json, "\""key"\""); ## 这行更快一些,但是如果有value是字符串,且刚好与要查找的key相同,会被误认为是key而导致值获取错误 + pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*"); + if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;} + + ++foundKeyCount; + start = 0; stop = 0; layer = 0; + for (i = pos + length(key) + 1; i <= length(json); ++i) { + lastChar = substr(json, i - 1, 1) + currChar = substr(json, i, 1) + + if (start <= 0) { + if (lastChar == ":") { + start = currChar == " " ? i + 1: i; + if (currChar == "{" || currChar == "[") { + layer = 1; + } + } + } else { + if (currChar == "{" || currChar == "[") { + ++layer; + } + if (currChar == "}" || currChar == "]") { + --layer; + } + if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) { + stop = currChar == "," ? i : i + 1 + layer; + break; + } + } + } + + if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) { + if (foundKeyCount == 0) {print defaultValue;} exit 0; + } else { + print substr(json, start, stop - start); + } + + json = substr(json, stop + 1, length(json) - stop) + } + }' +} + +# split_str "${tmp_data}" "|" +split_str_return_list=() +function split_str() { + local str_data=$1 + local str_pattern=$2 + + str=$str_data + delimiter=$str_pattern + + array=() + while [ "$str" ]; do + substring="${str%%"$delimiter"*}" + [ -z "$substring" ] && str="${str#"$delimiter"}" && continue + array+=( "$substring" ) + str="${str:${#substring}}" + [ "$str" == "$delimiter" ] && break + done + #declare -p array + + split_str_return_list=() + for var in "${array[@]}"; do + #echo "[v]=${var}" + split_str_return_list+=( "${var}" ) + done +} + +function trimL() { + local str=$1 + local toTrim=$2 + local dst=${str#$toTrim} + echo $dst +} +function trimR() { + local str=$1 + local toTrim=$2 + local dst=${str%$toTrim} + echo $dst +} +function trim() { + local str=$1 + local toTrim=$2 + str=${str#$toTrim} + local dst=${str%$toTrim} + echo $dst +} + +function hasVersion() { + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] hasVersion start" >> $logFile + + local str_ack=$jsonIn + + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] hasVersion done,$str_ack" >> $logFile + + pkgUrl=$(get_json_value "$str_ack" "path" "") + pkgVersion=$(get_json_value "$str_ack" "version" "-1") + pkgSign=$(get_json_value "$str_ack" "sign" "") + pkgName=$(get_json_value "$str_ack" "name" "") + + pkgUrl=$(trim "$pkgUrl" "\"") + pkgSign=$(trim "$pkgSign" "\"") + pkgName=$(trim "$pkgName" "\"") + [ -z "$pkgName" ] && echo "pkgName is empty skip" >> $logFile && return 0 + # pkgNameArrOnly + local found=false + for item in "${pkgNameArrOnly[@]}"; do + if [[ "$item" == "$pkgName" ]]; then + found=true + break + fi + done + [ ${#pkgNameArrOnly[@]} -ne 0 ] && [ "$found" == false ] && echo "pkgName not in $pkgNameArrOnly skip" >> $logFile && return 0 + + # pkgNameArrExcept + for item in "${pkgNameArrExcept[@]}"; do + if [[ "$item" == "$pkgName" ]]; then + echo "pkgName in $pkgNameArrExcept skip" >> $logFile && return 0 + fi + done + + versionLocal=$(apt show "$pkgName"|grep "Version:"|head -n 1|awk '{print $2}') + [ "$versionLocal" == "" ] && versionLocal="1.0.0" + outPath="$cur_dir/$pkgName.deb" + + + version=$(trim "$pkgVersion" "\"") + split_str "${version}" "." + len=${#split_str_return_list[@]} + if [ $len -lt 3 ];then + return 0 + fi + local v1=${split_str_return_list[0]} + local v2=${split_str_return_list[1]} + local v3=${split_str_return_list[2]} + + split_str "${versionLocal}" "." + local lv1=${split_str_return_list[0]} + local lv2=${split_str_return_list[1]} + local lv3=${split_str_return_list[2]} + + echo "[$nowtime] pkgVersion=$pkgVersion" >> $logFile + echo "[$nowtime] pkgUrl=$pkgUrl" >> $logFile + echo "[$nowtime] pkgSign=$pkgSign" >> $logFile + echo "[$nowtime] pkgName=$pkgName" >> $logFile + + [ -z "$pkgUrl" ] && echo "[$nowtime] pkgurl empty skip" >> $logFile && return 0 + [ -z "$pkgSign" ] && echo "[$nowtime] pkgSign empty skip" >> $logFile && return 0 + + echo "[$nowtime] v=$v1.$v2.$v3" >> $logFile + echo "[$nowtime] lv=$lv1.$lv2.$lv3" >> $logFile + + [ "$v1" -gt "$lv1" ] && return 1 + [ "$v2" -gt "$lv2" ] && return 1 + [ "$v3" -gt "$lv3" ] && return 1 + + return 0 +} + +function runInstall() { + + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] start..." >> $logFile + + hasVersion + local bUpdate=$? + if [ $bUpdate -eq 0 ]; then + nowtime=`date +"%Y-%m-%d %H:%M:%S"` + echo "[$nowtime] no need to update" >> $logFile + return + fi + echo "[$nowtime] need update" >> $logFile + + freshNow + rm -rf "$outPath" + str_ack=`wget -O "$outPath" $pkgUrl` + [ ! -e "$outPath" ] && echo "[$nowtime] $outPath not exist" >> $logFile && return + + freshNow + local fileSign=`sha256sum "$outPath"|awk '{print $1}'` + echo "[$nowtime] get sign,fileSign=$fileSign,$outPath" >> $logFile + + [ "$fileSign" != "$pkgSign" ] && echo "[$nowtime] $fileSign!=$pkgSign,$outPath skip" >> $logFile && return + + chmod +x "$outPath" + echo "[$nowtime] start uninstall $outPath" >> $logFile + bash $cur_dir/uninstall.sh "$pkgName" >> $logFile + echo "[$nowtime] start install $outPath" >> $logFile + apt install -y --allow-downgrades "$outPath" >> $logFile + + freshNow + echo "[$nowtime] finish" >> $logFile +} + +function main() { + initLog + str_ack=$( + curl --request GET \ + --url "$url" \ + --header 'content-type: application/json' \ + --data "" + ) + apps=($(echo "$str_ack" | jq -c '.[]')) + for item in "${apps[@]}"; do + jsonIn=$item + runInstall + sleep 2 + done +} +main + diff --git a/lsy-update/lsyupdate/data/lsyupdate/lsyupdate.sh b/lsy-update/lsyupdate/data/lsyupdate/lsyupdate.sh new file mode 100755 index 0000000..8ffce8d --- /dev/null +++ b/lsy-update/lsyupdate/data/lsyupdate/lsyupdate.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +cur_dir=$(dirname "$(realpath "$0")") +while true; do + bash "$cur_dir/lsydeb.sh" + sleep 20 +done diff --git a/lsy-update/lsyupdate/data/lsyupdate/uninstall.sh b/lsy-update/lsyupdate/data/lsyupdate/uninstall.sh new file mode 100755 index 0000000..4642301 --- /dev/null +++ b/lsy-update/lsyupdate/data/lsyupdate/uninstall.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# "$appname" +appname=$1 + +[ -z "$appname" ] && echo "appname is empty skip" && exit 0 + +apt remove -y "$appname" + +sudo systemctl stop "$appname".service +sudo systemctl disable "$appname".service +sudo dpkg -r "$appname" +sudo dpkg --purge "$appname" +sudo rm -rf /var/lib/dpkg/info/"$appname".* + +systemctl disable "$appname".service >/dev/null 2>&1 || true +rm -f /lib/systemd/system/"$appname".service +#rm -f /data/"$appname".log +systemctl daemon-reload +dpkg --remove --force-remove-reinstreq "$appname" \ No newline at end of file diff --git a/lsy-update/lsyupdate/lib/systemd/system/lsyupdate.service b/lsy-update/lsyupdate/lib/systemd/system/lsyupdate.service new file mode 100644 index 0000000..ee4a9e2 --- /dev/null +++ b/lsy-update/lsyupdate/lib/systemd/system/lsyupdate.service @@ -0,0 +1,12 @@ +[Unit] +Description=lsy update Service +After=network.target + +[Service] +ExecStart=/data/lsyupdate/lsyupdate.sh +Restart=always +User=root +Group=root + +[Install] +WantedBy=multi-user.target diff --git a/lsy-update/lsyupdate1.0.0.deb b/lsy-update/lsyupdate1.0.0.deb new file mode 100755 index 0000000..c9b5b25 Binary files /dev/null and b/lsy-update/lsyupdate1.0.0.deb differ diff --git a/lsy-update/lsyupdate1.0.0.debSign b/lsy-update/lsyupdate1.0.0.debSign new file mode 100644 index 0000000..e8b9103 --- /dev/null +++ b/lsy-update/lsyupdate1.0.0.debSign @@ -0,0 +1 @@ +b39ef51484b8466893a4c8bc8e8a5091efba7b528d575c39f7bdf7187c8d96d1 lsyupdate1.0.0.deb diff --git a/lsy-update/lsyupdate1.0.1.deb b/lsy-update/lsyupdate1.0.1.deb new file mode 100755 index 0000000..8e947cd Binary files /dev/null and b/lsy-update/lsyupdate1.0.1.deb differ diff --git a/lsy-update/lsyupdate1.0.1.debSign b/lsy-update/lsyupdate1.0.1.debSign new file mode 100644 index 0000000..9ad1cae --- /dev/null +++ b/lsy-update/lsyupdate1.0.1.debSign @@ -0,0 +1 @@ +88b109f8f6d0cdc6446267c9427ad4c52c0fdab62501b0e19f6a9405efe79e47 lsyupdate1.0.1.deb diff --git a/lsy-update/showVersion.sh b/lsy-update/showVersion.sh new file mode 100755 index 0000000..fb45236 --- /dev/null +++ b/lsy-update/showVersion.sh @@ -0,0 +1,2 @@ +apt show lsyupdate |grep Version + diff --git a/lsy-update/uninstallAll.sh b/lsy-update/uninstallAll.sh new file mode 100755 index 0000000..dd0096e --- /dev/null +++ b/lsy-update/uninstallAll.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# "$appname" +appname=$1 + +[ -z "$appname" ] && appname="lsyupdate" +[ -z "$appname" ] && echo "appname is empty skip" && exit 0 + +apt remove -y "$appname" + +sudo systemctl stop "$appname".service +sudo systemctl disable "$appname".service +sudo dpkg -r "$appname" +sudo dpkg --purge "$appname" +sudo rm -rf /var/lib/dpkg/info/"$appname".* + +systemctl disable "$appname".service >/dev/null 2>&1 || true +rm -f /lib/systemd/system/"$appname".service +rm -f /data/"$appname".log +systemctl daemon-reload +dpkg --remove --force-remove-reinstreq "$appname" \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2d19f10 --- /dev/null +++ b/readme.md @@ -0,0 +1,24 @@ +## + +修改版本号 +lsy-loader/lsyloader/DEBIAN/control +cd lsy-loader +sh build +生成deb + +修改版本号 +lsy-update/lsyupdate/DEBIAN/control +cd lsy-update +sh build +生成deb + +## +修改version.ini, 注意版本号,sign + +拷贝deb和ini到发布服务器目录 +cp -Rf *deb /data/wwwroot/deb.jxm.cool/deb/ + +## +确保version.ini配置正确,再拷贝到发布服务目录 +cp -Rf *ini /data/wwwroot/deb.jxm.cool/deb/ +