filer.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package command
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "google.golang.org/grpc/reflection"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/server"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. var (
  16. f FilerOptions
  17. )
  18. type FilerOptions struct {
  19. masters *string
  20. ip *string
  21. port *int
  22. publicPort *int
  23. collection *string
  24. defaultReplicaPlacement *string
  25. disableDirListing *bool
  26. maxMB *int
  27. dirListingLimit *int
  28. dataCenter *string
  29. enableNotification *bool
  30. disableHttp *bool
  31. cipher *bool
  32. // default leveldb directory, used in "weed server" mode
  33. defaultLevelDbDirectory *string
  34. }
  35. func init() {
  36. cmdFiler.Run = runFiler // break init cycle
  37. f.masters = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  38. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this collection")
  39. f.ip = cmdFiler.Flag.String("ip", "", "filer server http listen ip address")
  40. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  41. f.publicPort = cmdFiler.Flag.Int("port.readonly", 0, "readonly port opened to public")
  42. f.defaultReplicaPlacement = cmdFiler.Flag.String("defaultReplicaPlacement", "000", "default replication type if not specified")
  43. f.disableDirListing = cmdFiler.Flag.Bool("disableDirListing", false, "turn off directory listing")
  44. f.maxMB = cmdFiler.Flag.Int("maxMB", 32, "split files larger than the limit")
  45. f.dirListingLimit = cmdFiler.Flag.Int("dirListLimit", 100000, "limit sub dir listing size")
  46. f.dataCenter = cmdFiler.Flag.String("dataCenter", "", "prefer to write to volumes in this data center")
  47. f.disableHttp = cmdFiler.Flag.Bool("disableHttp", false, "disable http request, only gRpc operations are allowed")
  48. f.cipher = cmdFiler.Flag.Bool("encryptVolumeData", false, "encrypt data on volume servers")
  49. }
  50. var cmdFiler = &Command{
  51. UsageLine: "filer -port=8888 -master=<ip:port>[,<ip:port>]*",
  52. Short: "start a file server that points to a master server, or a list of master servers",
  53. Long: `start a file server which accepts REST operation for any files.
  54. //create or overwrite the file, the directories /path/to will be automatically created
  55. POST /path/to/file
  56. //get the file content
  57. GET /path/to/file
  58. //create or overwrite the file, the filename in the multipart request will be used
  59. POST /path/to/
  60. //return a json format subdirectory and files listing
  61. GET /path/to/
  62. The configuration file "filer.toml" is read from ".", "$HOME/.seaweedfs/", or "/etc/seaweedfs/", in that order.
  63. The example filer.toml configuration file can be generated by "weed scaffold -config=filer"
  64. `,
  65. }
  66. func runFiler(cmd *Command, args []string) bool {
  67. util.LoadConfiguration("security", false)
  68. f.startFiler()
  69. return true
  70. }
  71. func (fo *FilerOptions) startFiler() {
  72. defaultMux := http.NewServeMux()
  73. publicVolumeMux := defaultMux
  74. if *fo.publicPort != 0 {
  75. publicVolumeMux = http.NewServeMux()
  76. }
  77. defaultLevelDbDirectory := "./filerldb2"
  78. if fo.defaultLevelDbDirectory != nil {
  79. defaultLevelDbDirectory = *fo.defaultLevelDbDirectory + "/filerldb2"
  80. }
  81. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  82. Masters: strings.Split(*fo.masters, ","),
  83. Collection: *fo.collection,
  84. DefaultReplication: *fo.defaultReplicaPlacement,
  85. DisableDirListing: *fo.disableDirListing,
  86. MaxMB: *fo.maxMB,
  87. DirListingLimit: *fo.dirListingLimit,
  88. DataCenter: *fo.dataCenter,
  89. DefaultLevelDbDir: defaultLevelDbDirectory,
  90. DisableHttp: *fo.disableHttp,
  91. Port: uint32(*fo.port),
  92. Cipher: *fo.cipher,
  93. })
  94. if nfs_err != nil {
  95. glog.Fatalf("Filer startup error: %v", nfs_err)
  96. }
  97. if *fo.publicPort != 0 {
  98. publicListeningAddress := *fo.ip + ":" + strconv.Itoa(*fo.publicPort)
  99. glog.V(0).Infoln("Start Seaweed filer server", util.VERSION, "public at", publicListeningAddress)
  100. publicListener, e := util.NewListener(publicListeningAddress, 0)
  101. if e != nil {
  102. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  103. }
  104. go func() {
  105. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  106. glog.Fatalf("Volume server fail to serve public: %v", e)
  107. }
  108. }()
  109. }
  110. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.VERSION, *fo.ip, *fo.port)
  111. filerListener, e := util.NewListener(
  112. *fo.ip+":"+strconv.Itoa(*fo.port),
  113. time.Duration(10)*time.Second,
  114. )
  115. if e != nil {
  116. glog.Fatalf("Filer listener error: %v", e)
  117. }
  118. // starting grpc server
  119. grpcPort := *fo.port + 10000
  120. grpcL, err := util.NewListener(":"+strconv.Itoa(grpcPort), 0)
  121. if err != nil {
  122. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  123. }
  124. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.filer"))
  125. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  126. reflection.Register(grpcS)
  127. go grpcS.Serve(grpcL)
  128. httpS := &http.Server{Handler: defaultMux}
  129. if err := httpS.Serve(filerListener); err != nil {
  130. glog.Fatalf("Filer Fail to serve: %v", e)
  131. }
  132. }