Skip to content

Commit 9d790cb

Browse files
authored
Merge pull request #408 from Paraphraser/20210917-mosquitto-experimental
20210917 Mosquitto - HTTP not HTTPS during build + health check - experimental branch - PR 3 of 3
2 parents 99143a9 + e4e2ffc commit 9d790cb

File tree

6 files changed

+125
-4
lines changed

6 files changed

+125
-4
lines changed

.internal/templates/services/mosquitto/buildFiles/Dockerfile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
# Download base image
22
FROM eclipse-mosquitto:latest
33

4+
# see https://github.com/alpinelinux/docker-alpine/issues/98
5+
RUN sed -i 's/https/http/' /etc/apk/repositories
6+
47
# Add support tools
58
RUN apk update && apk add --no-cache rsync tzdata
69

710
# where IOTstack template files are stored
8-
ENV MOSQUITTO_IOTSTACK_DEFAULTS="/iotstack_defaults"
11+
ENV IOTSTACK_DEFAULTS_DIR="iotstack_defaults"
912

1013
# copy template files to image
11-
COPY --chown=mosquitto:mosquitto . /${MOSQUITTO_IOTSTACK_DEFAULTS}
14+
COPY --chown=mosquitto:mosquitto ${IOTSTACK_DEFAULTS_DIR} /${IOTSTACK_DEFAULTS_DIR}
15+
16+
# copy the health-check script into place
17+
ENV HEALTHCHECK_SCRIPT "iotstack_healthcheck.sh"
18+
COPY ${HEALTHCHECK_SCRIPT} /usr/local/bin/${HEALTHCHECK_SCRIPT}
19+
20+
# define the health check
21+
HEALTHCHECK \
22+
--start-period=30s \
23+
--interval=30s \
24+
--timeout=10s \
25+
--retries=3 \
26+
CMD ${HEALTHCHECK_SCRIPT} || exit 1
1227

1328
# replace the docker entry-point script
1429
ENV IOTSTACK_ENTRY_POINT="docker-entrypoint.sh"

.internal/templates/services/mosquitto/buildFiles/docker-entrypoint.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ set -e
44
# Set permissions
55
user="$(id -u)"
66
if [ "$user" = '0' -a -d "/mosquitto" ]; then
7-
rsync -arp --ignore-existing /${MOSQUITTO_IOTSTACK_DEFAULTS}/ "/mosquitto"
8-
chown -R mosquitto:mosquitto /mosquitto
7+
8+
rsync -arp --ignore-existing /${IOTSTACK_DEFAULTS_DIR}/ "/mosquitto"
9+
10+
chown -R mosquitto:mosquitto /mosquitto
11+
912
fi
1013

1114
exec "$@"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
user admin
2+
topic read #
3+
topic write #
4+
5+
pattern read #
6+
pattern write #
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# required by https://mosquitto.org/documentation/migrating-to-2-0/
2+
#
3+
listener 1883
4+
5+
# persistence enabled for remembering retain flag across restarts
6+
#
7+
persistence true
8+
persistence_location /mosquitto/data
9+
10+
# logging options:
11+
# enable one of the following (stdout = less wear on SD cards but
12+
# logs do not persist across restarts)
13+
#log_dest file /mosquitto/log/mosquitto.log
14+
log_dest stdout
15+
log_timestamp_format %Y-%m-%dT%H:%M:%S
16+
17+
# password handling:
18+
# password_file commented-out allow_anonymous true =
19+
# open access
20+
# password_file commented-out allow_anonymous false =
21+
# no access
22+
# password_file activated allow_anonymous true =
23+
# passwords omitted is permitted but
24+
# passwords provided must match pwfile
25+
# password_file activated allow_anonymous false =
26+
# no access without passwords
27+
# passwords provided must match pwfile
28+
#
29+
#password_file /mosquitto/pwfile/pwfile
30+
allow_anonymous true
31+
32+
# Uncomment to enable filters
33+
#acl_file /mosquitto/config/filter.acl

.internal/templates/services/mosquitto/buildFiles/iotstack_defaults/pwfile/pwfile

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env sh
2+
3+
# assume the following environment variables, all of which may be null
4+
# HEALTHCHECK_PORT
5+
# HEALTHCHECK_USER
6+
# HEALTHCHECK_PASSWORD
7+
# HEALTHCHECK_TOPIC
8+
9+
# set a default for the port
10+
HEALTHCHECK_PORT="${HEALTHCHECK_PORT:-1883}"
11+
12+
# strip any quotes from username and password
13+
HEALTHCHECK_USER="$(eval echo $HEALTHCHECK_USER)"
14+
HEALTHCHECK_PASSWORD="$(eval echo $HEALTHCHECK_PASSWORD)"
15+
16+
# set a default for the topic
17+
HEALTHCHECK_TOPIC="${HEALTHCHECK_TOPIC:-iotstack/mosquitto/healthcheck}"
18+
HEALTHCHECK_TOPIC="$(eval echo $HEALTHCHECK_TOPIC)"
19+
20+
# record the current date and time for the test payload
21+
PUBLISH=$(date)
22+
23+
# publish a retained message containing the timestamp
24+
mosquitto_pub \
25+
-h localhost \
26+
-p "$HEALTHCHECK_PORT" \
27+
-t "$HEALTHCHECK_TOPIC" \
28+
-m "$PUBLISH" \
29+
-u "$HEALTHCHECK_USER" \
30+
-P "$HEALTHCHECK_PASSWORD" \
31+
-r
32+
33+
# did that succeed?
34+
if [ $? -eq 0 ] ; then
35+
36+
# yes! now, subscribe to that same topic with a 2-second timeout
37+
# plus returning on the first message
38+
SUBSCRIBE=$(mosquitto_sub \
39+
-h localhost \
40+
-p "$HEALTHCHECK_PORT" \
41+
-t "$HEALTHCHECK_TOPIC" \
42+
-u "$HEALTHCHECK_USER" \
43+
-P "$HEALTHCHECK_PASSWORD" \
44+
-W 2 \
45+
-C 1 \
46+
)
47+
48+
# did the subscribe succeed?
49+
if [ $? -eq 0 ] ; then
50+
51+
# yes! do the publish and subscribe payloads compare equal?
52+
if [ "$PUBLISH" = "$SUBSCRIBE" ] ; then
53+
54+
# yes! return success
55+
exit 0
56+
57+
fi
58+
59+
fi
60+
61+
fi
62+
63+
# otherwise, return failure
64+
exit 1

0 commit comments

Comments
 (0)