filer.go 14 KB

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