Last active
March 2, 2016 01:13
-
-
Save IcaroBichir/2b78a499c0d15b4f3a82 to your computer and use it in GitHub Desktop.
Validation for Docker Services Startup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################# | |
# Right now we are assuming you have already # | |
# installed docker within your machine and your # | |
# current user belongs to docker system group # | |
################################################# | |
################# | |
# ELASTICSEARCH # | |
################# | |
function elasticsearch_validate() { | |
[[ `(curl -s http://localhost:"${ELASTICSEARCH_PORT}")` =~ "status" ]] && echo 0 || echo 1 | |
} | |
function elasticsearch_start() { | |
ELASTICSEARCH_ID=`docker run -d -P -t elasticsearch:1.4` | |
ELASTICSEARCH_PORT=`docker port ${ELASTICSEARCH_ID} | cut -d':' -f2 | tr '\n' ' ' | cut -d ' ' -f1` | |
} | |
docker_start elasticsearch ${RETRIES} | |
############# | |
# CASSANDRA # | |
############# | |
function cassandra_validate() { | |
STATUS=1 | |
set +e | |
python -c "from cassandra.cluster import Cluster; cluster = Cluster(['localhost'], port=${CASSANDRA_PORT}); session = cluster.connect(); session.execute('SELECT now() FROM system.local;'); cluster.shutdown();" | |
if [ ${?} -eq 0 ]; then | |
STATUS=0 | |
fi | |
set -e | |
echo ${STATUS} | |
} | |
function cassandra_start() { | |
CASSANDRA_ID=`docker run -d -P -t cassandra:2.1.6` | |
CASSANDRA_PORT=`docker port ${CASSANDRA_ID} | grep 9042 | cut -d':' -f2 | tr '\n' ' ' | cut -d ' ' -f1` | |
} | |
docker_start cassandra ${RETRIES} | |
############## | |
# POSTGRESQL # | |
############## | |
function postgres_validate { | |
[[ `echo 'SELECT version();' | psql -U postgres -h localhost -p ${POSTGRES_PORT} template1` =~ "PostgreSQL" ]] && echo 0 || echo 1 | |
} | |
function postgres_start { | |
POSTGRES_ID=`docker run -d -P -t postgres:9.3` | |
POSTGRES_PORT=`docker port ${POSTGRES_ID} | cut -d':' -f2` | |
} | |
docker_start postgres ${RETRIES} | |
######### | |
# Redis # | |
######### | |
function redis_validate { | |
[[ `(echo PING; sleep 1) | nc localhost $REDIS_PORT` == $'+PONG\r' ]] && echo 0 || echo 1 | |
} | |
function redis_start { | |
REDIS_ID=`docker run -d -P -t redis:2.8` | |
REDIS_PORT=`docker port ${REDIS_ID} | cut -d':' -f2` | |
} | |
docker_start redis ${RETRIES} | |
############# | |
# Docker up # | |
############# | |
function docker_start() { | |
SERVICE=${1} | |
RETRIES=${2} | |
CURRENT_RETRIAL=0 | |
while [ ${CURRENT_RETRIAL} -lt ${RETRIES} ]; do | |
${SERVICE}_start | |
CURRENT_VALIDATION=0 | |
VALIDATED=false | |
while [ ${CURRENT_VALIDATION} -lt 20 ]; do | |
VALIDATION_RESULT=$(${SERVICE}_validate) | |
if [[ ${VALIDATION_RESULT} == 0 ]] | |
then | |
VALIDATED=true | |
echo 'Service is up.' | |
break | |
fi | |
echo 'Service not loaded, yet.' | |
CURRENT_VALIDATION=$((++CURRENT_VALIDATION)) | |
sleep 1 | |
CURRENT_VALIDATION=$((++CURRENT_VALIDATION)) | |
sleep 1 | |
done | |
if ${VALIDATED}; then | |
break | |
fi | |
CURRENT_RETRIAL=$((++CURRENT_RETRIAL)) | |
sleep 1 | |
done | |
if ! ${VALIDATED}; then | |
echo 'Failed while starting docker service.' | |
exit 1 | |
fi | |
} | |
if [ -z ${RETRIES} ]; then | |
RETRIES=3 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment