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.

102 lines
2.9 KiB

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