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.

256 lines
10 KiB

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