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.

412 lines
16 KiB

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