github.com/openshift/installer@v1.4.17/pkg/asset/store/assetsfetcher.go (about)

     1  package store
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/sirupsen/logrus"
     9  
    10  	"github.com/openshift/installer/pkg/asset"
    11  )
    12  
    13  // AssetsFetcher it's used to retrieve and resolve a specified set of assets.
    14  type AssetsFetcher interface {
    15  	// Fetchs and persists all the writable assets from the configured assets store.
    16  	FetchAndPersist(context.Context, []asset.WritableAsset) error
    17  }
    18  
    19  type fetcher struct {
    20  	storeDir string
    21  }
    22  
    23  // NewAssetsFetcher creates a new AssetsFetcher instance for the specified assets store folder.
    24  func NewAssetsFetcher(storeDir string) AssetsFetcher {
    25  	return &fetcher{
    26  		storeDir: storeDir,
    27  	}
    28  }
    29  
    30  func asFileWriter(a asset.WritableAsset) asset.FileWriter {
    31  	switch v := a.(type) {
    32  	case asset.FileWriter:
    33  		return v
    34  	default:
    35  		return asset.NewDefaultFileWriter(a)
    36  	}
    37  }
    38  
    39  // Fetchs all the writable assets from the configured assets store.
    40  func (f *fetcher) FetchAndPersist(ctx context.Context, assets []asset.WritableAsset) error {
    41  	assetStore, err := NewStore(f.storeDir)
    42  	if err != nil {
    43  		return fmt.Errorf("failed to create asset store: %w", err)
    44  	}
    45  
    46  	for _, a := range assets {
    47  		err := assetStore.Fetch(ctx, a, assets...)
    48  		if err != nil {
    49  			err = errors.Wrapf(err, "failed to fetch %s", a.Name())
    50  		}
    51  
    52  		err2 := asFileWriter(a).PersistToFile(f.storeDir)
    53  		if err2 != nil {
    54  			err2 = errors.Wrapf(err2, "failed to write asset (%s) to disk", a.Name())
    55  			if err != nil {
    56  				logrus.Error(err2)
    57  				return err
    58  			}
    59  			return err2
    60  		}
    61  
    62  		if err != nil {
    63  			return err
    64  		}
    65  	}
    66  
    67  	return nil
    68  }