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.

126 lines
3.3 KiB

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