* auto-update: remove the update directory after successful update

This commit is contained in:
Simon Zolin 2019-06-05 18:00:17 +03:00
parent ec5e2be31f
commit a165410c9f
2 changed files with 12 additions and 11 deletions

View File

@ -149,14 +149,15 @@ func getUpdateInfo(jsonData []byte) (*updateInfo, error) {
return nil, fmt.Errorf("No need to update") return nil, fmt.Errorf("No need to update")
} }
u.updateDir = filepath.Join(workDir, fmt.Sprintf("agh-update-%s", u.newVer))
u.backupDir = filepath.Join(workDir, fmt.Sprintf("agh-backup-%s", VersionString))
_, pkgFileName := filepath.Split(u.pkgURL) _, pkgFileName := filepath.Split(u.pkgURL)
if len(pkgFileName) == 0 { if len(pkgFileName) == 0 {
return nil, fmt.Errorf("Invalid JSON") return nil, fmt.Errorf("Invalid JSON")
} }
u.pkgName = filepath.Join(workDir, pkgFileName) u.pkgName = filepath.Join(u.updateDir, pkgFileName)
u.updateDir = filepath.Join(workDir, fmt.Sprintf("update-%s", u.newVer))
u.backupDir = filepath.Join(workDir, fmt.Sprintf("backup-%s", VersionString))
u.configName = config.getConfigFilename() u.configName = config.getConfigFilename()
u.updateConfigName = filepath.Join(u.updateDir, "AdGuardHome", "AdGuardHome.yaml") u.updateConfigName = filepath.Join(u.updateDir, "AdGuardHome", "AdGuardHome.yaml")
if strings.HasSuffix(pkgFileName, ".zip") { if strings.HasSuffix(pkgFileName, ".zip") {
@ -361,6 +362,8 @@ func doUpdate(u *updateInfo) error {
log.Info("Updating from %s to %s. URL:%s Package:%s", log.Info("Updating from %s to %s. URL:%s Package:%s",
VersionString, u.newVer, u.pkgURL, u.pkgName) VersionString, u.newVer, u.pkgURL, u.pkgName)
_ = os.Mkdir(u.updateDir, 0755)
var err error var err error
err = getPackageFile(u) err = getPackageFile(u)
if err != nil { if err != nil {
@ -368,7 +371,6 @@ func doUpdate(u *updateInfo) error {
} }
log.Tracef("Unpacking the package") log.Tracef("Unpacking the package")
_ = os.Mkdir(u.updateDir, 0755)
_, file := filepath.Split(u.pkgName) _, file := filepath.Split(u.pkgName)
var files []string var files []string
if strings.HasSuffix(file, ".zip") { if strings.HasSuffix(file, ".zip") {
@ -434,7 +436,7 @@ func doUpdate(u *updateInfo) error {
log.Tracef("Renamed: %s -> %s", u.newBinName, u.curBinName) log.Tracef("Renamed: %s -> %s", u.newBinName, u.curBinName)
_ = os.Remove(u.pkgName) _ = os.Remove(u.pkgName)
// _ = os.RemoveAll(u.updateDir) _ = os.RemoveAll(u.updateDir)
return nil return nil
} }

View File

@ -12,20 +12,19 @@ func testDoUpdate(t *testing.T) {
pkgURL: "https://github.com/AdguardTeam/AdGuardHome/releases/download/v0.95/AdGuardHome_v0.95_linux_amd64.tar.gz", pkgURL: "https://github.com/AdguardTeam/AdGuardHome/releases/download/v0.95/AdGuardHome_v0.95_linux_amd64.tar.gz",
pkgName: "./AdGuardHome_v0.95_linux_amd64.tar.gz", pkgName: "./AdGuardHome_v0.95_linux_amd64.tar.gz",
newVer: "v0.95", newVer: "v0.95",
updateDir: "./update-v0.95", updateDir: "./agh-update-v0.95",
backupDir: "./backup-v0.94", backupDir: "./agh-backup-v0.94",
configName: "./AdGuardHome.yaml", configName: "./AdGuardHome.yaml",
updateConfigName: "./update-v0.95/AdGuardHome/AdGuardHome.yaml", updateConfigName: "./agh-update-v0.95/AdGuardHome/AdGuardHome.yaml",
curBinName: "./AdGuardHome", curBinName: "./AdGuardHome",
bkpBinName: "./backup-v0.94/AdGuardHome", bkpBinName: "./agh-backup-v0.94/AdGuardHome",
newBinName: "./update-v0.95/AdGuardHome/AdGuardHome", newBinName: "./agh-update-v0.95/AdGuardHome/AdGuardHome",
} }
e := doUpdate(&u) e := doUpdate(&u)
if e != nil { if e != nil {
t.Fatalf("FAILED: %s", e) t.Fatalf("FAILED: %s", e)
} }
os.RemoveAll(u.backupDir) os.RemoveAll(u.backupDir)
os.RemoveAll(u.updateDir)
} }
func testTargzFileUnpack(t *testing.T) { func testTargzFileUnpack(t *testing.T) {