init-influxdb.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/bin/bash
  2. set -e
  3. AUTH_ENABLED="$INFLUXDB_HTTP_AUTH_ENABLED"
  4. if [ -z "$AUTH_ENABLED" ]; then
  5. AUTH_ENABLED="$(grep -iE '^\s*auth-enabled\s*=\s*true' /etc/influxdb/influxdb.conf | grep -io 'true' | cat)"
  6. else
  7. AUTH_ENABLED="$(echo "$INFLUXDB_HTTP_AUTH_ENABLED" | grep -io 'true' | cat)"
  8. fi
  9. INIT_USERS=$([ ! -z "$AUTH_ENABLED" ] && [ ! -z "$INFLUXDB_ADMIN_USER" ] && echo 1 || echo)
  10. if ( [ ! -z "$INIT_USERS" ] || [ ! -z "$INFLUXDB_DB" ] || [ "$(ls -A /docker-entrypoint-initdb.d 2> /dev/null)" ] ) && [ ! "$(ls -d /var/lib/influxdb/meta 2>/dev/null)" ]; then
  11. INIT_QUERY=""
  12. CREATE_DB_QUERY="CREATE DATABASE $INFLUXDB_DB"
  13. if [ ! -z "$INIT_USERS" ]; then
  14. if [ -z "$INFLUXDB_ADMIN_PASSWORD" ]; then
  15. INFLUXDB_ADMIN_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
  16. echo "INFLUXDB_ADMIN_PASSWORD:$INFLUXDB_ADMIN_PASSWORD"
  17. fi
  18. INIT_QUERY="CREATE USER \"$INFLUXDB_ADMIN_USER\" WITH PASSWORD '$INFLUXDB_ADMIN_PASSWORD' WITH ALL PRIVILEGES"
  19. elif [ ! -z "$INFLUXDB_DB" ]; then
  20. INIT_QUERY="$CREATE_DB_QUERY"
  21. else
  22. INIT_QUERY="SHOW DATABASES"
  23. fi
  24. INFLUXDB_INIT_PORT="8086"
  25. INFLUXDB_HTTP_BIND_ADDRESS=127.0.0.1:$INFLUXDB_INIT_PORT INFLUXDB_HTTP_HTTPS_ENABLED=false influxd "$@" &
  26. pid="$!"
  27. INFLUX_CMD="influx -host 127.0.0.1 -port $INFLUXDB_INIT_PORT -execute "
  28. for i in {30..0}; do
  29. if $INFLUX_CMD "$INIT_QUERY" &> /dev/null; then
  30. break
  31. fi
  32. echo 'influxdb init process in progress...'
  33. sleep 1
  34. done
  35. if [ "$i" = 0 ]; then
  36. echo >&2 'influxdb init process failed.'
  37. exit 1
  38. fi
  39. if [ ! -z "$INIT_USERS" ]; then
  40. INFLUX_CMD="influx -host 127.0.0.1 -port $INFLUXDB_INIT_PORT -username ${INFLUXDB_ADMIN_USER} -password ${INFLUXDB_ADMIN_PASSWORD} -execute "
  41. if [ ! -z "$INFLUXDB_DB" ]; then
  42. $INFLUX_CMD "$CREATE_DB_QUERY"
  43. fi
  44. if [ ! -z "$INFLUXDB_USER" ] && [ -z "$INFLUXDB_USER_PASSWORD" ]; then
  45. INFLUXDB_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
  46. echo "INFLUXDB_USER_PASSWORD:$INFLUXDB_USER_PASSWORD"
  47. fi
  48. if [ ! -z "$INFLUXDB_USER" ]; then
  49. $INFLUX_CMD "CREATE USER \"$INFLUXDB_USER\" WITH PASSWORD '$INFLUXDB_USER_PASSWORD'"
  50. $INFLUX_CMD "REVOKE ALL PRIVILEGES FROM \"$INFLUXDB_USER\""
  51. if [ ! -z "$INFLUXDB_DB" ]; then
  52. $INFLUX_CMD "GRANT ALL ON \"$INFLUXDB_DB\" TO \"$INFLUXDB_USER\""
  53. fi
  54. fi
  55. if [ ! -z "$INFLUXDB_WRITE_USER" ] && [ -z "$INFLUXDB_WRITE_USER_PASSWORD" ]; then
  56. INFLUXDB_WRITE_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
  57. echo "INFLUXDB_WRITE_USER_PASSWORD:$INFLUXDB_WRITE_USER_PASSWORD"
  58. fi
  59. if [ ! -z "$INFLUXDB_WRITE_USER" ]; then
  60. $INFLUX_CMD "CREATE USER \"$INFLUXDB_WRITE_USER\" WITH PASSWORD '$INFLUXDB_WRITE_USER_PASSWORD'"
  61. $INFLUX_CMD "REVOKE ALL PRIVILEGES FROM \"$INFLUXDB_WRITE_USER\""
  62. if [ ! -z "$INFLUXDB_DB" ]; then
  63. $INFLUX_CMD "GRANT WRITE ON \"$INFLUXDB_DB\" TO \"$INFLUXDB_WRITE_USER\""
  64. fi
  65. fi
  66. if [ ! -z "$INFLUXDB_READ_USER" ] && [ -z "$INFLUXDB_READ_USER_PASSWORD" ]; then
  67. INFLUXDB_READ_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
  68. echo "INFLUXDB_READ_USER_PASSWORD:$INFLUXDB_READ_USER_PASSWORD"
  69. fi
  70. if [ ! -z "$INFLUXDB_READ_USER" ]; then
  71. $INFLUX_CMD "CREATE USER \"$INFLUXDB_READ_USER\" WITH PASSWORD '$INFLUXDB_READ_USER_PASSWORD'"
  72. $INFLUX_CMD "REVOKE ALL PRIVILEGES FROM \"$INFLUXDB_READ_USER\""
  73. if [ ! -z "$INFLUXDB_DB" ]; then
  74. $INFLUX_CMD "GRANT READ ON \"$INFLUXDB_DB\" TO \"$INFLUXDB_READ_USER\""
  75. fi
  76. fi
  77. fi
  78. for f in /docker-entrypoint-initdb.d/*; do
  79. case "$f" in
  80. *.sh) echo "$0: running $f"; . "$f" ;;
  81. *.iql) echo "$0: running $f"; $INFLUX_CMD "$(cat ""$f"")"; echo ;;
  82. *) echo "$0: ignoring $f" ;;
  83. esac
  84. echo
  85. done
  86. if ! kill -s TERM "$pid" || ! wait "$pid"; then
  87. echo >&2 'influxdb init process failed. (Could not stop influxdb)'
  88. exit 1
  89. fi
  90. fi