Skip to content

loading data files into cqlsh before the container get's exposed #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion 3.11/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
#!/bin/bash
set -e

# execute a cql file as a string statement to cqlsh
_execute() {
statement=$(<$1)
until echo "$statement" | cqlsh; do
echo "cqlsh: Cassandra is unavailable - retry later"
sleep 2
done
}

# determing how to execute the file based on extention
_process_init_file() {
local f="$1"; shift

case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.cql) echo "$0: running $f"; _execute "$f"; echo ;;
*.cql.gz) echo "$0: running $f"; gunzip -c "$f" | _execute; echo ;;
*) echo "$0: ignoring $f" ;;
esac
}

_check_files() {
until cqlsh -e 'describe cluster'; do
# processing the files in the data directory
echo "cassandra not ready, will wait";
sleep 2
done
echo "cassandra ready, processing files";
for f in ./data/*; do
echo "processing file $f"
_process_init_file "$f"
done
}

# first arg is `-f` or `--some-option`
# or there are no args
if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then
Expand All @@ -25,6 +59,24 @@ _ip_address() {
'
}

# when there is a ./data directory mounted, we want to process the files in it.
# first test if the "data" directory exists and contains files
if [ -d "./data" ]; then
if [ ! -z "$(ls -A /.data)" ]; then
exec "$@" &
# if there is an init-db.cql file, we probably want it to be executed first
# I rename it because scripts are executed in alphabetic order
if [ -e /data/init-db.cql ]
then
echo "Found an init-db.cql file"
mv /data/init-db.cql /data/1-init-db.cql
fi
_check_files
# after the files are loaded, restart cassandra with the normal settings.
pkill -f 'java.*cassandra'
fi
fi

if [ "$1" = 'cassandra' ]; then
: ${CASSANDRA_RPC_ADDRESS='0.0.0.0'}

Expand All @@ -45,7 +97,7 @@ if [ "$1" = 'cassandra' ]; then
fi
: ${CASSANDRA_SEEDS:="$CASSANDRA_BROADCAST_ADDRESS"}

sed -ri 's/(- seeds:).*/\1 "'"$CASSANDRA_SEEDS"'"/' "$CASSANDRA_CONFIG/cassandra.yaml"
sed -ri 's/(- seeds:).*/\1 "'"$CASSANDRA_SEEDS"'"/' "$CASSANDRA_CONFIG/cassandra.yaml"

for yaml in \
broadcast_address \
Expand All @@ -71,6 +123,7 @@ if [ "$1" = 'cassandra' ]; then
sed -ri 's/^('"$rackdc"'=).*/\1 '"$val"'/' "$CASSANDRA_CONFIG/cassandra-rackdc.properties"
fi
done

fi

exec "$@"