forked from kubernetes/perf-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-up-old-snapshots.sh
executable file
·97 lines (85 loc) · 2.57 KB
/
clean-up-old-snapshots.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
#!/usr/bin/env bash
# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
print-usage-and-exit() {
>&2 echo "Usage:"
>&2 echo " $0 delete|list <comma-sep-projects> <comma-sep-prefixes> <days-old>"
>&2 echo "Example:"
>&2 echo " $0 list k8s-e2e-gce-1-1,k8s-e2e-gce-1-2 ci-e2e-scalability,ci-e2e-kubemark 30"
exit $1
}
list-old-snapshots() {
project=$1
regexp=$2
days_old=$3
(
set -o xtrace
gcloud compute snapshots list \
--project "$project" \
--filter="creationTimestamp < -P${days_old}D AND name~\"${regexp}\"" \
--format="value(name)"
)
}
process-old-snapshots() {
command=$1
project=$2
regexp=$3
days_old=$4
for snapshot in $(list-old-snapshots "$project" "$regexp" "$days_old"); do
if [[ "$command" == "delete" ]]; then
echo "Removing $snapshot ..."
gcloud compute snapshots delete -q --project "$project" "$snapshot"
else
echo "Found: $snapshot"
fi
done
}
main() {
if [ "$#" -ne 4 ]; then
>&2 echo "Wrong number of parameters, expected 4, got $#"
print-usage-and-exit 1
fi
command=$1
projects=$2
prefixes=$3
days_old=$4
if ! [[ "$command" =~ ^(list|delete)$ ]]; then
>&2 echo "Invalid command: $command"
print-usage-and-exit 2
fi
if ! [[ "$projects" =~ ^[a-z0-9,-]+$ ]]; then
>&2 echo "Illegal characters in projects parameter: $projects"
print-usage-and-exit 3
fi
if ! [[ "$prefixes" =~ ^[a-z0-9,.-]+$ ]]; then
>&2 echo "Illegal characters in prefixes parameter: $prefixes"
print-usage-and-exit 4
fi
if ! [[ "$days_old" =~ ^[0-9]+$ ]]; then
>&2 echo "Days-old parameter must be an integer: $days_old"
print-usage-and-exit 5
fi
prefixes_alt="${prefixes//,/|}"
regexp="^(${prefixes_alt}).*-[0-9]{19}$"
echo "Looking for snapshots matched by $regexp"
IFS=',' read -ra projects_arr <<< "$projects"
for project in "${projects_arr[@]}"; do
echo "Processing project $project"
process-old-snapshots "$command" "$project" "$regexp" "$days_old"
done
}
main $@