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.

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