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.

150 lines
3.3 KiB

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