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

     1  package filecoinunsealed
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"errors"
     7  	"fmt"
     8  	"os"
     9  	"text/template"
    10  
    11  	"github.com/filecoin-project/bacalhau/pkg/model"
    12  	"github.com/filecoin-project/bacalhau/pkg/storage"
    13  	"github.com/filecoin-project/bacalhau/pkg/storage/util"
    14  	"github.com/filecoin-project/bacalhau/pkg/system"
    15  	"github.com/rs/zerolog/log"
    16  )
    17  
    18  type StorageProvider struct {
    19  	LocalPathTemplateString string
    20  	localPathTemplate       *template.Template
    21  }
    22  
    23  func NewStorage(_ *system.CleanupManager, localPathTemplate string) (*StorageProvider, error) {
    24  	t := template.New("bacalhau-storage-filecoin-unsealed-path")
    25  	t, err := t.Parse(localPathTemplate)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	storageHandler := &StorageProvider{
    30  		LocalPathTemplateString: localPathTemplate,
    31  		localPathTemplate:       t,
    32  	}
    33  	log.Debug().Msgf("Filecoin unsealed driver created with path template: %s", localPathTemplate)
    34  	return storageHandler, nil
    35  }
    36  
    37  func (driver *StorageProvider) IsInstalled(context.Context) (bool, error) {
    38  	return true, nil
    39  }
    40  
    41  func (driver *StorageProvider) HasStorageLocally(_ context.Context, volume model.StorageSpec) (bool, error) {
    42  	localPath, err := driver.getPathToVolume(volume)
    43  	if err != nil {
    44  		return false, err
    45  	}
    46  	if _, err := os.Stat(localPath); errors.Is(err, os.ErrNotExist) {
    47  		return false, nil
    48  	}
    49  	return true, nil
    50  }
    51  
    52  func (driver *StorageProvider) GetVolumeSize(_ context.Context, volume model.StorageSpec) (uint64, error) {
    53  	localPath, err := driver.getPathToVolume(volume)
    54  	if err != nil {
    55  		return 0, err
    56  	}
    57  	return util.DirSize(localPath)
    58  }
    59  
    60  func (driver *StorageProvider) PrepareStorage(
    61  	_ context.Context,
    62  	storageSpec model.StorageSpec,
    63  ) (storage.StorageVolume, error) {
    64  	localPath, err := driver.getPathToVolume(storageSpec)
    65  	if err != nil {
    66  		return storage.StorageVolume{}, err
    67  	}
    68  	return storage.StorageVolume{
    69  		Type:   storage.StorageVolumeConnectorBind,
    70  		Source: localPath,
    71  		Target: storageSpec.Path,
    72  	}, nil
    73  }
    74  
    75  func (driver *StorageProvider) CleanupStorage(context.Context, model.StorageSpec, storage.StorageVolume) error {
    76  	return nil
    77  }
    78  
    79  func (driver *StorageProvider) Upload(context.Context, string) (model.StorageSpec, error) {
    80  	return model.StorageSpec{}, fmt.Errorf("not implemented")
    81  }
    82  
    83  func (driver *StorageProvider) Explode(_ context.Context, spec model.StorageSpec) ([]model.StorageSpec, error) {
    84  	return []model.StorageSpec{
    85  		spec,
    86  	}, nil
    87  }
    88  
    89  func (driver *StorageProvider) getPathToVolume(volume model.StorageSpec) (string, error) {
    90  	var buffer bytes.Buffer
    91  	err := driver.localPathTemplate.Execute(&buffer, volume)
    92  	if err != nil {
    93  		return "", err
    94  	}
    95  	return buffer.String(), nil
    96  }
    97  
    98  // Compile time interface check:
    99  var _ storage.Storage = (*StorageProvider)(nil)