Skip to content

Commit 22c9b87

Browse files
authored
mosquitto.sh run native mosquitto in foreground (#148)
The goal of mosquitto.sh is to get a mosquitto service up and running so that tests can built and run in the native OS environment. A mosquitto container running on a MacOS host cannot share a unix socket from the Linux container to the MacOS host. Running a native mosquitto instance from mosquitto.sh solves this problem. The native mosquitto process can create/listen a host-compatible unix socket. The script is modified to run a locally installed mosquitto by default, if available, and fallback to running mosquitto in a container. For both the locally installed and containerized paths, the script now just runs in the foreground; no more start/stop/status subcommands.
1 parent 6c41888 commit 22c9b87

File tree

1 file changed

+42
-64
lines changed

1 file changed

+42
-64
lines changed

scripts/mosquitto.sh

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,61 @@
11
#!/bin/bash
22

3-
set -ex
3+
# Run mosquitto MQTT broker with configuration needed for the test suite.
4+
#
5+
# If mosquitto is installed locally, it will be run. Otherwise, fall back to
6+
# running a containerized version of mosquitto. The --container/-C option may be
7+
# supplied to force running a containerized mosquitto.
8+
#
9+
# N.B. On MacOS, a native mosquitto executable must be run to connect to
10+
# mosquitto using a unix domain socket.
411

5-
CONTAINER_ID="$(docker container ls | grep eclipse-mosquitto | awk {'print $1'})"
6-
COMMAND="$1"
7-
SCRIPTS=$(dirname $0)
8-
ROOT=$(dirname $SCRIPTS)
12+
set -eu -o pipefail
913

1014
usage()
1115
{
12-
echo "Usage: mosquitto.sh [start] [stop] [status]"
16+
echo "Usage: mosquitto.sh [--container|-C]"
1317
exit 2
1418
}
1519

16-
run()
20+
run-installed-mosquitto()
1721
{
18-
if [ -z "$CONTAINER_ID" ]; then
19-
docker run \
20-
-p 1883:1883 \
21-
-p 1884:1884 \
22-
-p 8883:8883 \
23-
-p 8080:8080 \
24-
-p 8081:8081 \
25-
-v $(pwd)/$ROOT/mosquitto/config:/mosquitto/config \
26-
-v $(pwd)/$ROOT/mosquitto/certs:/mosquitto/certs \
27-
eclipse-mosquitto
28-
else
29-
echo "Mosquitto is already running"
30-
fi
22+
mosquitto -c mosquitto/config/mosquitto.conf
3123
}
3224

33-
start()
25+
run-containerized-mosquitto()
3426
{
35-
if [ -z "$CONTAINER_ID" ]; then
36-
docker run \
37-
-d \
38-
-p 1883:1883 \
39-
-p 1884:1884 \
40-
-p 8883:8883 \
41-
-p 8080:8080 \
42-
-p 8081:8081 \
43-
-v $(pwd)/$HOME/../mosquitto/config:/mosquitto/config \
44-
-v $(pwd)/$HOME/../mosquitto/certs:/mosquitto/certs \
45-
eclipse-mosquitto
46-
else
47-
echo "Mosquitto is already running"
48-
fi
27+
docker run \
28+
-p 1883:1883 \
29+
-p 1884:1884 \
30+
-p 8883:8883 \
31+
-p 8080:8080 \
32+
-p 8081:8081 \
33+
-v "$(pwd)"/mosquitto/config:/mosquitto/config \
34+
-v "$(pwd)"/mosquitto/certs:/mosquitto/certs \
35+
eclipse-mosquitto
4936
}
5037

51-
stop()
52-
{
53-
if [ -n "$CONTAINER_ID" ]; then
54-
echo "Stopping mosquitto"
55-
docker container stop "$CONTAINER_ID"
56-
docker rm "$CONTAINER_ID"
57-
else
58-
echo "Mosquitto is already stopped"
59-
fi
60-
}
61-
62-
status()
63-
{
64-
if [ -n "$CONTAINER_ID" ]; then
65-
echo "Mosquitto is running"
66-
else
67-
echo "Mosquitto is not running"
68-
fi
69-
}
38+
USE_CONTAINER=0
7039

71-
if [ "$COMMAND" == "start" ]; then
72-
start
73-
elif [ "$COMMAND" == "stop" ]; then
74-
stop
75-
elif [ "$COMMAND" == "status" ]; then
76-
status
77-
elif [ -z "$COMMAND" ]; then
78-
run
79-
else
40+
if [[ $# -gt 1 ]]; then
8041
usage
81-
exit -1
42+
elif [[ $# -eq 1 ]]; then
43+
case "$1" in
44+
-C|--container) USE_CONTAINER=1 ;;
45+
*) usage ;;
46+
esac
8247
fi
8348

49+
cd "$(dirname "$(dirname "$0")")"
50+
51+
if [[ $USE_CONTAINER -eq 1 ]]; then
52+
run-containerized-mosquitto
53+
elif command -v mosquitto >/dev/null; then
54+
run-installed-mosquitto
55+
else
56+
if [ "$(uname)" = "Darwin" ]; then
57+
echo "mosquitto can be installed on MacOS with: brew install mosquitto"
58+
fi
59+
echo "notice: mosquitto not installed; running eclipse-mosquitto container instead..."
60+
run-containerized-mosquitto
61+
fi

0 commit comments

Comments
 (0)