github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/storage/noop/noop.go (about)

     1  package noop
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/filecoin-project/bacalhau/pkg/model"
     7  	"github.com/filecoin-project/bacalhau/pkg/storage"
     8  	"github.com/filecoin-project/bacalhau/pkg/system"
     9  )
    10  
    11  type StroageHandlerIsInstalled func(ctx context.Context) (bool, error)
    12  type StroageHandlerHasStorageLocally func(ctx context.Context, volume model.StorageSpec) (bool, error)
    13  type StroageHandlerGetVolumeSize func(ctx context.Context, volume model.StorageSpec) (uint64, error)
    14  type StroageHandlerPrepareStorage func(ctx context.Context, storageSpec model.StorageSpec) (storage.StorageVolume, error)
    15  type StroageHandlerCleanupStorage func(ctx context.Context, storageSpec model.StorageSpec, volume storage.StorageVolume) error
    16  type StroageHandlerUpload func(ctx context.Context, localPath string) (model.StorageSpec, error)
    17  type StroageHandlerExplode func(ctx context.Context, storageSpec model.StorageSpec) ([]model.StorageSpec, error)
    18  
    19  type StorageConfigExternalHooks struct {
    20  	IsInstalled       StroageHandlerIsInstalled
    21  	HasStorageLocally StroageHandlerHasStorageLocally
    22  	GetVolumeSize     StroageHandlerGetVolumeSize
    23  	PrepareStorage    StroageHandlerPrepareStorage
    24  	CleanupStorage    StroageHandlerCleanupStorage
    25  	Upload            StroageHandlerUpload
    26  	Explode           StroageHandlerExplode
    27  }
    28  
    29  type StorageConfig struct {
    30  	ExternalHooks StorageConfigExternalHooks
    31  }
    32  
    33  // a storage driver runs the downloads content
    34  // from a remote ipfs server and copies it to
    35  // to a local directory in preparation for
    36  // a job to run - it will remove the folder/file once complete
    37  
    38  type NoopStorage struct {
    39  	Config StorageConfig
    40  }
    41  
    42  func NewNoopStorage(_ context.Context, _ *system.CleanupManager, config StorageConfig) (*NoopStorage, error) {
    43  	storageHandler := &NoopStorage{
    44  		Config: config,
    45  	}
    46  	return storageHandler, nil
    47  }
    48  
    49  func NewNoopStorageWithConfig(_ context.Context, _ *system.CleanupManager, config StorageConfig) (*NoopStorage, error) {
    50  	storageHandler := &NoopStorage{
    51  		Config: config,
    52  	}
    53  	return storageHandler, nil
    54  }
    55  
    56  func (s *NoopStorage) IsInstalled(ctx context.Context) (bool, error) {
    57  	if s.Config.ExternalHooks.IsInstalled != nil {
    58  		handler := s.Config.ExternalHooks.IsInstalled
    59  		return handler(ctx)
    60  	}
    61  	return true, nil
    62  }
    63  
    64  func (s *NoopStorage) HasStorageLocally(ctx context.Context, volume model.StorageSpec) (bool, error) {
    65  	if s.Config.ExternalHooks.HasStorageLocally != nil {
    66  		handler := s.Config.ExternalHooks.HasStorageLocally
    67  		return handler(ctx, volume)
    68  	}
    69  	return true, nil
    70  }
    71  
    72  // we wrap this in a timeout because if the CID is not present on the network this seems to hang
    73  func (s *NoopStorage) GetVolumeSize(ctx context.Context, volume model.StorageSpec) (uint64, error) {
    74  	if s.Config.ExternalHooks.GetVolumeSize != nil {
    75  		handler := s.Config.ExternalHooks.GetVolumeSize
    76  		return handler(ctx, volume)
    77  	}
    78  	return 0, nil
    79  }
    80  
    81  func (s *NoopStorage) PrepareStorage(ctx context.Context, storageSpec model.StorageSpec) (storage.StorageVolume, error) {
    82  	if s.Config.ExternalHooks.PrepareStorage != nil {
    83  		handler := s.Config.ExternalHooks.PrepareStorage
    84  		return handler(ctx, storageSpec)
    85  	}
    86  	return storage.StorageVolume{
    87  		Type:   storage.StorageVolumeConnectorBind,
    88  		Source: "test",
    89  		Target: "test",
    90  	}, nil
    91  }
    92  
    93  func (s *NoopStorage) Upload(ctx context.Context, localPath string) (model.StorageSpec, error) {
    94  	if s.Config.ExternalHooks.Upload != nil {
    95  		handler := s.Config.ExternalHooks.Upload
    96  		return handler(ctx, localPath)
    97  	}
    98  	return model.StorageSpec{
    99  		StorageSource: model.StorageSourceIPFS,
   100  		CID:           "test",
   101  		Path:          "/",
   102  	}, nil
   103  }
   104  
   105  func (s *NoopStorage) Explode(ctx context.Context, spec model.StorageSpec) ([]model.StorageSpec, error) {
   106  	if s.Config.ExternalHooks.Explode != nil {
   107  		handler := s.Config.ExternalHooks.Explode
   108  		return handler(ctx, spec)
   109  	}
   110  	return []model.StorageSpec{}, nil
   111  }
   112  
   113  //nolint:lll // Exception to the long rule
   114  func (s *NoopStorage) CleanupStorage(ctx context.Context, storageSpec model.StorageSpec, volume storage.StorageVolume) error {
   115  	if s.Config.ExternalHooks.CleanupStorage != nil {
   116  		handler := s.Config.ExternalHooks.CleanupStorage
   117  		return handler(ctx, storageSpec, volume)
   118  	}
   119  	return nil
   120  }
   121  
   122  // Compile time interface check:
   123  var _ storage.Storage = (*NoopStorage)(nil)