old_package_purging.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. #
  3. # Script to handle package cloud retention policy
  4. # Our open source subscription is limited,
  5. # so we use this sript to control the number of packages maintained historically
  6. #
  7. # Dependencies:
  8. # - PACKAGE_CLOUD_RETENTION_DAYS
  9. # This is to indicate for how many days back we want to maintain the various RPM and DEB packages on package cloud
  10. #
  11. # Copyright : SPDX-License-Identifier: GPL-3.0-or-later
  12. #
  13. # Author : Pavlos Emm. Katsoulakis <paul@netdata.cloud>
  14. #
  15. set -e
  16. delete_files_for_version() {
  17. local v="$1"
  18. # Delete the selected filenames in version
  19. FILES_IN_VERSION=$(jq --sort-keys --arg v "${v}" '.[] | select ( .version | contains($v))' "${PKG_LIST_FILE}" | grep filename | cut -d':' -f 2)
  20. # Iterate through the files and delete them
  21. for pkg in ${FILES_IN_VERSION/\\n/}; do
  22. pkg=${pkg/,/}
  23. pkg=${pkg/\"/}
  24. pkg=${pkg/\"/}
  25. echo "Attempting yank on ${pkg}.."
  26. .travis/package_management/package_cloud_wrapper.sh yank "${PACKAGING_USER}/${DEPLOY_REPO}" "${pkg}" || echo "Nothing to yank or error on ${pkg}"
  27. done
  28. }
  29. # If we are not in netdata git repo, at the top level directory, fail
  30. TOP_LEVEL=$(basename "$(git rev-parse --show-toplevel)")
  31. CWD=$(git rev-parse --show-cdup)
  32. if [ -n "$CWD" ] || [ ! "${TOP_LEVEL}" == "netdata" ]; then
  33. echo "Run as .travis/package_management/$(basename "$0") from top level directory of netdata git repository"
  34. echo "Old packages yanking cancelled"
  35. exit 1
  36. fi
  37. if [ -z "${PACKAGING_USER}" ]; then
  38. echo "No PACKAGING_USER variable found"
  39. exit 1
  40. fi
  41. if [ -z "${DEPLOY_REPO}" ]; then
  42. echo "No DEPLOY_REPO variable found"
  43. exit 1
  44. fi
  45. if [ -z ${PKG_CLOUD_TOKEN} ]; then
  46. echo "No PKG_CLOUD_TOKEN variable found"
  47. exit 1
  48. fi
  49. if [ -z ${PACKAGE_CLOUD_RETENTION_DAYS} ]; then
  50. echo "No PACKAGE_CLOUD_RETENTION_DAYS variable found"
  51. exit 1
  52. fi
  53. TMP_DIR="$(mktemp -d /tmp/netdata-old-package-yanking-XXXXXX)"
  54. PKG_LIST_FILE="${TMP_DIR}/complete_package_list.json"
  55. DATE_EPOCH="1970-01-01"
  56. DATE_UNTIL_TO_DELETE=$(date --date="${PACKAGE_CLOUD_RETENTION_DAYS} day ago" +%Y-%m-%d)
  57. echo "Created temp directory: ${TMP_DIR}"
  58. echo "We will be purging contents up until ${DATE_UNTIL_TO_DELETE}"
  59. echo "Calling package could to retrieve all available packages on ${PACKAGING_USER}/${DEPLOY_REPO}"
  60. curl -sS "https://${PKG_CLOUD_TOKEN}:@packagecloud.io/api/v1/repos/${PACKAGING_USER}/${DEPLOY_REPO}/packages.json" > "${PKG_LIST_FILE}"
  61. # Get versions within the desired duration
  62. #
  63. VERSIONS_TO_PURGE=$(jq --arg s "${DATE_EPOCH}" --arg e "${DATE_UNTIL_TO_DELETE}" '
  64. [($s, $e) | strptime("%Y-%m-%d")[0:3]] as $r
  65. | map(select(
  66. (.created_at[:19] | strptime("%Y-%m-%dT%H:%M:%S")[0:3]) as $d
  67. | $d >= $r[0] and $d <= $r[1]
  68. ))' "${PKG_LIST_FILE}" | grep '"version":' | sort -u | sed -e 's/ //g' | cut -d':' -f2)
  69. echo "We will be deleting the following versions: ${VERSIONS_TO_PURGE}"
  70. for v in ${VERSIONS_TO_PURGE/\n//}; do
  71. v=${v/\"/}
  72. v=${v/\"/}
  73. v=${v/,/}
  74. echo "Remove all files for version $v"
  75. delete_files_for_version "${v}"
  76. done
  77. # Done, clean up
  78. [ -d "${TMP_DIR}" ] && rm -rf "${TMP_DIR}"