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.

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