diff --git a/internal/home/config.go b/internal/home/config.go index 8ddb691d..c010a414 100644 --- a/internal/home/config.go +++ b/internal/home/config.go @@ -163,8 +163,10 @@ type configuration struct { // [configmigrate.LastSchemaVersion]. SchemaVersion uint `yaml:"schema_version"` - // UnsafeCustomUpdateIndexURL is the URL to the custom update index. It's - // only used in testing purposes and should not be used in release. + // UnsafeCustomUpdateIndexURL is the URL to the custom update index. + // + // NOTE: It's only exists for testing purposes and should not be used in + // release. UnsafeCustomUpdateIndexURL bool `yaml:"unsafe_custom_update_index_url,omitempty"` } diff --git a/internal/home/controlupdate.go b/internal/home/controlupdate.go index acf3e0dd..aeab8810 100644 --- a/internal/home/controlupdate.go +++ b/internal/home/controlupdate.go @@ -75,7 +75,7 @@ func (web *webAPI) handleVersionJSON(w http.ResponseWriter, r *http.Request) { // update server. func (web *webAPI) requestVersionInfo(resp *versionResponse, recheck bool) (err error) { updater := web.conf.updater - for i := 0; i != 3; i++ { + for range 3 { resp.VersionInfo, err = updater.VersionInfo(recheck) if err == nil { return nil @@ -87,9 +87,10 @@ func (web *webAPI) requestVersionInfo(resp *versionResponse, recheck bool) (err // restarting our DNS server. Log and sleep for some time. // // See https://github.com/AdguardTeam/AdGuardHome/issues/934. - d := time.Duration(i) * time.Second - log.Info("update: temp net error: %q; sleeping for %s and retrying", err, d) - time.Sleep(d) + const sleepTime = 2 * time.Second + + log.Info("update: temp net error: %v; sleeping for %s and retrying", err, sleepTime) + time.Sleep(sleepTime) continue } diff --git a/internal/home/home.go b/internal/home/home.go index d6f11489..a7547ae6 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -606,11 +606,11 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) { confPath := configFilePath() - upd := newUpdater(Context.workDir, confPath, execPath, config) + upd := newUpdater(ctx, slogLogger, Context.workDir, confPath, execPath, config) // TODO(e.burkov): This could be made earlier, probably as the option's // effect. - cmdlineUpdate(opts, upd, slogLogger) + cmdlineUpdate(ctx, slogLogger, opts, upd) if !Context.firstRun { // Save the updated config. @@ -679,20 +679,40 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) { } // newUpdater creates a new AdGuard Home updater. -func newUpdater(workDir, confPath, execPath string, config *configuration) (upd *updater.Updater) { +func newUpdater( + ctx context.Context, + l *slog.Logger, + workDir string, + confPath string, + execPath string, + config *configuration, +) (upd *updater.Updater) { // envName is the name of the environment variable that can be used to // override the default version check URL. const envName = "ADGUARD_HOME_TEST_UPDATE_VERSION_URL" + customURLStr := os.Getenv(envName) + var versionURL *url.URL - if version.Channel() == version.ChannelRelease || !config.UnsafeCustomUpdateIndexURL { - // Go on, use the default URL. - } else if versionURLStr, ok := os.LookupEnv(envName); ok { + switch { + case + // Only enable custom update URL for development builds. + version.Channel() == version.ChannelRelease, + !config.UnsafeCustomUpdateIndexURL: + // Go on and use the default URL. + case customURLStr != "": var err error - versionURL, err = url.Parse(versionURLStr) + versionURL, err = url.Parse(customURLStr) if err != nil { - log.Error(envName+" is not a valid URL: %s", err) + l.ErrorContext( + ctx, + "invalid environment variable value", + "env", envName, + slogutil.KeyError, err, + ) } + default: + l.DebugContext(ctx, "environment variable value is not specified", "env", envName) } if versionURL == nil { @@ -703,7 +723,7 @@ func newUpdater(workDir, confPath, execPath string, config *configuration) (upd } } - log.Debug("using config path %q for updater", confPath) + l.DebugContext(ctx, "creating updater", "config_path", confPath) return updater.NewUpdater(&updater.Config{ Client: config.Filtering.HTTPClient, @@ -1023,7 +1043,7 @@ type jsonError struct { } // cmdlineUpdate updates current application and exits. l must not be nil. -func cmdlineUpdate(opts options, upd *updater.Updater, l *slog.Logger) { +func cmdlineUpdate(ctx context.Context, l *slog.Logger, opts options, upd *updater.Updater) { if !opts.performUpdate { return } @@ -1036,19 +1056,19 @@ func cmdlineUpdate(opts options, upd *updater.Updater, l *slog.Logger) { err := initDNSServer(nil, nil, nil, nil, nil, nil, &tlsConfigSettings{}, l) fatalOnError(err) - log.Info("cmdline update: performing update") + l.InfoContext(ctx, "performing update via cli") info, err := upd.VersionInfo(true) if err != nil { - log.Error("getting version info: %s", err) + l.ErrorContext(ctx, "getting version info", slogutil.KeyError, err) - os.Exit(1) + os.Exit(osutil.ExitCodeFailure) } if info.NewVersion == version.Version() { - log.Info("no updates available") + l.InfoContext(ctx, "no updates available") - os.Exit(0) + os.Exit(osutil.ExitCodeSuccess) } err = upd.Update(Context.firstRun) @@ -1056,10 +1076,10 @@ func cmdlineUpdate(opts options, upd *updater.Updater, l *slog.Logger) { err = restartService() if err != nil { - log.Debug("restarting service: %s", err) - log.Info("AdGuard Home was not installed as a service. " + + l.DebugContext(ctx, "restarting service", slogutil.KeyError, err) + l.InfoContext(ctx, "AdGuard Home was not installed as a service. "+ "Please restart running instances of AdGuardHome manually.") } - os.Exit(0) + os.Exit(osutil.ExitCodeSuccess) } diff --git a/internal/updater/updater_internal_test.go b/internal/updater/updater_internal_test.go index 0ed94641..362699e0 100644 --- a/internal/updater/updater_internal_test.go +++ b/internal/updater/updater_internal_test.go @@ -76,7 +76,8 @@ func TestUpdater_internal(t *testing.T) { u.clean() - t.Run("backup", func(t *testing.T) { + // Consider the following subtest necessary. + require.True(t, t.Run("backup", func(t *testing.T) { var d []byte d, err = os.ReadFile(filepath.Join(wd, "agh-backup", "AdGuardHome.yaml")) require.NoError(t, err) @@ -87,7 +88,7 @@ func TestUpdater_internal(t *testing.T) { require.NoError(t, err) assert.Equal(t, tc.exeName, string(d)) - }) + })) t.Run("updated", func(t *testing.T) { var d []byte