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.

114 lines
2.9 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. fs.configure -locationPrfix=/my/folder -
  24. `
  25. }
  26. func (c *commandFsConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. fsConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. locationPrefix := fsConfigureCommand.String("locationPrefix", "", "path prefix, required to update the path-specific configuration")
  29. collection := fsConfigureCommand.String("collection", "", "assign writes to this collection")
  30. replication := fsConfigureCommand.String("replication", "", "assign writes with this replication")
  31. ttl := fsConfigureCommand.String("ttl", "", "assign writes with this ttl")
  32. fsync := fsConfigureCommand.Bool("fsync", false, "fsync for the writes")
  33. isDelete := fsConfigureCommand.Bool("delete", false, "delete the configuration by locationPrefix")
  34. apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration")
  35. if err = fsConfigureCommand.Parse(args); err != nil {
  36. return nil
  37. }
  38. var buf bytes.Buffer
  39. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  40. request := &filer_pb.LookupDirectoryEntryRequest{
  41. Directory: filer.DirectoryEtc,
  42. Name: filer.FilerConfName,
  43. }
  44. respLookupEntry, err := filer_pb.LookupEntry(client, request)
  45. if err != nil {
  46. return err
  47. }
  48. return filer.StreamContent(commandEnv.MasterClient, &buf, respLookupEntry.Entry.Chunks, 0, math.MaxInt64)
  49. }); err != nil {
  50. return err
  51. }
  52. fc := filer.NewFilerConf()
  53. if err = fc.LoadFromBytes(buf.Bytes()); err != nil {
  54. return err
  55. }
  56. if *locationPrefix != "" {
  57. locConf := &filer_pb.FilerConf_PathConf{
  58. LocationPrefix: *locationPrefix,
  59. Collection: *collection,
  60. Replication: *replication,
  61. Ttl: *ttl,
  62. Fsync: *fsync,
  63. }
  64. if *isDelete {
  65. fc.DeleteLocationConf(*locationPrefix)
  66. } else {
  67. fc.AddLocationConf(locConf)
  68. }
  69. }
  70. buf.Reset()
  71. fc.ToText(&buf)
  72. fmt.Fprintf(writer, string(buf.Bytes()))
  73. if *apply {
  74. target := fmt.Sprintf("http://%s:%d%s/%s", commandEnv.option.FilerHost, commandEnv.option.FilerPort, filer.DirectoryEtc, filer.FilerConfName)
  75. // set the HTTP method, url, and request body
  76. req, err := http.NewRequest(http.MethodPut, target, &buf)
  77. if err != nil {
  78. return err
  79. }
  80. // set the request header Content-Type for json
  81. req.Header.Set("Content-Type", "text/plain; charset=utf-8")
  82. resp, err := http.DefaultClient.Do(req)
  83. if err != nil {
  84. return err
  85. }
  86. util.CloseResponse(resp)
  87. }
  88. return nil
  89. }