You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.8 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/golang/protobuf/proto"
  10. "io"
  11. "regexp"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandRemoteConfigure{})
  15. }
  16. type commandRemoteConfigure struct {
  17. }
  18. func (c *commandRemoteConfigure) Name() string {
  19. return "remote.configure"
  20. }
  21. func (c *commandRemoteConfigure) Help() string {
  22. return `remote storage configuration
  23. # see the current configurations
  24. remote.configure
  25. # set or update a configuration
  26. remote.configure -name=cloud1 -type=s3 -access_key=xxx -secret_key=yyy
  27. # delete one configuration
  28. remote.configure -delete -name=cloud1
  29. `
  30. }
  31. var (
  32. isAlpha = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString
  33. )
  34. func (c *commandRemoteConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  35. conf := &filer_pb.RemoteConf{}
  36. remoteConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  37. isDelete := remoteConfigureCommand.Bool("delete", false, "delete one remote storage by its name")
  38. remoteConfigureCommand.StringVar(&conf.Name, "name", "", "a short name to identify the remote storage")
  39. remoteConfigureCommand.StringVar(&conf.Type, "type", "s3", "storage type, currently only support s3")
  40. remoteConfigureCommand.StringVar(&conf.S3AccessKey, "s3.access_key", "", "s3 access key")
  41. remoteConfigureCommand.StringVar(&conf.S3SecretKey, "s3.secret_key", "", "s3 secret key")
  42. remoteConfigureCommand.StringVar(&conf.S3Region, "s3.region", "us-east-2", "s3 region")
  43. remoteConfigureCommand.StringVar(&conf.S3Endpoint, "s3.endpoint", "", "endpoint for s3-compatible local object store")
  44. if err = remoteConfigureCommand.Parse(args); err != nil {
  45. return nil
  46. }
  47. if conf.Name == "" {
  48. return c.listExistingRemoteStorages(commandEnv, writer)
  49. }
  50. if !isAlpha(conf.Name) {
  51. return fmt.Errorf("only letters and numbers allowed in name: %v", conf.Name)
  52. }
  53. if *isDelete {
  54. return c.deleteRemoteStorage(commandEnv, writer, conf.Name)
  55. }
  56. return c.saveRemoteStorage(commandEnv, writer, conf)
  57. }
  58. func (c *commandRemoteConfigure) listExistingRemoteStorages(commandEnv *CommandEnv, writer io.Writer) error {
  59. return filer_pb.ReadDirAllEntries(commandEnv, util.FullPath(filer.DirectoryEtcRemote), "", func(entry *filer_pb.Entry, isLast bool) error {
  60. if len(entry.Content) == 0 {
  61. fmt.Fprintf(writer, "skipping %s\n", entry.Name)
  62. return nil
  63. }
  64. conf := &filer_pb.RemoteConf{}
  65. if err := proto.Unmarshal(entry.Content, conf); err != nil {
  66. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err)
  67. }
  68. conf.S3SecretKey = ""
  69. fmt.Fprintf(writer, "%+v\n", conf)
  70. return nil
  71. })
  72. }
  73. func (c *commandRemoteConfigure) deleteRemoteStorage(commandEnv *CommandEnv, writer io.Writer, storageName string) error {
  74. return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  75. request := &filer_pb.DeleteEntryRequest{
  76. Directory: filer.DirectoryEtcRemote,
  77. Name: storageName,
  78. IgnoreRecursiveError: false,
  79. IsDeleteData: true,
  80. IsRecursive: true,
  81. IsFromOtherCluster: false,
  82. Signatures: nil,
  83. }
  84. _, err := client.DeleteEntry(context.Background(), request)
  85. if err == nil {
  86. fmt.Fprintf(writer, "removed: %s\n", storageName)
  87. }
  88. return err
  89. })
  90. }
  91. func (c *commandRemoteConfigure) saveRemoteStorage(commandEnv *CommandEnv, writer io.Writer, conf *filer_pb.RemoteConf) error {
  92. data, err := proto.Marshal(conf)
  93. if err != nil {
  94. return err
  95. }
  96. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  97. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, conf.Name, data)
  98. }); err != nil && err != filer_pb.ErrNotFound {
  99. return err
  100. }
  101. return nil
  102. }