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.

227 lines
9.8 KiB

3 years ago
  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 -s3.region=us-east-2
  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. remote.configure -name=cloud4 -type=aliyun -aliyun.access_key=xxx -aliyun.secret_key=yyy -aliyun.endpoint=oss-cn-shenzhen.aliyuncs.com -aliyun.region=cn-sehnzhen
  33. remote.configure -name=cloud5 -type=tencent -tencent.secret_id=xxx -tencent.secret_key=yyy -tencent.endpoint=cos.ap-guangzhou.myqcloud.com
  34. remote.configure -name=cloud6 -type=wasabi -wasabi.access_key=xxx -wasabi.secret_key=yyy -wasabi.endpoint=s3.us-west-1.wasabisys.com -wasabi.region=us-west-1
  35. remote.configure -name=cloud7 -type=storj -storj.access_key=xxx -storj.secret_key=yyy -storj.endpoint=https://gateway.us1.storjshare.io
  36. remote.configure -name=cloud8 -type=filebase -filebase.access_key=xxx -filebase.secret_key=yyy -filebase.endpoint=https://s3.filebase.com
  37. # delete one configuration
  38. remote.configure -delete -name=cloud1
  39. `
  40. }
  41. var (
  42. isAlpha = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString
  43. )
  44. func (c *commandRemoteConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  45. conf := &remote_pb.RemoteConf{}
  46. remoteConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  47. isDelete := remoteConfigureCommand.Bool("delete", false, "delete one remote storage by its name")
  48. remoteConfigureCommand.StringVar(&conf.Name, "name", "", "a short name to identify the remote storage")
  49. remoteConfigureCommand.StringVar(&conf.Type, "type", "s3", "[s3|gcs|azure|b2|aliyun|tencent|baidu|wasabi|hdfs|filebase|storj] storage type")
  50. remoteConfigureCommand.StringVar(&conf.S3AccessKey, "s3.access_key", "", "s3 access key")
  51. remoteConfigureCommand.StringVar(&conf.S3SecretKey, "s3.secret_key", "", "s3 secret key")
  52. remoteConfigureCommand.StringVar(&conf.S3Region, "s3.region", "us-east-2", "s3 region")
  53. remoteConfigureCommand.StringVar(&conf.S3Endpoint, "s3.endpoint", "", "endpoint for s3-compatible local object store")
  54. remoteConfigureCommand.StringVar(&conf.S3StorageClass, "s3.storage_class", "", "s3 storage class")
  55. remoteConfigureCommand.BoolVar(&conf.S3ForcePathStyle, "s3.force_path_style", true, "s3 force path style")
  56. remoteConfigureCommand.BoolVar(&conf.S3V4Signature, "s3.v4_signature", false, "s3 V4 signature")
  57. remoteConfigureCommand.StringVar(&conf.GcsGoogleApplicationCredentials, "gcs.appCredentialsFile", "", "google cloud storage credentials file, default to use env GOOGLE_APPLICATION_CREDENTIALS")
  58. remoteConfigureCommand.StringVar(&conf.AzureAccountName, "azure.account_name", "", "azure account name, default to use env AZURE_STORAGE_ACCOUNT")
  59. remoteConfigureCommand.StringVar(&conf.AzureAccountKey, "azure.account_key", "", "azure account name, default to use env AZURE_STORAGE_ACCESS_KEY")
  60. remoteConfigureCommand.StringVar(&conf.BackblazeKeyId, "b2.key_id", "", "backblaze keyID")
  61. 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")
  62. remoteConfigureCommand.StringVar(&conf.BackblazeEndpoint, "b2.endpoint", "", "backblaze endpoint")
  63. remoteConfigureCommand.StringVar(&conf.AliyunAccessKey, "aliyun.access_key", "", "Aliyun access key, default to use env ALICLOUD_ACCESS_KEY_ID")
  64. remoteConfigureCommand.StringVar(&conf.AliyunSecretKey, "aliyun.secret_key", "", "Aliyun secret key, default to use env ALICLOUD_ACCESS_KEY_SECRET")
  65. remoteConfigureCommand.StringVar(&conf.AliyunEndpoint, "aliyun.endpoint", "", "Aliyun endpoint")
  66. remoteConfigureCommand.StringVar(&conf.AliyunRegion, "aliyun.region", "", "Aliyun region")
  67. remoteConfigureCommand.StringVar(&conf.TencentSecretId, "tencent.secret_id", "", "Tencent Secret Id, default to use env COS_SECRETID")
  68. remoteConfigureCommand.StringVar(&conf.TencentSecretKey, "tencent.secret_key", "", "Tencent secret key, default to use env COS_SECRETKEY")
  69. remoteConfigureCommand.StringVar(&conf.TencentEndpoint, "tencent.endpoint", "", "Tencent endpoint")
  70. remoteConfigureCommand.StringVar(&conf.BaiduAccessKey, "baidu.access_key", "", "Baidu access key, default to use env BDCLOUD_ACCESS_KEY")
  71. remoteConfigureCommand.StringVar(&conf.BaiduSecretKey, "baidu.secret_key", "", "Baidu secret key, default to use env BDCLOUD_SECRET_KEY")
  72. remoteConfigureCommand.StringVar(&conf.BaiduEndpoint, "baidu.endpoint", "", "Baidu endpoint")
  73. remoteConfigureCommand.StringVar(&conf.BaiduRegion, "baidu.region", "", "Baidu region")
  74. remoteConfigureCommand.StringVar(&conf.WasabiAccessKey, "wasabi.access_key", "", "Wasabi access key")
  75. remoteConfigureCommand.StringVar(&conf.WasabiSecretKey, "wasabi.secret_key", "", "Wasabi secret key")
  76. 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")
  77. remoteConfigureCommand.StringVar(&conf.WasabiRegion, "wasabi.region", "", "Wasabi region")
  78. remoteConfigureCommand.StringVar(&conf.FilebaseAccessKey, "filebase.access_key", "", "Filebase access key")
  79. remoteConfigureCommand.StringVar(&conf.FilebaseSecretKey, "filebase.secret_key", "", "Filebase secret key")
  80. remoteConfigureCommand.StringVar(&conf.FilebaseEndpoint, "filebase.endpoint", "", "Filebase endpoint, https://s3.filebase.com")
  81. remoteConfigureCommand.StringVar(&conf.StorjAccessKey, "storj.access_key", "", "Storj access key")
  82. remoteConfigureCommand.StringVar(&conf.StorjSecretKey, "storj.secret_key", "", "Storj secret key")
  83. remoteConfigureCommand.StringVar(&conf.StorjEndpoint, "storj.endpoint", "", "Storj endpoint")
  84. var namenodes arrayFlags
  85. remoteConfigureCommand.Var(&namenodes, "hdfs.namenodes", "hdfs name node and port, example: namenode1:8020,namenode2:8020")
  86. remoteConfigureCommand.StringVar(&conf.HdfsUsername, "hdfs.username", "", "hdfs user name")
  87. remoteConfigureCommand.StringVar(&conf.HdfsServicePrincipalName, "hdfs.servicePrincipalName", "", `Kerberos service principal name for the namenode
  88. Example: hdfs/namenode.hadoop.docker
  89. Namenode running as service 'hdfs' with FQDN 'namenode.hadoop.docker'.
  90. `)
  91. remoteConfigureCommand.StringVar(&conf.HdfsDataTransferProtection, "hdfs.dataTransferProtection", "", "[authentication|integrity|privacy] Kerberos data transfer protection")
  92. if err = remoteConfigureCommand.Parse(args); err != nil {
  93. return nil
  94. }
  95. if conf.Type != "s3" {
  96. // clear out the default values
  97. conf.S3Region = ""
  98. conf.S3ForcePathStyle = false
  99. }
  100. if conf.Name == "" {
  101. return c.listExistingRemoteStorages(commandEnv, writer)
  102. }
  103. if !isAlpha(conf.Name) {
  104. return fmt.Errorf("only letters and numbers allowed in name: %v", conf.Name)
  105. }
  106. if *isDelete {
  107. return c.deleteRemoteStorage(commandEnv, writer, conf.Name)
  108. }
  109. return c.saveRemoteStorage(commandEnv, writer, conf)
  110. }
  111. func (c *commandRemoteConfigure) listExistingRemoteStorages(commandEnv *CommandEnv, writer io.Writer) error {
  112. return filer_pb.ReadDirAllEntries(commandEnv, util.FullPath(filer.DirectoryEtcRemote), "", func(entry *filer_pb.Entry, isLast bool) error {
  113. if len(entry.Content) == 0 {
  114. fmt.Fprintf(writer, "skipping %s\n", entry.Name)
  115. return nil
  116. }
  117. if !strings.HasSuffix(entry.Name, filer.REMOTE_STORAGE_CONF_SUFFIX) {
  118. return nil
  119. }
  120. conf := &remote_pb.RemoteConf{}
  121. if err := proto.Unmarshal(entry.Content, conf); err != nil {
  122. return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err)
  123. }
  124. conf.S3SecretKey = strings.Repeat("*", len(conf.S3SecretKey))
  125. m := jsonpb.Marshaler{
  126. EmitDefaults: false,
  127. Indent: " ",
  128. }
  129. err := m.Marshal(writer, conf)
  130. fmt.Fprintln(writer)
  131. return err
  132. })
  133. }
  134. func (c *commandRemoteConfigure) deleteRemoteStorage(commandEnv *CommandEnv, writer io.Writer, storageName string) error {
  135. return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  136. request := &filer_pb.DeleteEntryRequest{
  137. Directory: filer.DirectoryEtcRemote,
  138. Name: storageName + filer.REMOTE_STORAGE_CONF_SUFFIX,
  139. IgnoreRecursiveError: false,
  140. IsDeleteData: true,
  141. IsRecursive: true,
  142. IsFromOtherCluster: false,
  143. Signatures: nil,
  144. }
  145. _, err := client.DeleteEntry(context.Background(), request)
  146. if err == nil {
  147. fmt.Fprintf(writer, "removed: %s\n", storageName)
  148. }
  149. return err
  150. })
  151. }
  152. func (c *commandRemoteConfigure) saveRemoteStorage(commandEnv *CommandEnv, writer io.Writer, conf *remote_pb.RemoteConf) error {
  153. data, err := proto.Marshal(conf)
  154. if err != nil {
  155. return err
  156. }
  157. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  158. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, conf.Name+filer.REMOTE_STORAGE_CONF_SUFFIX, data)
  159. }); err != nil && err != filer_pb.ErrNotFound {
  160. return err
  161. }
  162. return nil
  163. }
  164. type arrayFlags []string
  165. func (i *arrayFlags) String() string {
  166. return "my string representation"
  167. }
  168. func (i *arrayFlags) Set(value string) error {
  169. *i = append(*i, value)
  170. return nil
  171. }