filer.go 9.5 KB

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