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.

164 lines
5.6 KiB

11 years ago
6 years ago
11 years ago
7 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
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package command
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/security"
  8. "github.com/spf13/viper"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/server"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. "google.golang.org/grpc/reflection"
  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. redirectOnRead *bool
  26. disableDirListing *bool
  27. maxMB *int
  28. dirListingLimit *int
  29. dataCenter *string
  30. enableNotification *bool
  31. disableHttp *bool
  32. metricsAddress *string
  33. metricsIntervalSec *int
  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", "", "filer server http listen ip address")
  42. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  43. f.publicPort = cmdFiler.Flag.Int("port.readonly", 0, "readonly port opened to public")
  44. f.defaultReplicaPlacement = cmdFiler.Flag.String("defaultReplicaPlacement", "000", "default replication type if not specified")
  45. f.redirectOnRead = cmdFiler.Flag.Bool("redirectOnRead", false, "whether proxy or redirect to volume server during file GET request")
  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.metricsAddress = cmdFiler.Flag.String("metrics.address", "", "Prometheus gateway address")
  52. f.metricsIntervalSec = cmdFiler.Flag.Int("metrics.intervalSeconds", 15, "Prometheus push interval in seconds")
  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 := "./filerdb"
  82. if fo.defaultLevelDbDirectory != nil {
  83. defaultLevelDbDirectory = *fo.defaultLevelDbDirectory + "/filerdb"
  84. }
  85. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  86. Masters: strings.Split(*fo.masters, ","),
  87. Collection: *fo.collection,
  88. DefaultReplication: *fo.defaultReplicaPlacement,
  89. RedirectOnRead: *fo.redirectOnRead,
  90. DisableDirListing: *fo.disableDirListing,
  91. MaxMB: *fo.maxMB,
  92. DirListingLimit: *fo.dirListingLimit,
  93. DataCenter: *fo.dataCenter,
  94. DefaultLevelDbDir: defaultLevelDbDirectory,
  95. DisableHttp: *fo.disableHttp,
  96. MetricsAddress: *fo.metricsAddress,
  97. MetricsIntervalSec: *fo.metricsIntervalSec,
  98. Port: *fo.port,
  99. })
  100. if nfs_err != nil {
  101. glog.Fatalf("Filer startup error: %v", nfs_err)
  102. }
  103. if *fo.publicPort != 0 {
  104. publicListeningAddress := *fo.ip + ":" + strconv.Itoa(*fo.publicPort)
  105. glog.V(0).Infoln("Start Seaweed filer server", util.VERSION, "public at", publicListeningAddress)
  106. publicListener, e := util.NewListener(publicListeningAddress, 0)
  107. if e != nil {
  108. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  109. }
  110. go func() {
  111. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  112. glog.Fatalf("Volume server fail to serve public: %v", e)
  113. }
  114. }()
  115. }
  116. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.VERSION, *fo.ip, *fo.port)
  117. filerListener, e := util.NewListener(
  118. *fo.ip+":"+strconv.Itoa(*fo.port),
  119. time.Duration(10)*time.Second,
  120. )
  121. if e != nil {
  122. glog.Fatalf("Filer listener error: %v", e)
  123. }
  124. // starting grpc server
  125. grpcPort := *fo.port + 10000
  126. grpcL, err := util.NewListener(":"+strconv.Itoa(grpcPort), 0)
  127. if err != nil {
  128. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  129. }
  130. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "filer"))
  131. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  132. reflection.Register(grpcS)
  133. go grpcS.Serve(grpcL)
  134. httpS := &http.Server{Handler: defaultMux}
  135. if err := httpS.Serve(filerListener); err != nil {
  136. glog.Fatalf("Filer Fail to serve: %v", e)
  137. }
  138. }