Skip to content

Commit

Permalink
internal/exec/engine: write empty cache config when not provided
Browse files Browse the repository at this point in the history
When the user-provided config is empty write an empty cache config.
Additionally restrict fetching configs to only `fetch` stages.
  • Loading branch information
arithx committed Jun 19, 2020
1 parent f1e8717 commit 9faafb6
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions internal/exec/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/coreos/go-systemd/v22/journal"
Expand Down Expand Up @@ -108,15 +109,19 @@ func (e Engine) Run(stageName string) error {
e.Fetcher.Offline = true
}

cfg, err := e.acquireConfig()
cfg, err := e.acquireConfig(stageName)
if err == resource.ErrNeedNet && stageName == "fetch-offline" {
err = SignalNeedNet()
if err != nil {
e.Logger.Crit("failed to signal neednet: %v", err)
}
return err
} else if err == errors.ErrEmpty {
e.Logger.Info("%v: ignoring user-provided config", err)
e.Logger.Info("%v: ignoring user-provided config & writing empty cache config", err)
if err = renameio.WriteFile(e.ConfigCache, []byte("{}"), 0640); err != nil {
e.Logger.Crit("failed to empty write cached config: %v", err)
return err
}
} else if err != nil {
e.Logger.Crit("failed to acquire config: %v", err)
return err
Expand Down Expand Up @@ -162,11 +167,24 @@ func logStructuredJournalEntry(config string, src string, isReferenced bool) err
return nil
}

// acquireConfig returns the configuration, first checking a local cache
// before attempting to fetch it from the provider.
func (e *Engine) acquireConfig() (cfg types.Config, err error) {
// acquireConfig returns the configuration from a local cache. If one
// does not exist then it will attempt to fetch the configuration from
// the provider when in fetch stages.
func (e *Engine) acquireConfig(stageName string) (cfg types.Config, err error) {
cfg, err = e.acquireCachedConfig()
if err != nil {
if os.IsNotExist(err) && strings.HasPrefix(stageName, "fetch") {
e.Logger.Info("fetching provider config")
cfg, err = e.acquireProviderConfig()
return
}
}
return
}

// First try read the config @ e.ConfigCache.
// acquireCachedConfig returns the configuration from a local cache if
// available
func (e *Engine) acquireCachedConfig() (cfg types.Config, err error) {
b, err := ioutil.ReadFile(e.ConfigCache)
if err == nil {
if err = json.Unmarshal(b, &cfg); err != nil {
Expand All @@ -179,9 +197,13 @@ func (e *Engine) acquireConfig() (cfg types.Config, err error) {
e.Logger.Crit("failed to update timeouts and CAs for fetcher: %v", err)
return
}
return
}
return
}

// acquireProviderConfig attempts to fetch the configuration from the
// provider.
func (e *Engine) acquireProviderConfig() (cfg types.Config, err error) {
// Create a new http client and fetcher with the timeouts set via the flags,
// since we don't have a config with timeout values we can use
timeout := int(e.FetchTimeout.Seconds())
Expand Down Expand Up @@ -222,7 +244,7 @@ func (e *Engine) acquireConfig() (cfg types.Config, err error) {
}

// Populate the config cache.
b, err = json.Marshal(cfg)
b, err := json.Marshal(cfg)
if err != nil {
e.Logger.Crit("failed to marshal cached config: %v", err)
return
Expand Down

0 comments on commit 9faafb6

Please sign in to comment.