Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Nov 22, 2024
1 parent 0452d8b commit c989eef
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
6 changes: 4 additions & 2 deletions internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
9 changes: 5 additions & 4 deletions internal/home/controlupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
56 changes: 38 additions & 18 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -1036,30 +1056,30 @@ 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)
fatalOnError(err)

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)
}
5 changes: 3 additions & 2 deletions internal/updater/updater_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit c989eef

Please sign in to comment.