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.

187 lines
7.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/pb/remote_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/golang/protobuf/jsonpb"
  11. "github.com/golang/protobuf/proto"
  12. "io"
  13. "regexp"
  14. "strings"
  15. )
  16. func init() {
  17. Commands = append(Commands, &commandRemoteConfigure{})
  18. }
  19. type commandRemoteConfigure struct {
  20. }
  21. func (c *commandRemoteConfigure) Name() string {
  22. return "remote.configure"
  23. }
  24. func (c *commandRemoteConfigure) Help() string {
  25. return `remote storage configuration
  26. # see the current configurations
  27. remote.configure
  28. # set or update a configuration
  29. remote.configure -name=cloud1 -type=s3 -s3.access_key=xxx -s3.secret_key=yyy
  30. remote.configure -name=cloud2 -type=gcs -gcs.appCredentialsFile=~/service-account-file.json
  31. remote.configure -name=cloud3 -type=azure -azure.account_name=xxx -azure.account_key=yyy
  32. # delete one configuration
  33. remote.configure -delete -name=cloud1
  34. `
  35. }
  36. var (
  37. isAlpha = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString
  38. )
  39. func (c *commandRemoteConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  40. conf := &remote_pb.RemoteConf{}
  41. remoteConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  42. isDelete := remoteConfigureCommand.Bool("delete", false, "delete one remote storage by its name")
  43. remoteConfigureCommand.StringVar(&conf.Name, "name", "", "a short name to identify the remote storage")
  44. remoteConfigureCommand.StringVar(&conf.Type, "type", "s3", "[s3|gcs|azure|b2|aliyun|tencent|baidu|wasabi] storage type")
  45. remoteConfigureCommand.StringVar(&conf.S3AccessKey, "s3.access_key", "", "s3 access key")
  46. remoteConfigureCommand.StringVar(&conf.S3SecretKey, "s3.secret_key", "", "s3 secret key")
  47. remoteConfigureCommand.StringVar(&conf.S3Region, "s3.region", "us-east-2", "s3 region")
  48. remoteConfigureCommand.StringVar(&conf.S3Endpoint, "s3.endpoint", "", "endpoint for s3-compatible local object store")
  49. remoteConfigureCommand.StringVar(&conf.S3StorageClass, "s3.storage_class", "", "s3 storage class")
  50. remoteConfigureCommand.BoolVar(&conf.S3ForcePathStyle, "s3.force_path_style", true, "s3 force path style")
  51. remoteConfigureCommand.StringVar(&conf.GcsGoogleApplicationCredentials, "gcs.appCredentialsFile", "", "google cloud storage credentials file, default to use env GOOGLE_APPLICATION_CREDENTIALS")
  52. remoteConfigureCommand.StringVar(&conf.AzureAccountName, "azure.account_name", "", "azure account name, default to use env AZURE_STORAGE_ACCOUNT")
  53. remoteConfigureCommand.StringVar(&conf.AzureAccountKey, "azure.account_key", "", "azure account name, default to use env AZURE_STORAGE_ACCESS_KEY")
  54. remoteConfigureCommand.StringVar(&conf.BackblazeKeyId, "b2.key_id", "", "backblaze keyID")
  55. 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")
  56. remoteConfigureCommand.StringVar(&conf.BackblazeEndpoint, "b2.endpoint", "", "backblaze endpoint")
  57. remoteConfigureCommand.StringVar(&conf.AliyunAccessKey, "aliyun.access_key", "", "Aliyun access key, default to use env ALICLOUD_ACCESS_KEY_ID")
  58. remoteConfigureCommand.StringVar(&conf.AliyunSecretKey, "aliyun.secret_key", "", "Aliyun secret key, default to use env ALICLOUD_ACCESS_KEY_SECRET")
  59. remoteConfigureCommand.StringVar(&conf.AliyunEndpoint, "aliyun.endpoint", "", "Aliyun endpoint")
  60. remoteConfigureCommand.StringVar(&conf.AliyunRegion, "aliyun.region", "", "Aliyun region")
  61. remoteConfigureCommand.StringVar(&conf.TencentSecretId, "tencent.secret_id", "", "Tencent Secret Id, default to use env COS_SECRETID")
  62. remoteConfigureCommand.StringVar(&conf.TencentSecretKey, "tencent.secret_key", "", "Tencent secret key, default to use env COS_SECRETKEY")
  63. remoteConfigureCommand.StringVar(&conf.TencentEndpoint, "tencent.endpoint", "", "Tencent endpoint")
  64. remoteConfigureCommand.StringVar(&conf.TencentRegion, "tencent.region", "", "Tencent region")
  65. remoteConfigureCommand.StringVar(&conf.BaiduAccessKey, "baidu.access_key", "", "Baidu access key, default to use env BDCLOUD_ACCESS_KEY")
  66. remoteConfigureCommand.StringVar(&conf.BaiduSecretKey, "baidu.secret_key", "", "Baidu secret key, default to use env BDCLOUD_SECRET_KEY")
  67. remoteConfigureCommand.StringVar(&conf.BaiduEndpoint, "baidu.endpoint", "", "Baidu endpoint")
  68. remoteConfigureCommand.StringVar(&conf.BaiduRegion, "baidu.region", "", "Baidu region")
  69. remoteConfigureCommand.StringVar(&conf.WasabiAccessKey, "wasabi.access_key", "", "Wasabi access key")
  70. remoteConfigureCommand.StringVar(&conf.WasabiSecretKey, "wasabi.secret_key", "", "Wasabi secret key")
  71. remoteConfigureCommand.StringVar(&conf.WasabiEndpoint, "wasabi.endpoint", "", "Wasabi endpoint, see https://wasabi.com/wp-content/themes/wasabi/docs/API_Guide/index.html#t=topics%2Fapidiff-intro.htm")
  72. remoteConfigureCommand.StringVar(&conf.WasabiRegion, "wasabi.region", "", "Wasabi region")
  73. if err = remoteConfigureCommand.Parse(args); err != nil {
  74. return nil
  75. }
  76. if conf.Name == "" {
  77. return c.listExistingRemoteStorages(commandEnv, writer)
  78. }
  79. if !isAlpha(conf.Name) {
  80. return fmt.Errorf("only letters and numbers allowed in name: %v", conf.Name)
  81. }
  82. if *isDelete {
  83. return c.deleteRemoteStorage(commandEnv, writer, conf.Name)
  84. }
  85. return c.saveRemoteStorage(commandEnv, writer, conf)
  86. }
  87. func (c *commandRemoteConfigure) listExistingRemoteStorages(commandEnv *CommandEnv, writer io.Writer) error {
  88. return filer_pb.ReadDirAllEntries(commandEnv, util.FullPath(filer.DirectoryEtcRemote), "", func(entry *filer_pb.Entry, isLast bool) error {
  89. if len(entry.Content) == 0 {
  90. fmt.Fprintf(writer, "skipping %s\n", entry.Name)
  91. return nil
  92. }
  93. if !strings.HasSuffix(entry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  94. return nil
  95. }
  96. conf := &remote_pb.RemoteConf{}
  97. if err := proto.Unmarshal(entry.Content, conf); err != nil {
  98. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err)
  99. }
  100. conf.S3SecretKey = strings.Repeat("*", len(conf.S3SecretKey))
  101. m := jsonpb.Marshaler{
  102. EmitDefaults: false,
  103. Indent: " ",
  104. }
  105. err := m.Marshal(writer, conf)
  106. fmt.Fprintln(writer)
  107. return err
  108. })
  109. }
  110. func (c *commandRemoteConfigure) deleteRemoteStorage(commandEnv *CommandEnv, writer io.Writer, storageName string) error {
  111. return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  112. request := &filer_pb.DeleteEntryRequest{
  113. Directory: filer.DirectoryEtcRemote,
  114. Name: storageName + filer.REMOTE_STORAGE_CONF_SUFFIX,
  115. IgnoreRecursiveError: false,
  116. IsDeleteData: true,
  117. IsRecursive: true,
  118. IsFromOtherCluster: false,
  119. Signatures: nil,
  120. }
  121. _, err := client.DeleteEntry(context.Background(), request)
  122. if err == nil {
  123. fmt.Fprintf(writer, "removed: %s\n", storageName)
  124. }
  125. return err
  126. })
  127. }
  128. func (c *commandRemoteConfigure) saveRemoteStorage(commandEnv *CommandEnv, writer io.Writer, conf *remote_pb.RemoteConf) error {
  129. data, err := proto.Marshal(conf)
  130. if err != nil {
  131. return err
  132. }
  133. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  134. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, conf.Name+filer.REMOTE_STORAGE_CONF_SUFFIX, data)
  135. }); err != nil && err != filer_pb.ErrNotFound {
  136. return err
  137. }
  138. return nil
  139. }