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.

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