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.

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