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.

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