2023-01-30 21:05:20 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-02-05 23:48:37 +08:00
|
|
|
"context"
|
2023-02-07 15:33:47 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2023-01-30 21:05:20 +08:00
|
|
|
"fmt"
|
2023-02-07 15:33:47 +08:00
|
|
|
"net/http"
|
2023-01-30 21:05:20 +08:00
|
|
|
"os"
|
2023-02-05 23:48:37 +08:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-01-30 21:05:20 +08:00
|
|
|
"time"
|
|
|
|
|
2023-02-05 23:48:37 +08:00
|
|
|
"gitee.com/openeuler/go-gitee/gitee"
|
2023-01-30 21:05:20 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2023-02-05 23:48:37 +08:00
|
|
|
"github.com/google/go-github/github"
|
2023-01-30 21:05:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type UpgradeService struct{}
|
|
|
|
|
|
|
|
type IUpgradeService interface {
|
2023-02-05 23:48:37 +08:00
|
|
|
Upgrade(req dto.Upgrade) error
|
2023-01-30 21:05:20 +08:00
|
|
|
SearchUpgrade() (*dto.UpgradeInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIUpgradeService() IUpgradeService {
|
|
|
|
return &UpgradeService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UpgradeService) SearchUpgrade() (*dto.UpgradeInfo, error) {
|
2023-02-10 18:07:29 +08:00
|
|
|
currentVersion, err := settingRepo.Get(settingRepo.WithByKey("SystemVersion"))
|
2023-01-30 21:05:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-07 15:33:47 +08:00
|
|
|
|
|
|
|
var releaseInfo dto.UpgradeInfo
|
|
|
|
isGiteeOK := checkValid("https://gitee.com/wanghe-fit2cloud/1Panel")
|
|
|
|
if isGiteeOK {
|
|
|
|
releaseInfo, err = u.loadLatestFromGitee()
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Error(err)
|
2023-02-05 23:48:37 +08:00
|
|
|
}
|
2023-01-30 21:05:20 +08:00
|
|
|
}
|
2023-02-07 15:33:47 +08:00
|
|
|
if len(releaseInfo.NewVersion) == 0 {
|
|
|
|
isGithubOK := checkValid("https://gitee.com/1Panel-dev/1Panel")
|
|
|
|
if isGithubOK {
|
|
|
|
releaseInfo, err = u.loadLatestFromGithub()
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Error(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-01-30 21:05:20 +08:00
|
|
|
}
|
2023-02-07 15:33:47 +08:00
|
|
|
if len(releaseInfo.NewVersion) != 0 {
|
2023-02-10 18:07:29 +08:00
|
|
|
isNew, err := compareVersion(currentVersion.Value, releaseInfo.NewVersion)
|
2023-02-13 15:48:18 +08:00
|
|
|
if !isNew || err != nil {
|
2023-02-07 15:33:47 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &releaseInfo, nil
|
2023-01-30 21:05:20 +08:00
|
|
|
}
|
2023-02-07 15:33:47 +08:00
|
|
|
|
|
|
|
return nil, errors.New("both gitee and github were unavailable")
|
2023-01-30 21:05:20 +08:00
|
|
|
}
|
|
|
|
|
2023-02-05 23:48:37 +08:00
|
|
|
func (u *UpgradeService) Upgrade(req dto.Upgrade) error {
|
2023-01-30 21:05:20 +08:00
|
|
|
global.LOG.Info("start to upgrade now...")
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
timeStr := time.Now().Format("20060102150405")
|
2023-02-05 23:48:37 +08:00
|
|
|
rootDir := fmt.Sprintf("%s/upgrade_%s/downloads", constant.TmpDir, timeStr)
|
|
|
|
originalDir := fmt.Sprintf("%s/upgrade_%s/original", constant.TmpDir, timeStr)
|
|
|
|
if err := os.MkdirAll(rootDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(originalDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-30 21:05:20 +08:00
|
|
|
|
2023-02-05 23:48:37 +08:00
|
|
|
downloadPath := fmt.Sprintf("https://gitee.com/%s/%s/releases/download/%s/", "wanghe-fit2cloud", "1Panel", req.Version)
|
2023-02-07 15:33:47 +08:00
|
|
|
isGiteeOK := checkValid(downloadPath)
|
|
|
|
if !isGiteeOK {
|
2023-02-05 23:48:37 +08:00
|
|
|
downloadPath = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/", "wanghe-fit2cloud", "1Panel", req.Version)
|
2023-02-07 15:33:47 +08:00
|
|
|
isGithubOK := checkValid(downloadPath)
|
|
|
|
if !isGithubOK {
|
|
|
|
return errors.New("both gitee and github were unavailabl")
|
|
|
|
}
|
2023-02-05 23:48:37 +08:00
|
|
|
}
|
2023-02-07 15:33:47 +08:00
|
|
|
|
2023-02-09 16:36:28 +08:00
|
|
|
fileName := fmt.Sprintf("1panel-%s-%s-%s.tar.gz", req.Version, "linux", runtime.GOARCH)
|
2023-02-02 15:01:37 +08:00
|
|
|
_ = settingRepo.Update("SystemStatus", "Upgrading")
|
2023-01-30 21:05:20 +08:00
|
|
|
go func() {
|
2023-02-05 23:48:37 +08:00
|
|
|
if err := fileOp.DownloadFile(downloadPath+fileName, rootDir+"/service.tar.gz"); err != nil {
|
|
|
|
global.LOG.Errorf("download service file failed, err: %v", err)
|
2023-02-09 16:36:28 +08:00
|
|
|
_ = settingRepo.Update("SystemStatus", "Free")
|
2023-01-30 21:05:20 +08:00
|
|
|
return
|
|
|
|
}
|
2023-02-05 23:48:37 +08:00
|
|
|
global.LOG.Info("download all file successful!")
|
2023-01-30 21:05:20 +08:00
|
|
|
defer func() {
|
2023-02-05 23:48:37 +08:00
|
|
|
_ = os.Remove(rootDir)
|
2023-01-30 21:05:20 +08:00
|
|
|
}()
|
2023-02-05 23:48:37 +08:00
|
|
|
if err := fileOp.Decompress(rootDir+"/service.tar.gz", rootDir, files.TarGz); err != nil {
|
2023-02-02 15:01:37 +08:00
|
|
|
global.LOG.Errorf("decompress file failed, err: %v", err)
|
2023-02-09 16:36:28 +08:00
|
|
|
_ = settingRepo.Update("SystemStatus", "Free")
|
2023-01-30 21:05:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := u.handleBackup(fileOp, originalDir); err != nil {
|
2023-02-02 15:01:37 +08:00
|
|
|
global.LOG.Errorf("handle backup original file failed, err: %v", err)
|
2023-02-09 16:36:28 +08:00
|
|
|
_ = settingRepo.Update("SystemStatus", "Free")
|
2023-01-30 21:05:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
global.LOG.Info("backup original data successful, now start to upgrade!")
|
|
|
|
|
|
|
|
if err := cpBinary(rootDir+"/1panel", "/usr/local/bin/1panel"); err != nil {
|
|
|
|
u.handleRollback(fileOp, originalDir, 1)
|
2023-02-02 15:01:37 +08:00
|
|
|
global.LOG.Errorf("upgrade 1panel failed, err: %v", err)
|
2023-01-30 21:05:20 +08:00
|
|
|
return
|
|
|
|
}
|
2023-02-05 23:48:37 +08:00
|
|
|
if err := cpBinary(fmt.Sprintf("%s/1panel-online-installer-%s/1pctl", rootDir, req.Version), "/usr/local/bin/1pctl"); err != nil {
|
2023-01-30 21:05:20 +08:00
|
|
|
u.handleRollback(fileOp, originalDir, 2)
|
2023-02-02 15:01:37 +08:00
|
|
|
global.LOG.Errorf("upgrade 1pctl failed, err: %v", err)
|
2023-01-30 21:05:20 +08:00
|
|
|
return
|
|
|
|
}
|
2023-02-05 23:48:37 +08:00
|
|
|
if err := cpBinary(fmt.Sprintf("%s/1panel-online-installer-%s/1panel/conf/1panel.service", rootDir, req.Version), "/etc/systemd/system/1panel.service"); err != nil {
|
2023-01-30 21:05:20 +08:00
|
|
|
u.handleRollback(fileOp, originalDir, 3)
|
2023-02-02 15:01:37 +08:00
|
|
|
global.LOG.Errorf("upgrade 1panel.service failed, err: %v", err)
|
2023-01-30 21:05:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
global.LOG.Info("upgrade successful!")
|
2023-02-05 23:48:37 +08:00
|
|
|
_ = settingRepo.Update("SystemVersion", req.Version)
|
|
|
|
_ = settingRepo.Update("SystemStatus", "Free")
|
|
|
|
_, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service")
|
2023-01-30 21:05:20 +08:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UpgradeService) handleBackup(fileOp files.FileOp, originalDir string) error {
|
|
|
|
if err := fileOp.Copy("/usr/local/bin/1panel", originalDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := fileOp.Copy("/usr/local/bin/1pctl", originalDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := fileOp.Copy("/etc/systemd/system/1panel.service", originalDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dbPath := global.CONF.System.DbPath + "/" + global.CONF.System.DbFile
|
|
|
|
if err := fileOp.Copy(dbPath, originalDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UpgradeService) handleRollback(fileOp files.FileOp, originalDir string, errStep int) {
|
|
|
|
dbPath := global.CONF.System.DbPath + "/1Panel.db"
|
2023-02-09 16:36:28 +08:00
|
|
|
_ = settingRepo.Update("SystemStatus", "Free")
|
2023-01-30 21:05:20 +08:00
|
|
|
if err := cpBinary(originalDir+"/1Panel.db", dbPath); err != nil {
|
|
|
|
global.LOG.Errorf("rollback 1panel failed, err: %v", err)
|
|
|
|
}
|
|
|
|
if err := cpBinary(originalDir+"/1panel", "/usr/local/bin/1panel"); err != nil {
|
|
|
|
global.LOG.Errorf("rollback 1pctl failed, err: %v", err)
|
|
|
|
}
|
|
|
|
if errStep == 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := cpBinary(originalDir+"/1pctl", "/usr/local/bin/1pctl"); err != nil {
|
|
|
|
global.LOG.Errorf("rollback 1panel failed, err: %v", err)
|
|
|
|
}
|
|
|
|
if errStep == 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := cpBinary(originalDir+"/1panel.service", "/etc/systemd/system/1panel.service"); err != nil {
|
|
|
|
global.LOG.Errorf("rollback 1panel failed, err: %v", err)
|
|
|
|
}
|
2023-02-09 16:36:28 +08:00
|
|
|
|
2023-01-30 21:05:20 +08:00
|
|
|
}
|
2023-02-05 23:48:37 +08:00
|
|
|
|
2023-02-07 15:33:47 +08:00
|
|
|
func (u *UpgradeService) loadLatestFromGithub() (dto.UpgradeInfo, error) {
|
|
|
|
var info dto.UpgradeInfo
|
2023-02-05 23:48:37 +08:00
|
|
|
client := github.NewClient(nil)
|
2023-02-10 18:07:29 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
2023-02-05 23:48:37 +08:00
|
|
|
stats, res, err := client.Repositories.GetLatestRelease(ctx, "wanghe-fit2cloud", "1Panel")
|
|
|
|
if res.StatusCode != 200 || err != nil {
|
2023-02-07 15:33:47 +08:00
|
|
|
return info, fmt.Errorf("load upgrade info from github failed, err: %v", err)
|
2023-02-05 23:48:37 +08:00
|
|
|
}
|
2023-02-07 15:33:47 +08:00
|
|
|
info.NewVersion = string(*stats.Name)
|
|
|
|
info.ReleaseNote = string(*stats.Body)
|
|
|
|
info.CreatedAt = stats.PublishedAt.Add(8 * time.Hour).Format("2006-01-02 15:04:05")
|
|
|
|
return info, nil
|
2023-02-05 23:48:37 +08:00
|
|
|
}
|
|
|
|
|
2023-02-07 15:33:47 +08:00
|
|
|
func (u *UpgradeService) loadLatestFromGitee() (dto.UpgradeInfo, error) {
|
|
|
|
var info dto.UpgradeInfo
|
2023-02-05 23:48:37 +08:00
|
|
|
client := gitee.NewAPIClient(gitee.NewConfiguration())
|
2023-02-10 18:07:29 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
2023-02-05 23:48:37 +08:00
|
|
|
stats, res, err := client.RepositoriesApi.GetV5ReposOwnerRepoReleasesLatest(ctx, "wanghe-fit2cloud", "1Panel", &gitee.GetV5ReposOwnerRepoReleasesLatestOpts{})
|
|
|
|
if res.StatusCode != 200 || err != nil {
|
2023-02-07 15:33:47 +08:00
|
|
|
return info, fmt.Errorf("load upgrade info from gitee failed, err: %v", err)
|
2023-02-05 23:48:37 +08:00
|
|
|
}
|
2023-02-07 15:33:47 +08:00
|
|
|
info.NewVersion = string(stats.Name)
|
|
|
|
info.ReleaseNote = string(stats.Body)
|
|
|
|
info.CreatedAt = stats.CreatedAt.Format("2006-01-02 15:04:05")
|
|
|
|
return info, nil
|
2023-02-05 23:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func compareVersion(version, newVersion string) (bool, error) {
|
|
|
|
if version == newVersion {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if len(version) == 0 || len(newVersion) == 0 {
|
|
|
|
return false, fmt.Errorf("incorrect version or new version entered %v -- %v", version, newVersion)
|
|
|
|
}
|
|
|
|
versions := strings.Split(strings.ReplaceAll(version, "v", ""), ".")
|
|
|
|
if len(versions) != 3 {
|
|
|
|
return false, fmt.Errorf("incorrect version input %v", version)
|
|
|
|
}
|
|
|
|
newVersions := strings.Split(strings.ReplaceAll(newVersion, "v", ""), ".")
|
|
|
|
if len(newVersions) != 3 {
|
|
|
|
return false, fmt.Errorf("incorrect newVersions input %v", version)
|
|
|
|
}
|
|
|
|
version1, _ := strconv.Atoi(versions[0])
|
|
|
|
newVersion1, _ := strconv.Atoi(newVersions[0])
|
|
|
|
if newVersion1 > version1 {
|
|
|
|
return true, nil
|
|
|
|
} else if newVersion1 == version1 {
|
|
|
|
version2, _ := strconv.Atoi(versions[1])
|
|
|
|
newVersion2, _ := strconv.Atoi(newVersions[1])
|
|
|
|
if newVersion2 > version2 {
|
|
|
|
return true, nil
|
|
|
|
} else if newVersion2 == version2 {
|
|
|
|
version3, _ := strconv.Atoi(versions[2])
|
|
|
|
newVersion3, _ := strconv.Atoi(newVersions[2])
|
|
|
|
if newVersion3 > version3 {
|
|
|
|
return true, nil
|
|
|
|
} else {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 15:33:47 +08:00
|
|
|
|
|
|
|
func checkValid(addr string) bool {
|
|
|
|
timeout := time.Duration(2 * time.Second)
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client := http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
if _, err := client.Get(addr); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|