1212
1313set -euxo pipefail
1414
15+ SCRIPT_PATH=$( dirname $( realpath -s $0 ) )
16+
1517moniker=$( echo " $ELASTICSEARCH_VERSION " | tr -C " [:alnum:]" ' -' )
1618suffix=rest-test
1719
@@ -21,10 +23,10 @@ CLUSTER_NAME=${CLUSTER_NAME-${moniker}${suffix}}
2123HTTP_PORT=${HTTP_PORT-9200}
2224
2325ELASTIC_PASSWORD=${ELASTIC_PASSWORD-changeme}
24- SSL_CERT=${SSL_CERT-" $PWD /certs/testnode.crt" }
25- SSL_KEY=${SSL_KEY-" $PWD /certs/testnode.key" }
26- SSL_CA=${SSL_CA-" $PWD /certs/ca.crt" }
27- SSL_CA_PEM=${SSL_CA-" $PWD /certs/ca.pem" }
26+ SSL_CERT=${SSL_CERT-" ${SCRIPT_PATH} /certs/testnode.crt" }
27+ SSL_KEY=${SSL_KEY-" ${SCRIPT_PATH} /certs/testnode.key" }
28+ SSL_CA=${SSL_CA-" ${SCRIPT_PATH} /certs/ca.crt" }
29+ SSL_CA_PEM=${SSL_CA-" ${SCRIPT_PATH} /certs/ca.pem" }
2830
2931DETACH=${DETACH-false}
3032CLEANUP=${CLEANUP-false}
@@ -48,7 +50,7 @@ function container_running {
4850 fi
4951}
5052function cleanup_node {
51- if container_running $1 ; then
53+ if container_running " $1 " ; then
5254 echo -e " \033[34;1mINFO:\033[0m Removing container $1 \033[0m"
5355 (docker container rm --force --volumes " $1 " ) || true
5456 cleanup_volume " $1 -${suffix} -data"
@@ -70,7 +72,7 @@ function cleanup {
7072 echo -e " \033[34;1mINFO:\033[0m clean the network if not detached (start and exit)\033[0m"
7173 cleanup_network " $NETWORK_NAME "
7274 fi
73- };
75+ };
7476trap " cleanup 0" EXIT
7577
7678if [[ " $CLEANUP " == " true" ]]; then
@@ -92,7 +94,7 @@ echo -e "\033[34;1mINFO:\033[0m Making sure previous run leftover infrastructure
9294cleanup 1
9395
9496echo -e " \033[34;1mINFO:\033[0m Creating network $NETWORK_NAME if it does not exist already \033[0m"
95- docker network inspect " $NETWORK_NAME " > /dev/null 2>&1 || docker network create " $NETWORK_NAME "
97+ docker network inspect " $NETWORK_NAME " > /dev/null 2>&1 || docker network create " $NETWORK_NAME "
9698
9799environment=($( cat << -END
98100 --env node.name=$NODE_NAME
@@ -166,28 +168,28 @@ docker run \
166168 --rm \
167169 docker.elastic.co/elasticsearch/" $ELASTICSEARCH_VERSION " ;
168170set +x
169-
171+
170172if [[ " $DETACH " == " true" ]]; then
171- until container_running $NODE_NAME && [[ " $( docker inspect -f " {{.State.Health.Status}}" ${NODE_NAME} ) " != " starting" ]]; do
173+ until ! container_running " $NODE_NAME " || (container_running " $NODE_NAME " && [[ " $( docker inspect -f " {{.State.Health.Status}}" ${NODE_NAME} ) " != " starting" ]]) ; do
172174 echo " "
173175 docker inspect -f " {{range .State.Health.Log}}{{.Output}}{{end}}" ${NODE_NAME}
174176 echo -e " \033[34;1mINFO:\033[0m waiting for node $NODE_NAME to be up\033[0m"
175177 sleep 2;
176178 done ;
177179
178180 # Always show logs if the container is running, this is very useful both on CI as well as while developing
179- if container_running $NODE_NAME ; then
181+ if container_running " $NODE_NAME " ; then
180182 docker logs $NODE_NAME
181183 fi
182184
183- if ! container_running $NODE_NAME || [[ " $( docker inspect -f " {{.State.Health.Status}}" ${NODE_NAME} ) " != " healthy" ]]; then
185+ if ! container_running " $NODE_NAME " || [[ " $( docker inspect -f " {{.State.Health.Status}}" ${NODE_NAME} ) " != " healthy" ]]; then
184186 cleanup 1
185- echo
187+ echo
186188 echo -e " \033[31;1mERROR:\033[0m Failed to start ${ELASTICSEARCH_VERSION} in detached mode beyond health checks\033[0m"
187189 echo -e " \033[31;1mERROR:\033[0m dumped the docker log before shutting the node down\033[0m"
188190 exit 1
189- else
190- echo
191+ else
192+ echo
191193 echo -e " \033[32;1mSUCCESS:\033[0m Detached and healthy: ${NODE_NAME} on docker network: ${NETWORK_NAME} \033[0m"
192194 echo -e " \033[32;1mSUCCESS:\033[0m Running on: ${url/ $NODE_NAME / localhost} :${HTTP_PORT} \033[0m"
193195 exit 0
0 commit comments