-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-init.sh
executable file
·296 lines (246 loc) · 10.4 KB
/
docker-init.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
#!/usr/bin/env bash
while getopts c:p:e:m:n:r: option
do
case "${option}"
in
c) WITH_CRON=${OPTARG};;
p) WITH_POSTGRES=${OPTARG};;
e) WITH_ES=${OPTARG};;
m) WITH_MYSQL=${OPTARG};;
n) PROJECT_NAME=${OPTARG};;
r) PORTRANGE_START=$OPTARG;;
esac
done
function read_input() {
echo ${WITH_CRON}
if [ "${WITH_CRON}" = "" ]; then
read -p 'Use cron (c) [y|N]: ' WITH_CRON
fi
if [ "${WITH_POSTGRES}" = "" ]; then
read -p 'Use postgres (p) [y|N]: ' WITH_POSTGRES
fi
if [ "${WITH_MYSQL}" = "" ]; then
read -p 'Use mysql (m) [Y|n]: ' WITH_MYSQL
fi
if [ "${WITH_ES}" = "" ]; then
read -p 'Use elastic search (e) [y|N]: ' WITH_ES
fi
if [ "${PROJECT_NAME}" = "" ]; then
read -p 'PROJECT_NAME (n) [my_nice_project]: ' PROJECT_NAME
fi
if [ "${PROJECT_NAME}" = "" ]; then
PROJECT_NAME=my_nice_project
fi
PROJECT_NAME_HYPHENIZED=${PROJECT_NAME//_/-}
echo ${PROJECT_NAME}
echo ${PROJECT_NAME_HYPHENIZED}
# calculate starting port number in steps of 100
RANGE=12080 # max port is at 65535
FLOOR=8100 # up to 1023 are privileged ports
number=0 #initialize
while [ "$number" -le $FLOOR ]
do
number=$RANDOM
let "number %= $RANGE" # Scales $number down within $RANGE.
mod=${number}
let "mod %= 100" # Scales $number down within steps of 100.
let "number -= $mod"
done
RANDOM_PORTRANGE_START=${number}
if [ "${PORTRANGE_START}" = "" ]; then
read -p "PORTRANGE_START (r) [${RANDOM_PORTRANGE_START}]: " PORTRANGE_START
fi
if [ "${PORTRANGE_START}" = "" ]; then
PORTRANGE_START=${RANDOM_PORTRANGE_START}
elif [ "${PORTRANGE_START}" -lt 1024 ]; then
echo "PORTRANGE_START have to be larger than 1023"
exit
elif [ "${PORTRANGE_START}" -gt 65510 ]; then
echo "PORTRANGE_START have to be lower than 65511"
exit
fi
# TODO ability to use SSL
#read -p 'Enable SSL [Y|n]: ' SSL_ENABLED
# TODO ensure all settings are correct
}
function replace_in_file() {
# exclude non existing files
if [ ! -f $1 ]; then
return
fi
TEMPLATE=$(<$1)
# maybe use awk for (inline) replacement
# escape this to get new lines
# in templates there is escaping of \ and $ with \ required
TEMPLATE="$(echo "${TEMPLATE}" | sed ':a;N;$!ba;s/\n/\\n/g' | sed 's/\$/\\$/g')"
sed 's|'"${3}"'|'"${TEMPLATE}"'|g' $2 > /tmp/Dockerfile
mv /tmp/Dockerfile $2
}
function remove_from_file () {
sed 's|'"${2}"'||g' $1 > /tmp/Dockerfile2
mv /tmp/Dockerfile2 $1
}
function add_yarn() {
echo add yarn
replace_in_file "templates/yarn/web/Dockerfile" "build/web/Dockerfile" "%%YARN%%"
}
function add_xdebug() {
echo add xdebug
replace_in_file "templates/xdebug/web/Dockerfile" "build/web/Dockerfile" "%%XDEBUG%%"
}
function add_panther() {
echo add panther
replace_in_file "templates/panther/web/Dockerfile" "build/web/Dockerfile" "%%PANTHER%%"
}
function handle_cron() {
if [ "${WITH_CRON}" = "y" ]; then
echo with cron
mkdir -p build/web/.docker/etc/cron.d/
cp templates/cron/web/.docker/etc/cron.d/* build/web/.docker/etc/cron.d/
replace_in_file "templates/cron/web/Dockerfile" "build/web/Dockerfile" "%%CRON%%"
replace_in_file "templates/cron/web/.docker/start-project.sh" "build/web/.docker/start-project.sh" "%%CRON%%"
else
echo no cron
remove_from_file "build/web/.docker/start-project.sh" "%%CRON%%"
remove_from_file "build/web/Dockerfile" "%%CRON%%"
fi
}
function handle_postgres() {
if [ "${WITH_POSTGRES}" = "y" ]; then
replace_in_file "templates/postgres/web/Dockerfile" "build/web/Dockerfile" "%%POSTGRES%%"
replace_in_file "templates/postgres/web/.env" "build/web/.env" "%%POSTGRES%%"
replace_in_file "templates/postgres/docker-compose.yml" "build/docker-compose.yml" "%%POSTGRES%%"
replace_in_file "templates/postgres/docker-compose.override.yml" "build/docker-compose.override.yml" "%%POSTGRES%%"
replace_in_file "templates/postgres/.env" "build/.env" "%%POSTGRES%%"
replace_in_file "templates/postgres/.env.live" "build/.env.live" "%%POSTGRES%%"
replace_in_file "templates/postgres/.env.stage" "build/.env.stage" "%%POSTGRES%%"
replace_in_file "templates/postgres/.env.test" "build/.env.test" "%%POSTGRES%%"
mkdir -p build/postgres/
cp templates/postgres/postgres/Dockerfile build/postgres/Dockerfile
DEPENDS_ON_POSTGRES="- postgres"
else
echo no postgres
remove_from_file "build/web/Dockerfile" "%%POSTGRES%%"
remove_from_file "build/web/.env" "%%POSTGRES%%"
remove_from_file "build/docker-compose.yml" "%%POSTGRES%%"
remove_from_file "build/docker-compose.override.yml" "%%POSTGRES%%"
remove_from_file "build/.env" "%%POSTGRES%%"
remove_from_file "build/.env.live" "%%POSTGRES%%"
remove_from_file "build/.env.stage" "%%POSTGRES%%"
remove_from_file "build/.env.test" "%%POSTGRES%%"
DEPENDS_ON_POSTGRES=""
fi
}
function handle_mysql() {
if [ "${WITH_MYSQL,,}" = "n" ]; then
echo no mysql
remove_from_file "build/web/Dockerfile" "%%MYSQL%%"
remove_from_file "build/web/.docker/start-project.sh" "%%MYSQL%%"
remove_from_file "build/web/.env" "%%MYSQL%%"
remove_from_file "build/docker-compose.yml" "%%MYSQL%%"
remove_from_file "build/docker-compose.override.yml" "%%MYSQL%%"
remove_from_file "build/.env" "%%MYSQL%%"
remove_from_file "build/.env.live" "%%MYSQL%%"
remove_from_file "build/.env.stage" "%%MYSQL%%"
remove_from_file "build/.env.test" "%%MYSQL%%"
DEPENDS_ON_MYSQL=""
else
echo with mysql
replace_in_file "templates/mysql/web/Dockerfile" "build/web/Dockerfile" "%%MYSQL%%"
replace_in_file "templates/mysql/web/.docker/start-project.sh" "build/web/.docker/start-project.sh" "%%MYSQL%%"
replace_in_file "templates/mysql/web/.env" "build/web/.env" "%%MYSQL%%"
replace_in_file "templates/mysql/docker-compose.yml" "build/docker-compose.yml" "%%MYSQL%%"
replace_in_file "templates/mysql/docker-compose.override.yml" "build/docker-compose.override.yml" "%%MYSQL%%"
replace_in_file "templates/mysql/.env" "build/.env" "%%MYSQL%%"
replace_in_file "templates/mysql/.env.live" "build/.env.live" "%%MYSQL%%"
replace_in_file "templates/mysql/.env.stage" "build/.env.stage" "%%MYSQL%%"
replace_in_file "templates/mysql/.env.test" "build/.env.test" "%%MYSQL%%"
DEPENDS_ON_MYSQL="- mysql"
fi
}
function handle_es() {
if [ "${WITH_ES}" = "y" ]; then
echo handling elastic search
replace_in_file "templates/elasticsearch/docker-compose.yml" "build/docker-compose.yml" "%%ELASTICSEARCH%%"
replace_in_file "templates/elasticsearch/docker-compose.override.yml" "build/docker-compose.override.yml" "%%ELASTICSEARCH%%"
replace_in_file "templates/elasticsearch/.env" "build/.env" "%%ELASTICSEARCH%%"
VOLUMES_ELASTICSEARCH="elasticsearch_data:"
DEPENDS_ON_ELASTICSEARCH="- elasticsearch"
else
echo no es - no handling yet
remove_from_file "build/docker-compose.yml" "%%ELASTICSEARCH%%"
remove_from_file "build/docker-compose.override.yml" "%%ELASTICSEARCH%%"
remove_from_file "build/.env" "%%ELASTICSEARCH%%"
DEPENDS_ON_ELASTICSEARCH=""
fi
}
function prepare_build_dir() {
mkdir -p build
rm -rf build/
# copy base templates
cp --recursive templates/base/. build/
}
function set_ports_vars() {
OUTER_WEB_PORT=$((PORTRANGE_START+0))
OUTER_MYSQL_PORT=$((PORTRANGE_START+1))
OUTER_POSTGRES_PORT=$((PORTRANGE_START+2))
OUTER_ELASTICSEARCH_PORT=$((PORTRANGE_START+3))
OUTER_WEBPACK_PORT=$((PORTRANGE_START+4))
OUTER_WEB_PORT_TEST=$((PORTRANGE_START+10))
OUTER_WEB_PORT_STAGE=$((PORTRANGE_START+11))
OUTER_WEB_PORT_LIVE=$((PORTRANGE_START+12))
}
function replace_word_in_file() {
sed 's|'"${3}"'|'"${1}"'|g' $2 > /tmp/Dockerfile_3
mv /tmp/Dockerfile_3 $2
}
function replace_project_names() {
find build -iname "*" -type f | \
while read I; do
replace_word_in_file "${PROJECT_NAME}" "$I" "%%PROJECT_NAME%%"
replace_word_in_file "${PROJECT_NAME_HYPHENIZED}" "$I" "%%PROJECT_NAME_HYPHENIZED%%"
replace_word_in_file "${OUTER_WEB_PORT}" "$I" "%%OUTER_WEB_PORT%%"
replace_word_in_file "${OUTER_MYSQL_PORT}" "$I" "%%OUTER_MYSQL_PORT%%"
replace_word_in_file "${OUTER_POSTGRES_PORT}" "$I" "%%OUTER_POSTGRES_PORT%%"
replace_word_in_file "${OUTER_ELASTICSEARCH_PORT}" "$I" "%%OUTER_ELASTICSEARCH_PORT%%"
replace_word_in_file "${OUTER_WEBPACK_PORT}" "$I" "%%OUTER_WEBPACK_PORT%%"
replace_word_in_file "${OUTER_WEB_PORT_TEST}" "$I" "%%OUTER_WEB_PORT_TEST%%"
replace_word_in_file "${OUTER_WEB_PORT_STAGE}" "$I" "%%OUTER_WEB_PORT_STAGE%%"
replace_word_in_file "${OUTER_WEB_PORT_LIVE}" "$I" "%%OUTER_WEB_PORT_LIVE%%"
replace_word_in_file "${DEPENDS_ON_MYSQL}" "$I" "%%DEPENDS_ON_MYSQL%%"
replace_word_in_file "${DEPENDS_ON_POSTGRES}" "$I" "%%DEPENDS_ON_POSTGRES%%"
replace_word_in_file "${DEPENDS_ON_ELASTICSEARCH}" "$I" "%%DEPENDS_ON_ELASTICSEARCH%%"
replace_word_in_file "${VOLUMES_ELASTICSEARCH}" "$I" "%%VOLUMES_ELASTICSEARCH%%"
if [ "${VOLUMES_ELASTICSEARCH}" = "" ]; then
replace_word_in_file "" "$I" "%%VOLUMES%%"
else
replace_word_in_file "volumes:" "$I" "%%VOLUMES%%"
fi
done
}
function execute_docker_compose() {
cd build
docker-compose --file docker-compose.yml --file docker-compose.override.yml config
# cp .env.stage .env
# docker-compose --file docker-compose.stage.yml --file docker-compose.yml config
# docker-compose --file docker-compose.yml --file docker-compose.override.yml up --build -d --remove-orphans
# docker-compose --file docker-compose.yml --file docker-compose.override.yml exec web tail -f /var/log/cron.log
# docker build --file build/Dockerfile .
}
function main() {
prepare_build_dir
read_input
set_ports_vars
add_yarn
add_xdebug
add_panther
handle_cron
handle_postgres
handle_mysql
handle_es
replace_project_names
execute_docker_compose
# TODO write next steps e.g. edit crontab
exit 0
}
main