command_fs_configure.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package shell
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "regexp"
  8. "strings"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  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 current configuration file content
  24. fs.configure
  25. # trying the changes and see the possible configuration file content
  26. fs.configure -locationPrefix=/my/folder -collection=abc
  27. fs.configure -locationPrefix=/my/folder -collection=abc -ttl=7d
  28. # example: configure adding only 1 physical volume for each bucket collection
  29. fs.configure -locationPrefix=/buckets/ -volumeGrowthCount=1
  30. # apply the changes
  31. fs.configure -locationPrefix=/my/folder -collection=abc -apply
  32. # delete the changes
  33. fs.configure -locationPrefix=/my/folder -delete -apply
  34. `
  35. }
  36. func (c *commandFsConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  37. fsConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  38. locationPrefix := fsConfigureCommand.String("locationPrefix", "", "path prefix, required to update the path-specific configuration")
  39. collection := fsConfigureCommand.String("collection", "", "assign writes to this collection")
  40. replication := fsConfigureCommand.String("replication", "", "assign writes with this replication")
  41. ttl := fsConfigureCommand.String("ttl", "", "assign writes with this ttl (e.g., 1m, 1h, 1d, 1w, 1y)")
  42. diskType := fsConfigureCommand.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  43. fsync := fsConfigureCommand.Bool("fsync", false, "fsync for the writes")
  44. isReadOnly := fsConfigureCommand.Bool("readOnly", false, "disable writes")
  45. maxFileNameLength := fsConfigureCommand.Uint("maxFileNameLength", 0, "file name length limits in bytes for compatibility with Unix-based systems")
  46. dataCenter := fsConfigureCommand.String("dataCenter", "", "assign writes to this dataCenter")
  47. rack := fsConfigureCommand.String("rack", "", "assign writes to this rack")
  48. dataNode := fsConfigureCommand.String("dataNode", "", "assign writes to this dataNode")
  49. volumeGrowthCount := fsConfigureCommand.Int("volumeGrowthCount", 0, "the number of physical volumes to add if no writable volumes")
  50. isDelete := fsConfigureCommand.Bool("delete", false, "delete the configuration by locationPrefix")
  51. apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration")
  52. if err = fsConfigureCommand.Parse(args); err != nil {
  53. return nil
  54. }
  55. fc, err := filer.ReadFilerConf(commandEnv.option.FilerAddress, commandEnv.option.GrpcDialOption, commandEnv.MasterClient)
  56. if err != nil {
  57. return err
  58. }
  59. if *locationPrefix != "" {
  60. infoAboutSimulationMode(writer, *apply, "-apply")
  61. locConf := &filer_pb.FilerConf_PathConf{
  62. LocationPrefix: *locationPrefix,
  63. Collection: *collection,
  64. Replication: *replication,
  65. Ttl: *ttl,
  66. Fsync: *fsync,
  67. MaxFileNameLength: uint32(*maxFileNameLength),
  68. DiskType: *diskType,
  69. VolumeGrowthCount: uint32(*volumeGrowthCount),
  70. ReadOnly: *isReadOnly,
  71. DataCenter: *dataCenter,
  72. Rack: *rack,
  73. DataNode: *dataNode,
  74. }
  75. // check collection
  76. if *collection != "" && strings.HasPrefix(*locationPrefix, "/buckets/") {
  77. return fmt.Errorf("one s3 bucket goes to one collection and not customizable")
  78. }
  79. // check replication
  80. if *replication != "" {
  81. rp, err := super_block.NewReplicaPlacementFromString(*replication)
  82. if err != nil {
  83. return fmt.Errorf("parse replication %s: %v", *replication, err)
  84. }
  85. if *volumeGrowthCount%rp.GetCopyCount() != 0 {
  86. return fmt.Errorf("volumeGrowthCount %d should be divided by replication copy count %d", *volumeGrowthCount, rp.GetCopyCount())
  87. }
  88. }
  89. // check ttl
  90. if *ttl != "" {
  91. regex := "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[mhdwMy]$"
  92. match, _ := regexp.MatchString(regex, *ttl)
  93. if !match {
  94. return fmt.Errorf("ttl should be of the following format [1 to 255][unit] (e.g., 5m, 2h, 180d, 1w, 2y)")
  95. }
  96. }
  97. // save it
  98. if *isDelete {
  99. fc.DeleteLocationConf(*locationPrefix)
  100. } else {
  101. fc.AddLocationConf(locConf)
  102. }
  103. }
  104. var buf2 bytes.Buffer
  105. fc.ToText(&buf2)
  106. fmt.Fprintf(writer, string(buf2.Bytes()))
  107. fmt.Fprintln(writer)
  108. if *apply {
  109. if err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  110. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf2.Bytes())
  111. }); err != nil && err != filer_pb.ErrNotFound {
  112. return err
  113. }
  114. }
  115. return nil
  116. }
  117. func infoAboutSimulationMode(writer io.Writer, forceMode bool, forceModeOption string) {
  118. if forceMode {
  119. return
  120. }
  121. fmt.Fprintf(writer, "Running in simulation mode. Use \"%s\" option to apply the changes.\n", forceModeOption)
  122. }