github.com/weaviate/weaviate@v1.24.6/modules/backup-s3/module.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package modstgs3
    13  
    14  import (
    15  	"context"
    16  	"net/http"
    17  	"os"
    18  	"strings"
    19  
    20  	"github.com/pkg/errors"
    21  	"github.com/sirupsen/logrus"
    22  	"github.com/weaviate/weaviate/entities/modulecapabilities"
    23  	"github.com/weaviate/weaviate/entities/moduletools"
    24  )
    25  
    26  const (
    27  	Name       = "backup-s3"
    28  	AltName1   = "s3"
    29  	s3Endpoint = "BACKUP_S3_ENDPOINT"
    30  	s3Bucket   = "BACKUP_S3_BUCKET"
    31  	s3UseSSL   = "BACKUP_S3_USE_SSL"
    32  
    33  	// this is an optional value, allowing for
    34  	// the backup to be stored in a specific
    35  	// directory inside the provided bucket.
    36  	//
    37  	// if left unset, the backup files will
    38  	// be stored directly in the root of the
    39  	// bucket.
    40  	s3Path = "BACKUP_S3_PATH"
    41  )
    42  
    43  type Module struct {
    44  	*s3Client
    45  	logger   logrus.FieldLogger
    46  	dataPath string
    47  }
    48  
    49  func New() *Module {
    50  	return &Module{}
    51  }
    52  
    53  func (m *Module) Name() string {
    54  	return Name
    55  }
    56  
    57  func (m *Module) IsExternal() bool {
    58  	return true
    59  }
    60  
    61  func (m *Module) AltNames() []string {
    62  	return []string{AltName1}
    63  }
    64  
    65  func (m *Module) Type() modulecapabilities.ModuleType {
    66  	return modulecapabilities.Backup
    67  }
    68  
    69  func (m *Module) Init(ctx context.Context,
    70  	params moduletools.ModuleInitParams,
    71  ) error {
    72  	m.logger = params.GetLogger()
    73  	m.dataPath = params.GetStorageProvider().DataPath()
    74  	bucket := os.Getenv(s3Bucket)
    75  	if bucket == "" {
    76  		return errors.Errorf("backup init: '%s' must be set", s3Bucket)
    77  	}
    78  	// SSL on by default
    79  	useSSL := strings.ToLower(os.Getenv(s3UseSSL)) != "false"
    80  	config := newConfig(os.Getenv(s3Endpoint), bucket, os.Getenv(s3Path), useSSL)
    81  	client, err := newClient(config, m.logger, m.dataPath)
    82  	if err != nil {
    83  		return errors.Wrap(err, "initialize S3 backup module")
    84  	}
    85  	m.s3Client = client
    86  	return nil
    87  }
    88  
    89  func (m *Module) RootHandler() http.Handler {
    90  	// TODO: remove once this is a capability interface
    91  	return nil
    92  }
    93  
    94  func (m *Module) MetaInfo() (map[string]interface{}, error) {
    95  	metaInfo := make(map[string]interface{}, 4)
    96  	metaInfo["endpoint"] = m.config.Endpoint
    97  	metaInfo["bucketName"] = m.config.Bucket
    98  	if root := m.config.BackupPath; root != "" {
    99  		metaInfo["rootName"] = root
   100  	}
   101  	metaInfo["useSSL"] = m.config.UseSSL
   102  	return metaInfo, nil
   103  }
   104  
   105  // verify we implement the modules.Module interface
   106  var (
   107  	_ = modulecapabilities.Module(New())
   108  	_ = modulecapabilities.BackupBackend(New())
   109  	_ = modulecapabilities.MetaProvider(New())
   110  )