koji_garbage_collector.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash
  2. # This script removes old scratch builds but also untags old builds whose only tag is built-by-xcp-ng,
  3. # then runs koji-gc so that they can be put in the trashcan if nothing references them anymore,
  4. # and then at next run be deleted.
  5. # *** remove old scratch builds ***
  6. TOPDIR=/mnt/koji
  7. TIMEARG="+30"
  8. IFS=$'\n'
  9. SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
  10. # koji-gc is able to flag builds for deletion, based on koji-gc policies defined in /etc/koji-gc/koji-gc.conf,
  11. # but the presence of the "built-by-xcp-ng" tag prevents it from doing so.
  12. # Consequently, we first look for such builds and untag them.
  13. echo
  14. echo "*** untag_lone_builds.py ***"
  15. su -l kojiadmin -c "$SCRIPT_DIR/untag_lone_builds.py"
  16. # delete builds in the trashcan tag in a previous pass
  17. echo
  18. echo "*** koji-gc --action=delete ***"
  19. su -l kojiadmin -c "koji-gc --action=delete"
  20. # mark new untagged builds for deletion (trashcan tag)
  21. echo
  22. echo "*** koji-gc --action=trash ***"
  23. su -l kojiadmin -c "koji-gc --action=trash"
  24. echo
  25. echo "*** Remove old scratch builds ***"
  26. # we completely remove those that are old enough
  27. # scratch directories are /mnt/koji/scratch/$username/task_$taskid/
  28. # note that $username might contain a slash (e.g. host principals)
  29. cd $TOPDIR/scratch/
  30. for x in $(find $TOPDIR/scratch/ -mindepth 2 -type d -name 'task_*' -prune -mtime $TIMEARG); do
  31. find "$x" -xdev "!" -type d "!" -name "*.deleted" -printf '%s\t %p\n' -delete -exec touch {}.deleted \; -exec chown apache.apache {}.deleted \;
  32. done
  33. echo
  34. echo "*** Remove old tasks ***"
  35. # for tasks, try to remove as a unit
  36. for x in $(find "$TOPDIR"/work/tasks/ -mindepth 2 -maxdepth 2 -type d -mtime $TIMEARG); do
  37. # delete broken symlinks (that will be links to deleted scratch builds) but leave other symlinks alone
  38. find "$x" -xdev -xtype l -printf '%s\t %p\n' -delete -exec touch {}.deleted \; -exec chown apache.apache {}.deleted \;
  39. # Delete SRPMs from buildSRPMFromSCM tasks.
  40. # There is a slight loss of information because the SRPM that is stored in the
  41. # packages/ directory is the one coming from the buildArch task.
  42. # In our case, they should be strictly equivalent, though.
  43. find "$x" -xdev -type f -name "*.src.rpm" -printf '%s\t %p\n' -delete -exec touch {}.deleted \; -exec chown apache.apache {}.deleted \;
  44. done
  45. # for anything else, just remove old stuff
  46. # but don't remove the top level dirs (e.g. cli-build)
  47. #for x in $(find "$TOPDIR"/work -maxdepth 1 -mindepth 1 \! -name tasks); do
  48. # find "$x" -xdev '!' -type d -mtime $TIMEARG -print
  49. # find "$x" -xdev '!' -type d -mtime $TIMEARG -print0 | xargs -0 -r rm -f
  50. # find "$x" -xdev -depth -mindepth 1 -type d -empty -print0 | xargs -0 -r rmdir
  51. # find "$x" -xdev -depth -mindepth 1 -type d -empty -print
  52. #done