2023-02-10 02:18:27 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-03-20 16:08:30 +01:00
|
|
|
"time"
|
2023-03-09 14:46:19 +01:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config/fslocal"
|
2023-05-08 13:31:54 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config/fsurl"
|
2023-02-10 02:18:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// FS represent a file system abstract for reading files.
|
|
|
|
type FS interface {
|
|
|
|
// Init initializes FS.
|
|
|
|
Init() error
|
|
|
|
|
|
|
|
// String must return human-readable representation of FS.
|
|
|
|
String() string
|
|
|
|
|
2023-03-09 14:46:19 +01:00
|
|
|
// List returns the list of file names which will be read via Read fn
|
|
|
|
List() ([]string, error)
|
|
|
|
|
2023-02-10 02:18:27 +01:00
|
|
|
// Read returns a list of read files in form of a map
|
|
|
|
// where key is a file name and value is a content of read file.
|
|
|
|
// Read must be called only after the successful Init call.
|
2023-03-09 14:46:19 +01:00
|
|
|
Read(files []string) (map[string][]byte, error)
|
2023-02-10 02:18:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
fsRegistryMu sync.Mutex
|
|
|
|
fsRegistry = make(map[string]FS)
|
|
|
|
)
|
|
|
|
|
|
|
|
// readFromFS parses the given path list and inits FS for each item.
|
2023-05-08 09:52:57 +02:00
|
|
|
// Once initialed, readFromFS will try to read and return files from each FS.
|
2023-02-10 02:18:27 +01:00
|
|
|
// readFromFS returns an error if at least one FS failed to init.
|
|
|
|
// The function can be called multiple times but each unique path
|
2023-05-08 09:52:57 +02:00
|
|
|
// will be initialed only once.
|
2023-02-10 02:18:27 +01:00
|
|
|
//
|
|
|
|
// It is allowed to mix different FS types in path list.
|
2023-03-20 16:08:30 +01:00
|
|
|
func readFromFS(paths []string) (map[string][]byte, error) {
|
2023-02-10 02:18:27 +01:00
|
|
|
var err error
|
|
|
|
result := make(map[string][]byte)
|
|
|
|
for _, path := range paths {
|
|
|
|
fsRegistryMu.Lock()
|
|
|
|
fs, ok := fsRegistry[path]
|
|
|
|
if !ok {
|
|
|
|
fs, err = newFS(path)
|
|
|
|
if err != nil {
|
|
|
|
fsRegistryMu.Unlock()
|
|
|
|
return nil, fmt.Errorf("error while parsing path %q: %w", path, err)
|
|
|
|
}
|
|
|
|
if err := fs.Init(); err != nil {
|
|
|
|
fsRegistryMu.Unlock()
|
|
|
|
return nil, fmt.Errorf("error while initializing path %q: %w", path, err)
|
|
|
|
}
|
|
|
|
fsRegistry[path] = fs
|
|
|
|
}
|
|
|
|
fsRegistryMu.Unlock()
|
|
|
|
|
2023-03-09 14:46:19 +01:00
|
|
|
list, err := fs.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to list files from %q", fs)
|
|
|
|
}
|
|
|
|
|
2023-03-20 16:08:30 +01:00
|
|
|
cLogger.Infof("found %d files to read from %q", len(list), fs)
|
2023-03-09 14:46:19 +01:00
|
|
|
|
|
|
|
if len(list) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-20 16:08:30 +01:00
|
|
|
ts := time.Now()
|
2023-03-09 14:46:19 +01:00
|
|
|
files, err := fs.Read(list)
|
2023-02-10 02:18:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error while reading files from %q: %w", fs, err)
|
|
|
|
}
|
2023-03-20 16:08:30 +01:00
|
|
|
cLogger.Infof("finished reading %d files in %v from %q", len(list), time.Since(ts), fs)
|
|
|
|
|
2023-02-10 02:18:27 +01:00
|
|
|
for k, v := range files {
|
|
|
|
if _, ok := result[k]; ok {
|
|
|
|
return nil, fmt.Errorf("duplicate found for file name %q: file names must be unique", k)
|
|
|
|
}
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newFS creates FS based on the give path.
|
|
|
|
// Supported file systems are: fs
|
2023-05-08 13:31:54 +02:00
|
|
|
func newFS(originPath string) (FS, error) {
|
2023-02-10 02:18:27 +01:00
|
|
|
scheme := "fs"
|
2023-05-08 13:31:54 +02:00
|
|
|
path := originPath
|
2023-02-10 02:18:27 +01:00
|
|
|
n := strings.Index(path, "://")
|
|
|
|
if n >= 0 {
|
|
|
|
scheme = path[:n]
|
|
|
|
path = path[n+len("://"):]
|
|
|
|
}
|
|
|
|
if len(path) == 0 {
|
|
|
|
return nil, fmt.Errorf("path cannot be empty")
|
|
|
|
}
|
|
|
|
switch scheme {
|
|
|
|
case "fs":
|
|
|
|
return &fslocal.FS{Pattern: path}, nil
|
2023-05-08 13:31:54 +02:00
|
|
|
case "http", "https":
|
|
|
|
return &fsurl.FS{Path: originPath}, nil
|
2023-02-10 02:18:27 +01:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported scheme %q", scheme)
|
|
|
|
}
|
|
|
|
}
|