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.

134 lines
2.8 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) confirmIsLocked() error {
  59. if ce.locker.isLocking {
  60. return nil
  61. }
  62. return fmt.Errorf("need to lock to continue")
  63. }
  64. func (ce *CommandEnv) checkDirectory(path string) error {
  65. dir, name := util.FullPath(path).DirAndName()
  66. exists, err := filer_pb.Exists(ce, dir, name, true)
  67. if !exists {
  68. return fmt.Errorf("%s is not a directory", path)
  69. }
  70. return err
  71. }
  72. func (ce *CommandEnv) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  73. filerGrpcAddress := fmt.Sprintf("%s:%d", ce.option.FilerHost, ce.option.FilerPort+10000)
  74. return pb.WithGrpcFilerClient(filerGrpcAddress, ce.option.GrpcDialOption, fn)
  75. }
  76. func (ce *CommandEnv) AdjustedUrl(hostAndPort string) string {
  77. return hostAndPort
  78. }
  79. func parseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  80. if strings.HasPrefix(entryPath, "http") {
  81. var u *url.URL
  82. u, err = url.Parse(entryPath)
  83. if err != nil {
  84. return
  85. }
  86. filerServer = u.Hostname()
  87. portString := u.Port()
  88. if portString != "" {
  89. filerPort, err = strconv.ParseInt(portString, 10, 32)
  90. }
  91. path = u.Path
  92. } else {
  93. err = fmt.Errorf("path should have full url /path/to/dirOrFile : %s", entryPath)
  94. }
  95. return
  96. }
  97. func findInputDirectory(args []string) (input string) {
  98. input = "."
  99. if len(args) > 0 {
  100. input = args[len(args)-1]
  101. if strings.HasPrefix(input, "-") {
  102. input = "."
  103. }
  104. }
  105. return input
  106. }