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.

247 lines
9.8 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.allowEmptyFolder = cmdFiler.Flag.Bool("s3.allowEmptyFolder", true, "allow empty folders")
  81. // start webdav on filer
  82. filerStartWebDav = cmdFiler.Flag.Bool("webdav", false, "whether to start webdav gateway")
  83. filerWebDavOptions.port = cmdFiler.Flag.Int("webdav.port", 7333, "webdav server http listen port")
  84. filerWebDavOptions.collection = cmdFiler.Flag.String("webdav.collection", "", "collection to create the files")
  85. filerWebDavOptions.replication = cmdFiler.Flag.String("webdav.replication", "", "replication to create the files")
  86. filerWebDavOptions.disk = cmdFiler.Flag.String("webdav.disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  87. filerWebDavOptions.tlsPrivateKey = cmdFiler.Flag.String("webdav.key.file", "", "path to the TLS private key file")
  88. filerWebDavOptions.tlsCertificate = cmdFiler.Flag.String("webdav.cert.file", "", "path to the TLS certificate file")
  89. filerWebDavOptions.cacheDir = cmdFiler.Flag.String("webdav.cacheDir", os.TempDir(), "local cache directory for file chunks")
  90. filerWebDavOptions.cacheSizeMB = cmdFiler.Flag.Int64("webdav.cacheCapacityMB", 1000, "local cache capacity in MB")
  91. // start iam on filer
  92. filerStartIam = cmdFiler.Flag.Bool("iam", false, "whether to start IAM service")
  93. filerIamOptions.port = cmdFiler.Flag.Int("iam.port", 8111, "iam server http listen port")
  94. }
  95. var cmdFiler = &Command{
  96. UsageLine: "filer -port=8888 -master=<ip:port>[,<ip:port>]*",
  97. Short: "start a file server that points to a master server, or a list of master servers",
  98. Long: `start a file server which accepts REST operation for any files.
  99. //create or overwrite the file, the directories /path/to will be automatically created
  100. POST /path/to/file
  101. //get the file content
  102. GET /path/to/file
  103. //create or overwrite the file, the filename in the multipart request will be used
  104. POST /path/to/
  105. //return a json format subdirectory and files listing
  106. GET /path/to/
  107. The configuration file "filer.toml" is read from ".", "$HOME/.seaweedfs/", "/usr/local/etc/seaweedfs/", or "/etc/seaweedfs/", in that order.
  108. If the "filer.toml" is not found, an embedded filer store will be created under "-defaultStoreDir".
  109. The example filer.toml configuration file can be generated by "weed scaffold -config=filer"
  110. `,
  111. }
  112. func runFiler(cmd *Command, args []string) bool {
  113. if *f.debug {
  114. go http.ListenAndServe(fmt.Sprintf(":%d", *f.debugPort), nil)
  115. }
  116. util.LoadConfiguration("security", false)
  117. go stats_collect.StartMetricsServer(*f.metricsHttpPort)
  118. filerAddress := util.JoinHostPort(*f.ip, *f.port)
  119. startDelay := time.Duration(2)
  120. if *filerStartS3 {
  121. filerS3Options.filer = &filerAddress
  122. go func() {
  123. time.Sleep(startDelay * time.Second)
  124. filerS3Options.startS3Server()
  125. }()
  126. startDelay++
  127. }
  128. if *filerStartWebDav {
  129. filerWebDavOptions.filer = &filerAddress
  130. go func() {
  131. time.Sleep(startDelay * time.Second)
  132. filerWebDavOptions.startWebDav()
  133. }()
  134. startDelay++
  135. }
  136. if *filerStartIam {
  137. filerIamOptions.filer = &filerAddress
  138. filerIamOptions.masters = f.mastersString
  139. go func() {
  140. time.Sleep(startDelay * time.Second)
  141. filerIamOptions.startIamServer()
  142. }()
  143. }
  144. f.masters = pb.ServerAddresses(*f.mastersString).ToAddresses()
  145. f.startFiler()
  146. return true
  147. }
  148. func (fo *FilerOptions) startFiler() {
  149. defaultMux := http.NewServeMux()
  150. publicVolumeMux := defaultMux
  151. if *fo.publicPort != 0 {
  152. publicVolumeMux = http.NewServeMux()
  153. }
  154. if *fo.portGrpc == 0 {
  155. *fo.portGrpc = 10000 + *fo.port
  156. }
  157. defaultLevelDbDirectory := util.ResolvePath(*fo.defaultLevelDbDirectory + "/filerldb2")
  158. filerAddress := pb.NewServerAddress(*fo.ip, *fo.port, *fo.portGrpc)
  159. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  160. Masters: fo.masters,
  161. Collection: *fo.collection,
  162. DefaultReplication: *fo.defaultReplicaPlacement,
  163. DisableDirListing: *fo.disableDirListing,
  164. MaxMB: *fo.maxMB,
  165. DirListingLimit: *fo.dirListingLimit,
  166. DataCenter: *fo.dataCenter,
  167. Rack: *fo.rack,
  168. DefaultLevelDbDir: defaultLevelDbDirectory,
  169. DisableHttp: *fo.disableHttp,
  170. Host: filerAddress,
  171. Cipher: *fo.cipher,
  172. SaveToFilerLimit: int64(*fo.saveToFilerLimit),
  173. ConcurrentUploadLimit: int64(*fo.concurrentUploadLimitMB) * 1024 * 1024,
  174. })
  175. if nfs_err != nil {
  176. glog.Fatalf("Filer startup error: %v", nfs_err)
  177. }
  178. if *fo.publicPort != 0 {
  179. publicListeningAddress := util.JoinHostPort(*fo.bindIp, *fo.publicPort)
  180. glog.V(0).Infoln("Start Seaweed filer server", util.Version(), "public at", publicListeningAddress)
  181. publicListener, e := util.NewListener(publicListeningAddress, 0)
  182. if e != nil {
  183. glog.Fatalf("Filer server public listener error on port %d:%v", *fo.publicPort, e)
  184. }
  185. go func() {
  186. if e := http.Serve(publicListener, publicVolumeMux); e != nil {
  187. glog.Fatalf("Volume server fail to serve public: %v", e)
  188. }
  189. }()
  190. }
  191. glog.V(0).Infof("Start Seaweed Filer %s at %s:%d", util.Version(), *fo.ip, *fo.port)
  192. filerListener, e := util.NewListener(
  193. util.JoinHostPort(*fo.bindIp, *fo.port),
  194. time.Duration(10)*time.Second,
  195. )
  196. if e != nil {
  197. glog.Fatalf("Filer listener error: %v", e)
  198. }
  199. // starting grpc server
  200. grpcPort := *fo.portGrpc
  201. grpcL, err := util.NewListener(util.JoinHostPort(*fo.bindIp, grpcPort), 0)
  202. if err != nil {
  203. glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
  204. }
  205. grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.filer"))
  206. filer_pb.RegisterSeaweedFilerServer(grpcS, fs)
  207. reflection.Register(grpcS)
  208. go grpcS.Serve(grpcL)
  209. httpS := &http.Server{Handler: defaultMux}
  210. if err := httpS.Serve(filerListener); err != nil {
  211. glog.Fatalf("Filer Fail to serve: %v", e)
  212. }
  213. }