This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
terminusdb-container
executable file
·375 lines (331 loc) · 11.4 KB
/
terminusdb-container
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/bash
#
# CONFIG
#
# Load ENV file
if [[ $1 != "nop" && -z "$TERMINUSDB_IGNORE_ENV" && -f ENV ]]; then
set -o allexport
# shellcheck disable=SC1091
# shellcheck source=ENV
source "$(pwd)/ENV"
set +o allexport
fi
# Container
TERMINUSDB_QUICKSTART_CONTAINER=${TERMINUSDB_CONTAINER:-terminusdb-server}
TERMINUSDB_QUICKSTART_REPOSITORY=${TERMINUSDB_REPOSITORY:-terminusdb.docker.scarf.sh/terminusdb/terminusdb-server}
# Version
TERMINUSDB_QUICKSTART_TAG=${TERMINUSDB_TAG:-latest}
# Volumes
TERMINUSDB_QUICKSTART_STORAGE=${TERMINUSDB_STORAGE:-terminusdb_storage}
TERMINUSDB_QUICKSTART_STORAGE_VOLUME=/app/terminusdb/storage
# Server
TERMINUSDB_QUICKSTART_PORT=${TERMINUSDB_PORT:-6363}
TERMINUSDB_QUICKSTART_PASS=${TERMINUSDB_PASS:-root}
TERMINUSDB_QUICKSTART_AUTOLOGIN=${TERMINUSDB_AUTOLOGIN_ENABLED:-true}
TERMINUSDB_QUICKSTART_JWT_ENABLED=${TERMINUSDB_JWT_ENABLED:-false}
TERMINUSDB_QUICKSTART_JWT_AGENT_NAME_PROPERTY=${TERMINUSDB_JWT_AGENT_NAME_PROPERTY:-''}
TERMINUSDB_QUICKSTART_JWKS=${TERMINUSDB_JWKS_ENDPOINT:-''}
TERMINUSDB_QUICKSTART_INSECURE_USER_HEADER_ENABLED=${TERMINUSDB_INSECURE_USER_HEADER_ENABLED:-false}
# HTTP
TERMINUSDB_QUICKSTART_SCHEME="http"
if [[ $TERMINUSDB_QUICKSTART_AUTOLOGIN == false ]]; then
TERMINUSDB_QUICKSTART_SERVER_IP=${TERMINUSDB_SERVER_IP:-0.0.0.0}
else
TERMINUSDB_QUICKSTART_SERVER_IP="127.0.0.1"
fi
TERMINUSDB_URL="$TERMINUSDB_QUICKSTART_SCHEME://127.0.0.1:$TERMINUSDB_QUICKSTART_PORT/"
if [[ "$(id -u)" == "0" || "$(uname)" == "Darwin" || "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
TERMINUSDB_QUICKSTART_DOCKER_DEFAULT="docker"
elif command -v sudo >/dev/null; then
TERMINUSDB_QUICKSTART_DOCKER_DEFAULT="sudo docker"
else
TERMINUSDB_QUICKSTART_DOCKER_DEFAULT="docker"
fi
TERMINUSDB_QUICKSTART_DOCKER=${TERMINUSDB_DOCKER:-$TERMINUSDB_QUICKSTART_DOCKER_DEFAULT}
function remove_old_volumes {
read -p "Are you sure? [y/N] " -r -n 1
printf "\n" # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
if ! $TERMINUSDB_QUICKSTART_DOCKER volume rm terminus_config $TERMINUSDB_QUICKSTART_STORAGE_VOLUME; then
printf "Could not remove volumes. Are you running an existing container of TerminusDB? Stop it first.\n"
fi
else
exit 2
fi
}
function version_check {
# The old TerminusDB had a prefix.db file needed, the new one
# does not as prefixes are handled in a different way. Therefore
# it is safe to assume that an existing storage volume with the
# prefix.db is a user running an old version of TerminusDB
if ! $TERMINUSDB_QUICKSTART_DOCKER volume inspect "$TERMINUSDB_QUICKSTART_STORAGE" > /dev/null 2>&1; then
# no volume? then it will be created later on and further checks are unnecessary
return 0
fi
if $TERMINUSDB_QUICKSTART_DOCKER run --rm -v "$TERMINUSDB_QUICKSTART_STORAGE_VOLUME":/home/ alpine cat /home/prefix.db > /dev/null 2>&1; then
printf "\
You are running an old version of terminusdb-server. The new %s version
uses a very different data structure which breaks backwards compatibility.
Continuing will remove your existing databases.\n" "$TERMINUSDB_QUICKSTART_TAG"
remove_old_volumes
fi
# Any other case will be handled by comparing storage version
retrieve_storage_version
STORAGE_VERSION=$?
retrieve_expected_storage_version
EXPECTED_STORAGE_VERSION=$?
if [ $STORAGE_VERSION -gt $EXPECTED_STORAGE_VERSION ];then
echo "Your TerminusDB store is not supported by this version of TerminusDB. Please upgrade TerminusDB."
exit 1
elif [ $STORAGE_VERSION -lt $EXPECTED_STORAGE_VERSION ];then
echo "Your store is outdated and needs to be upgraded. Run \`terminusdb-container upgrade\` to do so."
exit 1
fi
}
if [[ -z "$TERMINUSDB_LABEL_FILE" ]]; then
TERMINUSDB_QUICKSTART_SET_LABEL_FILE=""
else
TERMINUSDB_QUICKSTART_SET_LABEL_FILE="--label-file \"$TERMINUSDB_LABEL_FILE\""
fi
if [[ -z "$TERMINUSDB_NETWORK" ]]; then
TERMINUSDB_QUICKSTART_SET_NETWORK=""
else
TERMINUSDB_QUICKSTART_SET_NETWORK="--network \"$TERMINUSDB_NETWORK\""
fi
if [[ -z "$TERMINUSDB_LOCAL" ]]; then
TERMINUSDB_QUICKSTART_MOUNT_LOCAL=""
else
TERMINUSDB_QUICKSTART_MOUNT_LOCAL="-v \"$TERMINUSDB_LOCAL\":/app/local_files"
fi
if [[ -z "$TERMINUSDB_DASHBOARD_LOCAL_PATH" ]]; then
TERMINUSDB_QUICKSTART_DASHBOARD_COMMAND=""
else
TERMINUSDB_QUICKSTART_DASHBOARD_COMMAND="-v \"$TERMINUSDB_DASHBOARD_LOCAL_PATH\":/app/terminusdb/dashboard"
fi
#
# FUNCTIONS
#
_check_if_docker_is_in_path () {
if ! $TERMINUSDB_QUICKSTART_DOCKER --version > /dev/null; then
printf "Error! We searched for a way to run '%s', but, sadly, we couldn't find it.\n" \
"$TERMINUSDB_QUICKSTART_DOCKER"
exit 1
fi
}
# Run platform check
_platform_check () {
PLATFORM_ARCHITECTURE=$(uname -m)
if [[ $PLATFORM_ARCHITECTURE != "x86_64" ]] && [[ $PLATFORM_ARCHITECTURE != "arm64" ]]; then
printf "\
There are no Docker images for other CPU architectures than x86_64 and arm64 yet.
You can try to manually compile the components of TerminusDB by following the
instructions on the terminusdb-server repository although other architectures are not
officially supported yet.
https://github.com/terminusdb/terminusdb-server/blob/master/docs/BUILD.md \n"
exit 4
fi
}
_usage () {
printf "\
USAGE:
terminusdb-container [COMMAND]
help show usage
run run container
cli use the terminusdb cli
stop stop container
attach attach to prolog shell
exec execeute a command inside the container
upgrade attempt to upgrade the storage to the latest version of terminusdb
rm remove volumes\n"
}
_confirm() {
read -r -p "Are you sure? [y/N] " RESPONSE
case "$RESPONSE" in
[yY][eE][sS]|[yY])
return 0
;;
*)
return 1
;;
esac
}
retrieve_storage_version () {
# shellcheck disable=SC2046
return $(eval "$TERMINUSDB_QUICKSTART_DOCKER" run --rm \
"-v \"$TERMINUSDB_QUICKSTART_STORAGE:/storage:rw\"" \
"$TERMINUSDB_QUICKSTART_REPOSITORY:$TERMINUSDB_QUICKSTART_TAG" \
cat /storage/db/STORAGE_VERSION)
}
retrieve_terminusdb_major_version() {
# shellcheck disable=SC2046
return $(eval "$TERMINUSDB_QUICKSTART_DOCKER" run --rm \
"-v \"$TERMINUSDB_QUICKSTART_STORAGE:/storage:rw\"" \
"$TERMINUSDB_QUICKSTART_REPOSITORY:$TERMINUSDB_QUICKSTART_TAG" \
./terminusdb --version|grep 'TerminusDB v'|perl -pe 's|TerminusDB v([0-9]+).*|\1|')
}
retrieve_expected_storage_version() {
retrieve_terminusdb_major_version
MAJOR_VERSION=$?
case $MAJOR_VERSION in
10)
return 1
;;
11)
return 2
;;
*)
echo "TerminusDB version $MAJOR_VERSION is not supported by bootstrap"
exit 1
;;
esac
}
upgrade_image() {
if [[ $TERMINUSDB_QUICKSTART_TAG == "latest" || $TERMINUSDB_QUICKSTART_TAG == "dev" ]]; then
$TERMINUSDB_QUICKSTART_DOCKER image rm "$TERMINUSDB_QUICKSTART_REPOSITORY:$TERMINUSDB_QUICKSTART_TAG" 2>/dev/null
fi
}
_run () {
if [[ -z "$TERMINUSDB_QUICKSTART_JWKS" ]]; then
TERMINUSDB_QUICKSTART_JWKS_ENV=""
else
TERMINUSDB_QUICKSTART_JWKS_ENV="-e TERMINUSDB_SERVER_JWKS_ENDPOINT=\"$TERMINUSDB_QUICKSTART_JWKS\""
fi
eval "$TERMINUSDB_QUICKSTART_DOCKER" run -d -it --rm \
"--name \"$TERMINUSDB_QUICKSTART_CONTAINER\" -p \"$TERMINUSDB_QUICKSTART_SERVER_IP:$TERMINUSDB_QUICKSTART_PORT:6363\"" \
"-v \"$TERMINUSDB_QUICKSTART_STORAGE:$TERMINUSDB_QUICKSTART_STORAGE_VOLUME:rw\"" \
"$TERMINUSDB_QUICKSTART_DASHBOARD_COMMAND" \
"$TERMINUSDB_QUICKSTART_MOUNT_LOCAL" \
"$TERMINUSDB_QUICKSTART_SET_LABEL_FILE" \
"$TERMINUSDB_QUICKSTART_SET_NETWORK" \
"-e TERMINUSDB_SERVER_PORT=6363" \
"$TERMINUSDB_QUICKSTART_JWKS_ENV" \
"-e TERMINUSDB_INSECURE_USER_HEADER_ENABLED=\"$TERMINUSDB_QUICKSTART_INSECURE_USER_HEADER_ENABLED\"" \
"-e TERMINUSDB_LOG_FORMAT=\"$TERMINUSDB_LOG_FORMAT\"" \
"-e TERMINUSDB_LOG_LEVEL=\"$TERMINUSDB_LOG_LEVEL\"" \
"-e TERMINUSDB_INSECURE_USER_HEADER=\"$TERMINUSDB_INSECURE_USER_HEADER\"" \
"-e TERMINUSDB_ADMIN_PASS=\"$TERMINUSDB_QUICKSTART_PASS\"" \
"-e TERMINUSDB_AUTOLOGIN_ENABLED=\"$TERMINUSDB_QUICKSTART_AUTOLOGIN\"" \
"-e TERMINUSDB_JWT_ENABLED=\"$TERMINUSDB_QUICKSTART_JWT_ENABLED\"" \
"-e TERMINUSDB_JWT_AGENT_NAME_PROPERTY=\"$TERMINUSDB_QUICKSTART_JWT_AGENT_NAME_PROPERTY\"" \
"$TERMINUSDB_QUICKSTART_REPOSITORY:$TERMINUSDB_QUICKSTART_TAG"
}
_cli () {
EXTRA_OPTIONS=""
if echo "$TERMINUSDB_RUN_CMD" | grep -q '^\(fetch\|clone\|push\|pull\|user\)'
then
EXTRA_OPTIONS="-t"
fi
eval "$TERMINUSDB_QUICKSTART_DOCKER" -i "$EXTRA_OPTIONS" \
"$TERMINUSDB_QUICKSTART_CONTAINER" \
"./terminusdb" \
"$TERMINUSDB_RUN_CMD"
}
_stop () {
$TERMINUSDB_QUICKSTART_DOCKER stop "$TERMINUSDB_QUICKSTART_CONTAINER"
}
_restart () {
$TERMINUSDB_QUICKSTART_DOCKER stop "$TERMINUSDB_QUICKSTART_CONTAINER"
}
_attach () {
$TERMINUSDB_QUICKSTART_DOCKER attach "$TERMINUSDB_QUICKSTART_CONTAINER"
}
_exec () {
if [[ -n "$TERMINUSDB_RUN_CMD" ]]; then
eval "$TERMINUSDB_QUICKSTART_DOCKER" exec -it \
"$TERMINUSDB_QUICKSTART_CONTAINER" \
"$TERMINUSDB_RUN_CMD"
fi
}
_rm () {
if _confirm; then
$TERMINUSDB_QUICKSTART_DOCKER volume rm "$TERMINUSDB_QUICKSTART_STORAGE"
fi
}
_serve () {
if command -v python >/dev/null; then
python -m http.server 8888
else
printf "\npython is required for this feature\n"
fi
}
_upgrade() {
$TERMINUSDB_QUICKSTART_DOCKER run \
-e TERMINUSDB_QUICKSTART_STORAGE="$TERMINUSDB_QUICKSTART_STORAGE_VOLUME" \
-v "$TERMINUSDB_QUICKSTART_STORAGE:$TERMINUSDB_QUICKSTART_STORAGE_VOLUME:rw" \
--rm terminusdb/terminusdb-upgrade
}
printf "\nTERMINUSDB-BOOTSTRAP IS DEPRECATED. Use the docker compose in the main TerminusDB repository instead or check our "
printf "documentation regarding server and Kubernetes deployments\n\n"
_platform_check
#
# PROCESS ARGS
#
if [[ -n "$1" ]]; then
case "$1" in
"help")
_usage
;;
"run")
# _build
_check_if_docker_is_in_path
upgrade_image
version_check
_run > /dev/null \
&& printf "terminusdb-server container started %s\n" "$TERMINUSDB_URL" \
|| printf "\nIs the container already running?\n"
;;
"cli")
_check_if_docker_is_in_path
if [[ -n "$2" ]]; then
# shellcheck disable=SC2124
TERMINUSDB_RUN_CMD="${@:2}"
fi
_cli || printf "\nError on executing the CLI command, did you forget to run the container first with the run command?"
;;
"stop")
_check_if_docker_is_in_path
_stop > /dev/null \
&& printf "terminusdb-server container stopped\n" \
|| printf "\nIs the container running?\n"
;;
"attach")
_check_if_docker_is_in_path
printf "Ctrl+p Ctrl+q to detach\n\n"
_attach
;;
"rm")
_check_if_docker_is_in_path
printf "This will delete storage volume\n"
_rm \
|| printf "\nIs the container stopped?\n"
;;
"exec")
_check_if_docker_is_in_path
if [[ -n "$2" ]]; then
# shellcheck disable=SC2124
TERMINUSDB_RUN_CMD="${@:2}"
fi
printf "This will exec a command on terminusdb-server\n"
_exec \
|| printf "\nIs the container stopped?\n"
;;
"upgrade")
_upgrade
;;
"nop")
true
;;
"serve")
_check_if_docker_is_in_path
_serve
;;
*)
printf "Error! Command unknown: %s\n\n" "$1"
_usage
exit 1
;;
esac
else
_usage
fi
# vim:ft=sh