CodeCommitsIssuesPull requestsActionsInsightsSecurity
8579c26efb474e273972b1a49e6557ae80c9ab9b

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

docker/server/entrypoint.sh

103lines · modecode

1#!/bin/bash
2
3# set some vars
4CLICKHOUSE_CONFIG="${CLICKHOUSE_CONFIG:-/etc/clickhouse-server/config.xml}"
5if [ x"$UID" == x0 ]; then
6 USER="$(id -u clickhouse)"
7 GROUP="$(id -g clickhouse)"
8 gosu="gosu $USER:$GROUP"
9else
10 USER="$(id -u)"
11 GROUP="$(id -g)"
12 gosu=""
13fi
14
15# port is needed to check if clickhouse-server is ready for connections
16HTTP_PORT="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=http_port)"
17
18# get CH directories locations
19DATA_DIR="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=path || true)"
20TMP_DIR="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=tmp_path || true)"
21USER_PATH="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=user_files_path || true)"
22LOG_PATH="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=logger.log || true)"
23LOG_DIR="$(dirname $LOG_PATH || true)"
24ERROR_LOG_PATH="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=logger.errorlog || true)"
25ERROR_LOG_DIR="$(dirname $ERROR_LOG_PATH || true)"
26FORMAT_SCHEMA_PATH="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=format_schema_path || true)"
27CLICKHOUSE_USER="${CLICKHOUSE_USER:-default}"
28
29for dir in "$DATA_DIR" \
30 "$ERROR_LOG_DIR" \
31 "$LOG_DIR" \
32 "$TMP_DIR" \
33 "$USER_PATH" \
34 "$FORMAT_SCHEMA_PATH"
35do
36 # check if variable not empty
37 [ -z "$dir" ] && continue
38 # ensure directories exist
39 if ! mkdir -p "$dir"; then
40 echo "Couldn't create necessary directory: $dir"
41 exit 1
42 fi
43
44 if [ x"$UID" == x0 ] && [ "$CLICKHOUSE_DO_NOT_CHOWN" != "1" ]; then
45 # ensure proper directories permissions
46 chown -R "$USER:$GROUP" "$dir"
47 elif [ "$(stat -c %u "$dir")" != "$USER" ]; then
48 echo "Necessary directory '$dir' isn't owned by user with id '$USER'"
49 exit 1
50 fi
51done
52
53
54
55if [ -n "$(ls /docker-entrypoint-initdb.d/)" ]; then
56 $gosu /usr/bin/clickhouse-server --config-file=$CLICKHOUSE_CONFIG &
57 pid="$!"
58
59 # check if clickhouse is ready to accept connections
60 # will try to send ping clickhouse via http_port (max 12 retries, with 1 sec delay)
61 if ! wget --spider --quiet --tries=12 --waitretry=1 --retry-connrefused "http://localhost:$HTTP_PORT/ping" ; then
62 echo >&2 'ClickHouse init process failed.'
63 exit 1
64 fi
65
66 if [ ! -z "$CLICKHOUSE_PASSWORD" ]; then
67 printf -v WITH_PASSWORD '%s %q' "--password" "$CLICKHOUSE_PASSWORD"
68 fi
69
70 clickhouseclient=( clickhouse-client --multiquery -u $CLICKHOUSE_USER $WITH_PASSWORD )
71
72 echo
73 for f in /docker-entrypoint-initdb.d/*; do
74 case "$f" in
75 *.sh)
76 if [ -x "$f" ]; then
77 echo "$0: running $f"
78 "$f"
79 else
80 echo "$0: sourcing $f"
81 . "$f"
82 fi
83 ;;
84 *.sql) echo "$0: running $f"; cat "$f" | "${clickhouseclient[@]}" ; echo ;;
85 *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${clickhouseclient[@]}"; echo ;;
86 *) echo "$0: ignoring $f" ;;
87 esac
88 echo
89 done
90
91 if ! kill -s TERM "$pid" || ! wait "$pid"; then
92 echo >&2 'Finishing of ClickHouse init process failed.'
93 exit 1
94 fi
95fi
96
97# if no args passed to `docker run` or first argument start with `--`, then the user is passing clickhouse-server arguments
98if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then
99 exec $gosu /usr/bin/clickhouse-server --config-file=$CLICKHOUSE_CONFIG "$@"
100fi
101
102# Otherwise, we assume the user want to run his own process, for example a `bash` shell to explore this image
103exec "$@"