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.

124 lines
2.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "google.golang.org/grpc"
  9. "github.com/chrislusf/seaweedfs/weed/pb"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/chrislusf/seaweedfs/weed/wdclient"
  13. )
  14. type ShellOptions struct {
  15. Masters *string
  16. GrpcDialOption grpc.DialOption
  17. // shell transient context
  18. FilerHost string
  19. FilerPort int64
  20. Directory string
  21. }
  22. type CommandEnv struct {
  23. env map[string]string
  24. MasterClient *wdclient.MasterClient
  25. option ShellOptions
  26. locker *ExclusiveLocker
  27. }
  28. type command interface {
  29. Name() string
  30. Help() string
  31. Do([]string, *CommandEnv, io.Writer) error
  32. }
  33. var (
  34. Commands = []command{}
  35. )
  36. func NewCommandEnv(options ShellOptions) *CommandEnv {
  37. ce := &CommandEnv{
  38. env: make(map[string]string),
  39. MasterClient: wdclient.NewMasterClient(options.GrpcDialOption, pb.AdminShellClient, "", 0, strings.Split(*options.Masters, ",")),
  40. option: options,
  41. }
  42. ce.locker = NewExclusiveLocker(ce.MasterClient)
  43. return ce
  44. }
  45. func (ce *CommandEnv) parseUrl(input string) (path string, err error) {
  46. if strings.HasPrefix(input, "http") {
  47. err = fmt.Errorf("http://<filer>:<port> prefix is not supported any more")
  48. return
  49. }
  50. if !strings.HasPrefix(input, "/") {
  51. input = util.Join(ce.option.Directory, input)
  52. }
  53. return input, err
  54. }
  55. func (ce *CommandEnv) isDirectory(path string) bool {
  56. return ce.checkDirectory(path) == nil
  57. }
  58. func (ce *CommandEnv) checkDirectory(path string) error {
  59. dir, name := util.FullPath(path).DirAndName()
  60. exists, err := filer_pb.Exists(ce, dir, name, true)
  61. if !exists {
  62. return fmt.Errorf("%s is not a directory", path)
  63. }
  64. return err
  65. }
  66. func (ce *CommandEnv) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  67. filerGrpcAddress := fmt.Sprintf("%s:%d", ce.option.FilerHost, ce.option.FilerPort+10000)
  68. return pb.WithGrpcFilerClient(filerGrpcAddress, ce.option.GrpcDialOption, fn)
  69. }
  70. func (ce *CommandEnv) AdjustedUrl(hostAndPort string) string {
  71. return hostAndPort
  72. }
  73. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  74. if strings.HasPrefix(entryPath, "http") {
  75. var u *url.URL
  76. u, err = url.Parse(entryPath)
  77. if err != nil {
  78. return
  79. }
  80. filerServer = u.Hostname()
  81. portString := u.Port()
  82. if portString != "" {
  83. filerPort, err = strconv.ParseInt(portString, 10, 32)
  84. }
  85. path = u.Path
  86. } else {
  87. err = fmt.Errorf("path should have full url /path/to/dirOrFile : %s", entryPath)
  88. }
  89. return
  90. }
  91. func findInputDirectory(args []string) (input string) {
  92. input = "."
  93. if len(args) > 0 {
  94. input = args[len(args)-1]
  95. if strings.HasPrefix(input, "-") {
  96. input = "."
  97. }
  98. }
  99. return input
  100. }