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.

174 lines
5.9 KiB

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