Skip to content

Commit

Permalink
mosquitto.sh run native mosquitto in foreground (#148)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jpgrayson committed Nov 27, 2023
1 parent 6c41888 commit 22c9b87
Showing 1 changed file with 42 additions and 64 deletions.
106 changes: 42 additions & 64 deletions scripts/mosquitto.sh
Original file line number Diff line number Diff line change
@@ -1,83 +1,61 @@
#!/bin/bash

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

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

usage()
{
echo "Usage: mosquitto.sh [start] [stop] [status]"
echo "Usage: mosquitto.sh [--container|-C]"
exit 2
}

run()
run-installed-mosquitto()
{
if [ -z "$CONTAINER_ID" ]; then
docker run \
-p 1883:1883 \
-p 1884:1884 \
-p 8883:8883 \
-p 8080:8080 \
-p 8081:8081 \
-v $(pwd)/$ROOT/mosquitto/config:/mosquitto/config \
-v $(pwd)/$ROOT/mosquitto/certs:/mosquitto/certs \
eclipse-mosquitto
else
echo "Mosquitto is already running"
fi
mosquitto -c mosquitto/config/mosquitto.conf
}

start()
run-containerized-mosquitto()
{
if [ -z "$CONTAINER_ID" ]; then
docker run \
-d \
-p 1883:1883 \
-p 1884:1884 \
-p 8883:8883 \
-p 8080:8080 \
-p 8081:8081 \
-v $(pwd)/$HOME/../mosquitto/config:/mosquitto/config \
-v $(pwd)/$HOME/../mosquitto/certs:/mosquitto/certs \
eclipse-mosquitto
else
echo "Mosquitto is already running"
fi
docker run \
-p 1883:1883 \
-p 1884:1884 \
-p 8883:8883 \
-p 8080:8080 \
-p 8081:8081 \
-v "$(pwd)"/mosquitto/config:/mosquitto/config \
-v "$(pwd)"/mosquitto/certs:/mosquitto/certs \
eclipse-mosquitto
}

stop()
{
if [ -n "$CONTAINER_ID" ]; then
echo "Stopping mosquitto"
docker container stop "$CONTAINER_ID"
docker rm "$CONTAINER_ID"
else
echo "Mosquitto is already stopped"
fi
}

status()
{
if [ -n "$CONTAINER_ID" ]; then
echo "Mosquitto is running"
else
echo "Mosquitto is not running"
fi
}
USE_CONTAINER=0

if [ "$COMMAND" == "start" ]; then
start
elif [ "$COMMAND" == "stop" ]; then
stop
elif [ "$COMMAND" == "status" ]; then
status
elif [ -z "$COMMAND" ]; then
run
else
if [[ $# -gt 1 ]]; then
usage
exit -1
elif [[ $# -eq 1 ]]; then
case "$1" in
-C|--container) USE_CONTAINER=1 ;;
*) usage ;;
esac
fi

cd "$(dirname "$(dirname "$0")")"

if [[ $USE_CONTAINER -eq 1 ]]; then
run-containerized-mosquitto
elif command -v mosquitto >/dev/null; then
run-installed-mosquitto
else
if [ "$(uname)" = "Darwin" ]; then
echo "mosquitto can be installed on MacOS with: brew install mosquitto"
fi
echo "notice: mosquitto not installed; running eclipse-mosquitto container instead..."
run-containerized-mosquitto
fi

0 comments on commit 22c9b87

Please sign in to comment.