[WIP] working download and replace.

This commit is contained in:
Vladimir Stoilov
2024-08-30 12:40:51 +03:00
parent f7abb700bf
commit 701505ae75
18 changed files with 168 additions and 276 deletions

View File

@@ -1,10 +1,8 @@
package updates
import (
"errors"
"flag"
"fmt"
"sync/atomic"
"time"
"github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config"
@@ -14,10 +12,10 @@ import (
"github.com/safing/portmaster/service/updates/registry"
)
var applyUpdates bool
var autoUpdate bool
func init() {
flag.BoolVar(&applyUpdates, "update", false, "apply downloaded updates")
flag.BoolVar(&autoUpdate, "auto-update", false, "auto apply downloaded updates")
}
// Updates provides access to released artifacts.
@@ -25,9 +23,8 @@ type Updates struct {
m *mgr.Manager
states *mgr.StateMgr
updateBinaryWorkerMgr *mgr.WorkerMgr
updateIntelWorkerMgr *mgr.WorkerMgr
restartWorkerMgr *mgr.WorkerMgr
updateCheckWorkerMgr *mgr.WorkerMgr
upgraderWorkerMgr *mgr.WorkerMgr
EventResourcesUpdated *mgr.EventMgr[struct{}]
EventVersionsUpdated *mgr.EventMgr[struct{}]
@@ -37,15 +34,9 @@ type Updates struct {
instance instance
}
var shimLoaded atomic.Bool
// New returns a new UI module.
func New(instance instance) (*Updates, error) {
if !shimLoaded.CompareAndSwap(false, true) {
return nil, errors.New("only one instance allowed")
}
m := mgr.New("Updates")
// New returns a new Updates module.
func New(instance instance, name string, index registry.UpdateIndex) (*Updates, error) {
m := mgr.New(name)
module := &Updates{
m: m,
states: m.NewStateMgr(),
@@ -57,63 +48,47 @@ func New(instance instance) (*Updates, error) {
}
// Events
module.updateBinaryWorkerMgr = m.NewWorkerMgr("binary updater", module.checkForBinaryUpdates, nil)
module.updateIntelWorkerMgr = m.NewWorkerMgr("intel updater", module.checkForIntelUpdates, nil)
module.restartWorkerMgr = m.NewWorkerMgr("automatic restart", automaticRestart, nil)
module.updateCheckWorkerMgr = m.NewWorkerMgr("update checker", module.checkForUpdates, nil)
module.updateCheckWorkerMgr.Repeat(30 * time.Second)
module.upgraderWorkerMgr = m.NewWorkerMgr("upgrader", func(w *mgr.WorkerCtx) error {
err := module.registry.ApplyUpdates()
if err != nil {
// TODO(vladimir): Send notification to UI
log.Errorf("updates: failed to apply updates: %s", err)
} else {
module.instance.Restart()
}
return nil
}, nil)
binIndex := registry.UpdateIndex{
Directory: "/usr/lib/portmaster",
DownloadDirectory: "/var/lib/portmaster/new_bin",
Ignore: []string{"databases", "intel", "config.json"},
IndexURLs: []string{"http://localhost:8000/test-binary.json"},
IndexFile: "bin-index.json",
AutoApply: false,
}
intelIndex := registry.UpdateIndex{
Directory: "/var/lib/portmaster/intel",
DownloadDirectory: "/var/lib/portmaster/new_intel",
IndexURLs: []string{"http://localhost:8000/test-intel.json"},
IndexFile: "intel-index.json",
AutoApply: true,
}
module.registry = registry.New(binIndex, intelIndex)
module.registry = registry.New(index)
_ = module.registry.Initialize()
return module, nil
}
func (u *Updates) checkForBinaryUpdates(_ *mgr.WorkerCtx) error {
hasUpdates, err := u.registry.CheckForBinaryUpdates()
func (u *Updates) checkForUpdates(_ *mgr.WorkerCtx) error {
hasUpdates, err := u.registry.CheckForUpdates()
if err != nil {
log.Errorf("updates: failed to check for binary updates: %s", err)
log.Errorf("updates: failed to check for updates: %s", err)
}
if hasUpdates {
log.Infof("updates: there is updates available in the binary bundle")
err = u.registry.DownloadBinaryUpdates()
log.Infof("updates: there is updates available")
err = u.registry.DownloadUpdates()
if err != nil {
log.Errorf("updates: failed to download bundle: %s", err)
} else if autoUpdate {
u.ApplyUpdates()
}
} else {
log.Infof("updates: no new binary updates")
log.Infof("updates: no new updates")
u.EventResourcesUpdated.Submit(struct{}{})
}
return nil
}
func (u *Updates) checkForIntelUpdates(_ *mgr.WorkerCtx) error {
hasUpdates, err := u.registry.CheckForIntelUpdates()
if err != nil {
log.Errorf("updates: failed to check for intel updates: %s", err)
}
if hasUpdates {
log.Infof("updates: there is updates available in the intel bundle")
err = u.registry.DownloadIntelUpdates()
if err != nil {
log.Errorf("updates: failed to download bundle: %s", err)
}
} else {
log.Infof("updates: no new intel data updates")
}
return nil
func (u *Updates) ApplyUpdates() {
u.upgraderWorkerMgr.Go()
}
// States returns the state manager.
@@ -128,29 +103,7 @@ func (u *Updates) Manager() *mgr.Manager {
// Start starts the module.
func (u *Updates) Start() error {
// initConfig()
if applyUpdates {
err := u.registry.ApplyBinaryUpdates()
if err != nil {
log.Errorf("updates: failed to apply binary updates: %s", err)
}
err = u.registry.ApplyIntelUpdates()
if err != nil {
log.Errorf("updates: failed to apply intel updates: %s", err)
}
u.instance.Restart()
return nil
}
err := u.registry.Initialize()
if err != nil {
// TODO(vladimir): Find a better way to handle this error. The service will stop if parsing of the bundle files fails.
return fmt.Errorf("failed to initialize registry: %w", err)
}
u.updateBinaryWorkerMgr.Go()
u.updateIntelWorkerMgr.Go()
u.updateCheckWorkerMgr.Go()
return nil
}