-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
cibuild_docker.sh
executable file
·334 lines (277 loc) · 8.22 KB
/
cibuild_docker.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/bin/bash
## BASH COLORS
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
## DEFAULT VOLUME EXPORT from original Solr Dockerfile:
# see Dockerfile in desired version https://github.com/docker-solr/docker-solr/blob/abb53a7/8.5/Dockerfile
DEFAULT_IMAGE_VOLUME_EXPORT_PATH="/var/solr"
## Local docker things.
LOCAL_VOLUME_PATH=${HOME}"/solrcivolume"
LOCAL_VOLUME_NAME="solrci-volume"
LOCAL_IMAGE_NAME="solrci-image:latest"
LOCAL_CONTAINER_NAME="solrci-container"
## In Schema configuration available solr cores
AVAILABLE_CORES=(
"core_de"
"core_en"
"core_ar"
"core_hy"
"core_eu"
"core_ptbr"
"core_my"
"core_ca"
"core_zh"
"core_cs"
"core_da"
"core_nl"
"core_fi"
"core_fr"
"core_gl"
"core_el"
"core_hi"
"core_hu"
"core_id"
"core_it"
"core_ja"
"core_km"
"core_ko"
"core_lo"
"core_no"
"core_fa"
"core_pl"
"core_pt"
"core_ro"
"core_ru"
"core_es"
"core_sv"
"core_th"
"core_tr"
"core_uk"
"core_rs"
"core_ie"
"core_lv"
)
prettyPrintOrExitOnError ()
{
local output=${2}
# shellcheck disable=SC2015
[ "${1}" -eq 0 ] && echo -en "${GREEN}"' ✔\n'"${NC}" || { echo -en "${RED}"' ✘\n'"${NC}" "${output[@]}"; exit 1; }
}
assertDockerVersionIsGtOrEq193 ()
{
echo -n "Check Docker version is >= 19.03"
local DOCKER_VERSION
DOCKER_VERSION=$(docker version -f "{{.Server.Version}}")
local DOCKER_VERSION_MAJOR
DOCKER_VERSION_MAJOR=$(echo "$DOCKER_VERSION"| cut -d'.' -f 1)
local DOCKER_VERSION_MINOR
DOCKER_VERSION_MINOR=$(echo "$DOCKER_VERSION"| cut -d'.' -f 2)
if { [ "${DOCKER_VERSION_MAJOR}" -eq 19 ] && [ "${DOCKER_VERSION_MINOR}" -ge 3 ]; } || [ "${DOCKER_VERSION_MAJOR}" -gt 19 ]; then
prettyPrintOrExitOnError 0
else
echo -en "${RED}"' ✘\n'"${NC}"
echo -e "${RED}"'Docker version less than 19.0.3 can not continue'"${NC}"
exit 1
fi
}
isHTTP200 ()
{
response=$(curl --write-out %\{http_code\} --silent --output /dev/null "${1}")
if [ "$response" -eq "200" ] ; then
return 0
fi
return 1
}
isPathOwnedBySolr ()
{
local status=0
for path in "$@"
do
pathOwner=$(sudo stat -c '%u' "$path")
# shellcheck disable=SC2015
[ "$pathOwner" == 8983 ] && echo -e ' '"${GREEN}"'✔'"${NC}" "$path" || { echo -e ' '"${RED}"'✘'"${NC}" "$path"; status=1; }
done
return $status;
}
run_container ()
{
echo -n "Creating testvolume"
prettyPrintOrExitOnError $? "$(mkdir -p "$LOCAL_VOLUME_PATH" 2>&1)"
echo -n "Add permissions to solr group"
prettyPrintOrExitOnError $? "$(sudo chmod g+w "$LOCAL_VOLUME_PATH" 2>&1)"
echo -n "Changing group of volume to solr user"
prettyPrintOrExitOnError $? "$(sudo chown 8983:8983 "$LOCAL_VOLUME_PATH" 2>&1)"
echo -n "Create named volume inside of ~/solrcivolume"
prettyPrintOrExitOnError $? "$(docker volume create --name "$LOCAL_VOLUME_NAME" --opt type=none --opt device="$LOCAL_VOLUME_PATH" --opt o=bind 2>&1)"
echo -n "Starting container"
prettyPrintOrExitOnError $? "$(docker run --name="$LOCAL_CONTAINER_NAME" -d -p 127.0.0.1:8998:8983 -v "$LOCAL_VOLUME_NAME":"$DEFAULT_IMAGE_VOLUME_EXPORT_PATH" "$LOCAL_IMAGE_NAME" 2>&1)"
}
cleanUp ()
{
echo "Clean up the artifacts"
echo -n " stop container $LOCAL_CONTAINER_NAME"
prettyPrintOrExitOnError $? "$(docker stop "$LOCAL_CONTAINER_NAME" 2>&1)"
echo -n " remove container $LOCAL_CONTAINER_NAME"
prettyPrintOrExitOnError $? "$(docker container rm "$LOCAL_CONTAINER_NAME" 2>&1)"
echo -n " remove volume $LOCAL_VOLUME_NAME"
prettyPrintOrExitOnError $? "$(docker volume rm "$LOCAL_VOLUME_NAME" 2>&1)"
echo -n " remove image $LOCAL_IMAGE_NAME"
prettyPrintOrExitOnError $? "$(docker image rm "$LOCAL_IMAGE_NAME" 2>&1)"
echo -n " remove \"$LOCAL_VOLUME_PATH\" directory"
prettyPrintOrExitOnError $? "$(sudo rm -Rf "$LOCAL_VOLUME_PATH" 2>&1)"
# clean stdout
echo
}
isCoreAvailable ()
{
isHTTP200 "http://localhost:8998/solr/${1}/select" || { return 1; }
isHTTP200 "http://localhost:8998/solr/${1}/mlt?q=*" || { return 1; }
return 0;
}
isCoreUnavailable ()
{
if ! isCoreAvailable $1; then
return 0;
fi
return 1;
}
pingCore ()
{
# shellcheck disable=SC2015
isHTTP200 "http://localhost:8998/solr/${1}/admin/ping" && { return 0; } || { return 1; }
}
getExpandedListOfPathsAsSudo ()
{
if [[ $EUID -ne 0 ]] ; then
echo "Function is unusable as non root user, please call function as root."
return 1
fi
local paths=(
"${1}"/data
"${1}"/data/data
"${1}"/data/configsets/ext_solr_*/conf/
"${1}"/data/configsets/*/conf/_schema_analysis*.json
)
echo "${paths[@]}"
}
assertVolumeExportHasNotBeenChanged ()
{
echo -n "Check Dockerfile's VOLUME definition has not been changed."
local EXPORTED_VOLUME
EXPORTED_VOLUME=$(docker image inspect --format='{{ range $a, $b := .Config.Volumes }}{{ printf "%s " $a }}{{end}}' $LOCAL_IMAGE_NAME)
if [[ "$EXPORTED_VOLUME" == "$DEFAULT_IMAGE_VOLUME_EXPORT_PATH " ]]; then
prettyPrintOrExitOnError 0;
else
prettyPrintOrExitOnError 1 "${RED}"'\n The VOLUME definition of image has been changed to "'"$EXPORTED_VOLUME"'".\n\n"'"${NC}"
fi
}
assertDataPathIsCreatedByApacheSolr ()
{
local DATA_PATH
DATA_PATH="$LOCAL_VOLUME_PATH""/data/data"
echo -en "\nWaiting for data directory: ""$DATA_PATH"
while true ; do
((iteration++))
# wait 10 seconds(80 times 0.125s)
if [[ $iteration -gt 80 ]] ; then
echo -ne "${RED}"'\nTimeout by awaiting of data directory.\nApache Solr would normally have to do this.\n\n'"${NC}"
cleanUp
exit 1;
fi
if sudo test -d "$DATA_PATH" ; then
prettyPrintOrExitOnError 0;
return 0
fi
sleep 0.125
done
}
assertAllCoresAreUp ()
{
echo -e "\nWaiting for cores:"
local cores=("${AVAILABLE_CORES[@]}")
local iteration=0
while true ; do
((iteration++))
if [[ $iteration -gt 30 ]] ; then
echo -ne "${RED}"'\nTimeout by pinging the cores.\n\n'"${NC}"
cleanUp
exit 1;
fi
for key in "${!cores[@]}" ; do
if ! pingCore "${cores[$key]}";
then
echo -en " ""${cores[$key]}"
unset 'cores[key]'
prettyPrintOrExitOnError 0
fi
done
if [ "${#cores[@]}" -eq 0 ] ; then
return 0
fi
sleep 1
done
}
assertAllCoresAreQueriable ()
{
echo -e "\nCheck all cores are queriable:"
for core in "${AVAILABLE_CORES[@]}"
do
echo -n " $core"
prettyPrintOrExitOnError $? "$(isCoreAvailable "$core" 2>&1)"
done
}
assertNecessaryPathsAreOwnedBySolr ()
{
echo -e "\nCheck paths are owned by solr(8983):"
local paths
# shellcheck disable=SC2207
paths=($(sudo /bin/bash -c "$(declare -f getExpandedListOfPathsAsSudo); getExpandedListOfPathsAsSudo $LOCAL_VOLUME_PATH"))
isPathOwnedBySolr "${paths[@]}" || { echo -e "${RED}"'\nThe image has files, which are not owned by solr(8983) user.\n Please fix this issue.'"${NC}"; cleanUp; exit 1; }
}
assertCoresAreSwitchableViaEnvVar ()
{
echo -e "\nCheck all cores are disabled except desired by \$TYPO3_SOLR_ENABLED_CORES env:"
echo -n " stop container $LOCAL_CONTAINER_NAME" prettyPrintOrExitOnError $? "$(docker stop "$LOCAL_CONTAINER_NAME" 2>&1)"
echo -n "Starting container"
prettyPrintOrExitOnError $? "$(docker run --env TYPO3_SOLR_ENABLED_CORES='german english danish' --name="$LOCAL_CONTAINER_NAME" -d -p 127.0.0.1:8998:8983 -v "$LOCAL_VOLUME_NAME":"$DEFAULT_IMAGE_VOLUME_EXPORT_PATH" "$LOCAL_IMAGE_NAME" 2>&1)"
ENABLED_CORES=(
"core_de"
"core_en"
"core_da"
)
SOME_DISABLED_CORES=(
"core_fi"
"core_fr"
"core_gl"
"core_el"
"core_hi"
"core_hu"
"core_id"
"core_it"
"core_ja"
)
echo -e "\nCheck enabled cores are available:"
for core in "${ENABLED_CORES[@]}"
do
echo -n " $core is enabled"
prettyPrintOrExitOnError $? "$(isCoreAvailable "$core" 2>&1)"
done
echo -e "\nCheck few other cores are really disabled:"
for core in "${SOME_DISABLED_CORES[@]}"
do
echo -n " $core is disabled"
prettyPrintOrExitOnError $? "$(isCoreUnavailable "$core" 2>&1)"
done
}
### run the tests
assertDockerVersionIsGtOrEq193
assertVolumeExportHasNotBeenChanged
run_container
assertAllCoresAreUp
assertAllCoresAreQueriable
assertDataPathIsCreatedByApacheSolr
assertNecessaryPathsAreOwnedBySolr
assertCoresAreSwitchableViaEnvVar
echo -e "${GREEN}"'\nAll checks passed successfully!\n'"${NC}"
exit 0