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.

169 lines
5.7 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. // 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. }