From 71615811e4d281c2f5e3fc1ffed7620ec6217d89 Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Tue, 21 Apr 2020 15:11:17 +0200 Subject: [PATCH 1/5] Add update module status, allow disabling of updates --- updates/config.go | 43 ++++++++++++++++++++++++-- updates/main.go | 78 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 110 insertions(+), 11 deletions(-) diff --git a/updates/config.go b/updates/config.go index c0070d6f..79ed7c9a 100644 --- a/updates/config.go +++ b/updates/config.go @@ -5,14 +5,17 @@ import ( "fmt" "github.com/safing/portbase/config" + "github.com/safing/portbase/log" ) var ( releaseChannel config.StringOption devMode config.BoolOption + disableUpdates config.BoolOption - previousReleaseChannel string - previousDevMode bool + previousReleaseChannel string + updatesCurrentlyDisabled bool + previousDevMode bool ) func registerConfig() error { @@ -32,16 +35,35 @@ func registerConfig() error { return err } - return module.RegisterEventHook("config", "config change", "update registry config", updateRegistryConfig) + err = config.Register(&config.Option{ + Name: "Disable Updates", + Key: disableUpdatesKey, + Description: "Disable automatic updates.", + OptType: config.OptTypeBool, + ExpertiseLevel: config.ExpertiseLevelExpert, + ReleaseLevel: config.ReleaseLevelStable, + RequiresRestart: false, + DefaultValue: false, + ExternalOptType: "disable updates", + }) + if err != nil { + return err + } + + return nil } func initConfig() { releaseChannel = config.GetAsString(releaseChannelKey, releaseChannelStable) + disableUpdates = config.GetAsBool(disableUpdatesKey, false) + devMode = config.GetAsBool("core/devMode", false) } func updateRegistryConfig(_ context.Context, _ interface{}) error { changed := false + forceUpdate := false + if releaseChannel() != previousReleaseChannel { registry.SetBeta(releaseChannel() == releaseChannelBeta) previousReleaseChannel = releaseChannel() @@ -54,9 +76,24 @@ func updateRegistryConfig(_ context.Context, _ interface{}) error { changed = true } + if disableUpdates() != updatesCurrentlyDisabled { + updatesCurrentlyDisabled = disableUpdates() + changed = true + forceUpdate = !updatesCurrentlyDisabled + } + if changed { registry.SelectVersions() module.TriggerEvent(VersionUpdateEvent, nil) + + if forceUpdate { + module.Resolve(updateFailed) + TriggerUpdate() + log.Infof("Automatic updates enabled again.") + } else { + module.Warning(updateFailed, "Updates are disabled!") + log.Warningf("Automatic updates are now disabled.") + } } return nil diff --git a/updates/main.go b/updates/main.go index 3a7fef99..c1040d42 100644 --- a/updates/main.go +++ b/updates/main.go @@ -19,6 +19,8 @@ const ( releaseChannelStable = "stable" releaseChannelBeta = "beta" + disableUpdatesKey = "core/disableUpdates" + // ModuleName is the name of the update module // and can be used when declaring module dependencies. ModuleName = "updates" @@ -36,6 +38,10 @@ const ( // to check if new versions of their resources are // available by checking File.UpgradeAvailable(). ResourceUpdateEvent = "resource update" + + // TriggerUpdateEvent is the event that can be emitted + // by the updates module to trigger an update. + TriggerUpdateEvent = "trigger update" ) var ( @@ -46,15 +52,51 @@ var ( disableTaskSchedule bool ) +const ( + updateInProgress = "update-in-progress" + updateInProcessDescr = "Portmaster is currently checking and downloading updates." + updateFailed = "update-failed" +) + func init() { - module = modules.Register(ModuleName, registerConfig, start, stop, "base") + module = modules.Register(ModuleName, prep, start, stop, "base") module.RegisterEvent(VersionUpdateEvent) module.RegisterEvent(ResourceUpdateEvent) } +func prep() error { + if err := registerConfig(); err != nil { + return err + } + + module.RegisterEvent(TriggerUpdateEvent) + + return nil +} + func start() error { initConfig() + if err := module.RegisterEventHook( + "config", + "config change", + "update registry config", + updateRegistryConfig); err != nil { + return err + } + + if err := module.RegisterEventHook( + module.Name, + TriggerUpdateEvent, + "Check for and download available updates", + func(context.Context, interface{}) error { + TriggerUpdate() + return nil + }, + ); err != nil { + return err + } + var mandatoryUpdates []string if onWindows { mandatoryUpdates = []string{ @@ -115,8 +157,8 @@ func start() error { if !disableTaskSchedule { updateTask. - Repeat(24 * time.Hour). - MaxDelay(1 * time.Hour). + Repeat(1 * time.Hour). + MaxDelay(30 * time.Minute). Schedule(time.Now().Add(10 * time.Second)) } @@ -138,6 +180,7 @@ func TriggerUpdate() error { updateASAP = true } else { updateTask.StartASAP() + log.Debugf("updates: triggering update to run as soon as possible") } return nil @@ -156,14 +199,33 @@ func DisableUpdateSchedule() error { return nil } -func checkForUpdates(ctx context.Context) error { - if err := registry.UpdateIndexes(); err != nil { - return fmt.Errorf("updates: failed to update indexes: %w", err) +func checkForUpdates(ctx context.Context) (err error) { + if updatesCurrentlyDisabled { + log.Debugf("updates: automatic updates are disabled") + return nil + } + defer log.Debugf("updates: finished checking for updates") + + module.Hint(updateInProgress, updateInProcessDescr) + + defer func() { + if err == nil { + module.Resolve(updateInProgress) + module.Resolve(updateFailed) + } else { + module.Warning(updateFailed, "Failed to check for updates: "+err.Error()) + } + }() + + if err = registry.UpdateIndexes(); err != nil { + err = fmt.Errorf("failed to update indexes: %w", err) + return } - err := registry.DownloadUpdates(ctx) + err = registry.DownloadUpdates(ctx) if err != nil { - return fmt.Errorf("updates: failed to update: %w", err) + err = fmt.Errorf("failed to update: %w", err) + return } registry.SelectVersions() From 9be175c23888690c449db44ce42c9227a48646ae Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 21 Apr 2020 15:28:59 +0200 Subject: [PATCH 2/5] Fix earlier bug --- updates/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updates/config.go b/updates/config.go index 79ed7c9a..6fd90a67 100644 --- a/updates/config.go +++ b/updates/config.go @@ -71,7 +71,7 @@ func updateRegistryConfig(_ context.Context, _ interface{}) error { } if devMode() != previousDevMode { - registry.SetBeta(devMode()) + registry.SetDevMode(devMode()) previousDevMode = devMode() changed = true } From 27ed6da45f1263d1ce20b694b5a873e3852fb7df Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Tue, 21 Apr 2020 15:36:06 +0200 Subject: [PATCH 3/5] Fix linter warnings --- updates/config.go | 2 +- updates/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/updates/config.go b/updates/config.go index 6fd90a67..5cf70c53 100644 --- a/updates/config.go +++ b/updates/config.go @@ -88,7 +88,7 @@ func updateRegistryConfig(_ context.Context, _ interface{}) error { if forceUpdate { module.Resolve(updateFailed) - TriggerUpdate() + _ = TriggerUpdate() log.Infof("Automatic updates enabled again.") } else { module.Warning(updateFailed, "Updates are disabled!") diff --git a/updates/main.go b/updates/main.go index c1040d42..a03f822d 100644 --- a/updates/main.go +++ b/updates/main.go @@ -90,7 +90,7 @@ func start() error { TriggerUpdateEvent, "Check for and download available updates", func(context.Context, interface{}) error { - TriggerUpdate() + _ = TriggerUpdate() return nil }, ); err != nil { From 3d7d1fa0df1d52233b52d46ebde476641059d158 Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Tue, 21 Apr 2020 15:41:31 +0200 Subject: [PATCH 4/5] Remove useless modules.Resolve --- updates/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/updates/main.go b/updates/main.go index a03f822d..c6feb6e2 100644 --- a/updates/main.go +++ b/updates/main.go @@ -211,7 +211,6 @@ func checkForUpdates(ctx context.Context) (err error) { defer func() { if err == nil { module.Resolve(updateInProgress) - module.Resolve(updateFailed) } else { module.Warning(updateFailed, "Failed to check for updates: "+err.Error()) } From 0a0dda36e07b084e91f3398d430b836b829db877 Mon Sep 17 00:00:00 2001 From: Patrick Pacher Date: Tue, 21 Apr 2020 15:43:48 +0200 Subject: [PATCH 5/5] Change log message format --- updates/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/updates/config.go b/updates/config.go index 5cf70c53..59955549 100644 --- a/updates/config.go +++ b/updates/config.go @@ -89,10 +89,10 @@ func updateRegistryConfig(_ context.Context, _ interface{}) error { if forceUpdate { module.Resolve(updateFailed) _ = TriggerUpdate() - log.Infof("Automatic updates enabled again.") + log.Infof("updates: automatic updates enabled again.") } else { module.Warning(updateFailed, "Updates are disabled!") - log.Warningf("Automatic updates are now disabled.") + log.Warningf("updates: automatic updates are now disabled.") } }