-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
258 lines (234 loc) · 9.06 KB
/
run.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
#!/bin/bash -x
MY_DIR=$(dirname "$(readlink -f "$0")")
if [ $# -lt 1 ]; then
echo "Usage: "
echo " ${0} <container_shell_command>"
echo "e.g.: "
echo " ${0} ls -al "
fi
###################################################
#### ---- Change this only to use your own ----
###################################################
ORGANIZATION=openkbs
baseDataFolder="$HOME/data-docker"
###################################################
#### **** Container package information ****
###################################################
MY_IP=`ip route get 1|awk '{print$NF;exit;}'`
DOCKER_IMAGE_REPO=`echo $(basename $PWD)|tr '[:upper:]' '[:lower:]'|tr "/: " "_" `
imageTag="${ORGANIZATION}/${DOCKER_IMAGE_REPO}"
#PACKAGE=`echo ${imageTag##*/}|tr "/\-: " "_"`
PACKAGE="${imageTag##*/}"
###################################################
#### ---- (DEPRECATED but still supported) -----
#### ---- Volumes to be mapped (change this!) -----
###################################################
# (examples)
# IDEA_PRODUCT_NAME="IdeaIC2017"
# IDEA_PRODUCT_VERSION="3"
# IDEA_INSTALL_DIR="${IDEA_PRODUCT_NAME}.${IDEA_PRODUCT_VERSION}"
# IDEA_CONFIG_DIR=".${IDEA_PRODUCT_NAME}.${IDEA_PRODUCT_VERSION}"
# IDEA_PROJECT_DIR="IdeaProjects"
# VOLUMES_LIST="${IDEA_CONFIG_DIR} ${IDEA_PROJECT_DIR}"
# ---------------------------
# Variable: VOLUMES_LIST
# (NEW: use docker.env with "#VOLUMES_LIST=data workspace" to define entries)
# ---------------------------
## -- If you defined locally here,
## then the definitions of volumes map in "docker.env" will be ignored.
# VOLUMES_LIST="data workspace"
# ---------------------------
# OPTIONAL Variable: PORT PAIR
# (NEW: use docker.env with "#PORTS=18000:8000 17200:7200" to define entries)
# ---------------------------
## -- If you defined locally here,
## then the definitions of ports map in "docker.env" will be ignored.
#### Input: PORT - list of PORT to be mapped
# (examples)
#PORTS_LIST="18000:8000"
#PORTS_LIST=
#########################################################################################################
######################## DON'T CHANGE LINES STARTING BELOW (unless you need to) #########################
#########################################################################################################
LOCAL_VOLUME_DIR="${baseDataFolder}/${PACKAGE}"
## -- Container's internal Volume base DIR
DOCKER_VOLUME_DIR="/home/developer"
###################################################
#### ---- Detect docker ----
###################################################
DOCKER_ENV_FILE="./.env"
function detectDockerEnvFile() {
curr_dir=`pwd`
if [ -s "./.env" ]; then
echo "--- INFO: ./.env Docker Environment file (.env) FOUND!"
DOCKER_ENV_FILE="./.env"
else
echo "--- INFO: ./.env Docker Environment file (.env) NOT found!"
if [ -s "./docker.env" ]; then
DOCKER_ENV_FILE="./docker.env"
else
echo "*** WARNING: Docker Environment file (.env) or (docker.env) NOT found!"
fi
fi
}
detectDockerEnvFile
###################################################
#### ---- Function: Generate volume mappings ----
#### (Don't change!)
###################################################
VOLUME_MAP=""
#### Input: VOLUMES - list of volumes to be mapped
hasPattern=0
function hasPattern() {
detect=`echo $1|grep "$2"`
if [ "${detect}" != "" ]; then
hasPattern=1
else
hasPattern=0
fi
}
function generateVolumeMapping() {
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
for vol in $VOLUMES_LIST; do
echo "$vol"
hasColon=`echo $vol|grep ":"`
## -- allowing change local volume directories --
if [ "$hasColon" != "" ]; then
left=`echo $vol|cut -d':' -f1`
right=`echo $vol|cut -d':' -f2`
leftHasDot=`echo $left|grep "\./"`
if [ "$leftHasDot" != "" ]; then
## has "./data" on the left
if [[ ${right} == "/"* ]]; then
## -- pattern like: "./data:/containerPath/data"
echo "-- pattern like ./data:/data --"
VOLUME_MAP="${VOLUME_MAP} -v `pwd`/${left}:${right}"
else
## -- pattern like: "./data:data"
echo "-- pattern like ./data:data --"
VOLUME_MAP="${VOLUME_MAP} -v `pwd`/${left}:${DOCKER_VOLUME_DIR}/${right}"
fi
mkdir -p `pwd`/${left}
ls -al `pwd`/${left}
else
## No "./data" on the left
if [[ ${right} == "/"* ]]; then
## -- pattern like: "data:/containerPath/data"
echo "-- pattern like ./data:/data --"
VOLUME_MAP="${VOLUME_MAP} -v ${LOCAL_VOLUME_DIR}/${left}:${right}"
else
## -- pattern like: "data:data"
echo "-- pattern like data:data --"
VOLUME_MAP="${VOLUME_MAP} -v ${LOCAL_VOLUME_DIR}/${left}:${DOCKER_VOLUME_DIR}/${right}"
fi
mkdir -p ${LOCAL_VOLUME_DIR}/${left}
ls -al ${LOCAL_VOLUME_DIR}/${left}
fi
else
## -- pattern like: "data"
echo "-- default sub-directory (without prefix absolute path) --"
VOLUME_MAP="${VOLUME_MAP} -v ${LOCAL_VOLUME_DIR}/$vol:${DOCKER_VOLUME_DIR}/$vol"
mkdir -p ${LOCAL_VOLUME_DIR}/$vol
ls -al ${LOCAL_VOLUME_DIR}/$vol
fi
done
}
#### ---- Generate Volumes Mapping ----
generateVolumeMapping
echo ${VOLUME_MAP}
###################################################
#### ---- Function: Generate port mappings ----
#### (Don't change!)
###################################################
PORT_MAP=""
function generatePortMapping() {
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`
PORT_MAP="${PORT_MAP} -p ${host_port}:${docker_port}"
fi
done
}
#### ---- Generate Port Mapping ----
generatePortMapping
echo ${PORT_MAP}
###################################################
#### ---- Function: Generate privilege String ----
#### (Don't change!)
###################################################
privilegedString=""
function generatePrivilegedString() {
OS_VER=`which yum`
if [ "$OS_VER" == "" ]; then
# Ubuntu
echo "Ubuntu ... not SE-Lunix ... no privileged needed"
else
# CentOS/RHEL
privilegedString="--privileged"
fi
}
generatePrivilegedString
echo ${privilegedString}
###################################################
#### ---- Mostly, you don't need change below ----
###################################################
function cleanup() {
if [ ! "`docker ps -a|grep ${instanceName}`" == "" ]; then
docker rm -f ${instanceName}
fi
}
function displayURL() {
port=${1}
echo "... Go to: http://${MY_IP}:${port}"
#firefox http://${MY_IP}:${port} &
if [ "`which google-chrome`" != "" ]; then
/usr/bin/google-chrome http://${MY_IP}:${port} &
else
firefox http://${MY_IP}:${port} &
fi
}
###################################################
#### ---- Replace "Key=Value" withe new value ----
###################################################
function replaceKeyValue() {
inFile=${1:-${DOCKER_ENV_FILE}}
keyLike=$2
newValue=$3
if [ "$2" == "" ]; then
echo "**** ERROR: Empty Key value! Abort!"
exit 1
fi
sed -i -E 's/^('$keyLike'[[:blank:]]*=[[:blank:]]*).*/\1'$newValue'/' $inFile
}
#### ---- Replace docker.env with local user's UID and GID ----
replaceKeyValue ${DOCKER_ENV_FILE} "USER_ID" "$(id -u $USER)"
replaceKeyValue ${DOCKER_ENV_FILE} "GROUP_ID" "$(id -g $USER)"
## -- transform '-' and space to '_'
#instanceName=`echo $(basename ${imageTag})|tr '[:upper:]' '[:lower:]'|tr "/\-: " "_"`
instanceName=`echo $(basename ${imageTag})|tr '[:upper:]' '[:lower:]'|tr "/: " "_"`
echo "---------------------------------------------"
echo "---- Starting a Container for ${imageTag}"
echo "---------------------------------------------"
cleanup
#### run restart options: { no, on-failure, unless-stopped, always }
RESTART_OPTION=no
docker run -it \
--name=${instanceName} \
${privilegedString} \
${VOLUME_MAP} \
${PORT_MAP} \
${imageTag} $*
#cleanup