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.

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