dockerizing_cassandra/scripts/setup.sh

66 lines
2.2 KiB
Bash

#!/bin/bash
echo -e "RUNNING SETUP" >&2
KEYSPACE="dev_keyspace"
DUMP_DIR="/dump" # Ensure DUMP_DIR is defined
# Determine the IP address of the current node
IP_ADDRESS=$(hostname -I | awk '{print $1}')
# Print the IP address for debugging
echo -e "Node IP Address: $IP_ADDRESS" >&2
# Wait for cassandra1 to be ready if this is not the primary node
if [ "$PRIMARY_NODE" != "true" ]; then
echo -e "Waiting for cassandra1 to be ready..." >&2
/wait-for-it.sh cassandra1:9042 -t 60 -- echo "cassandra1 is ready" >&2
fi
# Start Cassandra in the background
cassandra -R &
# Wait for Cassandra to be ready
echo -e "Waiting for Cassandra to start..." >&2
until cqlsh $IP_ADDRESS -e "SHOW HOST" > /dev/null 2>&1; do
sleep 2
done
# Print the value of PRIMARY_NODE for debugging
echo -e "PRIMARY_NODE is set to: $PRIMARY_NODE" >&2
if [ "$PRIMARY_NODE" = "true" ]; then
# Check if the keyspace exists
echo -e "Checking if keyspace $KEYSPACE exists..." >&2
if ! cqlsh $IP_ADDRESS -e "DESCRIBE KEYSPACE $KEYSPACE;" > /dev/null 2>&1; then
echo -e "Keyspace $KEYSPACE does not exist. Creating keyspace and tables..." >&2
cqlsh $IP_ADDRESS -f /docker-entrypoint-initdb.d/dev_keyspace_schema.cql
else
echo -e "Keyspace $KEYSPACE already exists. Ensuring tables exist..." >&2
# Manually define the schema for legacy tables
cqlsh $IP_ADDRESS -e "CREATE TABLE IF NOT EXISTS dev_keyspace.hashtaggedposts (
id UUID PRIMARY KEY,
-- other columns
);"
# Add similar statements for all other tables
fi
else
echo -e "This is not the primary node. Skipping keyspace and table creation." >&2
fi
# Copy snapshots to the Cassandra data directory
echo -e "Copying snapshots..." >&2
cp -r $DUMP_DIR/* /var/lib/cassandra/data/
# Import snapshots into the Cassandra data directory
echo -e "Importing snapshots..." >&2
for table_dir in $(ls $DUMP_DIR); do
table_name=$(echo $table_dir | sed 's/-[a-f0-9]\{32\}$//')
echo -e "Importing table: $table_name from directory: $table_dir" >&2
echo -e "Command: nodetool import $KEYSPACE $table_name /var/lib/cassandra/data/$KEYSPACE/$table_dir" >&2
nodetool import $KEYSPACE $table_name /var/lib/cassandra/data/$KEYSPACE/$table_dir
done
# Keep the container running
tail -f /dev/null