command_fs_configure.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package shell
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "math"
  8. "net/http"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/weed/filer"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandFsConfigure{})
  16. }
  17. type commandFsConfigure struct {
  18. }
  19. func (c *commandFsConfigure) Name() string {
  20. return "fs.configure"
  21. }
  22. func (c *commandFsConfigure) Help() string {
  23. return `configure and apply storage options for each location
  24. # see the current configuration file content
  25. fs.configure
  26. # trying the changes and see the possible configuration file content
  27. fs.configure -locationPrfix=/my/folder -collection=abc
  28. fs.configure -locationPrfix=/my/folder -collection=abc -ttl=7d
  29. # apply the changes
  30. fs.configure -locationPrfix=/my/folder -collection=abc -apply
  31. # delete the changes
  32. fs.configure -locationPrfix=/my/folder -delete -apply
  33. `
  34. }
  35. func (c *commandFsConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  36. fsConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  37. locationPrefix := fsConfigureCommand.String("locationPrefix", "", "path prefix, required to update the path-specific configuration")
  38. collection := fsConfigureCommand.String("collection", "", "assign writes to this collection")
  39. replication := fsConfigureCommand.String("replication", "", "assign writes with this replication")
  40. ttl := fsConfigureCommand.String("ttl", "", "assign writes with this ttl")
  41. fsync := fsConfigureCommand.Bool("fsync", false, "fsync for the writes")
  42. isDelete := fsConfigureCommand.Bool("delete", false, "delete the configuration by locationPrefix")
  43. apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration")
  44. if err = fsConfigureCommand.Parse(args); err != nil {
  45. return nil
  46. }
  47. var buf bytes.Buffer
  48. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  49. request := &filer_pb.LookupDirectoryEntryRequest{
  50. Directory: filer.DirectoryEtc,
  51. Name: filer.FilerConfName,
  52. }
  53. respLookupEntry, err := filer_pb.LookupEntry(client, request)
  54. if err != nil {
  55. return err
  56. }
  57. return filer.StreamContent(commandEnv.MasterClient, &buf, respLookupEntry.Entry.Chunks, 0, math.MaxInt64)
  58. }); err != nil {
  59. return err
  60. }
  61. fc := filer.NewFilerConf()
  62. if err = fc.LoadFromBytes(buf.Bytes()); err != nil {
  63. return err
  64. }
  65. if *locationPrefix != "" {
  66. locConf := &filer_pb.FilerConf_PathConf{
  67. LocationPrefix: *locationPrefix,
  68. Collection: *collection,
  69. Replication: *replication,
  70. Ttl: *ttl,
  71. Fsync: *fsync,
  72. }
  73. if *collection != "" && strings.HasPrefix(*locationPrefix, "/buckets/") {
  74. return fmt.Errorf("one s3 bucket goes to one collection and not customizable.")
  75. }
  76. if *isDelete {
  77. fc.DeleteLocationConf(*locationPrefix)
  78. } else {
  79. fc.AddLocationConf(locConf)
  80. }
  81. }
  82. buf.Reset()
  83. fc.ToText(&buf)
  84. fmt.Fprintf(writer, string(buf.Bytes()))
  85. fmt.Fprintln(writer)
  86. if *apply {
  87. target := fmt.Sprintf("http://%s:%d%s/%s", commandEnv.option.FilerHost, commandEnv.option.FilerPort, filer.DirectoryEtc, filer.FilerConfName)
  88. // set the HTTP method, url, and request body
  89. req, err := http.NewRequest(http.MethodPut, target, &buf)
  90. if err != nil {
  91. return err
  92. }
  93. // set the request header Content-Type for json
  94. req.Header.Set("Content-Type", "text/plain; charset=utf-8")
  95. resp, err := http.DefaultClient.Do(req)
  96. if err != nil {
  97. return err
  98. }
  99. util.CloseResponse(resp)
  100. }
  101. return nil
  102. }