2021-09-04 12:02:20 +00:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
|
2021-09-05 21:38:31 +00:00
|
|
|
"google.golang.org/grpc"
|
2022-08-17 19:05:07 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-09-04 12:02:20 +00:00
|
|
|
)
|
|
|
|
|
2021-09-13 05:47:52 +00:00
|
|
|
func ReadMountMappings(grpcDialOption grpc.DialOption, filerAddress pb.ServerAddress) (mappings *remote_pb.RemoteStorageMapping, readErr error) {
|
2021-09-05 21:38:31 +00:00
|
|
|
var oldContent []byte
|
2023-01-20 09:48:12 +00:00
|
|
|
if readErr = pb.WithFilerClient(false, 0, filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2021-09-05 21:38:31 +00:00
|
|
|
oldContent, readErr = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
|
|
|
|
return readErr
|
|
|
|
}); readErr != nil {
|
|
|
|
return nil, readErr
|
|
|
|
}
|
|
|
|
|
|
|
|
mappings, readErr = UnmarshalRemoteStorageMappings(oldContent)
|
|
|
|
if readErr != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal mappings: %v", readErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-04 12:11:06 +00:00
|
|
|
func InsertMountMapping(filerClient filer_pb.FilerClient, dir string, remoteStorageLocation *remote_pb.RemoteStorageLocation) (err error) {
|
2021-09-04 12:02:20 +00:00
|
|
|
|
|
|
|
// read current mapping
|
|
|
|
var oldContent, newContent []byte
|
2021-12-26 08:15:03 +00:00
|
|
|
err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2021-09-04 12:02:20 +00:00
|
|
|
oldContent, err = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != filer_pb.ErrNotFound {
|
|
|
|
return fmt.Errorf("read existing mapping: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new mapping
|
2021-09-04 12:11:06 +00:00
|
|
|
newContent, err = addRemoteStorageMapping(oldContent, dir, remoteStorageLocation)
|
2021-09-04 12:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// save back
|
2021-12-26 08:15:03 +00:00
|
|
|
err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2021-09-04 12:02:20 +00:00
|
|
|
return SaveInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, newContent)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("save mapping: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-09-04 12:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteMountMapping(filerClient filer_pb.FilerClient, dir string) (err error) {
|
|
|
|
|
|
|
|
// read current mapping
|
|
|
|
var oldContent, newContent []byte
|
2021-12-26 08:15:03 +00:00
|
|
|
err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2021-09-04 12:09:38 +00:00
|
|
|
oldContent, err = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != filer_pb.ErrNotFound {
|
|
|
|
return fmt.Errorf("read existing mapping: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add new mapping
|
2021-09-04 12:11:06 +00:00
|
|
|
newContent, err = removeRemoteStorageMapping(oldContent, dir)
|
2021-09-04 12:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete mount %s: %v", dir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// save back
|
2021-12-26 08:15:03 +00:00
|
|
|
err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2021-09-04 12:09:38 +00:00
|
|
|
return SaveInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, newContent)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("save mapping: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-04 12:11:06 +00:00
|
|
|
func addRemoteStorageMapping(oldContent []byte, dir string, storageLocation *remote_pb.RemoteStorageLocation) (newContent []byte, err error) {
|
2021-09-04 12:09:38 +00:00
|
|
|
mappings, unmarshalErr := UnmarshalRemoteStorageMappings(oldContent)
|
|
|
|
if unmarshalErr != nil {
|
|
|
|
// skip
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the new mapping
|
|
|
|
mappings.Mappings[dir] = storageLocation
|
|
|
|
|
|
|
|
if newContent, err = proto.Marshal(mappings); err != nil {
|
|
|
|
return oldContent, fmt.Errorf("marshal mappings: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-04 12:11:06 +00:00
|
|
|
func removeRemoteStorageMapping(oldContent []byte, dir string) (newContent []byte, err error) {
|
2021-09-04 12:09:38 +00:00
|
|
|
mappings, unmarshalErr := UnmarshalRemoteStorageMappings(oldContent)
|
|
|
|
if unmarshalErr != nil {
|
|
|
|
return nil, unmarshalErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the new mapping
|
|
|
|
delete(mappings.Mappings, dir)
|
|
|
|
|
|
|
|
if newContent, err = proto.Marshal(mappings); err != nil {
|
|
|
|
return oldContent, fmt.Errorf("marshal mappings: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|