Skip to content

Commit

Permalink
feat: support for rollback in case of error in intermediate steps
Browse files Browse the repository at this point in the history
Signed-off-by: 0zyt <zyt0@duck.com>
  • Loading branch information
0zyt committed Oct 16, 2022
1 parent 84c801b commit 3f62a93
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion internal/pkg/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,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 := 0
defer func() {
for ; updateProgress >= 0; updateProgress-- {
switch updateProgress {
//If the error occur when step 1 (rename dtmFileName to `dtm-bak`), delete `dtm-tmp`
case 0:
if err := os.Remove(dtmTmpFilePath); err != nil {
log.Debugf("Dtm upgrade rollback error: %s", err.Error())
}

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

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

//the error occur int the step 4
case 3:
if err := os.Chmod(dtmFilePath, 0644); err != nil {
log.Debugf("Dtm upgrade rollback error: %s", err.Error())
}
case 4:
//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

0 comments on commit 3f62a93

Please sign in to comment.