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
9.8 KiB

11 years ago
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
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. "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. debug *bool
  49. debugPort *int
  50. }
  51. func init() {
  52. cmdFiler.Run = runFiler // break init cycle
  53. f.masters = cmdFiler.Flag.String("master", "localhost:9333", "comma-separated master servers")
  54. f.collection = cmdFiler.Flag.String("collection", "", "all data will be stored in this default collection")
  55. f.ip = cmdFiler.Flag.String("ip", util.DetectedHostAddress(), "filer server http listen ip address")
  56. f.bindIp = cmdFiler.Flag.String("ip.bind", "", "ip address to bind to")
  57. f.port = cmdFiler.Flag.Int("port", 8888, "filer server http 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.peers = cmdFiler.Flag.String("peers", "", "all filers sharing the same filer store in comma separated ip:port list")
  68. f.metricsHttpPort = cmdFiler.Flag.Int("metricsPort", 0, "Prometheus metrics listen port")
  69. f.saveToFilerLimit = cmdFiler.Flag.Int("saveToFilerLimit", 0, "files smaller than this limit will be saved in filer store")
  70. f.defaultLevelDbDirectory = cmdFiler.Flag.String("defaultStoreDir", ".", "if filer.toml is empty, use an embedded filer store in the directory")
  71. f.concurrentUploadLimitMB = cmdFiler.Flag.Int("concurrentUploadLimitMB", 128, "limit total concurrent upload size")
  72. f.debug = cmdFiler.Flag.Bool("debug", false, "serves runtime profiling data, e.g., http://localhost:<debug.port>/debug/pprof/goroutine?debug=2")
  73. f.debugPort = cmdFiler.Flag.Int("debug.port", 6060, "http port for debugging")
  74. // start s3 on filer
  75. filerStartS3 = cmdFiler.Flag.Bool("s3", false, "whether to start S3 gateway")
  76. filerS3Options.port = cmdFiler.Flag.Int("s3.port", 8333, "s3 server http listen port")
  77. filerS3Options.domainName = cmdFiler.Flag.String("s3.domainName", "", "suffix of the host name in comma separated list, {bucket}.{domainName}")
  78. filerS3Options.tlsPrivateKey = cmdFiler.Flag.String("s3.key.file", "", "path to the TLS private key file")
  79. filerS3Options.tlsCertificate = cmdFiler.Flag.String("s3.cert.file", "", "path to the TLS certificate file")
  80. filerS3Options.config = cmdFiler.Flag.String("s3.config", "", "path to the config file")
  81. filerS3Options.allowEmptyFolder = cmdFiler.Flag.Bool("s3.allowEmptyFolder", false, "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 := fmt.Sprintf("%s:%d", *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.masters
  140. go func() {
  141. time.Sleep(startDelay * time.Second)
  142. filerIamOptions.startIamServer()
  143. }()
  144. }
  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. defaultLevelDbDirectory := util.ResolvePath(*fo.defaultLevelDbDirectory + "/filerldb2")
  155. var peers []string
  156. if *fo.peers != "" {
  157. peers = strings.Split(*fo.peers, ",")
  158. }
  159. fs, nfs_err := weed_server.NewFilerServer(defaultMux, publicVolumeMux, &weed_server.FilerOption{
  160. Masters: strings.Split(*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: *fo.ip,
  171. Port: uint32(*fo.port),
  172. Cipher: *fo.cipher,
  173. SaveToFilerLimit: int64(*fo.saveToFilerLimit),
  174. Filers: peers,
  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 := *fo.bindIp + ":" + strconv.Itoa(*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. *fo.bindIp+":"+strconv.Itoa(*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.port + 10000
  203. grpcL, err := util.NewListener(*fo.bindIp+":"+strconv.Itoa(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. }