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 // 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 { if len(Context.updater.NewVersion) == 0 {
httpError(w, http.StatusBadRequest, "/update request isn't allowed now") httpError(w, http.StatusBadRequest, "/update request isn't allowed now")
return return

View File

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

View File

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

View File

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

View File

@ -20,18 +20,9 @@ import (
// Updater - Updater // Updater - Updater
type Updater struct { type Updater struct {
Client *http.Client Config // Updater configuration
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"
currentExeName string // current binary executable 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" updateDir string // "work_dir/agh-update-v0.103.0"
packageName string // "work_dir/agh-update-v0.103.0/pkg_name.tar.gz" packageName string // "work_dir/agh-update-v0.103.0/pkg_name.tar.gz"
backupDir string // "work_dir/agh-backup" backupDir string // "work_dir/agh-backup"
@ -44,12 +35,26 @@ type Updater struct {
versionCheckLastTime time.Time 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 // NewUpdater - creates a new instance of the Updater
func NewUpdater(workDir string) *Updater { func NewUpdater(cfg Config) *Updater {
u := &Updater{ return &Updater{
workDir: workDir, Config: cfg,
} }
return u
} }
// DoUpdate - conducts the auto-update // DoUpdate - conducts the auto-update
@ -95,14 +100,14 @@ func (u *Updater) DoUpdate() error {
} }
func (u *Updater) prepare() 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) _, pkgNameOnly := filepath.Split(u.PackageURL)
if len(pkgNameOnly) == 0 { if len(pkgNameOnly) == 0 {
return fmt.Errorf("invalid PackageURL") return fmt.Errorf("invalid PackageURL")
} }
u.packageName = filepath.Join(u.updateDir, pkgNameOnly) 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" exeName := "AdGuardHome"
if u.OS == "windows" { 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 // 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 // Getting the full path to the current binary file on UNIX and checking write permissions
// is more difficult. // is more difficult.
u.currentExeName = filepath.Join(u.workDir, exeName) u.currentExeName = filepath.Join(u.WorkDir, exeName)
if !util.FileExists(u.currentExeName) { if !util.FileExists(u.currentExeName) {
return fmt.Errorf("executable file %s doesn't exist", 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 // 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 { if err != nil {
return fmt.Errorf("copySupportingFiles(%s, %s) failed: %s", return fmt.Errorf("copySupportingFiles(%s, %s) failed: %s",
u.workDir, u.backupDir, err) u.WorkDir, u.backupDir, err)
} }
return nil return nil
@ -183,10 +188,10 @@ func (u *Updater) backup() error {
func (u *Updater) replace() error { func (u *Updater) replace() error {
// update/README.md -> workdir/README.md // 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 { if err != nil {
return fmt.Errorf("copySupportingFiles(%s, %s) failed: %s", 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) log.Debug("updater: renaming: %s -> %s", u.currentExeName, u.backupExeName)