forked from jbraeuer/nexus-oss-rpms
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathci
executable file
·204 lines (179 loc) · 6.31 KB
/
ci
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
# Language (force it so getopt messages are always in english, as the script)
LANG=en_EN
# Get script name
SCRIPT=$(basename ${0})
# Supported distributions
SUPPORTEDDISTROS='almalinux8 amazonlinux2 opensuse15.6'
# Supported Sonatype Nexus Repository major versions
NEXUSMAJORVERS='2 3'
# Allocate tty by default
TTY='-t'
# Default values to for nexus healt tests
MAXRETRIES=20
TIME_WAIT=10
# Use podman by default as container engine
CENGINE='podman'
# Default registry
REGISTRY='docker.io'
# Default registry userspace, not configurable for now
NAMESPACE='juliogonzalez'
print_info() {
echo -e "\033[1;36m[INFO] ${1}\033[0m"
}
print_error() {
echo -e "\033[1;31m[ERROR] ${1}\033[0m"
}
print_ok() {
echo -e "\033[1;32m[INFO] ${1}\033[0m"
}
print_incorrect_syntax() {
print_error "Incorrect syntax. Use ${SCRIPT} -h for help"
}
print_error_unsupported_distro() {
print_error "Unsupported distro. Use ${SCRIPT} -h for help"
}
print_error_unsupported_nexus_major_ver() {
print_error "Unsupported Sonatype Nexus Repository major version. Use ${SCRIPT} -h for help"
}
print_help() {
echo ""
echo "Script to perform nexus-oss-rpms CI"
echo ""
echo "Syntax: "
echo ""
echo "${SCRIPT} <ARGUMENTS>"
echo ""
echo "Mandatory arguments:"
echo ""
echo " --distro=<$(echo ${1}|tr ' ' '|')>"
echo " --nexus-major-ver=<$(echo ${2}|tr ' ' '|')>"
echo ""
echo "Optional arguments:"
echo ""
echo " --docker If present, docker will be used instead of podman"
echo " --registry=<registry> Specify an image registry. If absent, docker.io"
echo " will be used by default"
echo " --max-retries=<RETRIES> Number of retries to check Sonatype Nexus"
echo " Repository health. By default 20"
echo " --time-wait=<SECONDS> Time to wait between retries (by default 10)"
echo " --name=<CONTAINER_NAME> Define the container name"
echo " If undefined, container name will be"
echo " s3fs-fuse-rpm-<DISTRO>-<TIMESTAMP>"
echo " --remove-on-error If present, remove the container on errors"
echo " --notty If present, does not allocate a tty for docker"
echo ""
}
remove_container() {
${CENGINE} container rm -f ${1}
}
exit_error() {
if [ ${1} -ne 0 ]; then
print_error "An error happened! Check log!"
if [ ! -z ${REMOVE_ON_ERROR} ]; then
remove_container ${CONTAINER_NAME}
fi
exit 1
fi
}
container_run() {
if [ ! -z ${3} ]; then
local COMMAND_USER="-u ${3}"
fi
local COMMAND="${CENGINE} container exec -i ${TTY} ${COMMAND_USER} ${1} ${2}"
local RESULT=$(${COMMAND})
exit_error ${?}
if [ "${RESULT}" != "" ]; then
echo "${RESULT}"
fi
}
# read the options
ARGS=$(getopt -o h --long help,remove-on-error,notty,distro:,nexus-major-ver:,docker,registry:,max-retries:,time-wait:,name: -n "${SCRIPT}" -- "$@")
if [ $? -ne 0 ];
then
print_incorrect_syntax
exit 1
fi
eval set -- "${ARGS}"
# extract options and their arguments into variables
while true ; do
case "${1}" in
-h|--help) print_help "${SUPPORTEDDISTROS}" "${NEXUSMAJORVERS}"; exit 1;;
--remove-on-error) REMOVE_ON_ERROR="--rm"; shift 1 ;;
--notty) TTY=""; shift 1 ;;
--distro) DISTRO="${2}"; shift 2;;
--nexus-major-ver) NEXUSMAJORVER="${2}"; shift 2;;
--docker) CENGINE='docker'; shift 1;;
--registry) REGISTRY="${2}"; shift 2;;
--max-retries) MAXRETRIES="${2}"; shift 2;;
--time-wait) TIME_WAIT="${2}"; shift 2;;
--name) CONTAINER_NAME="${2}"; shift 2;;
--) shift ; break ;;
*) print_incorrect_syntax; exit 1;;
esac
done
# Check nexus major version
case "${NEXUSMAJORVER}" in
2) PACKAGE_NAME='nexus'
NEXUS_URL='http://localhost:8081/nexus/'
JAVA="1.8";;
3) PACKAGE_NAME='nexus3'
NEXUS_URL='http://localhost:8081/'
JAVA="17";;
*) print_error_unsupported_nexus_major_ver
exit 1;;
esac
# Check distribution
case "${DISTRO}" in
almalinux8) IMAGE="${REGISTRY}/${NAMESPACE}/almalinux8-nexus:latest";;
amazonlinux2) IMAGE="${REGISTRY}/${NAMESPACE}/amazonlinux2-nexus:latest";;
opensuse15.6) IMAGE="${REGISTRY}/${NAMESPACE}/opensuse15.6-nexus:latest";;
*) print_error_unsupported_distro
exit 1;;
esac
if [ "${CENGINE}" == "podman" ]; then
# --security-opt label=disable due to https://github.com/containers/podman/issues/3683
PRIVILEGED="${PRIVILEGED} --userns=keep-id --security-opt label=disable"
else
PRIVILEGED='--privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro'
fi
# Check name
if [ -z ${CONTAINER_NAME} ]; then
CONTAINER_NAME="nexus-oss-rpm-${DISTRO}-$(date +'%s')"
fi
print_info "Pulling latest image..."
${CENGINE} pull ${IMAGE}
print_info "Starting container ${CONTAINER_NAME}..."
${CENGINE} container run -i ${TTY} ${REMOVE_ON_ERROR} --name "${CONTAINER_NAME}" ${PRIVILEGED} -v ${PWD}:/tmp/nexus-oss-rpms -w /tmp/nexus-oss-rpms -d ${IMAGE}
print_info "Cleaning up"
container_run "${CONTAINER_NAME}" "./clean"
print_info "Use the right Java version (${JAVA} for Nexus ${NEXUSMAJORVER})..."
container_run "${CONTAINER_NAME}" "/opt/pick-java-alternative.sh ${JAVA}"
print_info "Building nexus-oss package..."
container_run "${CONTAINER_NAME}" "./nexus-oss-rpm -v ${NEXUSMAJORVER}" "ci"
print_info "Installing nexus-oss package..."
container_run "${CONTAINER_NAME}" "/bin/rpm -i RPMS/$HOSTTYPE/${PACKAGE_NAME}-*.*.$HOSTTYPE.rpm"
print_info "Starting nexus..."
container_run "${CONTAINER_NAME}" "/usr/bin/systemctl start ${PACKAGE_NAME}"
print_info "Checking application (${MAXRETRIES} retries, ${TIME_WAIT} seconds between retries)..."
RETRIES=0
while [ ${RETRIES} -lt ${MAXRETRIES} ]; do
HTTP_CODE=$(container_run "${CONTAINER_NAME}" "curl -s -o /dev/null -w %{http_code} ${NEXUS_URL}")
print_info "Status: ${HTTP_CODE}"
if [ "${HTTP_CODE}" == "200" ]; then
break
fi
((RETRIES+=1))
sleep ${TIME_WAIT}
done
if [ "${HTTP_CODE}" == "200" ]; then
print_info "Sonatype Nexus Repository is healty"
else
print_error "Could not verify application after ${MAXRETRIES} retries. Last HTTP error was ${HTTP_CODE}!"
exit_error 1
fi
print_info "Testing RPM removal..."
container_run "${CONTAINER_NAME}" "/bin/rpm -e ${PACKAGE_NAME}"
print_info "Removing container..."
remove_container ${CONTAINER_NAME}
print_ok "Everything is OK"