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.

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