-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker-entrypoint.sh
executable file
·228 lines (196 loc) · 5.92 KB
/
docker-entrypoint.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
#!/usr/bin/env bash
set -Eeo pipefail
# TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables)
INSTALLDIR=$ISC_PACKAGE_INSTALLDIR
if [ ! -z "$ISC_DATA_DIRECTORY" ]; then
if [ -d $ISC_DATA_DIRECTORY ] || mkdir $ISC_DATA_DIRECTORY 2>/dev/null; then
INSTALLDIR=$ISC_DATA_DIRECTORY
else
printf >&2 'Durable folder: %s does not exists, or cannot be created' "$ISC_DATA_DIRECTORY"
exit 1
fi
fi
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local var2="${var//_/}"
local fileVar="${var}_FILE"
local fileVar2="${var2}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
printf >&2 'error: both %s and %s are set (but are exclusive)\n' "$var" "$fileVar"
exit 1
fi
if [ "${!var2:-}" ] && [ "${!fileVar2:-}" ]; then
printf >&2 'error: both %s and %s are set (but are exclusive)\n' "$var2" "$fileVar2"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!var2:-}" ]; then
val="${!var2}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
elif [ "${!fileVar2:-}" ]; then
val="$(< "${!fileVar2}")"
fi
export "$var"="$val"
export "$var2"="$val"
unset "$fileVar"
unset "$fileVar2"
}
# check to see if this file is being run or sourced from another script
_is_sourced() {
# https://unix.stackexchange.com/a/215279
[ "${#FUNCNAME[@]}" -ge 2 ] \
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \
&& [ "${FUNCNAME[1]}" = 'source' ]
}
# usage: docker_process_init_files [file [file [...]]]
# ie: docker_process_init_files /always-initdb.d/*
# process initializer files, based on file extensions and permissions
docker_process_init_files() {
printf '\n'
local f
for f; do
case "$f" in
*.sh)
if [ -x "$f" ]; then
printf '%s: running %s\n' "$0" "$f"
"$f" > /proc/1/fd/1 2>&1
else
printf '%s: sourcing %s\n' "$0" "$f"
. "$f"
fi
;;
*.sql) printf '%s: running %s\n' "$0" "$f"; cat $f | docker_process_sql; printf '\n' ;;
*.sql.gz) printf '%s: running %s\n' "$0" "$f"; gunzip -c "$f" | docker_process_sql; printf '\n' ;;
*.sql.xz) printf '%s: running %s\n' "$0" "$f"; xzcat "$f" | docker_process_sql; printf '\n' ;;
*) printf '%s: ignoring %s\n' "$0" "$f" ;;
esac
printf '\n'
done
}
# Execute sql script, passed via stdin (or -f flag of pqsl)
# usage: docker_process_sql [irissqlcli-args]
# ie: docker_process_sql --nspace=USER <<<'INSERT ...'
# ie: docker_process_sql -e 'select 1'
# ie: docker_process_sql < my-file.sql
docker_process_sql() {
local query_runner=( /usr/irissys/bin/irispython -m irissqlcli )
"${query_runner[@]}" "$@"
}
# create initial database
# uses environment variables for input: IRIS_NAMESPACE
docker_setup_namespace() {
if ! docker_process_sql --nspace $IRIS_NAMESPACE -e 'SELECT $Namespace;' > /dev/null 2>&1; then
echo "Create namespace: $IRIS_NAMESPACE"
docker_process_sql --nspace %SYS <<-EOSQL > /dev/null
CREATE DATABASE "$IRIS_NAMESPACE";
EOSQL
fi
}
# Loads various settings that are used elsewhere in the script
# This should be called before any other functions
docker_setup_env() {
file_env 'IRIS_USERNAME' '_SYSTEM'
file_env 'IRIS_PASSWORD'
file_env 'IRIS_NAMESPACE' ${IRIS_DATABASE} 'USER'
file_env 'IRIS_URI' "iris+emb:///$IRIS_NAMESPACE"
declare -g IRIS_INIT
if [ -s "$INSTALLDIR/iris.init" ]; then
IRIS_INIT='true'
fi
}
docker_enable_callin() {
iris session $ISC_PACKAGE_INSTANCENAME -U%SYS <<-'EOSESS' > /dev/null
set prop("Enabled")=1
Do ##class(Security.Services).Modify("%Service_CallIn",.prop)
halt
EOSESS
}
docker_setup_username() {
if [ -z "$IRIS_PASSWORD" ]; then
return
fi
iris session $ISC_PACKAGE_INSTANCENAME -U%SYS <<-EOSESS > /dev/null
check(sc) if 'sc { do ##class(%SYSTEM.OBJ).DisplayError(sc) do ##class(%SYSTEM.Process).Terminate(, 1) }
set exists = ##class(Security.Users).Exists("$IRIS_USERNAME", .user)
if 'exists { set sc = ##class(Security.Users).Create("$IRIS_USERNAME", "%All", "$IRIS_PASSWORD") }
if exists,\$isobject(user) { set user.PasswordExternal = "$IRIS_PASSWORD", sc = user.%Save() }
do check(sc)
halt
EOSESS
}
_main() {
# if first arg looks like a flag, assume we want to run IRIS
if [[ $# -eq 0 ]] || [ "${1:0:1}" = '-' ]; then
set -- iris "$@"
fi
if [ "$1" = 'iris' ]; then
shift;
ARGS=()
# May accept multiple --after parameters, we'll execute all of them
AFTER=()
# Community Edition does not need ISCAgent
ISCAgent="false"
while [[ $# -gt 0 ]]; do
case $1 in
-a|--after)
AFTER+=("$2")
shift;shift;
;;
--ISCAgent)
ISCAgent="$2"
shift;shift;
;;
*)
ARGS+=("$1")
shift
;;
esac
done
ARGS+=("--ISCAgent")
ARGS+=("$ISCAgent")
ARGS+=("-a")
ARGS+=("$0 iris-after-start ${AFTER[@]@Q}")
set -- "${ARGS[@]}"
# to solve issues with iris-main.log, switch to the home
pushd ~
touch iris-main.log
/iris-main "$@" &
PID=$!
popd
trap "while kill -s SIGTERM $PID > /dev/null 2>&1;do wait $PID; done" TERM
trap "while kill -s SIGINT $PID > /dev/null 2>&1;do wait $PID; done" INT
wait $PID
elif [ "$1" = 'iris-after-start' ]; then
shift
while [[ $# -gt 0 ]]; do
eval "$1"
shift
done
docker_setup_env
ls /docker-entrypoint-initdb.d/ > /dev/null
if [ -z "$IRIS_INIT" ]; then
date > "$INSTALLDIR/iris.init"
docker_enable_callin
docker_setup_namespace
docker_setup_username
docker_process_init_files /docker-entrypoint-initdb.d/*
else
echo "Already initialized, skipping"
fi
else
exec "$@"
fi
}
if ! _is_sourced; then
_main "$@"
# else
# docker_setup_env
fi