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.

161 lines
5.4 KiB

11 years ago
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package command
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/security"
  4. "github.com/spf13/viper"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  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. grpcPort *int
  23. publicPort *int
  24. collection *string
  25. defaultReplicaPlacement *string
  26. redirectOnRead *bool
  27. disableDirListing *bool
  28. maxMB *int
  29. dirListingLimit *int
  30. dataCenter *string
  31. enableNotification *bool
  32. disableHttp *bool
  33. // default leveldb directory, used in "weed server" mode
  34. defaultLevelDbDirectory *string
  35. }
  36. func init() {
  37. cmdFiler.Run = runFiler // break init cycle
  38. f.masters = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  39. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this collection")
  40. f.ip = cmdFiler.Flag.String("ip", "", "filer server http listen ip address")
  41. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  42. f.grpcPort = cmdFiler.Flag.Int("port.grpc", 0, "filer grpc server listen port, default to http port + 10000")
  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. }
  52. var cmdFiler = &Command{
  53. UsageLine: "filer -port=8888 -master=<ip:port>[,<ip:port>]*",
  54. Short: "start a file server that points to a master server, or a list of master servers",
  55. Long: `start a file server which accepts REST operation for any files.
  56. //create or overwrite the file, the directories /path/to will be automatically created
  57. POST /path/to/file
  58. //get the file content
  59. GET /path/to/file
  60. //create or overwrite the file, the filename in the multipart request will be used
  61. POST /path/to/
  62. //return a json format subdirectory and files listing
  63. GET /path/to/
  64. The configuration file "filer.toml" is read from ".", "$HOME/.seaweedfs/", or "/etc/seaweedfs/", in that order.
  65. The example filer.toml configuration file can be generated by "weed scaffold -config=filer"
  66. `,
  67. }
  68. func runFiler(cmd *Command, args []string) bool {
  69. weed_server.LoadConfiguration("security", false)
  70. f.startFiler()
  71. return true
  72. }
  73. func (fo *FilerOptions) startFiler() {
  74. defaultMux := http.NewServeMux()
  75. publicVolumeMux := defaultMux
  76. if *fo.publicPort != 0 {
  77. publicVolumeMux = http.NewServeMux()
  78. }
  79. defaultLevelDbDirectory := "./filerdb"
  80. if fo.defaultLevelDbDirectory != nil {
  81. defaultLevelDbDirectory = *fo.defaultLevelDbDirectory + "/filerdb"
  82. }
  83. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  84. Masters: strings.Split(*fo.masters, ","),
  85. Collection: *fo.collection,
  86. DefaultReplication: *fo.defaultReplicaPlacement,
  87. RedirectOnRead: *fo.redirectOnRead,
  88. DisableDirListing: *fo.disableDirListing,
  89. MaxMB: *fo.maxMB,
  90. DirListingLimit: *fo.dirListingLimit,
  91. DataCenter: *fo.dataCenter,
  92. DefaultLevelDbDir: defaultLevelDbDirectory,
  93. DisableHttp: *fo.disableHttp,
  94. })
  95. if nfs_err != nil {
  96. glog.Fatalf("Filer startup error: %v", nfs_err)
  97. }
  98. if *fo.publicPort != 0 {
  99. publicListeningAddress := *fo.ip + ":" + strconv.Itoa(*fo.publicPort)
  100. glog.V(0).Infoln("Start Seaweed filer server", util.VERSION, "public at", publicListeningAddress)
  101. publicListener, e := util.NewListener(publicListeningAddress, 0)
  102. if e != nil {
  103. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  104. }
  105. go func() {
  106. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  107. glog.Fatalf("Volume server fail to serve public: %v", e)
  108. }
  109. }()
  110. }
  111. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.VERSION, *fo.ip, *fo.port)
  112. filerListener, e := util.NewListener(
  113. ":"+strconv.Itoa(*fo.port),
  114. time.Duration(10)*time.Second,
  115. )
  116. if e != nil {
  117. glog.Fatalf("Filer listener error: %v", e)
  118. }
  119. // starting grpc server
  120. grpcPort := *fo.grpcPort
  121. if grpcPort == 0 {
  122. grpcPort = *fo.port + 10000
  123. }
  124. grpcL, err := util.NewListener(":"+strconv.Itoa(grpcPort), 0)
  125. if err != nil {
  126. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  127. }
  128. grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "filer"))
  129. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  130. reflection.Register(grpcS)
  131. go grpcS.Serve(grpcL)
  132. httpS := &http.Server{Handler: defaultMux}
  133. if err := httpS.Serve(filerListener); err != nil {
  134. glog.Fatalf("Filer Fail to serve: %v", e)
  135. }
  136. }