-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauto-config-docker-compose.sh
More file actions
executable file
·329 lines (300 loc) · 14.4 KB
/
auto-config-docker-compose.sh
File metadata and controls
executable file
·329 lines (300 loc) · 14.4 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
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
#!/bin/bash -x
################################ Usage #######################################
## ---------- ##
## -- main -- ##
## ---------- ##
#set -e
DEBUG=0
function debug() {
if [ $DEBUG -gt 0 ]; then
echo $*
fi
}
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
PROJ_DIR=$(dirname $DIR)
cd ${PROJ_DIR}
bin/auto-config-with-template.sh $@ docker-compose.yml.template
###################################################
#### ---- Detect Docker Run Env files ----
###################################################
## -- (this script will include ./.env only if "./docker-run.env" not found
DOCKER_ENV_FILE="${PROJ_DIR}/.env"
function detectDockerRunEnvFile() {
curr_dir=`pwd`
if [ -s "./.env" ]; then
echo "--- INFO: ./.env FOUND to use as Docker Run Environment file!"
DOCKER_ENV_FILE="./.env"
else
echo "--- INFO: ./.env Docker Environment file (.env) NOT found!"
if [ -s "./docker.env" ]; then
echo "--- INFO: ./docker.env FOUND to use as Docker Run Environment file!"
DOCKER_ENV_FILE="./docker.env"
else
echo "*** WARNING: Docker Environment file (.env) or (docker.env) NOT found!"
fi
fi
}
detectDockerRunEnvFile
###################################################
#### ---- Function: Find Host IP ----
#### (Don't change!)
###################################################
SED_MAC_FIX="''"
function find_host_ip() {
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# ...
HOST_IP=`ip route get 1|grep via | awk '{print $7}'`
SED_MAC_FIX=
echo ${HOST_IP}
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OSX
HOST_IP=`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | grep -Fv 192.168 | awk '{print $2}'`
CP_OPTION=
echo ${HOST_IP}
fi
}
find_host_ip
#######################################################
#### ---- Function: docker-compose: port mappings ----
#### (Don't change!)
#######################################################
DOCKER_COMPOSE_FILE="${PROJ_DIR}/docker-compose.yml"
#{{PORTS_MAPPING}}
PORTS_MAPPING=""
prefix_yaml="\n - "
function generatePortMapping_dockercompose() {
if [ "$PORTS" == "" ]; then
## -- If locally defined in this file, then respect that first.
## -- Otherwise, go lookup the ${DOCKER_ENV_FILE} as ride-along source for volume definitions
PORTS_LIST=`cat ${DOCKER_ENV_FILE}|grep "^#PORTS_LIST= *"|sed "s/[#\"]//g"|cut -d'=' -f2-`
fi
for pp in ${PORTS_LIST}; do
#echo "$pp"
port_pair=`echo $pp | tr -d ' ' `
if [ ! "$port_pair" == "" ]; then
# -p ${local_dockerPort1}:${dockerPort1}
host_port=`echo $port_pair | tr -d ' ' | cut -d':' -f1`
docker_port=`echo $port_pair | tr -d ' ' | cut -d':' -f2`
PORTS_MAPPING="${PORTS_MAPPING}${prefix_yaml}\"${host_port}:${docker_port}\""
fi
done
if [ "${PORTS_MAPPING}" != "" ]; then
PORTS_MAPPING=" ports:${PORTS_MAPPING}\n"
fi
}
#### ---- Generate Port Mapping ----
generatePortMapping_dockercompose
echo -e "PORT_MAP=\n${PORTS_MAPPING}"
sed -i ${SED_MAC_FIX} "s%^.*\#{{PORTS_MAPPING}}%$PORTS_MAPPING%g" ${DOCKER_COMPOSE_FILE}
###########################################################
#### ---- Function: docker-compose: volumes mappings ----
#### (Don't change!)
###########################################################
function cutomizedVolume() {
DATA_VOLUME=$1
if [ "`echo $DATA_VOLUME|grep 'volume-'`" != "" ]; then
docker_volume=`echo $DATA_VOLUME | cut -d'-' -f2 | cut -d':' -f1`
dest_volume=`echo $DATA_VOLUME | cut -d'-' -f2 | cut -d':' -f2`
source_volume=$(basename $imageTag)_${docker_volume}
sudo docker volume create ${source_volume}
VOLUME_MAP="-v ${source_volume}:${dest_volume} ${VOLUME_MAP}"
else
echo "---- ${DATA_VOLUME} already is defined! Hence, ignore setup ${DATA_VOLUME} ..."
echo "---> VOLUME_MAP=${VOLUME_MAP}"
fi
}
EXCEPTION_SYS_DIR="/dev/shm /dev/snd /etc/hosts /var/run/docker.sock /tmp/.X11-unix"
function checkHostVolumePath() {
_left=$1
if [ ! -s ${_left} ]; then
if [[ ! "${EXCEPTION_SYS_DIR}" =~ .*${_left}.* ]]; then
mkdir -p ${_left}
sudo chown -R $USER:$USER ${_left}
if [ -s ${_left} ]; then
ls -al ${_left}
else
echo "*** ERROR: ${_left}: Not existing!"
fi
fi
fi
}
###################################################
#### **** Container Package information ****
###################################################
DOCKER_IMAGE_REPO=`echo $(basename $PWD)|tr '[:upper:]' '[:lower:]'|tr "/: " "_" `
imageTag="${ORGANIZATION}/${DOCKER_IMAGE_REPO}"
#PACKAGE=`echo ${imageTag##*/}|tr "/\-: " "_"`
PACKAGE="${imageTag##*/}"
###################################################
#### **** Container Volume Mapping information ****
###################################################
volmap_removed_sys_folders=""
function find_volumes_in_dockercompose() {
volmap_removed_sys_folders=""
_DOCKER_COMPOSE_FILE_=$1
if [ ! -s ${_DOCKER_COMPOSE_FILE_} ]; then
echo "*** WARNING ***: Can't find ${_DOCKER_COMPOSE_FILE_} template file! Ignore creation local volumes dir!"
else
#volumes="./app ./data ./workspace ./.vscode/extensions"
# cat docker-compose.yml.template |grep -E '^( |\t)*- .\/.*:.*$'|cut -d':' -f1|awk '{print $2}'
#volumes_current_dir="`cat ${_DOCKER_COMPOSE_FILE_} |grep -E '^( |\t)*- (\/|\.).*:.*$'|cut -d':' -f1' ` "
volmap_existing="`cat ${_DOCKER_COMPOSE_FILE_} |grep -E '^( |\t)*- (\/|\.).*:.*$' | awk '{print $2}' `"
echo -e "22===============>>>>\n\n volmap_existing = ${volmap_existing}"
for vol in ${volmap_existing}; do
echo "---> Volume: $vol"
vol_left="`echo $vol |cut -d':' -f1 `"
if [[ ! "${EXCEPTION_SYS_DIR}" =~ .*${vol_left}.* ]]; then
volmap_removed_sys_folders="${volmap_removed_sys_folders} $vol"
fi
done
fi
}
#find_volumes_in_dockercompose ${DOCKER_COMPOSE_FILE}
#### ---- Processing #{{MORE_VOLUMES_MAPPING}} : ---- ####
baseDataFolder="$HOME/data-docker"
LOCAL_VOLUME_DIR="${baseDataFolder}/${PACKAGE}"
## -- Container's internal Volume base DIR
DOCKER_VOLUME_DIR="/home/developer"
DOCKER_COMPOSE_FILE="${PROJ_DIR}/docker-compose.yml"
#{{VOLUMES_MAPPING}}
VOLUMES_MAPPING=""
VOLUMES_MAPPING_LIST=""
prefix_yaml=" - "
function generateVolumeMapping_dockercompose() {
if [ "$VOLUMES_LIST" == "" ]; then
## -- If locally defined in this file, then respect that first.
## -- Otherwise, go lookup the docker.env as ride-along source for volume definitions
VOLUMES_LIST=`cat ${DOCKER_ENV_FILE}|grep "^#VOLUMES_LIST= *"|sed "s/[#\"]//g"|cut -d'=' -f2-`
fi
echo -e "\n>>>> VOLUMES_LIST: $VOLUMES_LIST \n"
for vol in $VOLUMES_LIST; do
echo
echo ">>>>>>>>> $vol"
hasColon=`echo $vol|grep ":"`
## -- allowing change local volume directories --
if [ "$hasColon" != "" ]; then
if [ "`echo $vol|grep 'volume-'`" != "" ]; then
cutomizedVolume $vol
else
echo "************* hasColon=$hasColon"
left=`echo $vol|cut -d':' -f1`
right=`echo $vol|cut -d':' -f2`
leftHasDot=`echo $left|grep "^\./"`
if [ "$leftHasDot" != "" ]; then
## has "./data" on the left
debug "******** A. Left HAS Dot pattern: leftHasDot=$leftHasDot"
if [[ ${right} == "/"* ]]; then
## -- pattern like: "./data:/containerPath/data"
echo "******* A-1 -- pattern like ./data:/data --"
VOLUMES_MAPPING="${VOLUMES_MAPPING}${prefix_yaml}`pwd`/${left#./}:${right}"
VOLUMES_MAPPING_LIST="${VOLUMES_MAPPING_LIST} `pwd`/${left#./}:${right}"
else
## -- pattern like: "./data:data"
echo "******* A-2 -- pattern like ./data:data --"
VOLUMES_MAPPING="${VOLUMES_MAPPING}${prefix_yaml}`pwd`/${left#./}:${DOCKER_VOLUME_DIR}/${right}"
VOLUMES_MAPPING_LIST="${VOLUMES_MAPPING_LIST} `pwd`/${left#./}:${DOCKER_VOLUME_DIR}/${right}"
fi
checkHostVolumePath "`pwd`/${left}"
else
## No "./data" on the left
debug "******** B. Left No ./data on the left: leftHasDot=$leftHasDot"
leftHasAbsPath=`echo $left|grep "^/.*"`
if [ "$leftHasAbsPath" != "" ]; then
debug "******* B-1 ## Has pattern like /data on the left "
if [[ ${right} == "/"* ]]; then
## -- pattern like: "/data:/containerPath/data"
echo "****** B-1-a pattern like /data:/containerPath/data --"
VOLUMES_MAPPING="${VOLUMES_MAPPING}${prefix_yaml}${left}:${right}"
VOLUMES_MAPPING_LIST="${VOLUMES_MAPPING_LIST} ${left}:${right}"
else
## -- pattern like: "/data:data"
echo "----- B-1-b pattern like /data:data --"
VOLUMES_MAPPING="${VOLUMES_MAPPING}${prefix_yaml}${left}:${DOCKER_VOLUME_DIR}/${right}"
VOLUMES_MAPPING_LIST="${VOLUMES_MAPPING_LIST} ${left}:${DOCKER_VOLUME_DIR}/${right}"
fi
checkHostVolumePath "${left}"
else
debug "******* B-2 ## No pattern like /data on the left"
rightHasAbsPath=`echo $right|grep "^/.*"`
debug ">>>>>>>>>>>>> rightHasAbsPath=$rightHasAbsPath"
if [[ ${right} == "/"* ]]; then
echo "****** B-2-a pattern like: data:/containerPath/data"
debug "-- pattern like ./data:/data --"
VOLUMES_MAPPING="${VOLUMES_MAPPING}${prefix_yaml}${LOCAL_VOLUME_DIR}/${left}:${right}"
VOLUMES_MAPPING_LIST="${VOLUMES_MAPPING_LIST} ${LOCAL_VOLUME_DIR}/${left}:${right}"
else
debug "****** B-2-b ## -- pattern like: data:data"
VOLUMES_MAPPING="${VOLUMES_MAPPING}${prefix_yaml}${LOCAL_VOLUME_DIR}/${left}:${DOCKER_VOLUME_DIR}/${right}"
VOLUMES_MAPPING_LIST="${VOLUMES_MAPPING_LIST} ${LOCAL_VOLUME_DIR}/${left}:${DOCKER_VOLUME_DIR}/${right}"
fi
checkHostVolumePath "${left}"
fi
mkdir -p ${LOCAL_VOLUME_DIR}/${left}
#if [ $DEBUG -gt 0 ]; then ls -al ${LOCAL_VOLUME_DIR}/${left}; fi
fi
fi
else
## -- pattern like: "data" or "./data"
hasDot=`echo $left|grep "^\./"`
if [ "$hasDot" != "" ]; then
## pattern: "./data"
debug "-- pattern: ./data --"
VOLUMES_MAPPING="${VOLUMES_MAPPING}${prefix_yaml}`pwd`/${vol#./}:${DOCKER_VOLUME_DIR}/${vol#./}"
VOLUMES_MAPPING_LIST="${VOLUMES_MAPPING_LIST} `pwd`/${vol#./}:${DOCKER_VOLUME_DIR}/${vol#./}"
if [ ! -s `pwd`/${vol#./} ]; then
mkdir -p `pwd`/${vol#./}
fi
else
## pattern: "data"
debug "-- default sub-directory (without prefix absolute path) --"
VOLUMES_MAPPING="${VOLUMES_MAPPING}${prefix_yaml}${LOCAL_VOLUME_DIR}/$vol:${DOCKER_VOLUME_DIR}/$vol"
VOLUMES_MAPPING_LIST="${VOLUMES_MAPPING_LIST} ${LOCAL_VOLUME_DIR}/$vol:${DOCKER_VOLUME_DIR}/$vol"
if [ ! -s ${LOCAL_VOLUME_DIR}/$vol ]; then
mkdir -p ${LOCAL_VOLUME_DIR}/$vol
fi
if [ $DEBUG -gt 0 ]; then ls -al ${LOCAL_VOLUME_DIR}/$vol; fi
fi
fi
echo ">>> expanded VOLUMES_MAPPING: ${VOLUMES_MAPPING}"
done
}
#### ---- Generate Volumes Mapping ----
generateVolumeMapping_dockercompose
echo -e "\n>>>>\n EXCEPTION_SYS_DIR=\n${EXCEPTION_SYS_DIR}"
volumes_map_exists_in_dockercompose="`cat ${DOCKER_COMPOSE_FILE} |grep -E '^( |\t)*- (\/|\.|\\\$HOME|\~).*:.*$' | awk '{print $2}' `"
echo -e "====>volumes_map_exists_in_dockercompose = ${volumes_map_exists_in_dockercompose}"
echo -e "====> VOLUMES_MAPPING_LIST:\n${VOLUMES_MAPPING_LIST}"
MORE_VOLUMES_MAPPING=""
function find_volumes_in_dockercompose() {
_volmap_exists_with_expanded_home_dir="`eval echo ${volumes_map_exists_in_dockercompose}`"
echo -e "---->_volmap_exists_with_expanded_home_dir:\n ${_volmap_exists_with_expanded_home_dir} "
for volmap in $VOLUMES_MAPPING_LIST; do
#echo -e "----> checking volmap existing in docker-compose.yml or not: $volmap"
if [[ ! "${_volmap_exists_with_expanded_home_dir}" =~ .*${volmap}.* ]]; then
MORE_VOLUMES_MAPPING="${MORE_VOLUMES_MAPPING}${prefix_yaml}${volmap}\n"
fi
done
}
find_volumes_in_dockercompose
echo -e "================>MORE_VOLUMES_MAPPING:$MORE_VOLUMES_MAPPING"
sed -i ${SED_MAC_FIX} "s%^.*\#{{MORE_VOLUMES_MAPPING}}%$MORE_VOLUMES_MAPPING%g" ${DOCKER_COMPOSE_FILE}
#### ---- Auto create base directories for docker-compose's "volumes:" mapping ---- ####
#### ---- Current directory as base for additional child directories to be mapped into Container
DOCKER_COMPOSE_TEMPLATE="${PROJ_DIR}/docker-compose.yml.template"
function auto_mkdir_for_dockercompose() {
if [ ! -s ${DOCKER_COMPOSE_TEMPLATE} ]; then
echo "*** WARNING ***: Can't find ${DOCKER_COMPOSE_TEMPLATE} template file! Ignore creation local volumes dir!"
else
#volumes="./app ./data ./workspace ./.vscode/extensions"
# cat docker-compose.yml.template |grep -E '^( |\t)*- .\/.*:.*$'|cut -d':' -f1|awk '{print $2}'
volumes_current_dir="`cat ${DOCKER_COMPOSE_TEMPLATE} |grep -E '^( |\t)*- +.\/.*:.*$'|cut -d':' -f1|awk '{print $2}' ` "
echo -e ">>>> volumes_current_dir = $volumes_current_dir"
for v in $volumes_current_dir; do
if [ ! -s $v ]; then
checkHostVolumePath "${PROJ_DIR}/$v"
#mkdir -p ${PROJ_DIR}/$v
fi
done
fi
}
#auto_mkdir_for_dockercompose