[WIP] New updater
This commit is contained in:
@@ -1,20 +1,14 @@
|
||||
package updates
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portmaster/base/database"
|
||||
"github.com/safing/portmaster/base/dataroot"
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/base/updater"
|
||||
"github.com/safing/portmaster/service/mgr"
|
||||
"github.com/safing/portmaster/service/updates/helper"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -48,9 +42,6 @@ var (
|
||||
userAgentFromFlag string
|
||||
updateServerFromFlag string
|
||||
|
||||
updateASAP bool
|
||||
disableTaskSchedule bool
|
||||
|
||||
db = database.NewInterface(&database.Options{
|
||||
Local: true,
|
||||
Internal: true,
|
||||
@@ -60,163 +51,126 @@ var (
|
||||
// more context to requests made by the registry when
|
||||
// fetching resources from the update server.
|
||||
UserAgent = fmt.Sprintf("Portmaster (%s %s)", runtime.GOOS, runtime.GOARCH)
|
||||
|
||||
// DefaultUpdateURLs defines the default base URLs of the update server.
|
||||
DefaultUpdateURLs = []string{
|
||||
"https://updates.safing.io",
|
||||
}
|
||||
|
||||
// DisableSoftwareAutoUpdate specifies whether software updates should be disabled.
|
||||
// This is used on Android, as it will never require binary updates.
|
||||
DisableSoftwareAutoUpdate = false
|
||||
)
|
||||
|
||||
const (
|
||||
updatesDirName = "updates"
|
||||
|
||||
updateTaskRepeatDuration = 1 * time.Hour
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&updateServerFromFlag, "update-server", "", "set an alternative update server (full URL)")
|
||||
flag.StringVar(&userAgentFromFlag, "update-agent", "", "set an alternative user agent for requests to the update server")
|
||||
}
|
||||
|
||||
func prep() error {
|
||||
// Check if update server URL supplied via flag is a valid URL.
|
||||
if updateServerFromFlag != "" {
|
||||
u, err := url.Parse(updateServerFromFlag)
|
||||
if err != nil {
|
||||
return fmt.Errorf("supplied update server URL is invalid: %w", err)
|
||||
}
|
||||
if u.Scheme != "https" {
|
||||
return errors.New("supplied update server URL must use HTTPS")
|
||||
}
|
||||
}
|
||||
|
||||
if err := registerConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return registerAPIEndpoints()
|
||||
}
|
||||
|
||||
func start() error {
|
||||
initConfig()
|
||||
// module.restartWorkerMgr.Repeat(10 * time.Minute)
|
||||
// module.instance.Config().EventConfigChange.AddCallback("update registry config", updateRegistryConfig)
|
||||
|
||||
module.restartWorkerMgr.Repeat(10 * time.Minute)
|
||||
module.instance.Config().EventConfigChange.AddCallback("update registry config", updateRegistryConfig)
|
||||
// // create registry
|
||||
// registry = &updater.ResourceRegistry{
|
||||
// Name: ModuleName,
|
||||
// UpdateURLs: DefaultUpdateURLs,
|
||||
// UserAgent: UserAgent,
|
||||
// MandatoryUpdates: helper.MandatoryUpdates(),
|
||||
// AutoUnpack: helper.AutoUnpackUpdates(),
|
||||
// Verification: helper.VerificationConfig,
|
||||
// DevMode: devMode(),
|
||||
// Online: true,
|
||||
// }
|
||||
// // Override values from flags.
|
||||
// if userAgentFromFlag != "" {
|
||||
// registry.UserAgent = userAgentFromFlag
|
||||
// }
|
||||
// if updateServerFromFlag != "" {
|
||||
// registry.UpdateURLs = []string{updateServerFromFlag}
|
||||
// }
|
||||
|
||||
// create registry
|
||||
registry = &updater.ResourceRegistry{
|
||||
Name: ModuleName,
|
||||
UpdateURLs: DefaultUpdateURLs,
|
||||
UserAgent: UserAgent,
|
||||
MandatoryUpdates: helper.MandatoryUpdates(),
|
||||
AutoUnpack: helper.AutoUnpackUpdates(),
|
||||
Verification: helper.VerificationConfig,
|
||||
DevMode: devMode(),
|
||||
Online: true,
|
||||
}
|
||||
// Override values from flags.
|
||||
if userAgentFromFlag != "" {
|
||||
registry.UserAgent = userAgentFromFlag
|
||||
}
|
||||
if updateServerFromFlag != "" {
|
||||
registry.UpdateURLs = []string{updateServerFromFlag}
|
||||
}
|
||||
|
||||
// pre-init state
|
||||
updateStateExport, err := LoadStateExport()
|
||||
if err != nil {
|
||||
log.Debugf("updates: failed to load exported update state: %s", err)
|
||||
} else if updateStateExport.UpdateState != nil {
|
||||
err := registry.PreInitUpdateState(*updateStateExport.UpdateState)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// // pre-init state
|
||||
// updateStateExport, err := LoadStateExport()
|
||||
// if err != nil {
|
||||
// log.Debugf("updates: failed to load exported update state: %s", err)
|
||||
// } else if updateStateExport.UpdateState != nil {
|
||||
// err := registry.PreInitUpdateState(*updateStateExport.UpdateState)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
// initialize
|
||||
err = registry.Initialize(dataroot.Root().ChildDir(updatesDirName, 0o0755))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// err := registry.Initialize(dataroot.Root().ChildDir(updatesDirName, 0o0755))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// register state provider
|
||||
err = registerRegistryStateProvider()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
registry.StateNotifyFunc = pushRegistryState
|
||||
// // register state provider
|
||||
// err = registerRegistryStateProvider()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// registry.StateNotifyFunc = pushRegistryState
|
||||
|
||||
// Set indexes based on the release channel.
|
||||
warning := helper.SetIndexes(
|
||||
registry,
|
||||
initialReleaseChannel,
|
||||
true,
|
||||
enableSoftwareUpdates() && !DisableSoftwareAutoUpdate,
|
||||
enableIntelUpdates(),
|
||||
)
|
||||
if warning != nil {
|
||||
log.Warningf("updates: %s", warning)
|
||||
}
|
||||
// // Set indexes based on the release channel.
|
||||
// warning := helper.SetIndexes(
|
||||
// registry,
|
||||
// initialReleaseChannel,
|
||||
// true,
|
||||
// enableSoftwareUpdates() && !DisableSoftwareAutoUpdate,
|
||||
// enableIntelUpdates(),
|
||||
// )
|
||||
// if warning != nil {
|
||||
// log.Warningf("updates: %s", warning)
|
||||
// }
|
||||
|
||||
err = registry.LoadIndexes(module.m.Ctx())
|
||||
if err != nil {
|
||||
log.Warningf("updates: failed to load indexes: %s", err)
|
||||
}
|
||||
// err = registry.LoadIndexes(module.m.Ctx())
|
||||
// if err != nil {
|
||||
// log.Warningf("updates: failed to load indexes: %s", err)
|
||||
// }
|
||||
|
||||
err = registry.ScanStorage("")
|
||||
if err != nil {
|
||||
log.Warningf("updates: error during storage scan: %s", err)
|
||||
}
|
||||
// err = registry.ScanStorage("")
|
||||
// if err != nil {
|
||||
// log.Warningf("updates: error during storage scan: %s", err)
|
||||
// }
|
||||
|
||||
registry.SelectVersions()
|
||||
module.EventVersionsUpdated.Submit(struct{}{})
|
||||
// registry.SelectVersions()
|
||||
// module.EventVersionsUpdated.Submit(struct{}{})
|
||||
|
||||
// Initialize the version export - this requires the registry to be set up.
|
||||
err = initVersionExport()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// // Initialize the version export - this requires the registry to be set up.
|
||||
// err = initVersionExport()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// start updater task
|
||||
if !disableTaskSchedule {
|
||||
_ = module.updateWorkerMgr.Repeat(30 * time.Minute)
|
||||
}
|
||||
// // start updater task
|
||||
// if !disableTaskSchedule {
|
||||
// _ = module.updateWorkerMgr.Repeat(30 * time.Minute)
|
||||
// }
|
||||
|
||||
if updateASAP {
|
||||
module.updateWorkerMgr.Go()
|
||||
}
|
||||
// if updateASAP {
|
||||
// module.updateWorkerMgr.Go()
|
||||
// }
|
||||
|
||||
// react to upgrades
|
||||
if err := initUpgrader(); err != nil {
|
||||
return err
|
||||
}
|
||||
// // react to upgrades
|
||||
// if err := initUpgrader(); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
warnOnIncorrectParentPath()
|
||||
// warnOnIncorrectParentPath()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TriggerUpdate queues the update task to execute ASAP.
|
||||
func TriggerUpdate(forceIndexCheck, downloadAll bool) error {
|
||||
switch {
|
||||
case !forceIndexCheck && !enableSoftwareUpdates() && !enableIntelUpdates():
|
||||
return errors.New("automatic updating is disabled")
|
||||
// switch {
|
||||
// case !forceIndexCheck && !enableSoftwareUpdates() && !enableIntelUpdates():
|
||||
// return errors.New("automatic updating is disabled")
|
||||
|
||||
default:
|
||||
if forceIndexCheck {
|
||||
forceCheck.Set()
|
||||
}
|
||||
if downloadAll {
|
||||
forceDownload.Set()
|
||||
}
|
||||
// default:
|
||||
// if forceIndexCheck {
|
||||
// forceCheck.Set()
|
||||
// }
|
||||
// if downloadAll {
|
||||
// forceDownload.Set()
|
||||
// }
|
||||
|
||||
// If index check if forced, start quicker.
|
||||
module.updateWorkerMgr.Go()
|
||||
}
|
||||
// // If index check if forced, start quicker.
|
||||
// module.updateWorkerMgr.Go()
|
||||
// }
|
||||
|
||||
log.Debugf("updates: triggering update to run as soon as possible")
|
||||
return nil
|
||||
@@ -232,68 +186,66 @@ func DisableUpdateSchedule() error {
|
||||
// return errors.New("module already online")
|
||||
// }
|
||||
|
||||
disableTaskSchedule = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkForUpdates(ctx *mgr.WorkerCtx) (err error) {
|
||||
// Set correct error if context was canceled.
|
||||
defer func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
err = context.Canceled
|
||||
default:
|
||||
}
|
||||
}()
|
||||
// defer func() {
|
||||
// select {
|
||||
// case <-ctx.Done():
|
||||
// err = context.Canceled
|
||||
// default:
|
||||
// }
|
||||
// }()
|
||||
|
||||
// Get flags.
|
||||
forceIndexCheck := forceCheck.SetToIf(true, false)
|
||||
downloadAll := forceDownload.SetToIf(true, false)
|
||||
// // Get flags.
|
||||
// forceIndexCheck := forceCheck.SetToIf(true, false)
|
||||
// downloadAll := forceDownload.SetToIf(true, false)
|
||||
|
||||
// Check again if downloading updates is enabled, or forced.
|
||||
if !forceIndexCheck && !enableSoftwareUpdates() && !enableIntelUpdates() {
|
||||
log.Warningf("updates: automatic updates are disabled")
|
||||
return nil
|
||||
}
|
||||
// // Check again if downloading updates is enabled, or forced.
|
||||
// if !forceIndexCheck && !enableSoftwareUpdates() && !enableIntelUpdates() {
|
||||
// log.Warningf("updates: automatic updates are disabled")
|
||||
// return nil
|
||||
// }
|
||||
|
||||
defer func() {
|
||||
// Resolve any error and send success notification.
|
||||
if err == nil {
|
||||
log.Infof("updates: successfully checked for updates")
|
||||
notifyUpdateSuccess(forceIndexCheck)
|
||||
return
|
||||
}
|
||||
// defer func() {
|
||||
// // Resolve any error and send success notification.
|
||||
// if err == nil {
|
||||
// log.Infof("updates: successfully checked for updates")
|
||||
// notifyUpdateSuccess(forceIndexCheck)
|
||||
// return
|
||||
// }
|
||||
|
||||
// Log and notify error.
|
||||
log.Errorf("updates: check failed: %s", err)
|
||||
notifyUpdateCheckFailed(forceIndexCheck, err)
|
||||
}()
|
||||
// // Log and notify error.
|
||||
// log.Errorf("updates: check failed: %s", err)
|
||||
// notifyUpdateCheckFailed(forceIndexCheck, err)
|
||||
// }()
|
||||
|
||||
if err = registry.UpdateIndexes(ctx.Ctx()); err != nil {
|
||||
err = fmt.Errorf("failed to update indexes: %w", err)
|
||||
return //nolint:nakedret // TODO: Would "return err" work with the defer?
|
||||
}
|
||||
// if err = registry.UpdateIndexes(ctx.Ctx()); err != nil {
|
||||
// err = fmt.Errorf("failed to update indexes: %w", err)
|
||||
// return //nolint:nakedret // TODO: Would "return err" work with the defer?
|
||||
// }
|
||||
|
||||
err = registry.DownloadUpdates(ctx.Ctx(), downloadAll)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to download updates: %w", err)
|
||||
return //nolint:nakedret // TODO: Would "return err" work with the defer?
|
||||
}
|
||||
// err = registry.DownloadUpdates(ctx.Ctx(), downloadAll)
|
||||
// if err != nil {
|
||||
// err = fmt.Errorf("failed to download updates: %w", err)
|
||||
// return //nolint:nakedret // TODO: Would "return err" work with the defer?
|
||||
// }
|
||||
|
||||
registry.SelectVersions()
|
||||
// registry.SelectVersions()
|
||||
|
||||
// Unpack selected resources.
|
||||
err = registry.UnpackResources()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to unpack updates: %w", err)
|
||||
return //nolint:nakedret // TODO: Would "return err" work with the defer?
|
||||
}
|
||||
// // Unpack selected resources.
|
||||
// err = registry.UnpackResources()
|
||||
// if err != nil {
|
||||
// err = fmt.Errorf("failed to unpack updates: %w", err)
|
||||
// return //nolint:nakedret // TODO: Would "return err" work with the defer?
|
||||
// }
|
||||
|
||||
// Purge old resources
|
||||
registry.Purge(2)
|
||||
// // Purge old resources
|
||||
// registry.Purge(2)
|
||||
|
||||
module.EventResourcesUpdated.Submit(struct{}{})
|
||||
// module.EventResourcesUpdated.Submit(struct{}{})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -314,5 +266,6 @@ func RootPath() string {
|
||||
// return ""
|
||||
// }
|
||||
|
||||
return registry.StorageDir().Path
|
||||
// return registry.StorageDir().Path
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user