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.

143 lines
4.6 KiB

5 years ago
3 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/user"
  8. "strconv"
  9. "time"
  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. "github.com/chrislusf/seaweedfs/weed/server"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. var (
  18. webDavStandaloneOptions WebDavOption
  19. )
  20. type WebDavOption struct {
  21. filer *string
  22. port *int
  23. collection *string
  24. replication *string
  25. disk *string
  26. tlsPrivateKey *string
  27. tlsCertificate *string
  28. cacheDir *string
  29. cacheSizeMB *int64
  30. }
  31. func init() {
  32. cmdWebDav.Run = runWebDav // break init cycle
  33. webDavStandaloneOptions.filer = cmdWebDav.Flag.String("filer", "localhost:8888", "filer server address")
  34. webDavStandaloneOptions.port = cmdWebDav.Flag.Int("port", 7333, "webdav server http listen port")
  35. webDavStandaloneOptions.collection = cmdWebDav.Flag.String("collection", "", "collection to create the files")
  36. webDavStandaloneOptions.replication = cmdWebDav.Flag.String("replication", "", "replication to create the files")
  37. webDavStandaloneOptions.disk = cmdWebDav.Flag.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  38. webDavStandaloneOptions.tlsPrivateKey = cmdWebDav.Flag.String("key.file", "", "path to the TLS private key file")
  39. webDavStandaloneOptions.tlsCertificate = cmdWebDav.Flag.String("cert.file", "", "path to the TLS certificate file")
  40. webDavStandaloneOptions.cacheDir = cmdWebDav.Flag.String("cacheDir", os.TempDir(), "local cache directory for file chunks")
  41. webDavStandaloneOptions.cacheSizeMB = cmdWebDav.Flag.Int64("cacheCapacityMB", 1000, "local cache capacity in MB")
  42. }
  43. var cmdWebDav = &Command{
  44. UsageLine: "webdav -port=7333 -filer=<ip:port>",
  45. Short: "start a webdav server that is backed by a filer",
  46. Long: `start a webdav server that is backed by a filer.
  47. `,
  48. }
  49. func runWebDav(cmd *Command, args []string) bool {
  50. util.LoadConfiguration("security", false)
  51. glog.V(0).Infof("Starting Seaweed WebDav Server %s at https port %d", util.Version(), *webDavStandaloneOptions.port)
  52. return webDavStandaloneOptions.startWebDav()
  53. }
  54. func (wo *WebDavOption) startWebDav() bool {
  55. // detect current user
  56. uid, gid := uint32(0), uint32(0)
  57. if u, err := user.Current(); err == nil {
  58. if parsedId, pe := strconv.ParseUint(u.Uid, 10, 32); pe == nil {
  59. uid = uint32(parsedId)
  60. }
  61. if parsedId, pe := strconv.ParseUint(u.Gid, 10, 32); pe == nil {
  62. gid = uint32(parsedId)
  63. }
  64. }
  65. // parse filer grpc address
  66. filerAddress := pb.ServerAddress(*wo.filer)
  67. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  68. var cipher bool
  69. // connect to filer
  70. for {
  71. err := pb.WithGrpcFilerClient(false, filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  72. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  73. if err != nil {
  74. return fmt.Errorf("get filer %s configuration: %v", filerAddress, err)
  75. }
  76. cipher = resp.Cipher
  77. return nil
  78. })
  79. if err != nil {
  80. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *wo.filer, filerAddress.ToGrpcAddress())
  81. time.Sleep(time.Second)
  82. } else {
  83. glog.V(0).Infof("connected to filer %s grpc address %s", *wo.filer, filerAddress.ToGrpcAddress())
  84. break
  85. }
  86. }
  87. ws, webdavServer_err := weed_server.NewWebDavServer(&weed_server.WebDavOption{
  88. Filer: filerAddress,
  89. GrpcDialOption: grpcDialOption,
  90. Collection: *wo.collection,
  91. Replication: *wo.replication,
  92. DiskType: *wo.disk,
  93. Uid: uid,
  94. Gid: gid,
  95. Cipher: cipher,
  96. CacheDir: util.ResolvePath(*wo.cacheDir),
  97. CacheSizeMB: *wo.cacheSizeMB,
  98. })
  99. if webdavServer_err != nil {
  100. glog.Fatalf("WebDav Server startup error: %v", webdavServer_err)
  101. }
  102. httpS := &http.Server{Handler: ws.Handler}
  103. listenAddress := fmt.Sprintf(":%d", *wo.port)
  104. webDavListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  105. if err != nil {
  106. glog.Fatalf("WebDav Server listener on %s error: %v", listenAddress, err)
  107. }
  108. if *wo.tlsPrivateKey != "" {
  109. glog.V(0).Infof("Start Seaweed WebDav Server %s at https port %d", util.Version(), *wo.port)
  110. if err = httpS.ServeTLS(webDavListener, *wo.tlsCertificate, *wo.tlsPrivateKey); err != nil {
  111. glog.Fatalf("WebDav Server Fail to serve: %v", err)
  112. }
  113. } else {
  114. glog.V(0).Infof("Start Seaweed WebDav Server %s at http port %d", util.Version(), *wo.port)
  115. if err = httpS.Serve(webDavListener); err != nil {
  116. glog.Fatalf("WebDav Server Fail to serve: %v", err)
  117. }
  118. }
  119. return true
  120. }