Fix minor issues

This commit is contained in:
Andrey Meshkov 2020-07-22 20:27:20 +03:00
parent e3ea2528be
commit 6d5d183311
5 changed files with 70 additions and 60 deletions

View File

@ -64,7 +64,7 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
}
// Perform an update procedure to the latest available version
func handleUpdate(w http.ResponseWriter, r *http.Request) {
func handleUpdate(w http.ResponseWriter, _ *http.Request) {
if len(Context.updater.NewVersion) == 0 {
httpError(w, http.StatusBadRequest, "/update request isn't allowed now")
return

View File

@ -226,14 +226,16 @@ func run(args options) {
}
Context.autoHosts.Init("")
Context.updater = update.NewUpdater(Context.workDir)
Context.updater.Client = Context.client
Context.updater.VersionURL = versionCheckURL
Context.updater.VersionString = versionString
Context.updater.OS = runtime.GOOS
Context.updater.Arch = runtime.GOARCH
Context.updater.ARMVersion = ARMVersion
Context.updater.ConfigName = config.getConfigFilename()
Context.updater = update.NewUpdater(update.Config{
Client: Context.client,
WorkDir: Context.workDir,
VersionURL: versionCheckURL,
VersionString: versionString,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
ARMVersion: ARMVersion,
ConfigName: config.getConfigFilename(),
})
Context.clients.Init(config.Clients, Context.dhcpServer, &Context.autoHosts)
config.Clients = nil

View File

@ -11,15 +11,14 @@ const versionCheckPeriod = 8 * 60 * 60
// VersionInfo - VersionInfo
type VersionInfo struct {
NewVersion string
Announcement string
AnnouncementURL string
SelfUpdateMinVersion string
CanAutoUpdate bool
PackageURL string
NewVersion string // New version string
Announcement string // Announcement text
AnnouncementURL string // Announcement URL
SelfUpdateMinVersion string // Min version starting with which we can auto-update
CanAutoUpdate bool // If true - we can auto-update
}
// GetVersionResponse - GetVersionResponse
// GetVersionResponse - downloads version.json (if needed) and deserializes it
func (u *Updater) GetVersionResponse(forceRecheck bool) (VersionInfo, error) {
if !forceRecheck &&
u.versionCheckLastTime.Unix()+versionCheckPeriod > time.Now().Unix() {
@ -63,8 +62,8 @@ func (u *Updater) parseVersionResponse(data []byte) (VersionInfo, error) {
return info, fmt.Errorf("version.json: invalid data")
}
var ok bool
info.PackageURL, ok = u.getDownloadURL(versionJSON)
packageURL, ok := u.getDownloadURL(versionJSON)
if ok &&
info.NewVersion != u.VersionString &&
u.VersionString >= info.SelfUpdateMinVersion {
@ -72,7 +71,7 @@ func (u *Updater) parseVersionResponse(data []byte) (VersionInfo, error) {
}
u.NewVersion = info.NewVersion
u.PackageURL = info.PackageURL
u.PackageURL = packageURL
return info, nil
}

View File

@ -59,12 +59,13 @@ func TestUpdateGetVersion(t *testing.T) {
l, lport := startHTTPServer(jsonData)
defer func() { _ = l.Close() }()
u := NewUpdater("")
u.Client = &http.Client{}
u.VersionURL = fmt.Sprintf("http://127.0.0.1:%d/", lport)
u.OS = "linux"
u.Arch = "arm"
u.VersionString = "v0.103.0-beta1"
u := NewUpdater(Config{
Client: &http.Client{},
VersionURL: fmt.Sprintf("http://127.0.0.1:%d/", lport),
OS: "linux",
Arch: "arm",
VersionString: "v0.103.0-beta1",
})
info, err := u.GetVersionResponse(false)
assert.Nil(t, err)
@ -73,7 +74,6 @@ func TestUpdateGetVersion(t *testing.T) {
assert.Equal(t, "https://github.com/AdguardTeam/AdGuardHome/releases", info.AnnouncementURL)
assert.Equal(t, "v0.0", info.SelfUpdateMinVersion)
assert.True(t, info.CanAutoUpdate)
assert.Equal(t, "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz", info.PackageURL)
_ = l.Close()
@ -100,12 +100,14 @@ func TestUpdate(t *testing.T) {
l, lport := startHTTPServer(string(pkgData))
defer func() { _ = l.Close() }()
u := NewUpdater("aghtest")
u.Client = &http.Client{}
u.PackageURL = fmt.Sprintf("http://127.0.0.1:%d/AdGuardHome.tar.gz", lport)
u.VersionString = "v0.103.0"
u.NewVersion = "v0.103.1"
u.ConfigName = "aghtest/AdGuardHome.yaml"
u := NewUpdater(Config{
Client: &http.Client{},
PackageURL: fmt.Sprintf("http://127.0.0.1:%d/AdGuardHome.tar.gz", lport),
VersionString: "v0.103.0",
NewVersion: "v0.103.1",
ConfigName: "aghtest/AdGuardHome.yaml",
WorkDir: "aghtest",
})
assert.Nil(t, u.prepare())
u.currentExeName = "aghtest/AdGuardHome"
@ -161,13 +163,15 @@ func TestUpdateWindows(t *testing.T) {
l, lport := startHTTPServer(string(pkgData))
defer func() { _ = l.Close() }()
u := NewUpdater("aghtest")
u.Client = &http.Client{}
u.PackageURL = fmt.Sprintf("http://127.0.0.1:%d/AdGuardHome.zip", lport)
u.OS = "windows"
u.VersionString = "v0.103.0"
u.NewVersion = "v0.103.1"
u.ConfigName = "aghtest/AdGuardHome.yaml"
u := NewUpdater(Config{
WorkDir: "aghtest",
Client: &http.Client{},
PackageURL: fmt.Sprintf("http://127.0.0.1:%d/AdGuardHome.zip", lport),
OS: "windows",
VersionString: "v0.103.0",
NewVersion: "v0.103.1",
ConfigName: "aghtest/AdGuardHome.yaml",
})
assert.Nil(t, u.prepare())
u.currentExeName = "aghtest/AdGuardHome.exe"

View File

@ -20,18 +20,9 @@ import (
// Updater - Updater
type Updater struct {
Client *http.Client
VersionURL string // version.json URL
VersionString string
OS string // GOOS
Arch string // GOARCH
ARMVersion string // ARM version, e.g. "6"
NewVersion string // VersionInfo.NewVersion
PackageURL string // VersionInfo.PackageURL
ConfigName string // current config file ".../AdGuardHome.yaml"
Config // Updater configuration
currentExeName string // current binary executable
workDir string // updater work dir (where backup/upd dirs will be created)
updateDir string // "work_dir/agh-update-v0.103.0"
packageName string // "work_dir/agh-update-v0.103.0/pkg_name.tar.gz"
backupDir string // "work_dir/agh-backup"
@ -44,12 +35,26 @@ type Updater struct {
versionCheckLastTime time.Time
}
// Config - updater config
type Config struct {
Client *http.Client
VersionURL string // version.json URL
VersionString string
OS string // GOOS
Arch string // GOARCH
ARMVersion string // ARM version, e.g. "6"
NewVersion string // VersionInfo.NewVersion
PackageURL string // VersionInfo.PackageURL
ConfigName string // current config file ".../AdGuardHome.yaml"
WorkDir string // updater work dir (where backup/upd dirs will be created)
}
// NewUpdater - creates a new instance of the Updater
func NewUpdater(workDir string) *Updater {
u := &Updater{
workDir: workDir,
func NewUpdater(cfg Config) *Updater {
return &Updater{
Config: cfg,
}
return u
}
// DoUpdate - conducts the auto-update
@ -95,14 +100,14 @@ func (u *Updater) DoUpdate() error {
}
func (u *Updater) prepare() error {
u.updateDir = filepath.Join(u.workDir, fmt.Sprintf("agh-update-%s", u.NewVersion))
u.updateDir = filepath.Join(u.WorkDir, fmt.Sprintf("agh-update-%s", u.NewVersion))
_, pkgNameOnly := filepath.Split(u.PackageURL)
if len(pkgNameOnly) == 0 {
return fmt.Errorf("invalid PackageURL")
}
u.packageName = filepath.Join(u.updateDir, pkgNameOnly)
u.backupDir = filepath.Join(u.workDir, "agh-backup")
u.backupDir = filepath.Join(u.WorkDir, "agh-backup")
exeName := "AdGuardHome"
if u.OS == "windows" {
@ -118,7 +123,7 @@ func (u *Updater) prepare() error {
// If the binary file isn't found in working directory, we won't be able to auto-update
// Getting the full path to the current binary file on UNIX and checking write permissions
// is more difficult.
u.currentExeName = filepath.Join(u.workDir, exeName)
u.currentExeName = filepath.Join(u.WorkDir, exeName)
if !util.FileExists(u.currentExeName) {
return fmt.Errorf("executable file %s doesn't exist", u.currentExeName)
}
@ -172,10 +177,10 @@ func (u *Updater) backup() error {
}
// workdir/README.md -> backup/README.md
err = copySupportingFiles(u.unpackedFiles, u.workDir, u.backupDir)
err = copySupportingFiles(u.unpackedFiles, u.WorkDir, u.backupDir)
if err != nil {
return fmt.Errorf("copySupportingFiles(%s, %s) failed: %s",
u.workDir, u.backupDir, err)
u.WorkDir, u.backupDir, err)
}
return nil
@ -183,10 +188,10 @@ func (u *Updater) backup() error {
func (u *Updater) replace() error {
// update/README.md -> workdir/README.md
err := copySupportingFiles(u.unpackedFiles, u.updateDir, u.workDir)
err := copySupportingFiles(u.unpackedFiles, u.updateDir, u.WorkDir)
if err != nil {
return fmt.Errorf("copySupportingFiles(%s, %s) failed: %s",
u.updateDir, u.workDir, err)
u.updateDir, u.WorkDir, err)
}
log.Debug("updater: renaming: %s -> %s", u.currentExeName, u.backupExeName)