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.

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