Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
executable file 153 lines (134 sloc) 4.75 KB
#!/bin/bash
#
# Command Line Interface to start all services associated with the Tutorial
#
# For this tutorial the commands are merely a convenience script to run docker or docker-compose
#
# Each services script can be run using either docker-compose (the external tool with the hyphen -)
# or docker compose (the newer version directly bundled with Docker with a space )
#
# if you start up with the following command:
#
# ./services start legacy
#
# This will force the script to use docker-compose which may be more reliable in
# some cases (or if an older version of Docker is being used)
set -e
dockerCmd="docker compose"
if (( $# == 2 )); then
dockerCmd="docker-compose"
fi
if (( $# < 1 )); then
echo "Illegal number of parameters"
echo "usage: services [create|start|stop]"
exit 1
fi
loadData () {
docker run --rm -v $(pwd)/import-data:/import-data \
--network fiware_default \
--entrypoint /bin/ash curlimages/curl import-data
waitForIoTAgent
echo ""
}
stoppingContainers () {
echo -e "Force removal of \033[1mmqtt-subscriber\033[0m"
(docker stop mqtt-subscriber &>/dev/null) || (echo -e "\033[1mmqtt-subscriber\033[0m not running")
echo "Stopping running containers"
${dockerCmd} down -v --remove-orphans
}
displayServices () {
echo ""
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" --filter name="(fiware-*|mosquitto)"
echo ""
}
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"
printf "Adding appropriate \033[1mMongoDB\033[0m indexes for \033[1;36mIoT-Agent\033[0m ..."
docker exec db-mongo mongo --eval '
conn = new Mongo();
db = conn.getDB("iotagentul");
db.createCollection("devices");
db.devices.createIndex({"_id.service": 1, "_id.id": 1, "_id.type": 1});
db.devices.createIndex({"_id.type": 1});
db.devices.createIndex({"_id.id": 1});
db.createCollection("groups");
db.groups.createIndex({"_id.resource": 1, "_id.apikey": 1, "_id.service": 1});
db.groups.createIndex({"_id.type": 1});' > /dev/null
echo -e " \033[1;32mdone\033[0m"
}
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
}
waitForOrion () {
echo -e "\n⏳ Waiting for \033[1;34mOrion\033[0m to be available\n"
while ! [ `docker inspect --format='{{.State.Health.Status}}' fiware-orion` == "healthy" ]
do
echo -e "Context Broker HTTP state: " `curl -s -o /dev/null -w %{http_code} 'http://localhost:1026/version'` " (waiting for 200)"
sleep 1
done
}
waitForIoTAgent () {
echo -e "\n⏳ Waiting for \033[1;36mIoT-Agent\033[0m to be available\n"
while ! [ `docker inspect --format='{{.State.Health.Status}}' fiware-iot-agent` == "healthy" ]
do
echo -e "IoT Agent HTTP state: " `curl -s -o /dev/null -w %{http_code} 'http://localhost:4041/version'` " (waiting for 200)"
sleep 1
done
}
command="$1"
case "${command}" in
"help")
echo "usage: services [create|start|stop]"
;;
"start")
export $(cat .env | grep "#" -v)
stoppingContainers
echo -e "Starting containers: \033[1;34mOrion\033[0m, \033[1;36mIoT-Agent\033[0m, \033[1mTutorial\033[0m, \033[1mMosquitto\033[0m MQTT broker and a \033[1mMongoDB\033[0m database."
echo -e "- \033[1;34mOrion\033[0m is the context broker"
echo -e "- \033[1;36mIoT-Agent\033[0m is configured for the UltraLight Protocol"
echo -e "- \033[1mTutorial\033[0m acts as a series of dummy IoT Sensors over MQTT"
echo -e "- \033[1mMosquitto\033[0m acts as an MQTT Message Broker"
echo ""
${dockerCmd} up -d --remove-orphans
waitForMongo
addDatabaseIndex
waitForOrion
loadData
displayServices
echo -e "Now open \033[4mhttp://localhost:3000/device/monitor\033[0m"
;;
"stop")
export $(cat .env | grep "#" -v)
stoppingContainers
;;
"create")
export $(cat .env | grep "#" -v)
echo "Pulling Docker images"
docker pull curlimages/curl
${dockerCmd} pull
;;
*)
echo "Command not Found."
echo "usage: services [create|start|stop]"
exit 127;
;;
esac