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.

174 lines
6.3 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|b2|aliyun|tencent] 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. remoteConfigureCommand.StringVar(&conf.BackblazeKeyId, "b2.key_id", "", "backblaze keyID")
  54. remoteConfigureCommand.StringVar(&conf.BackblazeApplicationKey, "b2.application_key", "", "backblaze applicationKey. Note that your Master Application Key will not work with the S3 Compatible API. You must create a new key that is eligible for use. For more information: https://help.backblaze.com/hc/en-us/articles/360047425453")
  55. remoteConfigureCommand.StringVar(&conf.BackblazeEndpoint, "b2.endpoint", "", "backblaze endpoint")
  56. remoteConfigureCommand.StringVar(&conf.AliyunAccessKey, "aliyun.access_key", "", "Aliyun access key, default to use env ALICLOUD_ACCESS_KEY_ID")
  57. remoteConfigureCommand.StringVar(&conf.AliyunSecretKey, "aliyun.secret_key", "", "Aliyun secret key, default to use env ALICLOUD_ACCESS_KEY_SECRET")
  58. remoteConfigureCommand.StringVar(&conf.AliyunEndpoint, "aliyun.endpoint", "", "Aliyun endpoint")
  59. remoteConfigureCommand.StringVar(&conf.TencentSecretId, "tencent.secret_id", "", "Tencent Secret Id, default to use env COS_SECRETID")
  60. remoteConfigureCommand.StringVar(&conf.TencentSecretKey, "tencent.secret_key", "", "Tencent secret key, default to use env COS_SECRETKEY")
  61. remoteConfigureCommand.StringVar(&conf.TencentEndpoint, "tencent.endpoint", "", "Tencent endpoint")
  62. if err = remoteConfigureCommand.Parse(args); err != nil {
  63. return nil
  64. }
  65. if conf.Name == "" {
  66. return c.listExistingRemoteStorages(commandEnv, writer)
  67. }
  68. if !isAlpha(conf.Name) {
  69. return fmt.Errorf("only letters and numbers allowed in name: %v", conf.Name)
  70. }
  71. if *isDelete {
  72. return c.deleteRemoteStorage(commandEnv, writer, conf.Name)
  73. }
  74. return c.saveRemoteStorage(commandEnv, writer, conf)
  75. }
  76. func (c *commandRemoteConfigure) listExistingRemoteStorages(commandEnv *CommandEnv, writer io.Writer) error {
  77. return filer_pb.ReadDirAllEntries(commandEnv, util.FullPath(filer.DirectoryEtcRemote), "", func(entry *filer_pb.Entry, isLast bool) error {
  78. if len(entry.Content) == 0 {
  79. fmt.Fprintf(writer, "skipping %s\n", entry.Name)
  80. return nil
  81. }
  82. if !strings.HasSuffix(entry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  83. return nil
  84. }
  85. conf := &filer_pb.RemoteConf{}
  86. if err := proto.Unmarshal(entry.Content, conf); err != nil {
  87. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err)
  88. }
  89. conf.S3SecretKey = strings.Repeat("*", len(conf.S3SecretKey))
  90. m := jsonpb.Marshaler{
  91. EmitDefaults: false,
  92. Indent: " ",
  93. }
  94. err := m.Marshal(writer, conf)
  95. fmt.Fprintln(writer)
  96. return err
  97. })
  98. }
  99. func (c *commandRemoteConfigure) deleteRemoteStorage(commandEnv *CommandEnv, writer io.Writer, storageName string) error {
  100. return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  101. request := &filer_pb.DeleteEntryRequest{
  102. Directory: filer.DirectoryEtcRemote,
  103. Name: storageName + filer.REMOTE_STORAGE_CONF_SUFFIX,
  104. IgnoreRecursiveError: false,
  105. IsDeleteData: true,
  106. IsRecursive: true,
  107. IsFromOtherCluster: false,
  108. Signatures: nil,
  109. }
  110. _, err := client.DeleteEntry(context.Background(), request)
  111. if err == nil {
  112. fmt.Fprintf(writer, "removed: %s\n", storageName)
  113. }
  114. return err
  115. })
  116. }
  117. func (c *commandRemoteConfigure) saveRemoteStorage(commandEnv *CommandEnv, writer io.Writer, conf *filer_pb.RemoteConf) error {
  118. data, err := proto.Marshal(conf)
  119. if err != nil {
  120. return err
  121. }
  122. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  123. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, conf.Name+filer.REMOTE_STORAGE_CONF_SUFFIX, data)
  124. }); err != nil && err != filer_pb.ErrNotFound {
  125. return err
  126. }
  127. return nil
  128. }