Skip to content

Commit

Permalink
🧹 move provider update to providers.go (#3654)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Mar 26, 2024
1 parent b94da16 commit cd544f0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
63 changes: 1 addition & 62 deletions providers/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
pp "go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/resources"
coreconf "go.mondoo.com/cnquery/v10/providers/core/config"
"go.mondoo.com/cnquery/v10/providers/core/resources/versions/semver"
)

//go:generate mockgen -source=./coordinator.go -destination=./mock_coordinator.go -package=providers
Expand Down Expand Up @@ -99,66 +98,6 @@ func (c *coordinator) NextConnectionId() uint32 {
return c.lastConnectionID
}

func (c *coordinator) tryProviderUpdate(provider *Provider, update UpdateProvidersConfig) (*Provider, error) {
if provider.Path == "" {
return nil, errors.New("cannot determine installation path for provider")
}

statPath := provider.confJSONPath()
stat, err := os.Stat(statPath)
if err != nil {
return nil, err
}

if update.RefreshInterval > 0 {
mtime := stat.ModTime()
secs := time.Since(mtime).Seconds()
if secs < float64(update.RefreshInterval) {
lastRefresh := time.Since(mtime).String()
log.Debug().
Str("last-refresh", lastRefresh).
Str("provider", provider.Name).
Msg("no need to update provider")
return provider, nil
}
}

latest, err := LatestVersion(provider.Name)
if err != nil {
log.Warn().Msg(err.Error())
// we can just continue with the existing provider, no need to error up,
// the warning is enough since we are still functional
return provider, nil
}

semver := semver.Parser{}
diff, err := semver.Compare(provider.Version, latest)
if err != nil {
return nil, err
}
if diff >= 0 {
return provider, nil
}

log.Info().
Str("installed", provider.Version).
Str("latest", latest).
Msg("found a new version for '" + provider.Name + "' provider")
provider, err = installVersion(provider.Name, latest)
if err != nil {
return nil, err
}
PrintInstallResults([]*Provider{provider})
now := time.Now()
if err := os.Chtimes(statPath, now, now); err != nil {
log.Warn().
Str("provider", provider.Name).
Msg("failed to update refresh time on provider")
}

return provider, nil
}

func (c *coordinator) NewRuntime() *Runtime {
return c.newRuntime()
}
Expand Down Expand Up @@ -337,7 +276,7 @@ func (c *coordinator) unsafeStartProvider(id string, update UpdateProvidersConfi
// We do not stop on failed updates. Up until some other errors happens,
// things are still functional. We want to consider failure, possibly
// with a config entry in the future.
updated, err := c.tryProviderUpdate(provider, update)
updated, err := TryProviderUpdate(provider, update)
if err != nil {
log.Error().
Err(err).
Expand Down
61 changes: 61 additions & 0 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package providers
import (
"archive/tar"
"encoding/json"
"go.mondoo.com/cnquery/v10/providers/core/resources/versions/semver"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -784,6 +785,66 @@ func readProviderDir(pdir string) (*Provider, error) {
}, nil
}

func TryProviderUpdate(provider *Provider, update UpdateProvidersConfig) (*Provider, error) {
if provider.Path == "" {
return nil, errors.New("cannot determine installation path for provider")
}

statPath := provider.confJSONPath()
stat, err := os.Stat(statPath)
if err != nil {
return nil, err
}

if update.RefreshInterval > 0 {
mtime := stat.ModTime()
secs := time.Since(mtime).Seconds()
if secs < float64(update.RefreshInterval) {
lastRefresh := time.Since(mtime).String()
log.Debug().
Str("last-refresh", lastRefresh).
Str("provider", provider.Name).
Msg("no need to update provider")
return provider, nil
}
}

latest, err := LatestVersion(provider.Name)
if err != nil {
log.Warn().Msg(err.Error())
// we can just continue with the existing provider, no need to error up,
// the warning is enough since we are still functional
return provider, nil
}

semver := semver.Parser{}
diff, err := semver.Compare(provider.Version, latest)
if err != nil {
return nil, err
}
if diff >= 0 {
return provider, nil
}

log.Info().
Str("installed", provider.Version).
Str("latest", latest).
Msg("found a new version for '" + provider.Name + "' provider")
provider, err = installVersion(provider.Name, latest)
if err != nil {
return nil, err
}
PrintInstallResults([]*Provider{provider})
now := time.Now()
if err := os.Chtimes(statPath, now, now); err != nil {
log.Warn().
Str("provider", provider.Name).
Msg("failed to update refresh time on provider")
}

return provider, nil
}

func (p *Provider) LoadJSON() error {
path := p.confJSONPath()
res, err := afero.ReadFile(config.AppFs, path)
Expand Down

0 comments on commit cd544f0

Please sign in to comment.