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.

187 lines
4.9 KiB

5 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
2 years ago
5 years ago
2 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. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/operation"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "google.golang.org/grpc"
  12. "github.com/seaweedfs/seaweedfs/weed/pb"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/util"
  15. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  16. "github.com/seaweedfs/seaweedfs/weed/wdclient/exclusive_locks"
  17. )
  18. type ShellOptions struct {
  19. Masters *string
  20. GrpcDialOption grpc.DialOption
  21. // shell transient context
  22. FilerHost string
  23. FilerPort int64
  24. FilerGroup *string
  25. FilerAddress pb.ServerAddress
  26. Directory string
  27. }
  28. type CommandEnv struct {
  29. env map[string]string
  30. MasterClient *wdclient.MasterClient
  31. option *ShellOptions
  32. locker *exclusive_locks.ExclusiveLocker
  33. noLock bool
  34. }
  35. func NewCommandEnv(options *ShellOptions) *CommandEnv {
  36. ce := &CommandEnv{
  37. env: make(map[string]string),
  38. MasterClient: wdclient.NewMasterClient(options.GrpcDialOption, *options.FilerGroup, pb.AdminShellClient, "", "", "", *pb.ServerAddresses(*options.Masters).ToServiceDiscovery()),
  39. option: options,
  40. noLock: false,
  41. }
  42. ce.locker = exclusive_locks.NewExclusiveLocker(ce.MasterClient, "shell")
  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(args []string) error {
  59. if ce.locker.IsLocked() {
  60. return nil
  61. }
  62. ce.locker.SetMessage(fmt.Sprintf("%v", args))
  63. return fmt.Errorf("need to run \"lock\" first to continue")
  64. }
  65. func (ce *CommandEnv) isLocked() bool {
  66. if ce == nil {
  67. return true
  68. }
  69. if ce.noLock {
  70. return false
  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, 0, 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. }
  120. func readNeedleMeta(grpcDialOption grpc.DialOption, volumeServer pb.ServerAddress, volumeId uint32, needleValue needle_map.NeedleValue) (resp *volume_server_pb.ReadNeedleMetaResponse, err error) {
  121. err = operation.WithVolumeServerClient(false, volumeServer, grpcDialOption,
  122. func(client volume_server_pb.VolumeServerClient) error {
  123. if resp, err = client.ReadNeedleMeta(context.Background(), &volume_server_pb.ReadNeedleMetaRequest{
  124. VolumeId: volumeId,
  125. NeedleId: uint64(needleValue.Key),
  126. Offset: needleValue.Offset.ToActualOffset(),
  127. Size: int32(needleValue.Size),
  128. }); err != nil {
  129. return err
  130. }
  131. return nil
  132. },
  133. )
  134. return
  135. }
  136. func readNeedleStatus(grpcDialOption grpc.DialOption, sourceVolumeServer pb.ServerAddress, volumeId uint32, needleValue needle_map.NeedleValue) (resp *volume_server_pb.VolumeNeedleStatusResponse, err error) {
  137. err = operation.WithVolumeServerClient(false, sourceVolumeServer, grpcDialOption,
  138. func(client volume_server_pb.VolumeServerClient) error {
  139. if resp, err = client.VolumeNeedleStatus(context.Background(), &volume_server_pb.VolumeNeedleStatusRequest{
  140. VolumeId: volumeId,
  141. NeedleId: uint64(needleValue.Key),
  142. }); err != nil {
  143. return err
  144. }
  145. return nil
  146. },
  147. )
  148. return
  149. }
  150. func getCollectionName(commandEnv *CommandEnv, bucket string) string {
  151. if *commandEnv.option.FilerGroup != "" {
  152. return fmt.Sprintf("%s_%s", *commandEnv.option.FilerGroup, bucket)
  153. }
  154. return bucket
  155. }