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.

105 lines
3.1 KiB

3 months ago
4 years ago
4 years ago
4 years ago
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "math"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  9. "github.com/seaweedfs/seaweedfs/weed/security"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  12. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandS3CleanUploads{})
  16. }
  17. type commandS3CleanUploads struct{}
  18. func (c *commandS3CleanUploads) Name() string {
  19. return "s3.clean.uploads"
  20. }
  21. func (c *commandS3CleanUploads) Help() string {
  22. return `clean up stale multipart uploads
  23. Example:
  24. s3.clean.uploads -timeAgo 1.5h
  25. `
  26. }
  27. func (c *commandS3CleanUploads) HasTag(CommandTag) bool {
  28. return false
  29. }
  30. func (c *commandS3CleanUploads) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  31. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  32. uploadedTimeAgo := bucketCommand.Duration("timeAgo", 24*time.Hour, "created time before now. \"1.5h\" or \"2h45m\". Valid time units are \"m\", \"h\"")
  33. if err = bucketCommand.Parse(args); err != nil {
  34. return nil
  35. }
  36. signingKey := util.GetViper().GetString("jwt.filer_signing.key")
  37. var filerBucketsPath string
  38. filerBucketsPath, err = readFilerBucketsPath(commandEnv)
  39. if err != nil {
  40. return fmt.Errorf("read buckets: %v", err)
  41. }
  42. var buckets []string
  43. err = filer_pb.List(commandEnv, filerBucketsPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  44. buckets = append(buckets, entry.Name)
  45. return nil
  46. }, "", false, math.MaxUint32)
  47. if err != nil {
  48. return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
  49. }
  50. for _, bucket := range buckets {
  51. if err := c.cleanupUploads(commandEnv, writer, filerBucketsPath, bucket, *uploadedTimeAgo, signingKey); err != nil {
  52. fmt.Fprintf(writer, fmt.Sprintf("failed cleanup uploads for bucket %s: %v", bucket, err))
  53. }
  54. }
  55. return err
  56. }
  57. func (c *commandS3CleanUploads) cleanupUploads(commandEnv *CommandEnv, writer io.Writer, filerBucketsPath string, bucket string, timeAgo time.Duration, signingKey string) error {
  58. uploadsDir := filerBucketsPath + "/" + bucket + "/" + s3_constants.MultipartUploadsFolder
  59. var staleUploads []string
  60. now := time.Now()
  61. err := filer_pb.List(commandEnv, uploadsDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  62. ctime := time.Unix(entry.Attributes.Crtime, 0)
  63. if ctime.Add(timeAgo).Before(now) {
  64. staleUploads = append(staleUploads, entry.Name)
  65. }
  66. return nil
  67. }, "", false, math.MaxUint32)
  68. if err != nil {
  69. return fmt.Errorf("list uploads under %v: %v", uploadsDir, err)
  70. }
  71. var encodedJwt security.EncodedJwt
  72. if signingKey != "" {
  73. encodedJwt = security.GenJwtForFilerServer(security.SigningKey(signingKey), 15*60)
  74. }
  75. for _, staleUpload := range staleUploads {
  76. deleteUrl := fmt.Sprintf("http://%s%s/%s?recursive=true&ignoreRecursiveError=true", commandEnv.option.FilerAddress.ToHttpAddress(), uploadsDir, staleUpload)
  77. fmt.Fprintf(writer, "purge %s\n", deleteUrl)
  78. err = util_http.Delete(deleteUrl, string(encodedJwt))
  79. if err != nil && err.Error() != "" {
  80. return fmt.Errorf("purge %s/%s: %v", uploadsDir, staleUpload, err)
  81. }
  82. }
  83. return nil
  84. }