-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcreate_distro_containers.sh
executable file
·373 lines (315 loc) · 12.2 KB
/
create_distro_containers.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/bash
set -e
# usage : ./create_distro_containers.sh [--skip-rebuilding-ezp] [ --target ezstudio ]
# --push : Pushes the created images to a repository
# --pushonly : Only pushes the created images to a repository. In order to make this work, you need to first run this script without the --push or --pushonly parameter
# --install-type [ type ]: Install type used when running installer. Typicall values for type is "clean" and "demo"
# --skip-rebuilding-ezp : Assumes ezpublish.tar.gz is already created and will not generate one using the fig_ezpinstall.sh script
# --skip-running-install-script : Skip running the installer ( php app/console ezplatform:install ... )
# --target ezstudio : Create ezstudio containers instead of ezplatform
export COMPOSE_PROJECT_NAME=ezpublishdocker
source files/distro_containers.config
MAINCOMPOSE="docker-compose.yml"
EZPINSTALLCOMPOSE=""
DATE=`date +%Y%m%d`
CONFIGFILE=""
PUSH="false"
PUSHONLY="false"
REBUILD_EZP="true"
INSTALL_TYPE="demo"
RUN_INSTALL_SCRIPT="true"
BUILD_TARGET="ezplatform" # Could be "ezplatform" or "ezstudio"
ONLYCLEANUP="false"
# Let's try to connect to db for 2 minutes ( 24 * 5 sec intervalls )
MAXTRY=24
function parse_commandline_arguments
{
# Based on http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash, comment by Shane Day answered Jul 1 '14 at 1:20
while [ -n "$1" ]; do
# Copy so we can modify it (can't modify $1)
OPT="$1"
# Detect argument termination
if [ x"$OPT" = x"--" ]; then
shift
for OPT ; do
REMAINS="$REMAINS \"$OPT\""
done
break
fi
# Parse current opt
while [ x"$OPT" != x"-" ] ; do
case "$OPT" in
# Handle --flag=value opts like this
# -c=* | --config=* )
# CONFIGFILE="${OPT#*=}"
# shift
# ;;
# # and --flag value opts like this
-c* | --config )
CONFIGFILE="$2"
shift
;;
-e* | --ezp54 )
EZPINSTALLCOMPOSE="-f docker-compose_ezpinstall_php5.yml"
MAINCOMPOSE="$MAINCOMPOSE -f docker-compose_php5.yml"
;;
-p* | --push )
PUSH="true"
;;
-p* | --pushonly )
PUSH="true"
PUSHONLY="true"
;;
-s* | --skip-rebuilding-ezp )
REBUILD_EZP="false"
;;
-i* | --skip-running-install-script )
RUN_INSTALL_SCRIPT="false"
;;
-s* | --install-type )
INSTALL_TYPE="$2"
shift
;;
-t* | --target )
BUILD_TARGET="$2"
shift
;;
-z* | --cleanup )
ONLYCLEANUP="true"
;;
# Anything unknown is recorded for later
* )
REMAINS="$REMAINS \"$OPT\""
break
;;
esac
# Check for multiple short options
# NOTICE: be sure to update this pattern to match valid options
NEXTOPT="${OPT#-[st]}" # try removing single short opt
if [ x"$OPT" != x"$NEXTOPT" ] ; then
OPT="-$NEXTOPT" # multiple short opts, keep going
else
break # long form, exit inner loop
fi
done
# Done with that param. move to next
shift
done
# Set the non-parameters back into the positional parameters ($1 $2 ..)
eval set -- $REMAINS
if [ "$BUILD_TARGET" != "ezstudio" ] && [ "$BUILD_TARGET" != "ezplatform" ] ; then
echo "Invalid target : $BUILD_TARGET"
exit
fi
echo "REBUILD_EZP=$REBUILD_EZP"
echo "BUILD_TARGET=$BUILD_TARGET"
}
function getAppFolder
{
APP_FOLDER="app"
if [ -d volumes/ezpublish/ezpublish ]; then
APP_FOLDER="ezpublish"
fi
echo "App folder found : $APP_FOLDER"
}
function prepare
{
${COMPOSE_EXECUTION_PATH}docker-compose -f $MAINCOMPOSE kill
${COMPOSE_EXECUTION_PATH}docker-compose -f $MAINCOMPOSE rm --force -v
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_distribution.yml kill
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_distribution.yml rm --force -v
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_vardir.yml kill
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_vardir.yml rm --force -v
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_databasedump.yml kill
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_databasedump.yml rm --force -v
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_ezpinstall.yml $EZPINSTALLCOMPOSE kill
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_ezpinstall.yml $EZPINSTALLCOMPOSE rm --force -v
docker rmi ${COMPOSE_PROJECT_NAME}_ezpinstall || /bin/true
docker rmi ${COMPOSE_PROJECT_NAME}_distribution:latest || /bin/true
docker rmi ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_distribution:${DOCKER_BUILDVER} || /bin/true
docker rmi ${COMPOSE_PROJECT_NAME}_vardir:latest || /bin/true
docker rmi ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_vardir:${DOCKER_BUILDVER} || /bin/true
docker rmi ${COMPOSE_PROJECT_NAME}_databasedump:latest || /bin/true
docker rmi ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_databasedump:${DOCKER_BUILDVER} || /bin/true
if [ $REBUILD_EZP == "true" ]; then
sudo rm -rf volumes/ezpublish volumes/mysql || /bin/true
mkdir volumes/ezpublish volumes/mysql
touch volumes/mysql/.keep
fi
rm -f dockerfiles/distribution/ezpublish.tar.gz
if [ $ONLYCLEANUP == "true" ]; then
echo exiting
exit
fi
}
function install_ezpublish
{
if [ $REBUILD_EZP == "true" ]; then
if [ "$CONFIGFILE" == "" ]; then
./docker-compose_ezpinstall.sh $EZPINSTALLCOMPOSE
else
./docker-compose_ezpinstall.sh $EZPINSTALLCOMPOSE -c $CONFIGFILE
fi
else
# Workaround since ezphp container is not defined in docker-compose.yml
YMLFILE="docker-compose_ezpinstall.yml"
if [ "$SYMFONY_ENV" = "dev" ]; then
YMLFILE="docker-compose_ezpinstall_dev.yml"
fi
${COMPOSE_EXECUTION_PATH}docker-compose -f $YMLFILE build
fi
}
function waitForDatabaseToGetUp
{
local DBUP
local TRY
DBUP=false
TRY=1
while [ $DBUP == "false" ]; do
echo "Checking if mysql is up yet, attempt :$TRY"
docker-compose -f docker-compose.yml run -u ez --rm phpfpm1 /bin/bash -c 'echo "show databases" | mysql -u$DB_ENV_MYSQL_USER -p$DB_ENV_MYSQL_PASSWORD $DB_ENV_MYSQL_DATABASE -h db > /dev/null' && DBUP="true"
let TRY=$TRY+1
if [ $TRY -eq $MAXTRY ]; then
echo Max limit reached. Not able to connect to mysql. Running installer will likely fail
DBUP="true"
else
sleep 5;
fi
done
}
function run_installscript
{
if [ $RUN_INSTALL_SCRIPT == "false" ]; then
return 0
fi
#Start service containers and wait some seconds for mysql to get running
#FIXME : can be removed now?
${COMPOSE_EXECUTION_PATH}docker-compose -f $MAINCOMPOSE up -d # We must call "up" before "run", or else volumes definitions in .yml will not be treated correctly ( will mount all volumes in vfs/ folder ) ( must be a docker-compose bug )
waitForDatabaseToGetUp
# sleep 12
if [ $REBUILD_EZP == "true" ]; then
${COMPOSE_EXECUTION_PATH}docker-compose -f $MAINCOMPOSE run -u ez --rm phpfpm1 /bin/bash -c "php $APP_FOLDER/console --env=prod ezplatform:install $INSTALL_TYPE && php $APP_FOLDER/console cache:clear --env=prod"
fi
}
function warm_cache
{
${COMPOSE_EXECUTION_PATH}docker-compose -f $MAINCOMPOSE run -u ez --rm phpfpm1 /bin/bash -c "php $APP_FOLDER/console cache:warmup --env=prod"
}
function create_distribution_tarball
{
sudo tar -czf dockerfiles/distribution/ezpublish.tar.gz --directory volumes/ezpublish --exclude "./$APP_FOLDER/cache/*" --exclude "./$APP_FOLDER/logs/*" --exclude './web/var/*' .
sudo chown `whoami`: dockerfiles/distribution/ezpublish.tar.gz
}
function create_distribution_container
{
docker-compose -f docker-compose_distribution.yml up -d
}
function tag_distribution_container
{
echo "Tagging image : ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_distribution:${DOCKER_BUILDVER}"
docker tag ${COMPOSE_PROJECT_NAME}_distribution:latest ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_distribution:${DOCKER_BUILDVER}
}
function push_distribution_container
{
if [ $PUSH == "true" ]; then
echo "Pushing image : ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_distribution:${DOCKER_BUILDVER}"
docker push ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_distribution:${DOCKER_BUILDVER}
fi
}
function create_vardir_tarball
{
sudo tar -czf dockerfiles/vardir/vardir.tar.gz --directory volumes/ezpublish/web var
sudo chown `whoami`: dockerfiles/vardir/vardir.tar.gz
}
function create_vardir_container
{
docker-compose -f docker-compose_vardir.yml up -d
}
function tag_vardir_container
{
echo "Tagging image : ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_vardir:${DOCKER_BUILDVER}"
docker tag ${COMPOSE_PROJECT_NAME}_vardir:latest ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_vardir:${DOCKER_BUILDVER}
}
function push_vardir_container
{
if [ $PUSH == "true" ]; then
echo "Pushing image : ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_vardir:${DOCKER_BUILDVER}"
docker push ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_vardir:${DOCKER_BUILDVER}
fi
}
function create_mysql_tarball
{
${COMPOSE_EXECUTION_PATH}docker-compose -f $MAINCOMPOSE run -u ez phpfpm1 /bin/bash -c "mysqldump -u ezp -p${MYSQL_PASSWORD} -h db ezp > /tmp/ezp.sql"
docker cp ${COMPOSE_PROJECT_NAME}_phpfpm1_run_1:/tmp/ezp.sql dockerfiles/databasedump
docker rm ${COMPOSE_PROJECT_NAME}_phpfpm1_run_1
}
function create_mysql_container
{
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_databasedump.yml up -d
}
function tag_mysql_container
{
echo "Tagging image : ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_databasedump:${DOCKER_BUILDVER}"
docker tag ${COMPOSE_PROJECT_NAME}_databasedump:latest ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_databasedump:${DOCKER_BUILDVER}
}
function push_mysql_container
{
if [ $PUSH == "true" ]; then
echo "Pushing image : ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_databasedump:${DOCKER_BUILDVER}"
docker push ${DOCKER_REPOSITORY}/${DOCKER_USER}/${BUILD_TARGET}_databasedump:${DOCKER_BUILDVER}
fi
}
function create_initialize_container
{
${COMPOSE_EXECUTION_PATH}docker-compose -f docker-compose_initialize.yml up -d
}
function pushonly
{
if [ "$PUSHONLY" == "true" ]; then
echo push_distribution_container
push_distribution_container
echo push_vardir_container
push_vardir_container
echo push_mysql_container
push_mysql_container
exit
fi
}
echo parse_commandline_arguments
parse_commandline_arguments "$@"
echo pushonly:
pushonly
echo Prepare:
prepare
echo install_ezpublish:
install_ezpublish
echo getAppFolder:
getAppFolder
echo run_installscript:
run_installscript
echo warm_cache:
warm_cache
echo create_distribution_tarball
create_distribution_tarball
echo create_distribution_container
create_distribution_container
echo tag_distribution_container
tag_distribution_container
echo push_distribution_container
push_distribution_container
echo create_vardir_tarball
create_vardir_tarball
echo create_vardir_container
create_vardir_container
echo tag_vardir_container
tag_vardir_container
echo push_vardir_container
push_vardir_container
echo create_mysql_tarball
create_mysql_tarball
echo create_mysql_container
create_mysql_container
echo tag_mysql_container
tag_mysql_container
echo push_mysql_container
push_mysql_container