summaryrefslogtreecommitdiffstats
path: root/screwdriver/delete-old-artifactory-artifacts.sh
blob: 3167f91accb26f968b5a6d739161596343ca8c7b (plain) (blame)
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
#!/bin/bash
# Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.


set -euo pipefail

MAX_NUMBER_OF_RELEASES=200
ARTIFACTORY_URL="https://artifactory.yahooinc.com/artifactory"

# JFrog Cloud repo file
if [[ ! -f /etc/yum.repos.d/vespa.repo ]]; then
    cat << EOF > /etc/yum.repos.d/vespa.repo
[vespa-release-el8]
name=Vespa releases
baseurl=$ARTIFACTORY_URL/vespa/centos/8/release/\$basearch
gpgcheck=0
enabled=1
[vespa-release-el7]
name=Vespa releases
baseurl=$ARTIFACTORY_URL/vespa/centos/7/release/\$basearch
gpgcheck=0
enabled=1
EOF
fi

VERSIONS_TO_DELETE=$(dnf list --quiet --showduplicates --disablerepo='*' --enablerepo=vespa-release-el7,vespa-release-el8 vespa | awk '/[0-9].*\.[0-9].*\.[0-9].*/{print $2}' | sort -V | head -n -200 | grep -v "7.594.36")

RPMS_TO_DELETE=$(mktemp)
trap "rm -f $RPMS_TO_DELETE" EXIT

for VERSION in $VERSIONS_TO_DELETE; do
    curl -sSL -H "content-type:text/plain" -H "Authorization: Bearer $JFROG_API_TOKEN" \
         --data "items.find({ \"repo\": { \"\$eq\": \"vespa\" }, \"name\": {\"\$match\": \"vespa*$VERSION*\"}   }).include(\"repo\", \"path\", \"name\")" \
         "$ARTIFACTORY_URL/api/search/aql" \
        | jq -re ".results[]|\"$ARTIFACTORY_URL/\(.repo)/\(.path)/\(.name)\"" >> $RPMS_TO_DELETE
done

echo "Deleting the following RPMs:"
cat $RPMS_TO_DELETE

if [[ -n $SCREWDRIVER ]] && [[ -z $SD_PULL_REQUEST ]]; then
    for RPM in $(cat $RPMS_TO_DELETE); do
        curl -sSL -H "Authorization: Bearer $JFROG_API_TOKEN" -X DELETE $RPM
    done
fi