ES OOM alignment

Change-Id: I798502516a4b9200243ed072985faa9f528f2073
Issue-ID: SDC-912
Signed-off-by: Tal Gitelman <tg851x@intl.att.com>
diff --git a/sdc-os-chef/scripts/docker_run.sh b/sdc-os-chef/scripts/docker_run.sh
index 2a8591c..7e5498e 100755
--- a/sdc-os-chef/scripts/docker_run.sh
+++ b/sdc-os-chef/scripts/docker_run.sh
@@ -64,7 +64,7 @@
 
 	echo ""
 	echo ""
-	healthCheck_http_code=$(curl -o out.html -w '%{http_code}' -H "Accept: application/json" -H "Content-Type: application/json" -H "USER_ID: jh0003" http://localhost:8080/sdc2/rest/v1/user/demo;)
+	healthCheck_http_code=$(curl -o /dev/null -w '%{http_code}' -H "Accept: application/json" -H "Content-Type: application/json" -H "USER_ID: jh0003" http://localhost:8080/sdc2/rest/v1/user/demo;)
 	if [[ ${healthCheck_http_code} != 200 ]]
 	then
 		echo "Error [${healthCheck_http_code}] while user existance check"
@@ -74,6 +74,32 @@
 	return ${healthCheck_http_code}
 }
 
+function elasticHealthCheck {
+	echo "Elastic Health-Check:"
+	
+	COUNTER=0
+    while [  $COUNTER -lt 20 ]; do
+    	echo "Waiting ES docker to start"
+  		health_Check_http_code=$(curl -o /dev/null -w '%{http_code}' http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=120s)
+  		if [[ "$health_Check_http_code" -eq 200 ]]
+		then
+			break
+		fi
+		let COUNTER=COUNTER+1 
+		sleep 4
+	done
+	
+	healthCheck_http_code=$(curl -o /dev/null -w '%{http_code}' http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=120s)
+	if [[ "$health_Check_http_code" != 200 ]]
+	then
+		echo "Error [${healthCheck_http_code}] ES NOT started correctly"
+		exit ${healthCheck_http_code}
+	fi
+	echo "ES started correctly"
+	curl localhost:9200/_cluster/health?pretty=true
+	return ${healthCheck_http_code}
+}
+
 RELEASE=latest
 LOCAL=false
 RUNTESTS=false
@@ -140,7 +166,9 @@
 	echo "pulling code"
 	docker pull ${PREFIX}/sdc-elasticsearch:${RELEASE}
 fi
-docker run --detach --name sdc-es --env ENVNAME="${DEP_ENV}" --log-driver=json-file --log-opt max-size=100m --log-opt max-file=10 --memory 750m --env ES_JAVA_OPTS="-Xms512m -Xmx512m" --ulimit memlock=-1:-1 --ulimit nofile=4096:100000 --volume /etc/localtime:/etc/localtime:ro --env ES_HEAP_SIZE=1024M --volume ${WORKSPACE}/data/ES:/usr/share/elasticsearch/data --volume ${WORKSPACE}/data/environments:/root/chef-solo/environments --publish 9200:9200 --publish 9300:9300 ${PREFIX}/sdc-elasticsearch:${RELEASE}
+docker run -dit --name sdc-es --env ENVNAME="${DEP_ENV}" --log-driver=json-file --log-opt max-size=100m --log-opt max-file=10 --memory 750m --env ES_JAVA_OPTS="-Xms512m -Xmx512m" --ulimit memlock=-1:-1 --ulimit nofile=4096:100000 --volume /etc/localtime:/etc/localtime:ro --env ES_HEAP_SIZE=1024M --volume ${WORKSPACE}/data/ES:/usr/share/elasticsearch/data --volume ${WORKSPACE}/data/environments:/root/chef-solo/environments --publish 9200:9200 --publish 9300:9300 ${PREFIX}/sdc-elasticsearch:${RELEASE} /bin/sh
+
+elasticHealthCheck
 
 # Init-Elastic-Search
 echo "docker run sdc-init-elasticsearch..."
@@ -148,7 +176,13 @@
 	echo "pulling code"
 	docker pull ${PREFIX}/sdc-init-elasticsearch:${RELEASE}
 fi
-docker run --detach --name sdc-init-es --env ENVNAME="${DEP_ENV}" --log-driver=json-file --log-opt max-size=100m --log-opt max-file=10 --memory 750m --ulimit memlock=-1:-1 --ulimit nofile=4096:100000 --volume /etc/localtime:/etc/localtime:ro --volume ${WORKSPACE}/data/environments:/root/chef-solo/environments ${PREFIX}/sdc-init-elasticsearch:${RELEASE}
+docker run --name sdc-init-es --env ENVNAME="${DEP_ENV}" --log-driver=json-file --log-opt max-size=100m --log-opt max-file=10 --memory 750m --ulimit memlock=-1:-1 --ulimit nofile=4096:100000 --volume /etc/localtime:/etc/localtime:ro --volume ${WORKSPACE}/data/environments:/root/chef-solo/environments ${PREFIX}/sdc-init-elasticsearch:${RELEASE} 
+
+# Checking Elastic-Search-Init chef status
+if [ ! $? -eq 0 ]; then
+    echo "Elastic-Search Initialization failed"
+    exit $?
+fi
 
 # Cassandra
 echo "docker run sdc-cassandra..."
diff --git a/sdc-os-chef/sdc-elasticsearch/startup.sh b/sdc-os-chef/sdc-elasticsearch/startup.sh
index 6c9b0c0..1871d68 100644
--- a/sdc-os-chef/sdc-elasticsearch/startup.sh
+++ b/sdc-os-chef/sdc-elasticsearch/startup.sh
@@ -3,7 +3,8 @@
 cd /root/chef-solo/
 chef-solo -c solo.rb -E ${ENVNAME}
 
+chef_status=$?
+
 /docker-entrypoint.sh elasticsearch &
 
-while true; do sleep 2; done
-
+exec "$@";
\ No newline at end of file
diff --git a/sdc-os-chef/sdc-init-elasticsearch/startup.sh b/sdc-os-chef/sdc-init-elasticsearch/startup.sh
index ac26052..9a8cbc3 100644
--- a/sdc-os-chef/sdc-init-elasticsearch/startup.sh
+++ b/sdc-os-chef/sdc-init-elasticsearch/startup.sh
@@ -2,3 +2,5 @@
 
 cd /root/chef-solo/
 chef-solo -c solo.rb -E ${ENVNAME}
+
+exit $?
\ No newline at end of file