Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/app/service/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

var (
svcBasePath, _ = controller.GetServicePath("")
svcCoreName, _ = controller.LoadServiceName("1panel-core")

Check failure on line 30 in core/app/service/upgrade.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "1panel-core" 5 times.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZsRwoysnvR9WHfiQcv-&open=AZsRwoysnvR9WHfiQcv-&pullRequest=11322
selCoreName, _ = controller.SelectInitScript("1panel-core")
scriptCoreName, _ = controller.GetScriptName("1panel-core")
svcAgentName, _ = controller.LoadServiceName("1panel-agent")

Check failure on line 33 in core/app/service/upgrade.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "1panel-agent" 5 times.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZsRwoyrnvR9WHfiQcv9&open=AZsRwoyrnvR9WHfiQcv9&pullRequest=11322
selAgentName, _ = controller.SelectInitScript("1panel-agent")
scriptAgentName, _ = controller.GetScriptName("1panel-agent")
)
Expand Down Expand Up @@ -104,7 +104,7 @@
return notes, nil
}

func (u *UpgradeService) Upgrade(req dto.Upgrade) error {

Check failure on line 107 in core/app/service/upgrade.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 28 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=1Panel-dev_1Panel&issues=AZsRwoysnvR9WHfiQcv_&open=AZsRwoysnvR9WHfiQcv_&pullRequest=11322
global.LOG.Info("start to upgrade now...")
baseDir := path.Join(global.CONF.Base.InstallDir, fmt.Sprintf("1panel/tmp/upgrade/%s", req.Version))
downloadDir := path.Join(baseDir, "downloads")
Expand Down Expand Up @@ -156,13 +156,13 @@

global.LOG.Info("backup original data successful, now start to upgrade!")

if err := files.CopyItem(false, true, path.Join(tmpDir, "1panel-core"), "/usr/local/bin"); err != nil {
if err := files.CopyFileWithRename(path.Join(tmpDir, "1panel-core"), "/usr/local/bin/1panel-core.tmp"); err != nil {
global.LOG.Errorf("upgrade 1panel-core failed, err: %v", err)
_ = settingRepo.Update("SystemStatus", "Free")
u.handleRollback(originalDir, 1)
return
}
if err := files.CopyItem(false, true, path.Join(tmpDir, "1panel-agent"), "/usr/local/bin"); err != nil {
if err := files.CopyFileWithRename(path.Join(tmpDir, "1panel-agent"), "/usr/local/bin/1panel-agent.tmp"); err != nil {
global.LOG.Errorf("upgrade 1panel-agent failed, err: %v", err)
_ = settingRepo.Update("SystemStatus", "Free")
u.handleRollback(originalDir, 1)
Expand Down
19 changes: 19 additions & 0 deletions core/utils/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ func CopyItem(isDir, withName bool, src, dst string) error {
return nil
}

func CopyFileWithRename(src, dst string) error {
srcInfo, err := os.Stat(path.Dir(src))
if err != nil {
return err
}
if _, err := os.Stat(path.Dir(dst)); err != nil {
if os.IsNotExist(err) {
_ = os.MkdirAll(path.Dir(dst), srcInfo.Mode())
}
}
if err := cmd.RunDefaultBashCf("cp -f %s %s.tmp", src, dst); err != nil {
return fmt.Errorf("handle cp file failed, err: %v", err)
}
if err = cmd.RunDefaultBashCf("mv %s.tmp %s", dst, dst); err != nil {
return err
}
return nil
}

func HandleTar(sourceDir, targetDir, name, exclusionRules string, secret string) error {
if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(targetDir, os.ModePerm); err != nil {
Expand Down
Loading