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.

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