Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
executable file 226 lines (207 sloc) 7.08 KB
#!/bin/bash
#
# Command Line Interface to start all services associated with the Entity-Relationships Tutorial
#
# For this tutorial the commands are merely a convenience script to run docker compose
#
set -e
SCORPIO="http://scorpio:9090/scorpio/v1/info/"
ORION="http://orion:1026/version"
STELLIO="http://localhost:8080/actuator/health"
CONTEXT="https://fiware.github.io/tutorials.Step-by-Step/tutorials-context.jsonld"
dockerCmd="docker compose"
if (( $# == 2 )); then
dockerCmd="docker-compose"
fi
if (( $# < 1 )); then
echo "Illegal number of parameters"
echo "usage: services [create|orion|scorpio|stop]"
exit 1
fi
pause(){
printf " "
count="$1"
[ "$count" -gt 59 ] && printf "Waiting one minute " || printf " Waiting a few seconds ";
while [ "$count" -gt 0 ]
do
printf "."
sleep 3
count=$((count - 3))
done
echo ""
}
getHeartbeat(){
eval "response=$(docker run --network fiware_default --rm curlimages/curl -s -o /dev/null -w "%{http_code}" "$1")"
}
waitForOrion () {
echo -e "\n⏳ Waiting for \033[1;34mOrion-LD\033[0m to be available\n"
while ! [ `docker inspect --format='{{.State.Health.Status}}' fiware-orion` == "healthy" ]
do
echo -e "\nContext Broker HTTP state: ${response} (waiting for 200)"
pause 6
getHeartbeat "${ORION}"
done
}
waitForScorpio () {
echo -e "\n⏳ Waiting for \033[1;34mScorpio\033[0m to respond\n"
getHeartbeat "${SCORPIO}"
while [ "${response}" -eq 000 ]
do
echo -e "Context Broker HTTP state: ${response} (waiting for 500)"
pause 60
getHeartbeat "${SCORPIO}"
done
echo -e "\n⏳ Waiting for all \033[1;34mScorpio\033[0m services to be fully available\n"
while [ "$response" -eq 500 ]
do
echo -e "Context Broker HTTP state: ${response} (waiting for 200)"
pause 60
getHeartbeat "${SCORPIO}"
done
}
waitForStellio () {
echo -e "\n⏳ Waiting for \033[1;34mStellio\033[0m to respond\n"
waitSeconds=30
while [ `docker run --network fiware_default --rm curlimages/curl -s -o /dev/null -w %{http_code} 'http://stellio:8080/ngsi-ld/v1/entities/?type=X'` -eq 000 ]
do
echo -e "Context Broker HTTP state: " `curl -s -o /dev/null -w %{http_code} 'http://localhost:8080/ngsi-ld/v1/entities/?type=X'` " (waiting for 500)"
echo -e "Waiting for ${waitSeconds} seconds ..."
sleep ${waitSeconds}
done
echo -e "\n⏳ Waiting for all \033[1;34mStellio\033[0m services to be available\n"
while [ `docker run --network fiware_default --rm curlimages/curl -s -o /dev/null -w %{http_code} 'http://stellio:8080/ngsi-ld/v1/entities/?type=X'` -eq 500 ]
do
echo -e "Context Broker HTTP state: " `curl -s -o /dev/null -w %{http_code} 'http://localhost:8080/ngsi-ld/v1/entities/?type=X'` " (waiting for 200)"
echo -e "Waiting for ${waitSeconds} seconds ..."
sleep ${waitSeconds}
done
}
waitForContext () {
echo -e "\n⏳ Waiting for \033[1m@context\033[0m to be available\n"
getHeartbeat "${CONTEXT}"
while [ "${response}" -eq 000 ]
do
echo -e "\n@context HTTP state: ${response} (waiting for 200)"
pause 3
getHeartbeat "${CONTEXT}"
done
}
waitForMongo () {
echo -e "\n⏳ Waiting for \033[1mMongoDB\033[0m to be available\n"
while ! [ `docker inspect --format='{{.State.Health.Status}}' db-mongo` == "healthy" ]
do
sleep 1
done
}
loadData () {
echo ""
waitForContext
docker run --rm -v $(pwd)/import-data:/import-data \
--network fiware_default \
-e CONTEXT_BROKER=${CONTEXT_BROKER} \
-e TUTORIAL_DATA_MODELS_CONTEXT=${CONTEXT} \
--entrypoint /bin/ash curlimages/curl import-data
}
addDatabaseIndex () {
printf "Adding appropriate \033[1mMongoDB\033[0m indexes for \033[1;34mOrion\033[0m ..."
docker exec db-mongo mongo --eval '
conn = new Mongo();db.createCollection("orion");
db = conn.getDB("orion");
db.createCollection("entities");
db.entities.createIndex({"_id.servicePath": 1, "_id.id": 1, "_id.type": 1}, {unique: true});
db.entities.createIndex({"_id.type": 1});
db.entities.createIndex({"_id.id": 1});' > /dev/null
docker exec db-mongo mongo --eval '
conn = new Mongo();db.createCollection("orion-openiot");
db = conn.getDB("orion-openiot");
db.createCollection("entities");
db.entities.createIndex({"_id.servicePath": 1, "_id.id": 1, "_id.type": 1}, {unique: true});
db.entities.createIndex({"_id.type": 1});
db.entities.createIndex({"_id.id": 1});' > /dev/null
echo -e " \033[1;32mdone\033[0m"
}
stoppingContainers () {
CONTAINERS=$(docker ps -aq)
if [[ -n $CONTAINERS ]]; then
echo "Stopping containers"
docker rm -f $CONTAINERS
fi
VOLUMES=$(docker volume ls -qf dangling=true)
if [[ -n $VOLUMES ]]; then
echo "Removing old volumes"
docker volume rm $VOLUMES
fi
}
displayServices () {
echo ""
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" --filter name=$1-*
echo ""
}
command="$1"
case "${command}" in
"help")
echo "usage: services [create|orion|scorpio|stop]"
;;
"orion")
export $(cat .env | grep "#" -v)
stoppingContainers
echo -e "Starting containers: \033[1;34mOrion\033[0m, \033[1mTutorial\033[0m and a \033[1mMongoDB\033[0m database."
echo -e "- \033[1;34mOrion\033[0m is the context broker"
echo -e "- \033[1m@context\033[0m is supplied externally"
echo ""
${dockerCmd} -f docker-compose/common.yml -f docker-compose/orion-ld.yml up -d --remove-orphans --renew-anon-volumes
waitForMongo
addDatabaseIndex
waitForOrion
export CONTEXT_BROKER=orion:1026
loadData
displayServices fiware
;;
"scorpio")
export $(cat .env | grep "#" -v)
stoppingContainers
echo -e "Starting containers: \033[1;34mScorpio\033[0m, \033[1mKafka\033[0m, \033[1mZookeeper\033[0m and a \033[1mPostgres\033[0m database, \033[1;36mIoT-Agent\033[0m, \033[1mTutorial\033[0m and a \033[1mMongoDB\033[0m database."
echo -e "- \033[1;34mScorpio\033[0m is the context broker"
echo -e "- \033[1m@context\033[0m is supplied externally"
echo ""
${dockerCmd} -f docker-compose/common.yml -f docker-compose/scorpio-aaio.yml up -d --remove-orphans --renew-anon-volumes
waitForMongo
waitForScorpio
export CONTEXT_BROKER=scorpio:9090
loadData
displayServices fiware
;;
"stellio")
export $(cat .env | grep "#" -v)
stoppingContainers
echo -e "Starting containers: \033[1;34mStellio\033[0m, \033[1mKafka\033[0m, \033[1mZookeeper\033[0m, \033[1mNeo4j\033[0m, \033[1mPostgreSQL/TimescaleDB\033[0m, and a linked data \033[1mContext\033[0m."
echo -e "- \033[1;34mStellio\033[0m is the context broker\n"
echo -e "- \033[1m@context\033[0m is supplied externally"
echo ""
${dockerCmd} -f docker-compose/common.yml -f docker-compose/stellio.yml up -d --remove-orphans --renew-anon-volumes
waitForMongo
waitForStellio
export CONTEXT_BROKER=api-gateway:8080
loadData
displayServices stellio
;;
"stop")
export $(cat .env | grep "#" -v)
stoppingContainers
;;
"start")
export $(cat .env | grep "#" -v)
./services orion $2
;;
"create")
export $(cat .env | grep "#" -v)
echo "Pulling Docker images"
docker pull curlimages/curl
${dockerCmd} -f docker-compose/common.yml -f docker-compose/scorpio-aaio.yml -f docker-compose/orion-ld.yml -f docker-compose/stellio.yml pull
;;
*)
echo "Command not Found."
echo "usage: services [create|orion|scorpio|stop]"
exit 127;
;;
esac