cloudflare/ClickHouse
Publicmirrored fromhttps://github.com/cloudflare/ClickHouse
docker/server/entrypoint.sh
103lines · modecode
6 years ago
| 1 | #!/bin/bash |
| 2 | |
| 3 | # set some vars |
| 4 | CLICKHOUSE_CONFIG="${CLICKHOUSE_CONFIG:-/etc/clickhouse-server/config.xml}" |
| 5 | if [ x"$UID" == x0 ]; then |
| 6 | USER="$(id -u clickhouse)" |
| 7 | GROUP="$(id -g clickhouse)" |
| 8 | gosu="gosu $USER:$GROUP" |
| 9 | else |
| 10 | USER="$(id -u)" |
| 11 | GROUP="$(id -g)" |
| 12 | gosu="" |
| 13 | fi |
| 14 | |
| 15 | # port is needed to check if clickhouse-server is ready for connections |
| 16 | HTTP_PORT="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=http_port)" |
| 17 | |
| 18 | # get CH directories locations |
| 19 | DATA_DIR="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=path || true)" |
| 20 | TMP_DIR="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=tmp_path || true)" |
| 21 | USER_PATH="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=user_files_path || true)" |
| 22 | LOG_PATH="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=logger.log || true)" |
| 23 | LOG_DIR="$(dirname $LOG_PATH || true)" |
| 24 | ERROR_LOG_PATH="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=logger.errorlog || true)" |
| 25 | ERROR_LOG_DIR="$(dirname $ERROR_LOG_PATH || true)" |
| 26 | FORMAT_SCHEMA_PATH="$(clickhouse extract-from-config --config-file $CLICKHOUSE_CONFIG --key=format_schema_path || true)" |
| 27 | CLICKHOUSE_USER="${CLICKHOUSE_USER:-default}" |
| 28 | |
| 29 | for dir in "$DATA_DIR" \ |
| 30 | "$ERROR_LOG_DIR" \ |
| 31 | "$LOG_DIR" \ |
| 32 | "$TMP_DIR" \ |
| 33 | "$USER_PATH" \ |
| 34 | "$FORMAT_SCHEMA_PATH" |
| 35 | do |
| 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 |
| 51 | done |
| 52 | |
| 53 | |
| 54 | |
| 55 | if [ -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 |
| 95 | fi |
| 96 | |
| 97 | # if no args passed to `docker run` or first argument start with `--`, then the user is passing clickhouse-server arguments |
| 98 | if [[ $# -lt 1 ]] || [[ "$1" == "--"* ]]; then |
| 99 | exec $gosu /usr/bin/clickhouse-server --config-file=$CLICKHOUSE_CONFIG "$@" |
| 100 | fi |
| 101 | |
| 102 | # Otherwise, we assume the user want to run his own process, for example a `bash` shell to explore this image |
| 103 | exec "$@" |