filer.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package command
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "time"
  7. "google.golang.org/grpc/reflection"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/server"
  13. stats_collect "github.com/chrislusf/seaweedfs/weed/stats"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. var (
  17. f FilerOptions
  18. filerStartS3 *bool
  19. filerS3Options S3Options
  20. filerStartWebDav *bool
  21. filerWebDavOptions WebDavOption
  22. filerStartIam *bool
  23. filerIamOptions IamOptions
  24. )
  25. type FilerOptions struct {
  26. masters []pb.ServerAddress
  27. mastersString *string
  28. ip *string
  29. bindIp *string
  30. port *int
  31. portGrpc *int
  32. publicPort *int
  33. collection *string
  34. defaultReplicaPlacement *string
  35. disableDirListing *bool
  36. maxMB *int
  37. dirListingLimit *int
  38. dataCenter *string
  39. rack *string
  40. enableNotification *bool
  41. disableHttp *bool
  42. cipher *bool
  43. metricsHttpPort *int
  44. saveToFilerLimit *int
  45. defaultLevelDbDirectory *string
  46. concurrentUploadLimitMB *int
  47. debug *bool
  48. debugPort *int
  49. }
  50. func init() {
  51. cmdFiler.Run = runFiler // break init cycle
  52. f.mastersString = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  53. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this default collection")
  54. f.ip = cmdFiler.Flag.String("ip", util.DetectedHostAddress(), "filer server http listen ip address")
  55. f.bindIp = cmdFiler.Flag.String("ip.bind", "", "ip address to bind to")
  56. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http listen port")
  57. f.portGrpc = cmdFiler.Flag.Int("port.grpc", 0, "filer server grpc listen port")
  58. f.publicPort = cmdFiler.Flag.Int("port.readonly", 0, "readonly port opened to public")
  59. f.defaultReplicaPlacement = cmdFiler.Flag.String("defaultReplicaPlacement", "", "default replication type. If not specified, use master setting.")
  60. f.disableDirListing = cmdFiler.Flag.Bool("disableDirListing", false, "turn off directory listing")
  61. f.maxMB = cmdFiler.Flag.Int("maxMB", 4, "split files larger than the limit")
  62. f.dirListingLimit = cmdFiler.Flag.Int("dirListLimit", 100000, "limit sub dir listing size")
  63. f.dataCenter = cmdFiler.Flag.String("dataCenter", "", "prefer to read and write to volumes in this data center")
  64. f.rack = cmdFiler.Flag.String("rack", "", "prefer to write to volumes in this rack")
  65. f.disableHttp = cmdFiler.Flag.Bool("disableHttp", false, "disable http request, only gRpc operations are allowed")
  66. f.cipher = cmdFiler.Flag.Bool("encryptVolumeData", false, "encrypt data on volume servers")
  67. f.metricsHttpPort = cmdFiler.Flag.Int("metricsPort", 0, "Prometheus metrics listen port")
  68. f.saveToFilerLimit = cmdFiler.Flag.Int("saveToFilerLimit", 0, "files smaller than this limit will be saved in filer store")
  69. f.defaultLevelDbDirectory = cmdFiler.Flag.String("defaultStoreDir", ".", "if filer.toml is empty, use an embedded filer store in the directory")
  70. f.concurrentUploadLimitMB = cmdFiler.Flag.Int("concurrentUploadLimitMB", 128, "limit total concurrent upload size")
  71. f.debug = cmdFiler.Flag.Bool("debug", false, "serves runtime profiling data, e.g., http://localhost:<debug.port>/debug/pprof/goroutine?debug=2")
  72. f.debugPort = cmdFiler.Flag.Int("debug.port", 6060, "http port for debugging")
  73. // start s3 on filer
  74. filerStartS3 = cmdFiler.Flag.Bool("s3", false, "whether to start S3 gateway")
  75. filerS3Options.port = cmdFiler.Flag.Int("s3.port", 8333, "s3 server http listen port")
  76. filerS3Options.domainName = cmdFiler.Flag.String("s3.domainName", "", "suffix of the host name in comma separated list, {bucket}.{domainName}")
  77. filerS3Options.tlsPrivateKey = cmdFiler.Flag.String("s3.key.file", "", "path to the TLS private key file")
  78. filerS3Options.tlsCertificate = cmdFiler.Flag.String("s3.cert.file", "", "path to the TLS certificate file")
  79. filerS3Options.config = cmdFiler.Flag.String("s3.config", "", "path to the config file")
  80. filerS3Options.auditLogConfig = cmdFiler.Flag.String("s3.auditLogConfig", "", "path to the audit log config file")
  81. filerS3Options.allowEmptyFolder = cmdFiler.Flag.Bool("s3.allowEmptyFolder", true, "allow empty folders")
  82. // start webdav on filer
  83. filerStartWebDav = cmdFiler.Flag.Bool("webdav", false, "whether to start webdav gateway")
  84. filerWebDavOptions.port = cmdFiler.Flag.Int("webdav.port", 7333, "webdav server http listen port")
  85. filerWebDavOptions.collection = cmdFiler.Flag.String("webdav.collection", "", "collection to create the files")
  86. filerWebDavOptions.replication = cmdFiler.Flag.String("webdav.replication", "", "replication to create the files")
  87. filerWebDavOptions.disk = cmdFiler.Flag.String("webdav.disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  88. filerWebDavOptions.tlsPrivateKey = cmdFiler.Flag.String("webdav.key.file", "", "path to the TLS private key file")
  89. filerWebDavOptions.tlsCertificate = cmdFiler.Flag.String("webdav.cert.file", "", "path to the TLS certificate file")
  90. filerWebDavOptions.cacheDir = cmdFiler.Flag.String("webdav.cacheDir", os.TempDir(), "local cache directory for file chunks")
  91. filerWebDavOptions.cacheSizeMB = cmdFiler.Flag.Int64("webdav.cacheCapacityMB", 1000, "local cache capacity in MB")
  92. // start iam on filer
  93. filerStartIam = cmdFiler.Flag.Bool("iam", false, "whether to start IAM service")
  94. filerIamOptions.port = cmdFiler.Flag.Int("iam.port", 8111, "iam server http listen port")
  95. }
  96. var cmdFiler = &Command{
  97. UsageLine: "filer -port=8888 -master=<ip:port>[,<ip:port>]*",
  98. Short: "start a file server that points to a master server, or a list of master servers",
  99. Long: `start a file server which accepts REST operation for any files.
  100. //create or overwrite the file, the directories /path/to will be automatically created
  101. POST /path/to/file
  102. //get the file content
  103. GET /path/to/file
  104. //create or overwrite the file, the filename in the multipart request will be used
  105. POST /path/to/
  106. //return a json format subdirectory and files listing
  107. GET /path/to/
  108. The configuration file "filer.toml" is read from ".", "$HOME/.seaweedfs/", "/usr/local/etc/seaweedfs/", or "/etc/seaweedfs/", in that order.
  109. If the "filer.toml" is not found, an embedded filer store will be created under "-defaultStoreDir".
  110. The example filer.toml configuration file can be generated by "weed scaffold -config=filer"
  111. `,
  112. }
  113. func runFiler(cmd *Command, args []string) bool {
  114. if *f.debug {
  115. go http.ListenAndServe(fmt.Sprintf(":%d", *f.debugPort), nil)
  116. }
  117. util.LoadConfiguration("security", false)
  118. go stats_collect.StartMetricsServer(*f.metricsHttpPort)
  119. filerAddress := util.JoinHostPort(*f.ip, *f.port)
  120. startDelay := time.Duration(2)
  121. if *filerStartS3 {
  122. filerS3Options.filer = &filerAddress
  123. filerS3Options.bindIp = f.bindIp
  124. go func() {
  125. time.Sleep(startDelay * time.Second)
  126. filerS3Options.startS3Server()
  127. }()
  128. startDelay++
  129. }
  130. if *filerStartWebDav {
  131. filerWebDavOptions.filer = &filerAddress
  132. go func() {
  133. time.Sleep(startDelay * time.Second)
  134. filerWebDavOptions.startWebDav()
  135. }()
  136. startDelay++
  137. }
  138. if *filerStartIam {
  139. filerIamOptions.filer = &filerAddress
  140. filerIamOptions.masters = f.mastersString
  141. go func() {
  142. time.Sleep(startDelay * time.Second)
  143. filerIamOptions.startIamServer()
  144. }()
  145. }
  146. f.masters = pb.ServerAddresses(*f.mastersString).ToAddresses()
  147. f.startFiler()
  148. return true
  149. }
  150. func (fo *FilerOptions) startFiler() {
  151. defaultMux := http.NewServeMux()
  152. publicVolumeMux := defaultMux
  153. if *fo.publicPort != 0 {
  154. publicVolumeMux = http.NewServeMux()
  155. }
  156. if *fo.portGrpc == 0 {
  157. *fo.portGrpc = 10000 + *fo.port
  158. }
  159. defaultLevelDbDirectory := util.ResolvePath(*fo.defaultLevelDbDirectory + "/filerldb2")
  160. filerAddress := pb.NewServerAddress(*fo.ip, *fo.port, *fo.portGrpc)
  161. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  162. Masters: fo.masters,
  163. Collection: *fo.collection,
  164. DefaultReplication: *fo.defaultReplicaPlacement,
  165. DisableDirListing: *fo.disableDirListing,
  166. MaxMB: *fo.maxMB,
  167. DirListingLimit: *fo.dirListingLimit,
  168. DataCenter: *fo.dataCenter,
  169. Rack: *fo.rack,
  170. DefaultLevelDbDir: defaultLevelDbDirectory,
  171. DisableHttp: *fo.disableHttp,
  172. Host: filerAddress,
  173. Cipher: *fo.cipher,
  174. SaveToFilerLimit: int64(*fo.saveToFilerLimit),
  175. ConcurrentUploadLimit: int64(*fo.concurrentUploadLimitMB) * 1024 * 1024,
  176. })
  177. if nfs_err != nil {
  178. glog.Fatalf("Filer startup error: %v", nfs_err)
  179. }
  180. if *fo.publicPort != 0 {
  181. publicListeningAddress := util.JoinHostPort(*fo.bindIp, *fo.publicPort)
  182. glog.V(0).Infoln("Start Seaweed filer server", util.Version(), "public at", publicListeningAddress)
  183. publicListener, e := util.NewListener(publicListeningAddress, 0)
  184. if e != nil {
  185. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  186. }
  187. go func() {
  188. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  189. glog.Fatalf("Volume server fail to serve public: %v", e)
  190. }
  191. }()
  192. }
  193. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.Version(), *fo.ip, *fo.port)
  194. filerListener, e := util.NewListener(
  195. util.JoinHostPort(*fo.bindIp, *fo.port),
  196. time.Duration(10)*time.Second,
  197. )
  198. if e != nil {
  199. glog.Fatalf("Filer listener error: %v", e)
  200. }
  201. // starting grpc server
  202. grpcPort := *fo.portGrpc
  203. grpcL, err := util.NewListener(util.JoinHostPort(*fo.bindIp, grpcPort), 0)
  204. if err != nil {
  205. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  206. }
  207. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.filer"))
  208. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  209. reflection.Register(grpcS)
  210. go grpcS.Serve(grpcL)
  211. httpS := &http.Server{Handler: defaultMux}
  212. if err := httpS.Serve(filerListener); err != nil {
  213. glog.Fatalf("Filer Fail to serve: %v", e)
  214. }
  215. }