-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudsmith-quota.sh
executable file
·103 lines (82 loc) · 2.69 KB
/
cloudsmith-quota.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
# Copyright (C) 2023 Tvheadend Project (https://tvheadend.org)
# SPDX-License-Identifier: MIT
set -euo pipefail
if [ -z "${CLOUDSMITH_API_KEY}" ]; then
echo "CLOUDSMITH_API_KEY not available, aborting!"
exit 1
fi
if [ -z "${CLOUDSMITH_OWNER}" ]; then
echo "CLOUDSMITH_OWNER not available, aborting!"
exit 1
fi
if [ -z "${CLOUDSMITH_REPO}" ]; then
echo "CLOUDSMITH_REPO not available, aborting!"
exit 1
fi
# max pagesize
PAGESIZE="500"
# get repo file count
FILES=$(curl -i --silent --url "https://api.cloudsmith.io/v1/packages/$CLOUDSMITH_OWNER/$CLOUDSMITH_REPO/?page=2&page_size=1" \
--header "X-Api-Key: $CLOUDSMITH_API_KEY" | \
awk '/x-pagination-count/ {print $2}' | tr -d '\r')
# calculate the max pages needed
(( PAGE = ( FILES / PAGESIZE ) + 1 ))
# results sorted oldest first
for (( i=1; i<PAGE; i++ )); do
JSON+=$(curl --silent --request GET \
--url "https://api.cloudsmith.io/v1/packages/$CLOUDSMITH_OWNER/$CLOUDSMITH_REPO/?page=${i}&page_size=${PAGESIZE}&sort=+date" \
--header "X-Api-Key: ${CLOUDSMITH_API_KEY}" \
--header 'accept: application/json' | \
jq -r '.[]')
done
# get plan limit
LIMIT=$(curl --silent --request GET \
--url "https://api.cloudsmith.io/v1/quota/oss/$CLOUDSMITH_OWNER/" \
--header "X-Api-Key: ${CLOUDSMITH_API_KEY}" \
--header 'accept: application/json' | \
jq -r '.[].raw.storage.plan_limit')
# target is 90% of limit
TARGET=$(( LIMIT * 90/100 ))
# array of filesizes
SIZES=$(echo "${JSON}" | jq -r '.size')
SIZES=($SIZES)
# array of filenames
NAMES=$(echo "${JSON}" | jq -r '.filename')
NAMES=($NAMES)
# array of file slugs
SLUGS=$(echo "${JSON}" | jq -r '.slug_perm')
SLUGS=($SLUGS)
# array length
LENGTH=${#SIZES[@]}
# calculate before size
BEFORE=$(( ${SIZES[@]/%/ +} 0))
# iterate over the array to take action
for (( i=0; i<LENGTH; i++ )); do
# if reposize over threshold delete file and element
if [[ $((${SIZES[@]/%/ +} 0)) -gt $TARGET ]]; then
curl --request DELETE \
--url "https://api.cloudsmith.io/v1/packages/$CLOUDSMITH_OWNER/$CLOUDSMITH_REPO/${SLUGS[$i]}/" \
--header "X-Api-Key: ${CLOUDSMITH_API_KEY}" \
--header 'accept: application/json'
echo "deleting: ${SLUGS[$i]} - ${SIZES[$i]} - ${NAMES[$i]}"
unset NAMES[$i]
unset SIZES[$i]
unset SLUGS[$i]
fi
NAMES=( ${NAMES[@]} )
SIZES=( ${SIZES[@]} )
SLUGS=( ${SLUGS[@]} )
done
# calculate after size
AFTER=$(( ${SIZES[@]/%/ +} 0))
# summary
echo "Cloudsmith Quota Summary:"
echo " limit $LIMIT"
echo " target $TARGET"
if (( BEFORE > AFTER )); then
echo "current $AFTER"
else
echo "current $BEFORE"
fi
exit