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.

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