|
1
|
#!/bin/bash
|
|
2
|
|
|
3
|
#set -x
|
|
4
|
|
|
5
|
function usage()
|
|
6
|
{
|
|
7
|
local fusage="$1"
|
|
8
|
|
|
9
|
if [[ -n "$fusage" ]]; then
|
|
10
|
fwdline1="[-dlw]"
|
|
11
|
fwdline2="Create a container based off the \"${fwddocker_image}\" Docker image"
|
|
12
|
fwdline3=""
|
|
13
|
else
|
|
14
|
fwdline1="[-dlwf]"
|
|
15
|
fwdline2="Create a container based off the \"${docker_image}\" Docker image"
|
|
16
|
fwdline3="-f = Include FWD in the container. Specify a tag with --tag=<tag>, if desired. (default=${tag})."
|
|
17
|
fi
|
|
18
|
|
|
19
|
cat <<EOF
|
|
20
|
Usage: $0 ${fwdline1} [-v <volume>] [--port-gui] [--port-debug] [--port-jmx] [--port-pg=<port>] \
|
|
21
|
[--tag=<tag>] [--name=<name>] \
|
|
22
|
[--pg<14|15|16|17>] \
|
|
23
|
[--pgport=<port>] \
|
|
24
|
[--host] \
|
|
25
|
[--manual_port=<hport:cport>[,<hport:cport>...]] \
|
|
26
|
[--manual_mount=<ext_vol:int_vol>[,<ext_vol:int_vol>...]] \
|
|
27
|
[--startup_hook=<startup_file>] \
|
|
28
|
[--jdk<21|25>] \
|
|
29
|
[--detach] \
|
|
30
|
[--app=<appdir>] \
|
|
31
|
[--fwd_lib=<fwd_lib>] \
|
|
32
|
[-c <cmd_line>]
|
|
33
|
|
|
34
|
${fwdline2}
|
|
35
|
|
|
36
|
Where:
|
|
37
|
-d = dryrun (just display commands)
|
|
38
|
-l = local image. Don't use goldencode/ repository.
|
|
39
|
-w = Use fwd account instead of mapping in ${USER} account.
|
|
40
|
EOF
|
|
41
|
[[ -n "$fwdline3" ]] && echo " $fwdline3"
|
|
42
|
cat <<EOF
|
|
43
|
-v = Volume for database to mount to /opt/db (if PostgreSQL included).
|
|
44
|
--port-gui = Open typical GUI ports (7443 and 7449-7459)
|
|
45
|
--port-debug = Open typical debug ports (2080 and 5000)
|
|
46
|
--port-jmx = Open JMX 22100 port
|
|
47
|
--port-pg=<port> = Open <port> (usually 5432) to the PGPORT (override container port ${pgport_val} with --pgport=<port>)
|
|
48
|
--tag=<tag> = Use given tag for FWD included in the image (default=${tag}).
|
|
49
|
--name=<name> = Use <name> as the container name and hostname, instead of the generated one. It fails if a container with that name already exists.
|
|
50
|
--pg<14|15|16|17> = Include given PostgreSQL in the container. The default is no PostgreSQL.
|
|
51
|
--pgport=<port> Use this port for PostgreSQL. The default is ${pgport_val}. It will export PGPORT=<port>.
|
|
52
|
--pgdata=<pgdata> Startup PostgreSQL using this cluster. It will export PGDATA=/opt/db/<pgdata>
|
|
53
|
--host = Use host's network.
|
|
54
|
--manual_port=<hport:cport>[,<hport:cport>...] include one or more port openings with "-p hport:cport".
|
|
55
|
--manual_mount=<ext_vol:int_vol>[,<ext_vol:int_vol>...] include one or more mountpoints with "-v ext_vol:int_vol"
|
|
56
|
--startup_hook=<startup_file> the base_entrypoint.sh will hook <startup_file> for an installation instead of the default \"$hook_default\""
|
|
57
|
--jdk<21|25> = Use given JDK as the default for the container. The default is JDK${jdk_version}.
|
|
58
|
--app=<appdir> = Set /opt/app directory in container to give <appdir> directory on host, typically the deploy directory.
|
|
59
|
--fwd_lib=<fwd_lib> = Set FWD_LIB environment variable to <fwd_lib> path. This allows scripts to find FWD.
|
|
60
|
--detach = Run the container detached. Use "docker attach ..." to attach and "CTRL-p CTRL-q" to detach again.
|
|
61
|
-c <cmd_line> = Pass a command line to the Docker container.
|
|
62
|
|
|
63
|
-? or -h or --help = show usage.
|
|
64
|
EOF
|
|
65
|
}
|
|
66
|
|
|
67
|
# Function to determine the next available container name,
|
|
68
|
# if the given container name is already taken
|
|
69
|
get_container()
|
|
70
|
{
|
|
71
|
local base_name=$1
|
|
72
|
local max=$2
|
|
73
|
local candidate_name="$base_name"
|
|
74
|
local cid
|
|
75
|
|
|
76
|
for ((i = 0; i < max; i++)); do
|
|
77
|
# Only bump the name if there is a container of the initial name
|
|
78
|
if [ $i -gt 0 ]; then
|
|
79
|
candidate_name="${base_name}${i}"
|
|
80
|
fi
|
|
81
|
|
|
82
|
cid=$(docker ps -aq -f name=^/${candidate_name}$)
|
|
83
|
if [ -z "$cid" ]; then
|
|
84
|
echo "$candidate_name"
|
|
85
|
return
|
|
86
|
fi
|
|
87
|
done
|
|
88
|
|
|
89
|
echo "0"
|
|
90
|
}
|
|
91
|
|
|
92
|
get_longopt_value()
|
|
93
|
{
|
|
94
|
local opt_name="$1"
|
|
95
|
local optarg="$2"
|
|
96
|
local value
|
|
97
|
|
|
98
|
if [[ "$optarg" != "${opt_name}="* ]]; then
|
|
99
|
echo "ERROR: --${opt_name} requires a value." >&2
|
|
100
|
return 1
|
|
101
|
fi
|
|
102
|
|
|
103
|
value="${optarg#*=}"
|
|
104
|
if [[ -z "$value" ]]; then
|
|
105
|
echo "ERROR: --${opt_name} requires a non-empty value." >&2
|
|
106
|
return 1
|
|
107
|
fi
|
|
108
|
|
|
109
|
printf '%s' "$value"
|
|
110
|
}
|
|
111
|
|
|
112
|
csv_to_docker_args()
|
|
113
|
{
|
|
114
|
local opt_name="$1"
|
|
115
|
local docker_flag="$2"
|
|
116
|
local values="$3"
|
|
117
|
local -n out_args_ref=$4
|
|
118
|
local item
|
|
119
|
|
|
120
|
IFS=',' read -r -a entries <<< "$values"
|
|
121
|
|
|
122
|
for item in "${entries[@]}"; do
|
|
123
|
# Trim whitespace around each comma-separated item.
|
|
124
|
item="${item#"${item%%[![:space:]]*}"}"
|
|
125
|
item="${item%"${item##*[![:space:]]}"}"
|
|
126
|
|
|
127
|
if [[ -z "$item" ]]; then
|
|
128
|
echo "ERROR: ${opt_name} contains an empty list item: $values" >&2
|
|
129
|
return 1
|
|
130
|
fi
|
|
131
|
|
|
132
|
if [[ "$item" != *:* ]]; then
|
|
133
|
echo "ERROR: ${opt_name} item must contain ':': $item" >&2
|
|
134
|
return 1
|
|
135
|
fi
|
|
136
|
|
|
137
|
out_args_ref+=( "$docker_flag" "$item" )
|
|
138
|
done
|
|
139
|
}
|
|
140
|
|
|
141
|
# Defaults
|
|
142
|
jdk_version="17"
|
|
143
|
tag="latest"
|
|
144
|
repo="goldencode/"
|
|
145
|
pull="always"
|
|
146
|
gui_ports=()
|
|
147
|
jmx_port=()
|
|
148
|
pgport_val="5432"
|
|
149
|
portpg_open=""
|
|
150
|
debug_ports=()
|
|
151
|
docker_image="basedev_ubuntu_24.04"
|
|
152
|
fwddocker_image="fwddev_4.0_ubuntu_24.04"
|
|
153
|
hook_default="./docker/startup_hook.sh"
|
|
154
|
run_detached=""
|
|
155
|
mount_args=()
|
|
156
|
env_args=()
|
|
157
|
port_args=()
|
|
158
|
manual_port_args=()
|
|
159
|
manual_mount_args=()
|
|
160
|
|
|
161
|
while getopts ":h?dlwv:c:-:f" opt; do
|
|
162
|
case $opt in
|
|
163
|
d ) mycmd="echo"
|
|
164
|
;;
|
|
165
|
l ) unset repo
|
|
166
|
pull="never"
|
|
167
|
;;
|
|
168
|
w ) use_fwd_id=true
|
|
169
|
;;
|
|
170
|
v ) vol="${OPTARG}"
|
|
171
|
;;
|
|
172
|
c ) cmd_line="${OPTARG}"
|
|
173
|
;;
|
|
174
|
f ) fwd="_fwd"
|
|
175
|
fwd_flag=true
|
|
176
|
;;
|
|
177
|
- ) case ${OPTARG} in
|
|
178
|
"port-gui" ) gui_ports=( -p "7443:7443" -p "7449-7459:7449-7459" ) ;;
|
|
179
|
"port-debug" ) debug_ports=( -p "2080:2080" -p "5000:5000" ) ;;
|
|
180
|
"port-jmx" ) jmx_port=( -p "22100:22100" ) ;;
|
|
181
|
"port-pg="* ) portpg_open=$(echo $OPTARG | cut -d"=" -f2) ;;
|
|
182
|
"tag="* ) tag=$(echo $OPTARG | cut -d"=" -f2) ;;
|
|
183
|
"name="* ) explicit_name=$(get_longopt_value "name" "$OPTARG") || { usage; exit 1; } ;;
|
|
184
|
"manual_port="* ) value=$(get_longopt_value "manual_port" "$OPTARG") || { usage; exit 1; }
|
|
185
|
csv_to_docker_args "--manual_port" "-p" "$value" manual_port_args || { usage; exit 1; } ;;
|
|
186
|
"manual_mount="*) value=$(get_longopt_value "manual_mount" "$OPTARG") || { usage; exit 1; }
|
|
187
|
csv_to_docker_args "--manual_mount" "-v" "$value" manual_mount_args || { usage; exit 1; } ;;
|
|
188
|
"startup_hook"* ) hook_file=$(echo $OPTARG | cut -d"=" -f2) ;;
|
|
189
|
"pg1"* ) pg_version="${OPTARG}"
|
|
190
|
pg_flag=true ;;
|
|
191
|
"pgport="* ) pgport_val=$(echo $OPTARG | cut -d"=" -f2) ;;
|
|
192
|
"pgdata="* ) pgdata_val=$(echo $OPTARG | cut -d"=" -f2) ;;
|
|
193
|
"host" ) host_network="--network host" ;;
|
|
194
|
"jdk"* ) jdk_version="${OPTARG}" ;;
|
|
195
|
"app="* ) appdir=$(echo $OPTARG | cut -d"=" -f2) ;;
|
|
196
|
"fwd_lib="* ) fwd_lib=$(echo $OPTARG | cut -d"=" -f2) ;;
|
|
197
|
"detach" ) run_detached="--detach" ;;
|
|
198
|
"help" ) usage
|
|
199
|
exit 0 ;;
|
|
200
|
"fhelp" ) usage -f
|
|
201
|
exit 0 ;;
|
|
202
|
* ) echo "Unknown option: --${OPTARG}"
|
|
203
|
usage
|
|
204
|
exit 1 ;;
|
|
205
|
esac
|
|
206
|
;;
|
|
207
|
h | \? ) if [[ "$OPTARG" == "?" || "$opt" == "h" ]]; then
|
|
208
|
usage
|
|
209
|
exit 1
|
|
210
|
else
|
|
211
|
echo "Unknown option: -${OPTARG}"
|
|
212
|
usage
|
|
213
|
exit 1
|
|
214
|
fi
|
|
215
|
;;
|
|
216
|
esac
|
|
217
|
done
|
|
218
|
shift $(($OPTIND - 1))
|
|
219
|
|
|
220
|
# Check on PostgreSQL
|
|
221
|
if [[ "$pg_flag" == true ]]; then
|
|
222
|
if [[ $pg_version =~ ^pg([0-9]+)$ ]]; then
|
|
223
|
numver="${BASH_REMATCH[1]}"
|
|
224
|
else
|
|
225
|
echo "ERROR: Invalid format '$pg_version' (expected 'pgXX' or 'XX')" >&2
|
|
226
|
exit 1
|
|
227
|
fi
|
|
228
|
# Verify valid versions
|
|
229
|
case "$numver" in
|
|
230
|
14|15|16|17)
|
|
231
|
pg="_pg${numver}"
|
|
232
|
;;
|
|
233
|
*)
|
|
234
|
echo "ERROR: Unsupported PostgreSQL version '$numver' (valid: 14, 15, 16, 17)" >&2
|
|
235
|
exit 1
|
|
236
|
;;
|
|
237
|
esac
|
|
238
|
if [ -z "$vol" ]; then
|
|
239
|
echo "ERROR: No volume passed for database." >&2
|
|
240
|
usage
|
|
241
|
exit 1
|
|
242
|
else
|
|
243
|
mount_args+=( -v "$vol:/opt/db" )
|
|
244
|
fi
|
|
245
|
# Setup PGPORT for container's usage
|
|
246
|
env_args+=( -e "PGPORT=${pgport_val}" )
|
|
247
|
# Setup PG ports, if specified to be open
|
|
248
|
[[ -n "$portpg_open" ]] && port_args+=( -p "${portpg_open}:${pgport_val}" )
|
|
249
|
# Setup PGDATA if the cluster is to be started
|
|
250
|
[[ -n "$pgdata_val" ]] && env_args+=( -e "PGDATA=/opt/db/${pgdata_val}" )
|
|
251
|
fi
|
|
252
|
|
|
253
|
# Check on Java
|
|
254
|
if [[ $jdk_version =~ ^jdk([0-9]+)$ ]]; then
|
|
255
|
numver="${BASH_REMATCH[1]}"
|
|
256
|
elif [[ $jdk_version =~ ^[0-9]+$ ]]; then
|
|
257
|
numver=$jdk_version
|
|
258
|
else
|
|
259
|
echo "ERROR: Invalid format '$jdk_version' (expected 'jdkXX' or 'XX')" >&2
|
|
260
|
exit 1
|
|
261
|
fi
|
|
262
|
env_args+=( -e "JDK_VERSION=${numver}" )
|
|
263
|
jdk="_jdk${numver:-17}"
|
|
264
|
|
|
265
|
# Build Docker image name based on fwd, pg, and jdk
|
|
266
|
[ "$fwd_flag" = true ] && docker_image=${fwddocker_image}${pg}${jdk} || docker_image+=${pg}
|
|
267
|
|
|
268
|
max_containers=10 # Maximum of the same container we allow concurrently
|
|
269
|
container_name=${docker_image}_${tag}_container
|
|
270
|
container_name="${container_name//[^[:alnum:]_-]/}"
|
|
271
|
|
|
272
|
(( ${#gui_ports[@]} > 0 )) && port_args+=( "${gui_ports[@]}" )
|
|
273
|
(( ${#debug_ports[@]} > 0 )) && port_args+=( "${debug_ports[@]}" )
|
|
274
|
(( ${#jmx_port[@]} > 0 )) && port_args+=( "${jmx_port[@]}" )
|
|
275
|
(( ${#manual_port_args[@]} > 0 )) && port_args+=( "${manual_port_args[@]}" )
|
|
276
|
if [ -n "$explicit_name" ]; then
|
|
277
|
# An explicit name is used as given; it is never bumped with a numeric suffix.
|
|
278
|
if [[ ! "$explicit_name" =~ ^[a-zA-Z0-9][a-zA-Z0-9_.-]*$ ]]; then
|
|
279
|
echo "ERROR: Invalid --name '${explicit_name}' (use letters, digits, '_', '.' and '-', starting with a letter or digit)." >&2
|
|
280
|
exit 1
|
|
281
|
fi
|
|
282
|
if [ -n "$(docker ps -aq -f name=^/${explicit_name}$)" ]; then
|
|
283
|
echo "ERROR: A container named ${explicit_name} already exists. Remove it with \"docker rm -f ${explicit_name}\" or pick another --name." >&2
|
|
284
|
exit 1
|
|
285
|
fi
|
|
286
|
container_name="$explicit_name"
|
|
287
|
else
|
|
288
|
check_container_name=$(get_container $container_name $max_containers)
|
|
289
|
if [ "$check_container_name" == "0" ]; then
|
|
290
|
echo "ERROR: Too many ${container_name} containers (max=${max_containers}) running. "
|
|
291
|
exit
|
|
292
|
else
|
|
293
|
container_name=$check_container_name
|
|
294
|
fi
|
|
295
|
fi
|
|
296
|
|
|
297
|
if [[ "$use_fwd_id" = true ]]; then
|
|
298
|
mount_args+=( -v "${HOME}:/home/fwd" )
|
|
299
|
env_args+=( -e "HOST_UID=1000" -e "HOST_GID=1000" -e "HOME=/home/fwd" -e "USER=fwd" )
|
|
300
|
working_dir="/home/fwd"
|
|
301
|
user="fwd"
|
|
302
|
else
|
|
303
|
host_pwd="$(pwd -P)"
|
|
304
|
mount_args+=( -v "${HOME}:/home/${USER}" )
|
|
305
|
mount_args+=( -v "/etc/passwd:/etc/passwd:ro" )
|
|
306
|
mount_args+=( -v "/etc/group:/etc/group:ro" )
|
|
307
|
mount_args+=( -v "/etc/shadow:/etc/shadow:ro" )
|
|
308
|
mount_args+=( -v "/etc/sudoers:/etc/sudoers:ro" )
|
|
309
|
env_args+=( -e "HOST_UID=$(id -u)" )
|
|
310
|
env_args+=( -e "HOST_GID=$(id -g)" )
|
|
311
|
env_args+=( -e "HOME=${HOME}" )
|
|
312
|
env_args+=( -e "USER=${USER}" )
|
|
313
|
working_dir="$host_pwd"
|
|
314
|
user=$USER
|
|
315
|
[[ "$fwd_flag" = true ]] && user_override=( --user "root" ) || true
|
|
316
|
fi
|
|
317
|
(( ${#manual_mount_args[@]} > 0 )) && mount_args+=( "${manual_mount_args[@]}" )
|
|
318
|
|
|
319
|
# Check on appdir passed in
|
|
320
|
[ -n "$appdir" ] && mount_args+=( -v "${appdir}:/opt/app" )
|
|
321
|
|
|
322
|
# Check on fwd_lib passed in
|
|
323
|
[ -n "$fwd_lib" ] && env_args+=( -e "FWD_LIB=${fwd_lib}" )
|
|
324
|
|
|
325
|
# Check on startup hook
|
|
326
|
[ -z "$hook_file" ] && hook_file=$hook_default
|
|
327
|
[ -f "$hook_file" ] && mount_args+=( -v "${hook_file}:/docker_install_hook.sh:ro" )
|
|
328
|
|
|
329
|
# Complete docker run command array
|
|
330
|
docker_cmd=( docker run )
|
|
331
|
|
|
332
|
[[ -n "$run_detached" ]] && docker_cmd+=( "$run_detached" )
|
|
333
|
docker_cmd+=( -e "DBASH=true" )
|
|
334
|
|
|
335
|
[[ -n "$host_network" ]] && docker_cmd+=( --network host )
|
|
336
|
|
|
337
|
docker_cmd+=( --name "$container_name" -h "$container_name" )
|
|
338
|
|
|
339
|
(( ${#user_override[@]} > 0 )) && docker_cmd+=( "${user_override[@]}" )
|
|
340
|
|
|
341
|
[[ -z "$run_detached" ]] && docker_cmd+=( --rm )
|
|
342
|
docker_cmd+=( -it )
|
|
343
|
|
|
344
|
(( ${#port_args[@]} > 0 )) && docker_cmd+=( "${port_args[@]}" )
|
|
345
|
|
|
346
|
(( ${#mount_args[@]} > 0 )) && docker_cmd+=( "${mount_args[@]}" )
|
|
347
|
|
|
348
|
docker_cmd+=( --pull "$pull" )
|
|
349
|
|
|
350
|
(( ${#env_args[@]} > 0 )) && docker_cmd+=( "${env_args[@]}" )
|
|
351
|
|
|
352
|
docker_cmd+=( -w "$working_dir" )
|
|
353
|
|
|
354
|
docker_cmd+=( "${repo}${docker_image}:${tag}" )
|
|
355
|
docker_cmd+=( "$user" -- )
|
|
356
|
|
|
357
|
if [[ -n "$cmd_line" ]]; then
|
|
358
|
docker_cmd+=( bash -lc "$cmd_line" )
|
|
359
|
else
|
|
360
|
docker_cmd+=( bash )
|
|
361
|
fi
|
|
362
|
|
|
363
|
printf 'Running: '
|
|
364
|
printf '%q ' "${docker_cmd[@]}"
|
|
365
|
echo
|
|
366
|
|
|
367
|
if [[ "$mycmd" == "echo" ]]; then
|
|
368
|
:
|
|
369
|
else
|
|
370
|
"${docker_cmd[@]}"
|
|
371
|
fi
|
|
372
|
|
|
373
|
exit 0
|