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.

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