-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmariadb-image-builder
More file actions
207 lines (175 loc) · 7.71 KB
/
mariadb-image-builder
File metadata and controls
207 lines (175 loc) · 7.71 KB
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
#!/bin/bash
function finalizeBuildStep() {
[ "$1" ] || return #total start time
[ "$2" ] || return #step start time
[ "$3" ] || return #previous step end time
[ "$4" ] || return #buildstep
totalStartTime=$(date -d "${1}" +%s)
startTime=$(date -d "${2}" +%s)
endTime=$(date -d "${3}" +%s)
timeZone=$(date +"%Z")
diffSeconds="$(($endTime-$startTime))"
diffTime=$(date -d @${diffSeconds} +"%H:%M:%S" -u)
diffTotalSeconds="$(($endTime-$totalStartTime))"
diffTotalTime=$(date -d @${diffTotalSeconds} +"%H:%M:%S" -u)
echo -e "##############################################\nSTEP ${4}: Completed at ${3} (${timeZone}) Duration ${diffTime} Elapsed ${diffTotalTime}\n##############################################"
}
buildStartTime="$(date +"%Y-%m-%d %H:%M:%S")"
echo "======================="
echo "Starting image-builder"
echo "======================="
echo
echo -e "##############################################\nBEGIN Variable setup\n##############################################"
echo "Current time: $(date +"%Y-%m-%d %H:%M:%S")"
echo
# source the variables required
IMAGE_BUILD_DATA=$(database-image-task dump)
if [ "$(echo "$IMAGE_BUILD_DATA" | jq -rc '.databaseType')" == "mysql" ]; then
ln -s mysql.Dockerfile Dockerfile
else
ln -s mariadb.Dockerfile Dockerfile
fi
DEBUG=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.debug')
# Set up the MTK variables
export MTK_HOSTNAME=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.mtk.host')
export MTK_DATABASE=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.mtk.database')
export MTK_USERNAME=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.mtk.username')
export MTK_PASSWORD=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.mtk.password')
BUILDER_BACKUP_IMAGE_NAME=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.resultImageName')
BUILDER_BACKUP_IMAGE_TAG=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.resultImageTag')
BUILDER_BACKUP_IMAGE_DATABASE_NAME=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.resultImageDatabaseName')
echo $BUILDER_BACKUP_IMAGE_NAME
# error out if registry username and password aren't provided
if [ -z $(echo "$IMAGE_BUILD_DATA" | jq -rc '.registryUsername') ]; then
echo "BUILDER_REGISTRY_USERNAME not defined"
exit 1
fi
if [ -z $(echo "$IMAGE_BUILD_DATA" | jq -rc '.registryPassword') ]; then
echo "BUILDER_REGISTRY_PASSWORD not defined"
exit 1
fi
##### Phase 1: Set up all the initial variables
# Generic variables
date=`date -I`
san_db_dump_filename="sanitised-dump.sql"
# set an additional tag value if not also provided
backup_image_tag=${BUILDER_BACKUP_IMAGE_TAG:-"backup-${date}"}
backup_image_full="${BUILDER_BACKUP_IMAGE_NAME}:${backup_image_tag}"
echo backup_image_full=$backup_image_full
echo BUILDER_BACKUP_IMAGE_NAME=$BUILDER_BACKUP_IMAGE_NAME
echo backup_image_tag=$backup_image_tag
##### Phase 2: MTK dump
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
finalizeBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "Variable setup"
previousStepEnd=${currentStepEnd}
echo -e "##############################################\nBEGIN Database dump\n##############################################"
echo "Current time: $(date +"%Y-%m-%d %H:%M:%S")"
echo
# dump the MTK YAML to the mtk file if it has been provided, otherwise mtk will just dump the entire database as is
if [ -n "$(echo "$IMAGE_BUILD_DATA" | jq -c '.mtkYAML')" ]; then
mtk_filename=mtk.yml
echo "$IMAGE_BUILD_DATA" | jq -rc '.mtkYAML | @base64d' > $mtk_filename
export MTK_CONFIG="$mtk_filename"
fi
# Run MTK here
if [ "$DEBUG" == "true" ]; then
echo
env | egrep "MTK|^DB_|${LAGOON_SERVICE_NAME}_" | sort
echo
set +o xtrace
fi
if [ "$(echo "$IMAGE_BUILD_DATA" | jq -c '.extendedInsertRows // false')" != "false" ]; then
export MTK_EXTENDED_INSERT_ROWS=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.extendedInsertRows')
fi
mtk-dump dump "$MTK_DATABASE" > "$san_db_dump_filename"
mtk_return_value=$?
if [ "$DEBUG" == "true" ]; then
set +o xtrace
fi
if [ "$mtk_return_value" -ne 0 ]; then
echo "Got errors running mtk-dump"
cat "$san_db_dump_filename"
exit $mtk_return_value
fi
##### Phase 3: Make container with sanitised DB
echo
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
finalizeBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "Database dump"
previousStepEnd=${currentStepEnd}
echo -e "##############################################\nBEGIN Make container with sanitised DB\n##############################################"
echo "Current time: $(date +"%Y-%m-%d %H:%M:%S")"
echo
## using docker-host in lagoon, perhaps use a different dockerhost for this
export DOCKER_HOST=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.dockerHost')
DOCKER_HOST_COUNTER=1
DOCKER_HOST_TIMEOUT=10
until docker -H ${DOCKER_HOST} info &> /dev/null
do
if [ $DOCKER_HOST_COUNTER -lt $DOCKER_HOST_TIMEOUT ]; then
let DOCKER_HOST_COUNTER=DOCKER_HOST_COUNTER+1
echo "${DOCKER_HOST} not available yet, waiting for 5 secs"
sleep 5
else
echo "could not connect to ${DOCKER_HOST}"
exit 1
fi
done
# BUILDER_IMAGE_NAME is the upstream mariadb/mysql as it has support for importing in a particular way
# CLEAN_IMAGE_NAME is the lagoon database image used to copy the imported database into
# BACKUP_IMAGE_NAME is the resulting built image to be tagged and pushed (eg quay.io/myproject/image)
# BACKUP_IMAGE_TAG is optional and will default to `backup-${date}`
# these have to be the same base `mariadb/mysql` version to work (ie mariadb:10.6 as the builder, and uselagoon/mariadb-10.6-drupal:latest as the clean resulting image)
# build the image, but exit on error
if [ "$DEBUG" == "true" ]; then
set -o xtrace
fi
set -o errexit
# template out the my.cnf file for the images
envsubst < my.cnf.tpl > my.cnf
envsubst < import.my.cnf.tpl > import.my.cnf
docker build --network=host --build-arg BUILDER_IMAGE="$(echo "$IMAGE_BUILD_DATA" | jq -rc '.sourceImage')" \
--build-arg CLEAN_IMAGE="$(echo "$IMAGE_BUILD_DATA" | jq -rc '.cleanImage')" \
-t ${backup_image_full} \
-t "${BUILDER_BACKUP_IMAGE_NAME}:latest" .
if [ "$DEBUG" == "true" ]; then
set +o xtrace
fi
set +o errexit
##### Phase 4: Save new container to registry
echo
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
finalizeBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "Make container with sanitised DB"
previousStepEnd=${currentStepEnd}
echo -e "##############################################\nBEGIN Save new container to registry\n##############################################"
echo "Current time: $(date +"%Y-%m-%d %H:%M:%S")"
echo
# Log in to dockerhub or other registry
# Reading credentials order is:
# - ${XDG_RUNTIME_DIR}/containers/auth.json (and writing)
# - $HOME/.docker/config.json
# BUILDER_REGISTRY_USERNAME is the name to log in to the registry
# BUILDER_REGISTRY_PASSWORD is the password of the user
# BUILDER_REGISTRY_HOST is required if not using dockerhub, eg: `quay.io`
set -o errexit
echo $(echo "$IMAGE_BUILD_DATA" | jq -rc '.registryPassword') | docker login $(echo "$IMAGE_BUILD_DATA" | jq -rc '.registryHost') -u "$(echo "$IMAGE_BUILD_DATA" | jq -rc '.registryUsername')" --password-stdin
set +o errexit
BUILDER_PUSH_TAGS=$(echo "$IMAGE_BUILD_DATA" | jq -rc '.pushTags')
# Push the image to remote
if [ "$BUILDER_PUSH_TAGS" == "both" ] || [ "$BUILDER_PUSH_TAGS" == "latest" ]; then
docker push "${BUILDER_BACKUP_IMAGE_NAME}:latest"
if [ "$BUILDER_REMOVE_IMAGE" != "skip" ]; then
docker rmi --force "${BUILDER_BACKUP_IMAGE_NAME}:latest"
fi
fi
if [ "$BUILDER_PUSH_TAGS" == "both" ] || [ "$BUILDER_PUSH_TAGS" == "default" ]; then
docker push "${backup_image_full}"
if [ "$BUILDER_REMOVE_IMAGE" != "skip" ]; then
docker rmi --force "${backup_image_full}"
fi
fi
echo
echo "========================"
echo "Finishing image-builder"
echo "========================"
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
finalizeBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "Save new container to registry"