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.

248 lines
10 KiB

11 years ago
11 years ago
11 years ago
5 years ago
7 years ago
11 years ago
4 years ago
11 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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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. "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. go func() {
  124. time.Sleep(startDelay * time.Second)
  125. filerS3Options.startS3Server()
  126. }()
  127. startDelay++
  128. }
  129. if *filerStartWebDav {
  130. filerWebDavOptions.filer = &filerAddress
  131. go func() {
  132. time.Sleep(startDelay * time.Second)
  133. filerWebDavOptions.startWebDav()
  134. }()
  135. startDelay++
  136. }
  137. if *filerStartIam {
  138. filerIamOptions.filer = &filerAddress
  139. filerIamOptions.masters = f.mastersString
  140. go func() {
  141. time.Sleep(startDelay * time.Second)
  142. filerIamOptions.startIamServer()
  143. }()
  144. }
  145. f.masters = pb.ServerAddresses(*f.mastersString).ToAddresses()
  146. f.startFiler()
  147. return true
  148. }
  149. func (fo *FilerOptions) startFiler() {
  150. defaultMux := http.NewServeMux()
  151. publicVolumeMux := defaultMux
  152. if *fo.publicPort != 0 {
  153. publicVolumeMux = http.NewServeMux()
  154. }
  155. if *fo.portGrpc == 0 {
  156. *fo.portGrpc = 10000 + *fo.port
  157. }
  158. defaultLevelDbDirectory := util.ResolvePath(*fo.defaultLevelDbDirectory + "/filerldb2")
  159. filerAddress := pb.NewServerAddress(*fo.ip, *fo.port, *fo.portGrpc)
  160. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  161. Masters: fo.masters,
  162. Collection: *fo.collection,
  163. DefaultReplication: *fo.defaultReplicaPlacement,
  164. DisableDirListing: *fo.disableDirListing,
  165. MaxMB: *fo.maxMB,
  166. DirListingLimit: *fo.dirListingLimit,
  167. DataCenter: *fo.dataCenter,
  168. Rack: *fo.rack,
  169. DefaultLevelDbDir: defaultLevelDbDirectory,
  170. DisableHttp: *fo.disableHttp,
  171. Host: filerAddress,
  172. Cipher: *fo.cipher,
  173. SaveToFilerLimit: int64(*fo.saveToFilerLimit),
  174. ConcurrentUploadLimit: int64(*fo.concurrentUploadLimitMB) * 1024 * 1024,
  175. })
  176. if nfs_err != nil {
  177. glog.Fatalf("Filer startup error: %v", nfs_err)
  178. }
  179. if *fo.publicPort != 0 {
  180. publicListeningAddress := util.JoinHostPort(*fo.bindIp, *fo.publicPort)
  181. glog.V(0).Infoln("Start Seaweed filer server", util.Version(), "public at", publicListeningAddress)
  182. publicListener, e := util.NewListener(publicListeningAddress, 0)
  183. if e != nil {
  184. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  185. }
  186. go func() {
  187. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  188. glog.Fatalf("Volume server fail to serve public: %v", e)
  189. }
  190. }()
  191. }
  192. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.Version(), *fo.ip, *fo.port)
  193. filerListener, e := util.NewListener(
  194. util.JoinHostPort(*fo.bindIp, *fo.port),
  195. time.Duration(10)*time.Second,
  196. )
  197. if e != nil {
  198. glog.Fatalf("Filer listener error: %v", e)
  199. }
  200. // starting grpc server
  201. grpcPort := *fo.portGrpc
  202. grpcL, err := util.NewListener(util.JoinHostPort(*fo.bindIp, grpcPort), 0)
  203. if err != nil {
  204. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  205. }
  206. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.filer"))
  207. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  208. reflection.Register(grpcS)
  209. go grpcS.Serve(grpcL)
  210. httpS := &http.Server{Handler: defaultMux}
  211. if err := httpS.Serve(filerListener); err != nil {
  212. glog.Fatalf("Filer Fail to serve: %v", e)
  213. }
  214. }