aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-packages/mdbc-docker/src/main/docker/docker-files/scripts/start-cassandra.sh
blob: 362c9e9a2ef018fb90dad5f8743cf3538aa06e93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash

# This enhances the standard docker entrypoint script by running cql scripts
# and shell scripts present in /docker-entrypoint-initdb.d. The public native
# transport port is not exposed until all the scripts have been completed.

if [[ $PRIVATE_PORT == "" ]]
then
	PRIVATE_PORT=19042
fi

if [[ $START_TIMEOUT_SECS == "" ]]
then
	START_TIMEOUT_SECS=300
fi

function getPort
{
	grep "^native_transport_port:" /etc/cassandra/cassandra.yaml | cut -d: -f2 | tr -d '\t '
}

function setPort
{
	local port=$1
	sed -i "s/^native_transport_port:.*/native_transport_port: $port/" /etc/cassandra/cassandra.yaml
}

function usage
{
	echo "usage: $(basename $0) [-sX]"
}

sleep=0
args=

while [[ $# != 0 ]]
do
	case "$1" in
	-s*)
		sleep=${1:2}
		shift 1
		;;
	*)
		usage && exit 1
		;;
	esac
done

if [[ $# != 0 ]]
then
	usage && exit 1
fi

if [[ $sleep != 0 ]]
then
	echo "Delaying startup by $sleep seconds"
	sleep $sleep
fi

# Note: this does not start up cassandra
echo "Running docker-entrypoint.sh to configure cassandra"
/docker-entrypoint.sh true || exit 1

echo "Enabling password authentication"
sed -i 's/^authenticator: AllowAllAuthenticator/authenticator: PasswordAuthenticator/g' /etc/cassandra/cassandra.yaml || exit 1

if [[ $(/bin/ls -1 /docker-entrypoint-initdb.d 2>/dev/null | wc -l) == 0 ]]
then
	echo "Starting cassandra"
else
	# Get the public native transport port from cassandra.yaml

	public_port=$(getPort)

	if [[ $public_port == "" ]]
	then
		echo "ERROR: No native_transport_port in /etc/cassandra/cassandra.yaml"
		exit 1
	fi

	echo "Starting cassandra on port $PRIVATE_PORT"
	setPort $PRIVATE_PORT || exit 1

	/docker-entrypoint.sh cassandra

	if [[ $? != 0 ]]
	then
		setPort $public_port
		exit 1
	fi

	tries=$START_TIMEOUT_SECS

	while true
	do
		if echo "describe keyspaces;" | cqlsh -u cassandra -p cassandra 127.0.0.1 $PRIVATE_PORT >/dev/null 2>&1
		then
			break
		fi

		tries=$((tries-1))

		if [[ $tries == 0 ]]
		then
			setPort $public_port
			echo "Timed out waiting for cassandra to start on port $PRIVATE_PORT"
			exit 1
		fi

		sleep 1
	done

	echo "Cassandra is started on port $PRIVATE_PORT"

	for file in $(/bin/ls -1 /docker-entrypoint-initdb.d 2>/dev/null)
	do
		case "$file" in
		*.sh)
			echo "Running $file"
			source "/docker-entrypoint-initdb.d/$file"
			;;
		*.cql)
			echo "Running $file"
			cqlsh -u cassandra -p cassandra -f "/docker-entrypoint-initdb.d/$file" 127.0.0.1 $PRIVATE_PORT
			;;
        	*)
			echo "Ignoring $file"
    		esac
	done

	sleep 5

	echo "Restarting cassandra on port $public_port"

	setPort $public_port
	pkill java

	if [[ $? != 0 ]]
	then
		echo "Failed to restart cassandra (kill failed)"
		exit 1
	fi

	sleep 5
fi

# NOTE: this starts cassandra in the foreground
/docker-entrypoint.sh cassandra -f