filer.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package command
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "google.golang.org/grpc/reflection"
  9. "github.com/chrislusf/seaweedfs/weed/util/log"
  10. "github.com/chrislusf/seaweedfs/weed/pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. "github.com/chrislusf/seaweedfs/weed/server"
  14. stats_collect "github.com/chrislusf/seaweedfs/weed/stats"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. var (
  18. f FilerOptions
  19. filerStartS3 *bool
  20. filerS3Options S3Options
  21. )
  22. type FilerOptions struct {
  23. masters *string
  24. ip *string
  25. bindIp *string
  26. port *int
  27. publicPort *int
  28. collection *string
  29. defaultReplicaPlacement *string
  30. disableDirListing *bool
  31. maxMB *int
  32. dirListingLimit *int
  33. dataCenter *string
  34. rack *string
  35. enableNotification *bool
  36. disableHttp *bool
  37. cipher *bool
  38. peers *string
  39. metricsHttpPort *int
  40. // default leveldb directory, used in "weed server" mode
  41. defaultLevelDbDirectory *string
  42. }
  43. func init() {
  44. cmdFiler.Run = runFiler // break init cycle
  45. f.masters = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  46. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this collection")
  47. f.ip = cmdFiler.Flag.String("ip", util.DetectedHostAddress(), "filer server http listen ip address")
  48. f.bindIp = cmdFiler.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
  49. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  50. f.publicPort = cmdFiler.Flag.Int("port.readonly", 0, "readonly port opened to public")
  51. f.defaultReplicaPlacement = cmdFiler.Flag.String("defaultReplicaPlacement", "", "default replication type. If not specified, use master setting.")
  52. f.disableDirListing = cmdFiler.Flag.Bool("disableDirListing", false, "turn off directory listing")
  53. f.maxMB = cmdFiler.Flag.Int("maxMB", 32, "split files larger than the limit")
  54. f.dirListingLimit = cmdFiler.Flag.Int("dirListLimit", 100000, "limit sub dir listing size")
  55. f.dataCenter = cmdFiler.Flag.String("dataCenter", "", "prefer to read and write to volumes in this data center")
  56. f.rack = cmdFiler.Flag.String("rack", "", "prefer to write to volumes in this rack")
  57. f.disableHttp = cmdFiler.Flag.Bool("disableHttp", false, "disable http request, only gRpc operations are allowed")
  58. f.cipher = cmdFiler.Flag.Bool("encryptVolumeData", false, "encrypt data on volume servers")
  59. f.peers = cmdFiler.Flag.String("peers", "", "all filers sharing the same filer store in comma separated ip:port list")
  60. f.metricsHttpPort = cmdFiler.Flag.Int("metricsPort", 0, "Prometheus metrics listen port")
  61. // start s3 on filer
  62. filerStartS3 = cmdFiler.Flag.Bool("s3", false, "whether to start S3 gateway")
  63. filerS3Options.port = cmdFiler.Flag.Int("s3.port", 8333, "s3 server http listen port")
  64. filerS3Options.domainName = cmdFiler.Flag.String("s3.domainName", "", "suffix of the host name in comma separated list, {bucket}.{domainName}")
  65. filerS3Options.tlsPrivateKey = cmdFiler.Flag.String("s3.key.file", "", "path to the TLS private key file")
  66. filerS3Options.tlsCertificate = cmdFiler.Flag.String("s3.cert.file", "", "path to the TLS certificate file")
  67. filerS3Options.config = cmdFiler.Flag.String("s3.config", "", "path to the config file")
  68. }
  69. var cmdFiler = &Command{
  70. UsageLine: "filer -port=8888 -master=<ip:port>[,<ip:port>]*",
  71. Short: "start a file server that points to a master server, or a list of master servers",
  72. Long: `start a file server which accepts REST operation for any files.
  73. //create or overwrite the file, the directories /path/to will be automatically created
  74. POST /path/to/file
  75. //get the file content
  76. GET /path/to/file
  77. //create or overwrite the file, the filename in the multipart request will be used
  78. POST /path/to/
  79. //return a json format subdirectory and files listing
  80. GET /path/to/
  81. The configuration file "filer.toml" is read from ".", "$HOME/.seaweedfs/", or "/etc/seaweedfs/", in that order.
  82. The example filer.toml configuration file can be generated by "weed scaffold -config=filer"
  83. `,
  84. }
  85. func runFiler(cmd *Command, args []string) bool {
  86. util.LoadConfiguration("security", false)
  87. go stats_collect.StartMetricsServer(*f.metricsHttpPort)
  88. if *filerStartS3 {
  89. filerAddress := fmt.Sprintf("%s:%d", *f.ip, *f.port)
  90. filerS3Options.filer = &filerAddress
  91. go func() {
  92. time.Sleep(2 * time.Second)
  93. filerS3Options.startS3Server()
  94. }()
  95. }
  96. f.startFiler()
  97. return true
  98. }
  99. func (fo *FilerOptions) startFiler() {
  100. defaultMux := http.NewServeMux()
  101. publicVolumeMux := defaultMux
  102. if *fo.publicPort != 0 {
  103. publicVolumeMux = http.NewServeMux()
  104. }
  105. defaultLevelDbDirectory := "./filerldb2"
  106. if fo.defaultLevelDbDirectory != nil {
  107. defaultLevelDbDirectory = util.ResolvePath(*fo.defaultLevelDbDirectory + "/filerldb2")
  108. }
  109. var peers []string
  110. if *fo.peers != "" {
  111. peers = strings.Split(*fo.peers, ",")
  112. }
  113. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  114. Masters: strings.Split(*fo.masters, ","),
  115. Collection: *fo.collection,
  116. DefaultReplication: *fo.defaultReplicaPlacement,
  117. DisableDirListing: *fo.disableDirListing,
  118. MaxMB: *fo.maxMB,
  119. DirListingLimit: *fo.dirListingLimit,
  120. DataCenter: *fo.dataCenter,
  121. Rack: *fo.rack,
  122. DefaultLevelDbDir: defaultLevelDbDirectory,
  123. DisableHttp: *fo.disableHttp,
  124. Host: *fo.ip,
  125. Port: uint32(*fo.port),
  126. Cipher: *fo.cipher,
  127. Filers: peers,
  128. })
  129. if nfs_err != nil {
  130. log.Fatalf("Filer startup error: %v", nfs_err)
  131. }
  132. if *fo.publicPort != 0 {
  133. publicListeningAddress := *fo.bindIp + ":" + strconv.Itoa(*fo.publicPort)
  134. log.Infoln("Start Seaweed filer server", util.Version(), "public at", publicListeningAddress)
  135. publicListener, e := util.NewListener(publicListeningAddress, 0)
  136. if e != nil {
  137. log.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  138. }
  139. go func() {
  140. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  141. log.Fatalf("Volume server fail to serve public: %v", e)
  142. }
  143. }()
  144. }
  145. log.Infof("Start Seaweed Filer %s at %s:%d", util.Version(), *fo.ip, *fo.port)
  146. filerListener, e := util.NewListener(
  147. *fo.bindIp+":"+strconv.Itoa(*fo.port),
  148. time.Duration(10)*time.Second,
  149. )
  150. if e != nil {
  151. log.Fatalf("Filer listener error: %v", e)
  152. }
  153. // starting grpc server
  154. grpcPort := *fo.port + 10000
  155. grpcL, err := util.NewListener(*fo.bindIp+":"+strconv.Itoa(grpcPort), 0)
  156. if err != nil {
  157. log.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  158. }
  159. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.filer"))
  160. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  161. reflection.Register(grpcS)
  162. go grpcS.Serve(grpcL)
  163. httpS := &http.Server{Handler: defaultMux}
  164. if err := httpS.Serve(filerListener); err != nil {
  165. log.Fatalf("Filer Fail to serve: %v", e)
  166. }
  167. }