CodeCommitsIssuesPull requestsActionsInsightsSecurity
b43e75200ef18da61db217f0b93cedaec1dba117

Branches

Tags

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

Clone

HTTPS

Download ZIP

docker/server/entrypoint.sh

97lines · 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)"
27
28for dir in "$DATA_DIR" \
29 "$ERROR_LOG_DIR" \
30 "$LOG_DIR" \
31 "$TMP_DIR" \
32 "$USER_PATH" \
33 "$FORMAT_SCHEMA_PATH"
34do
35 # check if variable not empty
36 [ -z "$dir" ] && continue
37 # ensure directories exist
38 if ! mkdir -p "$dir"; then
39 echo "Couldn't create necessary directory: $dir"
40 exit 1
41 fi
42
43 if [ x"$UID" == x0 ] && [ "$CLICKHOUSE_DO_NOT_CHOWN" != "1" ]; then
44 # ensure proper directories permissions
45 chown -R "$USER:$GROUP" "$dir"
46 elif [ "$(stat -c %u "$dir")" != "$USER" ]; then
47 echo "Necessary directory '$dir' isn't owned by user with id '$USER'"
48 exit 1
49 fi
50done
51
52
53
54if [ -n "$(ls /docker-entrypoint-initdb.d/)" ]; then
55 $gosu /usr/bin/clickhouse-server --config-file=$CLICKHOUSE_CONFIG &
56 pid="$!"
57
58 # check if clickhouse is ready to accept connections
59 # will try to send ping clickhouse via http_port (max 12 retries, with 1 sec delay)
60 if ! wget --spider --quiet --tries=12 --waitretry=1 --retry-connrefused "http://localhost:$HTTP_PORT/ping" ; then
61 echo >&2 'ClickHouse init process failed.'
62 exit 1
63 fi
64
65 clickhouseclient=( clickhouse-client --multiquery )
66 echo
67 for f in /docker-entrypoint-initdb.d/*; do
68 case "$f" in
69 *.sh)
70 if [ -x "$f" ]; then
71 echo "$0: running $f"
72 "$f"
73 else
74 echo "$0: sourcing $f"
75 . "$f"
76 fi
77 ;;
78 *.sql) echo "$0: running $f"; cat "$f" | "${clickhouseclient[@]}" ; echo ;;
79 *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${clickhouseclient[@]}"; echo ;;
80 *) echo "$0: ignoring $f" ;;
81 esac
82 echo
83 done
84
85 if ! kill -s TERM "$pid" || ! wait "$pid"; then
86 echo >&2 'Finishing of ClickHouse init process failed.'
87 exit 1
88 fi
89fi
90
91# if no args passed to `docker run` or first argument start with `--`, then the user is passing clickhouse-server arguments
92if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then
93 exec $gosu /usr/bin/clickhouse-server --config-file=$CLICKHOUSE_CONFIG "$@"
94fi
95
96# Otherwise, we assume the user want to run his own process, for example a `bash` shell to explore this image
97exec "$@"