-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclean-nightly.sh
executable file
·124 lines (114 loc) · 4.3 KB
/
clean-nightly.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash -e
################################################################################
# This program and the accompanying materials are made available under the terms of the
# Eclipse Public License v2.0 which accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-v20.html
#
# SPDX-License-Identifier: EPL-2.0
#
# Copyright IBM Corporation 2018, 2019
################################################################################
################################################################################
# This script cleans up nightly builds.
#
# Example: ./clean-nightly.sh -a artifactory-server repo/path/to/nightly/
################################################################################
################################################################################
# constants
SCRIPT_NAME=$(basename "$0")
SCRIPT_PWD=$(cd $(dirname "$0") && pwd)
RUN_PWD=$(pwd)
DEFAULT_ARTIFACTORY_SERVER=zowe-jack
DEFAULT_NIGHTLY_BUILD_FULL_PATH=libs-release-local/org/zowe/nightly/
NIGHTLY_BUILD_PATTERNS="zowe-1.*.pax zowe-smpe-1.*.tar zowe-smpe-1.*.zip zowe-containerization-1.*.zip server-bundle.amd64-1.*.tar cli/zowe-cli-1.*.zip v2/zowe-2.*.pax zowe-smpe-2.*.tar zowe-smpe-2.*.zip v3/zowe-3.*.pax zowe-smpe-3.*.tar zowe-smpe-3.*.zip"
NIGHTLY_BUILD_COUNT=30
################################################################################
# variables
ARTIFACTORY_SERVER=$DEFAULT_ARTIFACTORY_SERVER
NIGHTLY_BUILD_FULL_PATH=$DEFAULT_NIGHTLY_BUILD_FULL_PATH
# allow to exit by ctrl+c
function finish {
echo "[${SCRIPT_NAME}] interrupted"
exit 1
}
trap finish SIGINT
################################################################################
# parse parameters
function usage {
echo "Clean nightly builds on jFrog Artifactories."
echo
echo "Usage: $SCRIPT_NAME [OPTIONS] <path>"
echo
echo "Options:"
echo " -h Display this help message."
echo " -a Artifactory server id. Optional, default is $DEFAULT_ARTIFACTORY_SERVER."
echo
}
while getopts ":ha:" opt; do
case $opt in
h)
usage
exit 0
;;
a)
ARTIFACTORY_SERVER=$OPTARG
;;
\?)
echo "[${SCRIPT_NAME}][error] invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "[${SCRIPT_NAME}][error] invalid option argument: -$OPTARG requires an argument" >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
NIGHTLY_BUILD_FULL_PATH=$1
if [ -z "${NIGHTLY_BUILD_FULL_PATH}" ]; then
NIGHTLY_BUILD_FULL_PATH=$DEFAULT_NIGHTLY_BUILD_FULL_PATH
fi
NIGHTLY_BUILD_REPO=$(echo $NIGHTLY_BUILD_FULL_PATH | sed -e 's#^\([^/]\{1,\}\)/\(.\{1,\}\)#\1#')
NIGHTLY_BUILD_PATH=$(echo $NIGHTLY_BUILD_FULL_PATH | sed -e 's#^\([^/]\{1,\}\)/\(.\{1,\}\)#\2#')
################################################################################
# essential validations
if [ -z "$NIGHTLY_BUILD_FULL_PATH" ]; then
echo "[${SCRIPT_NAME}][error] path is required."
exit 1
fi
if [ -z "$NIGHTLY_BUILD_REPO" ]; then
echo "[${SCRIPT_NAME}][error] couldn't find repository from path."
exit 1
fi
if [ -z "$NIGHTLY_BUILD_PATH" ]; then
echo "[${SCRIPT_NAME}][error] couldn't find artifact path from path."
exit 1
fi
################################################################################
echo "[${SCRIPT_NAME}] Cleaning $NIGHTLY_BUILD_FULL_PATH on ${ARTIFACTORY_SERVER} ..."
echo
################################################################################
for pattern in $NIGHTLY_BUILD_PATTERNS; do
echo "[${SCRIPT_NAME}] Checking $NIGHTLY_BUILD_FULL_PATH$pattern ..."
ARTIFACTS="$(jfrog rt s --server-id "${ARTIFACTORY_SERVER}" --sort-by created "${NIGHTLY_BUILD_FULL_PATH}${pattern}" | jq -r '.[].path')"
echo -e "$ARTIFACTS"
echo
ARTIFACTS_COUNT=$(echo -e "$ARTIFACTS" | wc -l)
if [ $ARTIFACTS_COUNT -gt $NIGHTLY_BUILD_COUNT ]; then
SHOULD_DELETE_COUNT="$(($ARTIFACTS_COUNT - $NIGHTLY_BUILD_COUNT))"
echo "[${SCRIPT_NAME}] Will delete ${SHOULD_DELETE_COUNT} artifact(s) ..."
SHOULD_DELETE="$(echo -e "$ARTIFACTS" | head -n ${SHOULD_DELETE_COUNT})"
for one in $SHOULD_DELETE; do
echo "[${SCRIPT_NAME}] - ${one} ..."
jfrog rt del --server-id "${ARTIFACTORY_SERVER}" --quiet "${one}"
sleep 0.3
done
else
echo "[${SCRIPT_NAME}] no need to clean"
fi
echo
done
################################################################################
echo
echo "[${SCRIPT_NAME}] done."
exit 0