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.

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