filer.go 5.7 KB

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