commands.go 3.0 KB

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