forked from ct-Open-Source/ct-Smart-Home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·207 lines (167 loc) · 4.9 KB
/
start.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/bash
function detect_zigbee_device {
if usb_dev=$(lsusb -d 0451:); then
usb_dev_count=$(ls -1 /dev/ttyACM* 2>/dev/null | wc -l)
if [ "$usb_dev_count" -gt 1 ]; then
>&2 echo "There are multiple devices connected, that could be Zigbee USB adaptors. Please check data/zigbee/configuration.yml, if the device is wrong. /dev/ttyACM0 is used as the default."
echo "/dev/ttyACM0"
fi
if [ -c /dev/ttyACM0 ]; then
echo "/dev/ttyACM0"
else
>&2 echo "I could not find /dev/ttyACM0. Please check your hardware."
fi
else
>&2 echo No Texas Instruments USB device found.
echo "False"
fi
}
function create_mosquitto_config {
cat > data/mqtt/config/mosquitto.conf <<EOF
log_type all
listener 1883
listener 9001
protocol websockets
# Uncomment the following lines and create a passwd file using mosquitto_passwd to enable authentication.
#password_file /mosquitto/config/passwd
# Set this to false, to enable authentication
allow_anonymous true
EOF
touch data/mqtt/config/passwd
}
function create_zigbee2mqtt_config {
cat > data/zigbee/configuration.yaml <<EOF
# Home Assistant integration (MQTT discovery)
homeassistant: true
# allow new devices to join
permit_join: true
# enable frontend
frontend:
port: 1881
experimental:
new_api: true
# MQTT settings
mqtt:
# MQTT base topic for zigbee2mqtt MQTT messages
base_topic: zigbee2mqtt
# MQTT server URL
server: 'mqtt://mqtt'
# MQTT server authentication, uncomment if required:
# user: my_user
# password: my_password
advanced:
channel: 25
network_key: GENERATE
EOF
echo '⚠️ Disable permit_join in data/zigbee/configuration.yaml or the Zigbee2MQTT webinterface on port 1881, after you have paired all of your devices!'
}
function fix_permissions {
echo '📄 Setting the permissions of the configurations in the data folder.'
sudo chown 1883:1883 data/mqtt
sudo chown -Rf 1883:1883 data/mqtt/*
sudo chown 1000:1000 data/nodered
sudo chown -Rf 1000:1000 data/nodered/*
}
function build_data_structure {
echo '📄 Configuration folder ./data is missing. Creating it from scratch.'
mkdir -p data/mqtt/config
mkdir -p data/zigbee/
mkdir -p data/nodered/
if [ ! -f data/mqtt/config/mosquitto.conf ]; then
create_mosquitto_config
fi
if [ ! -f data/zigbee/configuration.yaml ]; then
create_zigbee2mqtt_config
fi
fix_permissions
}
function check_dependencies {
if ! [ -x "$(command -v docker-compose)" ]; then
echo '⚠️ Error: docker-compose is not installed.' >&2
exit 1
fi
if ! [ -x "$(command -v git)" ]; then
echo '⚠️ Error: git is not installed.' >&2
exit 1
fi
}
function start {
device=$(detect_zigbee_device)
if [ $device == "False" ]; then
echo '⚠️ No Zigbee adaptor found. Not starting Zigbee2MQTT.'
container="nodered mqtt"
fi
if [ ! -d data ]; then
build_data_structure
fi
echo '🏃 Starting the containers'
docker-compose up -d $container
echo '⚠️ After you made yourself familiar with the setup, it'"'"'s strongly suggested to secure the services. Read the "Security" section in the README!'
}
function stop {
echo '🛑 Stopping all containers'
docker-compose stop
}
function update {
if [[ ! -d ".git" ]]
then
echo "🛑You have manually downloaded the release version of c't-Smart-Home.
The automatic update only works with a cloned Git repository.
Try backing up your settings shutting down all containers with
docker-compose down --remove orphans
Then copy the current version from GitHub to this folder and run
./start.sh start.
Alternatively create a Git clone of the repository."
exit 1
fi
echo '☠️ Shutting down all running containers and removing them.'
docker-compose down --remove-orphans
if [ ! $? -eq 0 ]; then
echo '⚠️ Updating failed. Please check the repository on GitHub.'
fi
echo '⬇️ Pulling latest release via git.'
git fetch --tags
latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $latestTag
if [ ! $? -eq 0 ]; then
echo '⚠️ Updating failed. Please check the repository on GitHub.'
fi
echo '⬇️ Pulling docker images.'
docker-compose pull
if [ ! $? -eq 0 ]; then
echo '⚠️ Updating failed. Please check the repository on GitHub.'
fi
start
fix_permissions
}
check_dependencies
case "$1" in
"start")
start
;;
"stop")
stop
;;
"update")
update
;;
"fix")
fix_permissions
;;
"data")
build_data_structure
;;
* )
cat << EOF
🏡 c't-Smart-Home – setup script
—————————————————————————————
Usage:
start.sh update – update to the latest release version
start.sh fix – correct the permissions in the data folder
start.sh start – run all containers
start.sh stop – stop all containers
start.sh data – set up the data folder needed for the containers, but run none of them. Useful for personalized setups.
Check https://github.com/ct-Open-Source/ct-Smart-Home/ for updates.
EOF
;;
esac