-
Notifications
You must be signed in to change notification settings - Fork 9
/
cleanup-patches.sh
executable file
·89 lines (89 loc) · 3.91 KB
/
cleanup-patches.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
#!/usr/bin/env bash
set -o pipefail
# Deregisters old application package versions. Each patch version corresponds to a package manifest
# in an Amazon S3 access point managed by AWS Panorama. Deletes the manifest file, but not binary
# assets such as container images. Binary assets can be used by multiple patch versions. Verify that
# no in-use manifests reference binary assets before deleting them from the access point.
if [[ $# -eq 1 ]] ; then
PACKAGE_NAME=$1
else
echo "Usage: ./cleanup-patches.sh <package-name>"
exit 1
fi
PACKAGE_LIST=$(aws panorama list-packages)
while [ -x ${PACKAGE_ID} ]; do
PACKAGE_ID=$(echo "${PACKAGE_LIST}" | jq -r --arg PACKAGE_NAME "${PACKAGE_NAME}" '.Packages[] | select (.PackageName == $PACKAGE_NAME) | .PackageId')
NEXT_TOKEN=$(echo "${PACKAGE_LIST}" | jq -r '.NextToken')
if [ -x ${PACKAGE_ID} ]; then
if [ "${NEXT_TOKEN}" = "null" ]; then
echo "Package not found."
exit 1
fi
PACKAGE_LIST=$(aws panorama list-packages --next-token ${NEXT_TOKEN})
fi
done
PACKAGE=$(aws panorama describe-package --package-id ${PACKAGE_ID})
BUCKET=$(echo "${PACKAGE}" | jq -r '.StorageLocation.Bucket')
MANI_PREFIX=$(echo "${PACKAGE}" | jq -r '.StorageLocation.ManifestPrefixLocation')
BINARY_PREFIX=$(echo "${PACKAGE}" | jq -r '.StorageLocation.BinaryPrefixLocation')
MANIS=$(aws s3api list-objects --bucket ${BUCKET} --prefix ${MANI_PREFIX})
PATCH_DATES=($((echo ${MANIS} | jq -r '.Contents |=sort_by(.LastModified) | [.Contents[].LastModified] | @sh') | tr -d \'\"))
MANI_PATHS=($((echo ${MANIS} | jq -r '.Contents |=sort_by(.LastModified) | [.Contents[].Key] | @sh') | tr -d \'\"))
PATCH_ID_REGEX='([A-Za-z0-9]{64})\.json'
PACKAGE_VERSION_REGEX='/manifests/([0-9]\.[0-9])/'
get_patch_id() {
PATH=$1
if [[ ${PATH} =~ ${PATCH_ID_REGEX} ]]; then
PATCH_ID=${BASH_REMATCH[1]}
echo ${PATCH_ID}
fi
}
get_package_version() {
PATH=$1
if [[ ${PATH} =~ ${PACKAGE_VERSION_REGEX} ]]; then
PACKAGE_VERSION=${BASH_REMATCH[1]}
echo ${PACKAGE_VERSION}
fi
}
echo "PATCH VERSIONS"
for (( c=0; c<${#PATCH_DATES[@]}; c++ ))
do
PATCH_ID=$(get_patch_id ${MANI_PATHS[${c}]})
PACKAGE_VERSION=$(get_package_version ${MANI_PATHS[${c}]})
echo "${PATCH_DATES[${c}]} : Version ${PACKAGE_VERSION}.${PATCH_ID}"
done
while [ -x ${NUM_VERSIONS} ]; do
echo "Deregister how many old versions?"
read NUM_VERSIONS
done
if [[ "${NUM_VERSIONS}" -ge "${#PATCH_DATES[@]}" ]]; then
echo "Only ${#PATCH_DATES[@]} patch versions available."
exit 1
fi
for (( c=0; c<${NUM_VERSIONS}; c++ ))
do
PATCH_ID=$(get_patch_id ${MANI_PATHS[${c}]})
PACKAGE_VERSION=$(get_package_version ${MANI_PATHS[${c}]})
echo "DEREGISTERING ${PATCH_ID}"
echo "aws panorama deregister-package-version --package-id ${PACKAGE_ID} --package-version ${PACKAGE_VERSION} --patch-version ${PATCH_ID}"
RESPONSE=$(aws panorama deregister-package-version --package-id ${PACKAGE_ID} --package-version ${PACKAGE_VERSION} --patch-version ${PATCH_ID} 2>&1)
if [[ ${RESPONSE} =~ "ResourceNotFoundException" ]]; then
echo "Patch not found, continuing"
else
if [[ ${RESPONSE} =~ "Exception" ]]; then
echo "${RESPONSE}"
exit 1
fi
fi
echo "SAVING COPY OF MANIFEST"
aws s3 cp s3://${BUCKET}/${MANI_PATHS[${c}]} .
echo "DELETING MANIFEST FROM AMAZON S3"
echo "aws s3api delete-object --bucket ${BUCKET} --key ${MANI_PATHS[${c}]}"
aws s3api delete-object --bucket ${BUCKET} --key ${MANI_PATHS[${c}]}
echo ""
done
echo "Package manifests deregistered and deleted. Find assets referenced by patch versions"
echo "in local copy of manifests. Assets might be used by other registered patch versions."
echo "To see all assets and remaining manifests in Amazon S3, run the following commands."
echo "aws s3 ls s3://${BUCKET}/${MANI_PREFIX}/${PACKAGE_VERSION}/"
echo "aws s3 ls s3://${BUCKET}/${BINARY_PREFIX}/"