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.

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