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.

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