Skip to content

Commit

Permalink
Added the possibility to initialize influxdb
Browse files Browse the repository at this point in the history
Includes support for custom .sh-scripts and .iql-files on influxdb
initialization.
  • Loading branch information
PSanetra authored and jsternberg committed Aug 31, 2017
1 parent 941d149 commit 98b2605
Show file tree
Hide file tree
Showing 20 changed files with 1,020 additions and 2 deletions.
47 changes: 47 additions & 0 deletions circle-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ log_msg "Verifying docker daemon connectivity"
docker version

failed_builds=()
tags=()

# Gather directories with a Dockerfile and sanitize the path to remove leading
# a leading ./ and multiple slashes into a single slash.
Expand All @@ -34,6 +35,8 @@ for path in $dockerfiles; do
log_msg "Building docker image $tag (from $path)"
if ! docker_build -t "$tag" "$path"; then
failed_builds+=("$tag")
else
tags+=("$tag")
fi
done

Expand All @@ -46,3 +49,47 @@ else
done
exit ${#failed_builds[@]}
fi

# Image tests:

assert_equals() {
local actual=$1
local expected=$2
local test_name=$3

if [ "$actual" != "$expected" ]; then
msg="$test_name: '$actual' is not equal to '$expected'"
failed_tests+=("$msg")
fi
}

assert_contains() {
local actual="$1"
local expected_substring="$2"
local test_name="$3"

if [[ "$actual" != *"$expected_substring"* ]]; then
msg="$test_name: '$actual' does not contain '$expected_substring'"
failed_tests+=("$msg")
fi
}

failed_tests=()

# Iterate over every circle-test.sh script in any subdirectory
circle_tests=$(find "$dir" -mindepth 2 -name circle-test.sh -print0)
for path in $circle_tests; do
log_msg "Executing $path"
. $path
done

if [ ${#failed_tests[@]} -eq 0 ]; then
log_msg "All tests succeeded."
else
log_msg "The following tests failed:"
for ((i = 0; i < ${#failed_tests[@]}; i++)); do
echo " ${failed_tests[$i]}"
done
exit ${#failed_tests[@]}
fi

1 change: 1 addition & 0 deletions influxdb/1.2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ EXPOSE 8086
VOLUME /var/lib/influxdb

COPY entrypoint.sh /entrypoint.sh
COPY init-influxdb.sh /init-influxdb.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["influxd"]
3 changes: 3 additions & 0 deletions influxdb/1.2/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ FROM alpine:3.5

RUN echo 'hosts: files dns' >> /etc/nsswitch.conf

RUN apk add --no-cache bash

ENV INFLUXDB_VERSION 1.2.4
RUN set -ex && \
apk add --no-cache --virtual .build-deps wget gnupg tar ca-certificates && \
Expand Down Expand Up @@ -30,5 +32,6 @@ EXPOSE 8086
VOLUME /var/lib/influxdb

COPY entrypoint.sh /entrypoint.sh
COPY init-influxdb.sh /init-influxdb.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["influxd"]
4 changes: 4 additions & 0 deletions influxdb/1.2/alpine/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ if [ "${1:0:1}" = '-' ]; then
set -- influxd "$@"
fi

if [ "$1" = 'influxd' ]; then
/init-influxdb.sh "${@:2}"
fi

exec "$@"
120 changes: 120 additions & 0 deletions influxdb/1.2/alpine/init-influxdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/bin/bash
set -e

AUTH_ENABLED="$INFLUXDB_HTTP_AUTH_ENABLED"

if [ -z "$AUTH_ENABLED" ]; then
AUTH_ENABLED="$(grep -iE '^\s*auth-enabled\s*=\s*true' /etc/influxdb/influxdb.conf | grep -io 'true' | cat)"
else
AUTH_ENABLED="$(echo ""$INFLUXDB_HTTP_AUTH_ENABLED"" | grep -io 'true' | cat)"
fi

INIT_USERS=$([ ! -z "$AUTH_ENABLED" ] && [ ! -z "$INFLUXDB_ADMIN_USER" ] && echo 1 || echo)

if ( [ ! -z "$INIT_USERS" ] || [ ! -z "$INFLUXDB_DB" ] || [ "$(ls -A /docker-entrypoint-initdb.d 2> /dev/null)" ] ) && [ ! "$(ls -A /var/lib/influxdb)" ]; then

INIT_QUERY=""
CREATE_DB_QUERY="CREATE DATABASE $INFLUXDB_DB"

if [ ! -z "$INIT_USERS" ]; then

if [ -z "$INFLUXDB_ADMIN_PASSWORD" ]; then
INFLUXDB_ADMIN_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
echo "INFLUXDB_ADMIN_PASSWORD:$INFLUXDB_ADMIN_PASSWORD"
fi

INIT_QUERY="CREATE USER $INFLUXDB_ADMIN_USER WITH PASSWORD '$INFLUXDB_ADMIN_PASSWORD' WITH ALL PRIVILEGES"
elif [ ! -z "$INFLUXDB_DB" ]; then
INIT_QUERY="$CREATE_DB_QUERY"
else
INIT_QUERY="SHOW DATABASES"
fi

INFLUXDB_INIT_PORT="8086"

INFLUXDB_HTTP_BIND_ADDRESS=127.0.0.1:$INFLUXDB_INIT_PORT influxd "$@" &
pid="$!"

INFLUX_CMD="influx -host 127.0.0.1 -port $INFLUXDB_INIT_PORT -execute "

for i in {30..0}; do
if $INFLUX_CMD "$INIT_QUERY" &> /dev/null; then
break
fi
echo 'influxdb init process in progress...'
sleep 1
done

if [ "$i" = 0 ]; then
echo >&2 'influxdb init process failed.'
exit 1
fi

if [ ! -z "$INIT_USERS" ]; then

INFLUX_CMD="influx -host 127.0.0.1 -port $INFLUXDB_INIT_PORT -username ${INFLUXDB_ADMIN_USER} -password ${INFLUXDB_ADMIN_PASSWORD} -execute "

if [ ! -z "$INFLUXDB_DB" ]; then
$INFLUX_CMD "$CREATE_DB_QUERY"
fi

if [ ! -z "$INFLUXDB_USER" ] && [ -z "$INFLUXDB_USER_PASSWORD" ]; then
INFLUXDB_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
echo "INFLUXDB_USER_PASSWORD:$INFLUXDB_USER_PASSWORD"
fi

if [ ! -z "$INFLUXDB_USER" ]; then
$INFLUX_CMD "CREATE USER $INFLUXDB_USER WITH PASSWORD '$INFLUXDB_USER_PASSWORD'"

$INFLUX_CMD "REVOKE ALL PRIVILEGES FROM ""$INFLUXDB_USER"""

if [ ! -z "$INFLUXDB_DB" ]; then
$INFLUX_CMD "GRANT ALL ON ""$INFLUXDB_DB"" TO ""$INFLUXDB_USER"""
fi
fi

if [ ! -z "$INFLUXDB_WRITE_USER" ] && [ -z "$INFLUXDB_WRITE_USER_PASSWORD" ]; then
INFLUXDB_WRITE_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
echo "INFLUXDB_WRITE_USER_PASSWORD:$INFLUXDB_WRITE_USER_PASSWORD"
fi

if [ ! -z "$INFLUXDB_WRITE_USER" ]; then
$INFLUX_CMD "CREATE USER $INFLUXDB_WRITE_USER WITH PASSWORD '$INFLUXDB_WRITE_USER_PASSWORD'"
$INFLUX_CMD "REVOKE ALL PRIVILEGES FROM ""$INFLUXDB_WRITE_USER"""

if [ ! -z "$INFLUXDB_DB" ]; then
$INFLUX_CMD "GRANT WRITE ON ""$INFLUXDB_DB"" TO ""$INFLUXDB_WRITE_USER"""
fi
fi

if [ ! -z "$INFLUXDB_READ_USER" ] && [ -z "$INFLUXDB_READ_USER_PASSWORD" ]; then
INFLUXDB_READ_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
echo "INFLUXDB_READ_USER_PASSWORD:$INFLUXDB_READ_USER_PASSWORD"
fi

if [ ! -z "$INFLUXDB_READ_USER" ]; then
$INFLUX_CMD "CREATE USER $INFLUXDB_READ_USER WITH PASSWORD '$INFLUXDB_READ_USER_PASSWORD'"
$INFLUX_CMD "REVOKE ALL PRIVILEGES FROM ""$INFLUXDB_READ_USER"""

if [ ! -z "$INFLUXDB_DB" ]; then
$INFLUX_CMD "GRANT READ ON ""$INFLUXDB_DB"" TO ""$INFLUXDB_READ_USER"""
fi
fi

fi

for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.iql) echo "$0: running $f"; $INFLUX_CMD "$(cat ""$f"")"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done

if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'influxdb init process failed. (Could not stop influxdb)'
exit 1
fi

fi
6 changes: 5 additions & 1 deletion influxdb/1.2/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ if [ "${1:0:1}" = '-' ]; then
set -- influxd "$@"
fi

exec "$@"
if [ "$1" = 'influxd' ]; then
/init-influxdb.sh "${@:2}"
fi

exec "$@"
120 changes: 120 additions & 0 deletions influxdb/1.2/init-influxdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/bin/bash
set -e

AUTH_ENABLED="$INFLUXDB_HTTP_AUTH_ENABLED"

if [ -z "$AUTH_ENABLED" ]; then
AUTH_ENABLED="$(grep -iE '^\s*auth-enabled\s*=\s*true' /etc/influxdb/influxdb.conf | grep -io 'true' | cat)"
else
AUTH_ENABLED="$(echo ""$INFLUXDB_HTTP_AUTH_ENABLED"" | grep -io 'true' | cat)"
fi

INIT_USERS=$([ ! -z "$AUTH_ENABLED" ] && [ ! -z "$INFLUXDB_ADMIN_USER" ] && echo 1 || echo)

if ( [ ! -z "$INIT_USERS" ] || [ ! -z "$INFLUXDB_DB" ] || [ "$(ls -A /docker-entrypoint-initdb.d 2> /dev/null)" ] ) && [ ! "$(ls -A /var/lib/influxdb)" ]; then

INIT_QUERY=""
CREATE_DB_QUERY="CREATE DATABASE $INFLUXDB_DB"

if [ ! -z "$INIT_USERS" ]; then

if [ -z "$INFLUXDB_ADMIN_PASSWORD" ]; then
INFLUXDB_ADMIN_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
echo "INFLUXDB_ADMIN_PASSWORD:$INFLUXDB_ADMIN_PASSWORD"
fi

INIT_QUERY="CREATE USER $INFLUXDB_ADMIN_USER WITH PASSWORD '$INFLUXDB_ADMIN_PASSWORD' WITH ALL PRIVILEGES"
elif [ ! -z "$INFLUXDB_DB" ]; then
INIT_QUERY="$CREATE_DB_QUERY"
else
INIT_QUERY="SHOW DATABASES"
fi

INFLUXDB_INIT_PORT="8086"

INFLUXDB_HTTP_BIND_ADDRESS=127.0.0.1:$INFLUXDB_INIT_PORT influxd "$@" &
pid="$!"

INFLUX_CMD="influx -host 127.0.0.1 -port $INFLUXDB_INIT_PORT -execute "

for i in {30..0}; do
if $INFLUX_CMD "$INIT_QUERY" &> /dev/null; then
break
fi
echo 'influxdb init process in progress...'
sleep 1
done

if [ "$i" = 0 ]; then
echo >&2 'influxdb init process failed.'
exit 1
fi

if [ ! -z "$INIT_USERS" ]; then

INFLUX_CMD="influx -host 127.0.0.1 -port $INFLUXDB_INIT_PORT -username ${INFLUXDB_ADMIN_USER} -password ${INFLUXDB_ADMIN_PASSWORD} -execute "

if [ ! -z "$INFLUXDB_DB" ]; then
$INFLUX_CMD "$CREATE_DB_QUERY"
fi

if [ ! -z "$INFLUXDB_USER" ] && [ -z "$INFLUXDB_USER_PASSWORD" ]; then
INFLUXDB_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
echo "INFLUXDB_USER_PASSWORD:$INFLUXDB_USER_PASSWORD"
fi

if [ ! -z "$INFLUXDB_USER" ]; then
$INFLUX_CMD "CREATE USER $INFLUXDB_USER WITH PASSWORD '$INFLUXDB_USER_PASSWORD'"

$INFLUX_CMD "REVOKE ALL PRIVILEGES FROM ""$INFLUXDB_USER"""

if [ ! -z "$INFLUXDB_DB" ]; then
$INFLUX_CMD "GRANT ALL ON ""$INFLUXDB_DB"" TO ""$INFLUXDB_USER"""
fi
fi

if [ ! -z "$INFLUXDB_WRITE_USER" ] && [ -z "$INFLUXDB_WRITE_USER_PASSWORD" ]; then
INFLUXDB_WRITE_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
echo "INFLUXDB_WRITE_USER_PASSWORD:$INFLUXDB_WRITE_USER_PASSWORD"
fi

if [ ! -z "$INFLUXDB_WRITE_USER" ]; then
$INFLUX_CMD "CREATE USER $INFLUXDB_WRITE_USER WITH PASSWORD '$INFLUXDB_WRITE_USER_PASSWORD'"
$INFLUX_CMD "REVOKE ALL PRIVILEGES FROM ""$INFLUXDB_WRITE_USER"""

if [ ! -z "$INFLUXDB_DB" ]; then
$INFLUX_CMD "GRANT WRITE ON ""$INFLUXDB_DB"" TO ""$INFLUXDB_WRITE_USER"""
fi
fi

if [ ! -z "$INFLUXDB_READ_USER" ] && [ -z "$INFLUXDB_READ_USER_PASSWORD" ]; then
INFLUXDB_READ_USER_PASSWORD="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32;echo;)"
echo "INFLUXDB_READ_USER_PASSWORD:$INFLUXDB_READ_USER_PASSWORD"
fi

if [ ! -z "$INFLUXDB_READ_USER" ]; then
$INFLUX_CMD "CREATE USER $INFLUXDB_READ_USER WITH PASSWORD '$INFLUXDB_READ_USER_PASSWORD'"
$INFLUX_CMD "REVOKE ALL PRIVILEGES FROM ""$INFLUXDB_READ_USER"""

if [ ! -z "$INFLUXDB_DB" ]; then
$INFLUX_CMD "GRANT READ ON ""$INFLUXDB_DB"" TO ""$INFLUXDB_READ_USER"""
fi
fi

fi

for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.iql) echo "$0: running $f"; $INFLUX_CMD "$(cat ""$f"")"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done

if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'influxdb init process failed. (Could not stop influxdb)'
exit 1
fi

fi
1 change: 1 addition & 0 deletions influxdb/1.3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ EXPOSE 8086
VOLUME /var/lib/influxdb

COPY entrypoint.sh /entrypoint.sh
COPY init-influxdb.sh /init-influxdb.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["influxd"]
3 changes: 2 additions & 1 deletion influxdb/1.3/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM alpine:3.5

RUN echo 'hosts: files dns' >> /etc/nsswitch.conf
RUN apk add --no-cache tzdata
RUN apk add --no-cache tzdata bash

ENV INFLUXDB_VERSION 1.3.5
RUN set -ex && \
Expand Down Expand Up @@ -31,5 +31,6 @@ EXPOSE 8086
VOLUME /var/lib/influxdb

COPY entrypoint.sh /entrypoint.sh
COPY init-influxdb.sh /init-influxdb.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["influxd"]
4 changes: 4 additions & 0 deletions influxdb/1.3/alpine/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ if [ "${1:0:1}" = '-' ]; then
set -- influxd "$@"
fi

if [ "$1" = 'influxd' ]; then
/init-influxdb.sh "${@:2}"
fi

exec "$@"
Loading

0 comments on commit 98b2605

Please sign in to comment.