entrypoint.sh 3.6 KB

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