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.

194 lines
6.8 KiB

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