all: enable updater for some cases

This commit is contained in:
Eugene Burkov 2024-11-26 15:07:09 +03:00
parent 872cd3a18c
commit c7f3822929
3 changed files with 61 additions and 29 deletions

View File

@ -12,7 +12,6 @@ import (
"net/url" "net/url"
"os" "os"
"os/signal" "os/signal"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"slices" "slices"
@ -495,11 +494,40 @@ func checkPorts() (err error) {
return nil return nil
} }
// isUpdateEnabled returns true if the update is enabled for current
// configuration. It also logs the decision. customURL should be true if the
// updater is using a custom URL.
func isUpdateEnabled(ctx context.Context, l *slog.Logger, opts *options, customURL bool) (ok bool) {
switch ch := version.Channel(); {
case opts.disableUpdate:
l.DebugContext(ctx, "updates are disabled by command-line option")
return false
case
ch == version.ChannelDevelopment,
ch == version.ChannelCandidate:
if customURL {
l.DebugContext(ctx, "updates are enabled due to custom URL is used")
} else {
l.DebugContext(ctx, "updates are disabled for development and candidate builds")
}
return customURL
default:
l.DebugContext(ctx, "updates are enabled")
return true
}
}
// initWeb initializes the web module.
func initWeb( func initWeb(
ctx context.Context,
opts options, opts options,
clientBuildFS fs.FS, clientBuildFS fs.FS,
upd *updater.Updater, upd *updater.Updater,
l *slog.Logger, l *slog.Logger,
customURL bool,
) (web *webAPI, err error) { ) (web *webAPI, err error) {
var clientFS fs.FS var clientFS fs.FS
if opts.localFrontend { if opts.localFrontend {
@ -513,17 +541,7 @@ func initWeb(
} }
} }
disableUpdate := opts.disableUpdate disableUpdate := !isUpdateEnabled(ctx, l, &opts, customURL)
switch version.Channel() {
case
version.ChannelDevelopment,
version.ChannelCandidate:
disableUpdate = true
}
if disableUpdate {
log.Info("AdGuard Home updates are disabled")
}
webConf := &webConfig{ webConf := &webConfig{
updater: upd, updater: upd,
@ -544,7 +562,7 @@ func initWeb(
web = newWebAPI(webConf, l) web = newWebAPI(webConf, l)
if web == nil { if web == nil {
return nil, fmt.Errorf("initializing web: %w", err) return nil, errors.Error("can not initialize web")
} }
return web, nil return web, nil
@ -557,6 +575,8 @@ func fatalOnError(err error) {
} }
// run configures and starts AdGuard Home. // run configures and starts AdGuard Home.
//
// TODO(e.burkov): Make opts a pointer.
func run(opts options, clientBuildFS fs.FS, done chan struct{}) { func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
// Configure working dir. // Configure working dir.
err := initWorkingDir(opts) err := initWorkingDir(opts)
@ -606,7 +626,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
confPath := configFilePath() confPath := configFilePath()
upd := newUpdater(ctx, slogLogger, Context.workDir, confPath, execPath, config) upd, customURL := newUpdater(ctx, slogLogger, Context.workDir, confPath, execPath, config)
// TODO(e.burkov): This could be made earlier, probably as the option's // TODO(e.burkov): This could be made earlier, probably as the option's
// effect. // effect.
@ -638,7 +658,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
onConfigModified() onConfigModified()
} }
Context.web, err = initWeb(opts, clientBuildFS, upd, slogLogger) Context.web, err = initWeb(ctx, opts, clientBuildFS, upd, slogLogger, customURL)
fatalOnError(err) fatalOnError(err)
statsDir, querylogDir, err := checkStatsAndQuerylogDirs(&Context, config) statsDir, querylogDir, err := checkStatsAndQuerylogDirs(&Context, config)
@ -676,7 +696,8 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
<-done <-done
} }
// newUpdater creates a new AdGuard Home updater. // newUpdater creates a new AdGuard Home updater. customURL is true if the user
// has specified a custom version announcement URL.
func newUpdater( func newUpdater(
ctx context.Context, ctx context.Context,
l *slog.Logger, l *slog.Logger,
@ -684,7 +705,7 @@ func newUpdater(
confPath string, confPath string,
execPath string, execPath string,
config *configuration, config *configuration,
) (upd *updater.Updater) { ) (upd *updater.Updater, customURL bool) {
// envName is the name of the environment variable that can be used to // envName is the name of the environment variable that can be used to
// override the default version check URL. // override the default version check URL.
const envName = "ADGUARD_HOME_TEST_UPDATE_VERSION_URL" const envName = "ADGUARD_HOME_TEST_UPDATE_VERSION_URL"
@ -693,15 +714,16 @@ func newUpdater(
var versionURL *url.URL var versionURL *url.URL
switch { switch {
case case version.Channel() == version.ChannelRelease:
// Only enable custom update URL for development builds. // Only enable custom update URL for development builds.
version.Channel() == version.ChannelRelease, l.DebugContext(ctx, "custom update URL is disabled for release builds")
!config.UnsafeCustomUpdateIndexURL: case !config.UnsafeCustomUpdateIndexURL:
// Go on and use the default URL. // Go on and use the default URL.
l.DebugContext(ctx, "custom update URL is disabled", "env", customURLStr)
case customURLStr != "": case customURLStr != "":
var err error var err error
versionURL, err = url.Parse(customURLStr) versionURL, err = url.Parse(customURLStr)
if err != nil { if customURL = err == nil; !customURL {
l.ErrorContext( l.ErrorContext(
ctx, ctx,
"invalid environment variable value", "invalid environment variable value",
@ -714,11 +736,7 @@ func newUpdater(
} }
if versionURL == nil { if versionURL == nil {
versionURL = &url.URL{ versionURL = updater.DefaultVersionURL()
Scheme: urlutil.SchemeHTTPS,
Host: "static.adtidy.org",
Path: path.Join("adguardhome", version.Channel(), "version.json"),
}
} }
l.DebugContext(ctx, "creating updater", "config_path", confPath) l.DebugContext(ctx, "creating updater", "config_path", confPath)
@ -735,7 +753,7 @@ func newUpdater(
ConfName: confPath, ConfName: confPath,
ExecPath: execPath, ExecPath: execPath,
VersionCheckURL: versionURL, VersionCheckURL: versionURL,
}) }), customURL
} }
// checkPermissions checks and migrates permissions of the files and directories // checkPermissions checks and migrates permissions of the files and directories

View File

@ -101,6 +101,8 @@ type webAPI struct {
// newWebAPI creates a new instance of the web UI and API server. l must not be // newWebAPI creates a new instance of the web UI and API server. l must not be
// nil. // nil.
//
// TODO(a.garipov): Return a proper error.
func newWebAPI(conf *webConfig, l *slog.Logger) (w *webAPI) { func newWebAPI(conf *webConfig, l *slog.Logger) (w *webAPI) {
log.Info("web: initializing") log.Info("web: initializing")

View File

@ -12,6 +12,7 @@ import (
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -22,6 +23,7 @@ import (
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/ioutil" "github.com/AdguardTeam/golibs/ioutil"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil/urlutil"
) )
// Updater is the AdGuard Home updater. // Updater is the AdGuard Home updater.
@ -62,11 +64,21 @@ type Updater struct {
prevCheckResult VersionInfo prevCheckResult VersionInfo
} }
// DefaultVersionURL returns the default URL for the version announcement.
func DefaultVersionURL() *url.URL {
return &url.URL{
Scheme: urlutil.SchemeHTTPS,
Host: "static.adtidy.org",
Path: path.Join("adguardhome", version.Channel(), "version.json"),
}
}
// Config is the AdGuard Home updater configuration. // Config is the AdGuard Home updater configuration.
type Config struct { type Config struct {
Client *http.Client Client *http.Client
// VersionCheckURL is url to the latest version announcement. // VersionCheckURL is URL to the latest version announcement. It must not
// be nil, see [DefaultVersionURL].
VersionCheckURL *url.URL VersionCheckURL *url.URL
Version string Version string
@ -87,7 +99,7 @@ type Config struct {
ExecPath string ExecPath string
} }
// NewUpdater creates a new Updater. // NewUpdater creates a new Updater. conf must not be nil.
func NewUpdater(conf *Config) *Updater { func NewUpdater(conf *Config) *Updater {
return &Updater{ return &Updater{
client: conf.Client, client: conf.Client,