Add update module status, allow disabling of updates

This commit is contained in:
Patrick Pacher
2020-04-21 15:11:17 +02:00
parent f78dd18869
commit 71615811e4
2 changed files with 110 additions and 11 deletions

View File

@@ -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