Guest User

Untitled

a guest
Oct 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. BASE_PATH=/var/opt/gitlab/gitlab-rails/shared/registry/docker/registry/v2/repositories
  4. CONFIG_YML_FILE=/export/docker-registry/gitlab-docker-registry/config.yml
  5.  
  6. DRY_RUN=0
  7. KEEP_LAST_IMAGES=10
  8. RUN_GARBAGE_COLLECTOR=0
  9. GITLAB_CTL_COMMAND=`which gitlab-ctl`
  10.  
  11.  
  12. if [ ! -x "${GITLAB_CTL_COMMAND}" ]; then
  13. echo "Missing gitlab-ctl command"
  14. exit 1
  15. fi
  16.  
  17.  
  18. while (( "$#" )); do
  19. case "$1" in
  20.  
  21. "-b" | "--base-path")
  22. BASE_PATH=$2
  23. shift
  24. ;;
  25.  
  26. "-r" | "--run-gc")
  27. RUN_GARBAGE_COLLECTOR=1
  28. ;;
  29.  
  30. "-d"|"--dry-run")
  31. DRY_RUN=1
  32. ;;
  33.  
  34. "-k"|"--keep")
  35. if ! ( echo $2 | grep -q '^[0-9]\+$') || [ $2 -eq 0 ]; then
  36. echo "Invalid value for keep last images '$2'"
  37. exit 1
  38. fi
  39. KEEP_LAST_IMAGES=$2
  40. shift
  41. ;;
  42.  
  43. "-c" | "--config-path")
  44. BASE_PATH=$2
  45. shift
  46. ;;
  47.  
  48. "-h"|"--help")
  49. echo "Usage: ${0} [options]"
  50. echo "Options:"
  51. echo -e "\t-k NUM, --keep NUM"
  52. echo -e "\t\tKeeps last NUM revisions, except current tags"
  53. echo
  54. echo -e "\t-d, --dry-run"
  55. echo -e "\t\tEnables dry run, no changes will be made"
  56. echo
  57. echo -e "\t-b, --base-path"
  58. echo -e "\t\tSets base path of Gitlab Registry repository storage"
  59. echo
  60. echo -e "\t-r, --run-gc"
  61. echo -e "\t\tStarts garbage collector after revision removal"
  62. echo
  63. echo -e "\t-c, --config-path"
  64. echo -e "\t\tSets the full path to the registry config.yml, to be used together with the garbace collector option (r)"
  65. exit 0
  66. ;;
  67.  
  68. *)
  69. echo "Unknown argument: $1"
  70. exit 1
  71. ;;
  72. esac
  73. shift
  74. done
  75.  
  76. IFS=$'\n'
  77. used_hashes=`mktemp`
  78. marked_hashes=`mktemp`
  79. for repository in `find ${BASE_PATH} -mindepth 2 -maxdepth 2 -type d | sed "s#${BASE_PATH}/##"`; do
  80. for tag_hash in ${BASE_PATH}/${repository}/_manifests/tags/*/current/link; do
  81. cat "${tag_hash}" | cut -d':' -f2;
  82. done > "${used_hashes}"
  83.  
  84. echo "Removing revisions of $repository:"
  85. ls -t ${BASE_PATH}/${repository}/_manifests/revisions/sha256 | fgrep -vf "${used_hashes}" | tail -n+${KEEP_LAST_IMAGES} | tee ${marked_hashes}
  86. if [ ${DRY_RUN} -ne 1 ]; then
  87. cat ${marked_hashes} | sed "s#^#${BASE_PATH}/${repository}/_manifests/revisions/sha256/#" | xargs rm -rf
  88. fi
  89. done
  90. rm ${used_hashes}
  91. rm ${marked_hashes}
  92.  
  93.  
  94. if [ ${DRY_RUN} -eq 0 -a ${RUN_GARBAGE_COLLECTOR} -eq 1 ]; then
  95. "${GITLAB_CTL_COMMAND}" registry-garbage-collect "$CONFIG_YML_FILE"
  96. fi
Add Comment
Please, Sign In to add comment