mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-18 13:01:51 +03:00
all: imp code
This commit is contained in:
parent
0452d8b356
commit
c989eef715
4 changed files with 50 additions and 26 deletions
|
@ -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"`
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue