forked from gcgarner/IOTstack
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #406 from Paraphraser/20210917-mosquitto-master
20210917 Mosquitto - HTTP not HTTPS during build + health check - master branch - PR 1 of 3
- Loading branch information
Showing
3 changed files
with
216 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env sh | ||
|
||
# assume the following environment variables, all of which may be null | ||
# HEALTHCHECK_PORT | ||
# HEALTHCHECK_USER | ||
# HEALTHCHECK_PASSWORD | ||
# HEALTHCHECK_TOPIC | ||
|
||
# set a default for the port | ||
HEALTHCHECK_PORT="${HEALTHCHECK_PORT:-1883}" | ||
|
||
# strip any quotes from username and password | ||
HEALTHCHECK_USER="$(eval echo $HEALTHCHECK_USER)" | ||
HEALTHCHECK_PASSWORD="$(eval echo $HEALTHCHECK_PASSWORD)" | ||
|
||
# set a default for the topic | ||
HEALTHCHECK_TOPIC="${HEALTHCHECK_TOPIC:-iotstack/mosquitto/healthcheck}" | ||
HEALTHCHECK_TOPIC="$(eval echo $HEALTHCHECK_TOPIC)" | ||
|
||
# record the current date and time for the test payload | ||
PUBLISH=$(date) | ||
|
||
# publish a retained message containing the timestamp | ||
mosquitto_pub \ | ||
-h localhost \ | ||
-p "$HEALTHCHECK_PORT" \ | ||
-t "$HEALTHCHECK_TOPIC" \ | ||
-m "$PUBLISH" \ | ||
-u "$HEALTHCHECK_USER" \ | ||
-P "$HEALTHCHECK_PASSWORD" \ | ||
-r | ||
|
||
# did that succeed? | ||
if [ $? -eq 0 ] ; then | ||
|
||
# yes! now, subscribe to that same topic with a 2-second timeout | ||
# plus returning on the first message | ||
SUBSCRIBE=$(mosquitto_sub \ | ||
-h localhost \ | ||
-p "$HEALTHCHECK_PORT" \ | ||
-t "$HEALTHCHECK_TOPIC" \ | ||
-u "$HEALTHCHECK_USER" \ | ||
-P "$HEALTHCHECK_PASSWORD" \ | ||
-W 2 \ | ||
-C 1 \ | ||
) | ||
|
||
# did the subscribe succeed? | ||
if [ $? -eq 0 ] ; then | ||
|
||
# yes! do the publish and subscribe payloads compare equal? | ||
if [ "$PUBLISH" = "$SUBSCRIBE" ] ; then | ||
|
||
# yes! return success | ||
exit 0 | ||
|
||
fi | ||
|
||
fi | ||
|
||
fi | ||
|
||
# otherwise, return failure | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters