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.

157 lines
4.5 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.S3StorageClass, "s3.storage_class", "", "s3 storage class")
  48. remoteConfigureCommand.StringVar(&conf.GcsGoogleApplicationCredentials, "gcs.appCredentialsFile", "", "google cloud storage credentials file, or leave it empty to use env GOOGLE_APPLICATION_CREDENTIALS")
  49. if err = remoteConfigureCommand.Parse(args); err != nil {
  50. return nil
  51. }
  52. if conf.Name == "" {
  53. return c.listExistingRemoteStorages(commandEnv, writer)
  54. }
  55. if !isAlpha(conf.Name) {
  56. return fmt.Errorf("only letters and numbers allowed in name: %v", conf.Name)
  57. }
  58. if *isDelete {
  59. return c.deleteRemoteStorage(commandEnv, writer, conf.Name)
  60. }
  61. return c.saveRemoteStorage(commandEnv, writer, conf)
  62. }
  63. func (c *commandRemoteConfigure) listExistingRemoteStorages(commandEnv *CommandEnv, writer io.Writer) error {
  64. return filer_pb.ReadDirAllEntries(commandEnv, util.FullPath(filer.DirectoryEtcRemote), "", func(entry *filer_pb.Entry, isLast bool) error {
  65. if len(entry.Content) == 0 {
  66. fmt.Fprintf(writer, "skipping %s\n", entry.Name)
  67. return nil
  68. }
  69. if !strings.HasSuffix(entry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  70. return nil
  71. }
  72. conf := &filer_pb.RemoteConf{}
  73. if err := proto.Unmarshal(entry.Content, conf); err != nil {
  74. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err)
  75. }
  76. conf.S3SecretKey = strings.Repeat("*", len(conf.S3SecretKey))
  77. m := jsonpb.Marshaler{
  78. EmitDefaults: false,
  79. Indent: " ",
  80. }
  81. err := m.Marshal(writer, conf)
  82. fmt.Fprintln(writer)
  83. return err
  84. })
  85. }
  86. func (c *commandRemoteConfigure) deleteRemoteStorage(commandEnv *CommandEnv, writer io.Writer, storageName string) error {
  87. return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  88. request := &filer_pb.DeleteEntryRequest{
  89. Directory: filer.DirectoryEtcRemote,
  90. Name: storageName + filer.REMOTE_STORAGE_CONF_SUFFIX,
  91. IgnoreRecursiveError: false,
  92. IsDeleteData: true,
  93. IsRecursive: true,
  94. IsFromOtherCluster: false,
  95. Signatures: nil,
  96. }
  97. _, err := client.DeleteEntry(context.Background(), request)
  98. if err == nil {
  99. fmt.Fprintf(writer, "removed: %s\n", storageName)
  100. }
  101. return err
  102. })
  103. }
  104. func (c *commandRemoteConfigure) saveRemoteStorage(commandEnv *CommandEnv, writer io.Writer, conf *filer_pb.RemoteConf) error {
  105. data, err := proto.Marshal(conf)
  106. if err != nil {
  107. return err
  108. }
  109. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  110. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, conf.Name+filer.REMOTE_STORAGE_CONF_SUFFIX, data)
  111. }); err != nil && err != filer_pb.ErrNotFound {
  112. return err
  113. }
  114. return nil
  115. }