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.

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