-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmariadb-image-builder
156 lines (126 loc) · 5.17 KB
/
mariadb-image-builder
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
#!/bin/bash
echo "======================="
echo "Starting image-builder"
echo "======================="
echo
echo "=== Phase 1: variable setup ==="
# source the variables required
IMAGE_BUILD_DATA=$(database-image-task dump)
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
echo
echo "=== Phase 2: MTK dump ==="
# 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
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
echo "=== Phase 3: Make container with sanitised DB ==="
## 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 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` 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
ln -s mariadb.Dockerfile Dockerfile
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
echo "=== Phase 4: Save new container to registry ==="
# 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`
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
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"
fi
if [ "$BUILDER_PUSH_TAGS" == "both" ] || [ "$BUILDER_PUSH_TAGS" == "default" ]; then
docker push "${backup_image_full}"
fi
echo
echo "========================"
echo "Finishing image-builder"
echo "========================"