-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_mosquitto_config.sh
executable file
·80 lines (62 loc) · 2.4 KB
/
20_mosquitto_config.sh
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
#!/bin/bash
#
# Initial config of Mosquitto
#
echo "=> Initial config of Mosquitto started"
if [ ! -e .env ]
then
echo "<!> Please create proper .env file! Exiting."
exit 1
fi
if [ ! -e mqtt.env ]
then
echo "<!> Please create proper mqtt.env file! Exiting."
exit 1
fi
set -o allexport
source .env
source mqtt.env
set +o allexport
# ${STACKDIR} is from .env
cd ${STACKDIR}
echo "Shutting down container(s)"
${COMPOSECOMMAND} -f docker-compose.yml down
mosquitto_image=`grep "image: eclipse-mosquitto" docker-compose.yml | awk -F":" '{print $2":"$3}'`
echo "Creating base config file"
echo -e "listener 1883
log_dest file /mosquitto/log/mosquitto.log
log_timestamp_format %Y-%m-%d %H:%M:%S
allow_anonymous false
password_file /mosquitto/config/passwd
persistence_location /mosquitto/data/
persistence_file mosquitto.db
persistence true \n" > ${STACKDIR}/mosquitto/config/mosquitto.conf
echo "Making sure that there is a passwd and log file"
MOSQUITTO_PASSWD_FILE=${STACKDIR}/mosquitto/config/passwd
MOSQUITTO_LOG_FILE=${STACKDIR}/mosquitto/log/mosquitto.log
if [ ! -e ${MOSQUITTO_PASSWD_FILE} ]
then
touch ${MOSQUITTO_PASSWD_FILE}
fi
if [ ! -e ${MOSQUITTO_LOG_FILE} ]
then
touch ${MOSQUITTO_LOG_FILE}
fi
sudo chmod -R ug+rw ${STACKDIR}/mosquitto
sudo chmod -R o+r ${STACKDIR}/mosquitto
sudo chmod -R ugo-x ${STACKDIR}/mosquitto/log/mosquitto.log
#This chown needed exactly as it is below!
sudo chown -R 1883:1883 ${STACKDIR}/mosquitto
echo "Setting up password for image ${mosquitto_image}"
docker run --rm -v ${STACKDIR}/mosquitto/config:/mosquitto/config -v ${STACKDIR}/mosquitto/log:/mosquitto/log ${mosquitto_image} sh -c "mosquitto_passwd -b /mosquitto/config/passwd ${MQTT_USER} ${MQTT_PASSWORD}"
echo "/mosquitto/config/passwd:"
docker run --rm -v ${STACKDIR}/mosquitto/config:/mosquitto/config -v ${STACKDIR}/mosquitto/log:/mosquitto/log ${mosquitto_image} sh -c "cat /mosquitto/config/passwd"
echo "Starting up image ${mosquitto_image}"
${COMPOSECOMMAND} -f docker-compose.yml up -d mosquitto
echo "Show some logs, press Ctrl-C to quit from logs"
${COMPOSECOMMAND} -f docker-compose.yml logs --tail=50 -f
echo "Shutting down container(s)"
${COMPOSECOMMAND} -f docker-compose.yml down
echo 'mqtt: !include mqtt_config.yaml' >> ${STACKDIR}/homeassistant/configuration.yaml
echo 'sensor: !include sensor_config.yaml' >> ${STACKDIR}/homeassistant/configuration.yaml
echo "=> Initial config of Mosquitto completed"