Skip to content

Commit

Permalink
feat: 优化更新和重启流程
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 23, 2023
1 parent e28b00f commit 0e10823
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
57 changes: 43 additions & 14 deletions pkg/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,14 @@ func GenerateVersions(start, end string) ([]string, error) {
}

type PanelInfo struct {
Name string `json:"name"`
Version string `json:"version"`
DownloadUrl string `json:"download_url"`
Body string `json:"body"`
Date string `json:"date"`
Name string `json:"name"`
Version string `json:"version"`
DownloadName string `json:"download_name"`
DownloadUrl string `json:"download_url"`
Body string `json:"body"`
Date string `json:"date"`
Checksums string `json:"checksums"`
ChecksumsUrl string `json:"checksums_url"`
}

// GetLatestPanelVersion 获取最新版本
Expand Down Expand Up @@ -189,25 +192,33 @@ func GetLatestPanelVersion() (PanelInfo, error) {
}
fileName := file.Name()

var name, version, body, date, downloadUrl string
var name, version, body, date, downloadName, downloadUrl, checksums, checksumsUrl string
if isChina {
name = Exec("jq -r '.name' " + fileName)
version = Exec("jq -r '.tag_name' " + fileName)
body = Exec("jq -r '.description' " + fileName)
date = Exec("jq -r '.created_at' " + fileName)
checksums = Exec("jq -r '.assets.links[] | select(.name | contains(\"checksums\")) | .name' " + fileName)
checksumsUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"checksums\")) | .direct_asset_url' " + fileName)
if IsArm() {
downloadName = Exec("jq -r '.assets.links[] | select(.name | contains(\"arm64\")) | .name' " + fileName)
downloadUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"arm64\")) | .direct_asset_url' " + fileName)
} else {
downloadName = Exec("jq -r '.assets.links[] | select(.name | contains(\"amd64v2\")) | .name' " + fileName)
downloadUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"amd64v2\")) | .direct_asset_url' " + fileName)
}
} else {
name = Exec("jq -r '.name' " + fileName)
version = Exec("jq -r '.tag_name' " + fileName)
body = Exec("jq -r '.body' " + fileName)
date = Exec("jq -r '.published_at' " + fileName)
checksums = Exec("jq -r '.assets[] | select(.name | contains(\"checksums\")) | .name' " + fileName)
checksumsUrl = Exec("jq -r '.assets[] | select(.name | contains(\"checksums\")) | .browser_download_url' " + fileName)
if IsArm() {
downloadName = Exec("jq -r '.assets[] | select(.name | contains(\"arm64\")) | .name' " + fileName)
downloadUrl = Exec("jq -r '.assets[] | select(.name | contains(\"arm64\")) | .browser_download_url' " + fileName)
} else {
downloadName = Exec("jq -r '.assets[] | select(.name | contains(\"amd64v2\")) | .name' " + fileName)
downloadUrl = Exec("jq -r '.assets[] | select(.name | contains(\"amd64v2\")) | .browser_download_url' " + fileName)
}
}
Expand All @@ -216,7 +227,10 @@ func GetLatestPanelVersion() (PanelInfo, error) {
info.Version = version
info.Body = body
info.Date = date
info.DownloadName = downloadName
info.DownloadUrl = downloadUrl
info.Checksums = checksums
info.ChecksumsUrl = checksumsUrl

return info, nil
}
Expand Down Expand Up @@ -252,25 +266,33 @@ func GetPanelVersion(version string) (PanelInfo, error) {
}
fileName := file.Name()

var name, version2, body, date, downloadUrl string
var name, version2, body, date, downloadName, downloadUrl, checksums, checksumsUrl string
if isChina {
name = Exec("jq -r '.name' " + fileName)
version2 = Exec("jq -r '.tag_name' " + fileName)
body = Exec("jq -r '.description' " + fileName)
date = Exec("jq -r '.created_at' " + fileName)
checksums = Exec("jq -r '.assets.links[] | select(.name | contains(\"checksums\")) | .name' " + fileName)
checksumsUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"checksums\")) | .direct_asset_url' " + fileName)
if IsArm() {
downloadName = Exec("jq -r '.assets.links[] | select(.name | contains(\"arm64\")) | .name' " + fileName)
downloadUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"arm64\")) | .direct_asset_url' " + fileName)
} else {
downloadName = Exec("jq -r '.assets.links[] | select(.name | contains(\"amd64v2\")) | .name' " + fileName)
downloadUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"amd64v2\")) | .direct_asset_url' " + fileName)
}
} else {
name = Exec("jq -r '.name' " + fileName)
version2 = Exec("jq -r '.tag_name' " + fileName)
body = Exec("jq -r '.body' " + fileName)
date = Exec("jq -r '.published_at' " + fileName)
checksums = Exec("jq -r '.assets[] | select(.name | contains(\"checksums\")) | .name' " + fileName)
checksumsUrl = Exec("jq -r '.assets[] | select(.name | contains(\"checksums\")) | .browser_download_url' " + fileName)
if IsArm() {
downloadName = Exec("jq -r '.assets[] | select(.name | contains(\"arm64\")) | .name' " + fileName)
downloadUrl = Exec("jq -r '.assets[] | select(.name | contains(\"arm64\")) | .browser_download_url' " + fileName)
} else {
downloadName = Exec("jq -r '.assets[] | select(.name | contains(\"amd64v2\")) | .name' " + fileName)
downloadUrl = Exec("jq -r '.assets[] | select(.name | contains(\"amd64v2\")) | .browser_download_url' " + fileName)
}
}
Expand All @@ -279,7 +301,10 @@ func GetPanelVersion(version string) (PanelInfo, error) {
info.Version = version2
info.Body = body
info.Date = date
info.DownloadName = downloadName
info.DownloadUrl = downloadUrl
info.Checksums = checksums
info.ChecksumsUrl = checksumsUrl

return info, nil
}
Expand All @@ -302,21 +327,25 @@ func UpdatePanel(panelInfo PanelInfo) error {
color.Greenln("清理完成")

color.Greenln("正在下载...")
Exec("wget -T 120 -t 3 -O /www/panel/panel.zip " + panelInfo.DownloadUrl)

if !Exists("/www/panel/panel.zip") {
Exec("wget -T 120 -t 3 -O /www/panel/" + panelInfo.DownloadName + " " + panelInfo.DownloadUrl)
Exec("wget -T 20 -t 3 -O /www/panel/" + panelInfo.Checksums + " " + panelInfo.ChecksumsUrl)
if !Exists("/www/panel/"+panelInfo.DownloadName) || !Exists("/www/panel/"+panelInfo.Checksums) {
return errors.New("下载失败")
}

color.Greenln("下载完成")

color.Greenln("更新新版本...")
Exec("cd /www/panel && unzip -o panel.zip && rm -rf panel.zip && chmod 700 panel && bash scripts/update_panel.sh")
color.Greenln("校验下载文件...")
check := Exec("cd /www/panel && sha256sum -c " + panelInfo.Checksums + " --ignore-missing")
if check != panelInfo.DownloadName+": OK" {
return errors.New("下载文件校验失败")
}
color.Greenln("文件校验完成")

color.Greenln("更新新版本...")
Exec("cd /www/panel && unzip -o " + panelInfo.DownloadName + " && rm -rf " + panelInfo.DownloadName + " && chmod 700 panel && bash scripts/update_panel.sh")
if !Exists("/www/panel/panel") {
return errors.New("更新失败,可能是下载过程中出现了问题")
}

color.Greenln("更新完成")

color.Greenln("恢复面板配置...")
Expand Down
9 changes: 1 addition & 8 deletions scripts/install_panel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,8 @@ Init_Panel() {
fi
wget -T 20 -t 3 -O ${setup_Path}/panel/${checksumsFileName} "${checksumsFile}"

# 处理 checksums 文件
if [ "${ARCH}" == "x86_64" ]; then
sed -i '/linux_arm64/d' ${setup_Path}/panel/${checksumsFileName}
elif [ "${ARCH}" == "aarch64" ]; then
sed -i '/linux_amd64/d' ${setup_Path}/panel/${checksumsFileName}
fi

cd ${setup_Path}/panel
if ! sha256sum --status -c ${checksumsFileName}; then
if ! sha256sum --status -c ${checksumsFileName} --ignore-missing; then
echo -e $HR
echo "错误:面板压缩包 checksum 校验失败,文件可能被篡改或不完整,已终止操作"
exit 1
Expand Down

0 comments on commit 0e10823

Please sign in to comment.