-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices
More file actions
executable file
·159 lines (152 loc) · 5.03 KB
/
Copy pathservices
File metadata and controls
executable file
·159 lines (152 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/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
# ./services stop
# 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)
#
# =================================================================
# Local variables
# =================================================================
set -e
DOCKER_CMD="docker-compose"
USAGE="usage: services [help | start [test] | stop]"
##Parameters handle
if [[ $# < 1 || $# > 2 ]]; then
echo "Illegal number of parameters"
echo $USAGE
exit 1;
fi
if [[ $# == 2 && "$1" == "start" ]]; then
if [[ "$2" == "test" ]]; then
DOCKER_CMD="$DOCKER_CMD --profile test"
else
echo "Wrong parameter"
echo $USAGE
exit 1;
fi
fi
# =================================================================
# Script functions()
# =================================================================
loadData () {
docker run --rm -v $(pwd)/src/import-initial-context-data.sh:/import-initial-context-data.sh: \
--network fiware_arduino_default \
--entrypoint /bin/ash curlimages/curl import-initial-context-data.sh
waitForIoTAgent
echo ""
}
# Display the table of running services
displayServices () {
echo ""
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" --filter label="com.wordpress.fcosfc=fiware_arduino"
echo ""
}
# Run when ./services stop
stoppingContainers () {
CONTAINERS=$(docker ps --filter "label=com.wordpress.fcosfc=fiware_arduino" -aq)
if [[ -n $CONTAINERS ]]; then
echo "Stopping containers"
docker rm -f $CONTAINERS || true
fi
VOLUMES=$(docker volume ls -qf dangling=true)
if [[ -n $VOLUMES ]]; then
echo "Removing old volumes"
docker volume rm $VOLUMES || true
fi
NETWORKS=$(docker network ls --filter "label=com.wordpress.fcosfc=fiware_arduino" -q)
if [[ -n $NETWORKS ]]; then
echo "Removing tutorial networks"
docker network rm $NETWORKS || true
fi
}
#Create Docker images
addDatabaseIndex () {
#MongoDB
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"
}
# =================================================================
# Wait functions()
# =================================================================
#Mongo DB
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
}
# Orion Context Broker
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
}
#IOT agent initialisation
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
}
# =================================================================
# Main functions()
# =================================================================
command="$1"
case "${command}" in
"help")
echo $USAGE
;;
"start")
export $(cat docker/.env | grep "#" -v)
stoppingContainers
echo -e "Starting containers: \033[1;34mOrion\033[0m and a \033[1mMongoDB\033[0m database."
echo -e "- \033[1;34mOrion\033[0m is the context broker"
echo ""
${DOCKER_CMD} -f docker/docker-compose.yaml up -d --remove-orphans
waitForMongo
addDatabaseIndex
waitForOrion
loadData
displayServices
echo -e "Now open \033[4mhttp://localhost:3000/device/monitor\033[0m"
;;
"stop")
export $(cat docker/.env | grep "#" -v)
stoppingContainers
;;
*)
echo "Command not Found."
echo $USAGE
exit 127;
;;
esac