command_remote_configure.go 11 KB

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