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.

160 lines
5.4 KiB

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