Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for rollback in case of error in intermediate steps #1379

Merged
merged 5 commits into from
Jan 17, 2023
Merged
Changes from 4 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
46 changes: 45 additions & 1 deletion internal/pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import (
)

const (
//the rollback step for the applyUpgrade function
STEP1 = iota
STEP2
STEP3
STEP4
COMPLETED

assetName = "dtm-" + runtime.GOOS + "-" + runtime.GOARCH
dtmTmpFileName = "dtm-tmp"
dtmBakFileName = "dtm-bak"
Expand Down Expand Up @@ -137,30 +144,67 @@ func parseVersion(old, new string) (*semver.Version, *semver.Version, error) {
// (2) rename `dtm-tmp` to current dtm file name.
// (3) grant new dtm file execute permission.
// (4) remove `dtm-bak` binary file.
// TODO(hxcGit): Support for rollback in case of error in intermediate steps

func applyUpgrade(workDir string) error {
dtmFilePath := filepath.Join(workDir, dtmFileName)
dtmBakFilePath := filepath.Join(workDir, dtmBakFileName)
dtmTmpFilePath := filepath.Join(workDir, dtmTmpFileName)
updateProgress := STEP1
defer func() {
for ; updateProgress >= STEP1; updateProgress-- {
switch updateProgress {
//If the error occur when step 1 (rename dtmFileName to `dtm-bak`), delete `dtm-tmp`
case STEP1:
if err := os.Remove(dtmTmpFilePath); err != nil {
log.Debugf("Dtm upgrade rollback error: %s", err.Error())
}

//the error occur in the step 2
case STEP2:
if err := os.Rename(dtmBakFilePath, dtmFilePath); err != nil {
log.Debugf("Dtm upgrade rollback error: %s", err.Error())
}

//the error occur in the step 3
case STEP3:
if err := os.Rename(dtmFilePath, dtmTmpFilePath); err != nil {
log.Debugf("Dtm upgrade rollback error: %s", err.Error())
}

//the error occur in the step 4
case STEP4:
if err := os.Chmod(dtmFilePath, 0644); err != nil {
log.Debugf("Dtm upgrade rollback error: %s", err.Error())
}
case COMPLETED:
//Successfully completed all step
return
}
}
}()

if err := os.Rename(dtmFilePath, dtmBakFilePath); err != nil {
return err
}
updateProgress++
log.Debugf("Dtm upgrade: rename %s to dtm-bak successfully.", dtmFileName)

if err := os.Rename(dtmTmpFilePath, dtmFilePath); err != nil {
return err
}
updateProgress++
log.Debugf("Dtm upgrade: rename dtm-tmp to %s successfully.", dtmFileName)

if err := os.Chmod(dtmFilePath, 0755); err != nil {
return err
}
updateProgress++
log.Debugf("Dtm upgrade: grant %s execute permission successfully.", dtmFileName)

if err := os.Remove(dtmBakFilePath); err != nil {
return err
}
updateProgress++
log.Debug("Dtm upgrade: remove dtm-bak successfully.")

return nil
Expand Down