commands.go 3.0 KB

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