-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostroot.sh
executable file
·369 lines (325 loc) · 13.9 KB
/
postroot.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/bash
# To use important variables from command line use the following code:
COMMAND=$0 # Zero argument is shell command
PTEMPDIR=$1 # First argument is temp folder during install
PSHNAME=$2 # Second argument is Plugin-Name for scipts etc.
PDIR=$3 # Third argument is Plugin installation folder
PVERSION=$4 # Forth argument is Plugin version
#LBHOMEDIR=$5 # Comes from /etc/environment now. Fifth argument is
# Base folder of LoxBerry
PTEMPPATH=$6 # Sixth argument is full temp path during install (see also $1)
# Combine them with /etc/environment
PCGI=$LBPCGI/$PDIR
PHTML=$LBPHTML/$PDIR
PTEMPL=$LBPTEMPL/$PDIR
PDATA=$LBPDATA/$PDIR
PLOG=$LBPLOG/$PDIR # Note! This is stored on a Ramdisk now!
PCONFIG=$LBPCONFIG/$PDIR
PSBIN=$LBPSBIN/$PDIR
PBIN=$LBPBIN/$PDIR
INFLUXDBIN=`which influxd`
INFLUXBIN=`which influx`
OPENSSLBIN=`which openssl`
TELEGRAFBIN=`which telegraf`
ERROR=0
UPGRADE=0
DATE=`date +%Y%m%d%H%M%S`
function pause(){
read -p "$*"
}
# Checking for InfluxDB and Telegraf
if [ ! -x $INFLUXDBIN ]; then
echo "<FAIL> Seems that InfluxDB was not installed correctly. Giving up."
exit 2
fi
if [ ! -x $INFLUXBIN ]; then
echo "<FAIL> Seems that InfluxDB was not installed correctly. Giving up."
exit 2
fi
if [ ! -x $TELEGRAFBIN ]; then
echo "<FAIL> Seems that Telegraf was not installed correctly. Giving up."
exit 2
fi
# Stop all services
echo "<INFO> Stopping InfluxDB and Telegraf."
systemctl stop influxdb
systemctl stop telegraf
systemctl stop grafana-server
# Add all users/groups to each other
echo "<INFO> Adding user loxberry to groups influxdb, telegraf, grafana..."
usermod -a -G influxdb,telegraf,grafana loxberry
echo "<INFO> Adding user influxdb to group loxberry..."
usermod -a -G loxberry influxdb
echo "<INFO> Adding user telegraf to group loxberry..."
usermod -a -G loxberry telegraf
echo "<INFO> Adding user grafana to group loxberry..."
usermod -a -G loxberry grafana
#pause 'Press [Enter] key to continue...'
# Check if we are in upgrade mode
if [ -d $LBHOMEDIR/data/plugins/$PTEMPDIR\_upgrade ]; then
echo "<INFO> We are in Upgrade mode. Use existing database and credentials."
UPGRADE=1
# Log
if [ -n "$(ls -A "$LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade/log" 2>/dev/null)" ]; then
chown -R loxberry:loxberry $PLOG
rsync -Iav $LBHOMEDIR/data/plugins/$PTEMPDIR\_upgrade/log/* $PLOG/
if [ $? -ne 0 ]; then
echo "<FAIL> Restoring log files failed. Giving up."
#pause 'Press [Enter] key to continue...'
mv $LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade $LBHOMEDIR/data/plugins/${DATE}_FAILED_INSTALLATION_STATS4LOX
exit 2
fi
else
echo "<INFO> Folder is empty. Nothing will be restored."
fi
# Data
if [ -n "$(ls -A "$LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade/data" 2>/dev/null)" ]; then
chown -R loxberry:loxberry $PDATA
rsync -Iav $LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade/data/* $PDATA/
if [ $? -ne 0 ]; then
echo "<FAIL> Restoring data files failed. Giving up."
#pause 'Press [Enter] key to continue...'
mv $LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade $LBHOMEDIR/data/plugins/${DATE}_FAILED_INSTALLATION_STATS4LOX
exit 2
fi
else
echo "<INFO> Folder is empty. Nothing will be restored."
fi
# Config
if [ -n "$(ls -A "$LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade/config/" 2>/dev/null)" ]; then
chown -R loxberry:loxberry $PCONFIG
rsync -Iav --exclude "systemd/*" --exclude "sysctl.conf" $LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade/config/* $PCONFIG/
if [ $? -ne 0 ]; then
echo "<FAIL> Restoring config files failed. Giving up."
#pause 'Press [Enter] key to continue...'
mv $LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade $LBHOMEDIR/data/plugins/${DATE}_FAILED_INSTALLATION_STATS4LOX
exit 2
fi
else
echo "<INFO> Folder is empty. Nothing will be restored."
fi
#pause 'Press [Enter] key to continue...'
# Create backup
mkdir -p $PDATA/backups/plugininstall
mv $LBHOMEDIR/data/plugins/${PTEMPDIR}_upgrade $PDATA/backups/plugininstall/${DATE}_backup_plugininstall
PWD=`pwd`
cd $PDATA/backups/plugininstall
7z a ${DATE}_backup_plugininstall.7z ${DATE}_backup_plugininstall '-xr!*.7z'
if [ $? -eq 0 ]; then
rm -rf $PDATA/backups/plugininstall/${DATE}_backup_plugininstall
fi
chown -R loxberry:loxberry $PDATA/backups/plugininstall
cd $PWD
fi
# Get InfluxDB credentials
INFLUXDBUSER=`jq -r '.influx.influxdbuser' $PCONFIG/cred.json`
INFLUXDBPASS=`jq -r '.influx.influxdbpass' $PCONFIG/cred.json`
if [ "$INFLUXDBUSER" = "" ]; then
INFLUXDBUSER="stats4lox"
INFLUXDBPASS="loxberry"
fi
# Debug
#echo "Influx User: $INFLUXDBUSER"
#echo "Influx Pass: $INFLUXDBPASS"
# Activate own config delivered with plugin
echo "<INFO> Activating my own InfluxDB configuration."
if [ -d /etc/influxdb ] && [ ! -L /etc/influxdb ]; then
rm -rf /etc/influxdb.orig
mv /etc/influxdb /etc/influxdb.orig
fi
rm -rf /etc/influxdb > /dev/null 2>&1
ln -s $PCONFIG/influxdb /etc/influxdb
#chown -R loxberry:loxberry $PCONFIG/influxdb
if [ ! -e $PCONFIG/influxdb/influxdb-selfsigned.key ]; then
echo "<INFO> No SSL certificates for InfluxDB found."
echo "<INFO> Creating (new) self-signed SSL certificates."
$OPENSSLBIN req -x509 -nodes -newkey rsa:2048 -keyout $PCONFIG/influxdb/influxdb-selfsigned.key -out $PCONFIG/influxdb/influxdb-selfsigned.crt -days 3650 -subj "/C=DE/ST=Austria/L=Kollerschlag/O=LoxBerry"
#chown loxberry:loxberry $PCONFIG/influxdb/influxdb-selfsigned.*
chmod 660 $PCONFIG/influxdb/influxdb-selfsigned.*
else
echo "<INFO> Found SSL certificates for InfluxDB. I will not create new ones."
fi
# Correct permissions - influxdb must have write permissions to database folders
echo "<INFO> Set permissions for user influxdb for all config/data folders: $PDATA/influxdb $PCONFIG/influxdb"
chown -R influxdb:loxberry $PDATA/influxdb
chown -R influxdb:loxberry $PCONFIG/influxdb
# Debug:
echo "<INFO> Current file permisssions in $PDATA/influxdb:"
ls -l $PDATA/influxdb
echo "<INFO> Current file permisssions in $PCONFIG/influxdb"
ls -l $PCONFIG/influxdb
# Enlarge UDP/IP receive buffer limit for import
echo "<INFO> Enlarge Unix receive buffer limit..."
sysctl -w net.unix.max_dgram_qlen=10000
rm -f /etc/sysctl.d/96-stats4lox.conf
ln -s $PCONFIG/sysctl.conf /etc/sysctl.d/96-stats4lox.conf
# Systemd DropIn Config
echo "<INFO> Install Drop-In for Influx and Telegraf and Grafana systemd services..."
rm -f /etc/systemd/system/influxdb.service.d/00-stats4lox.conf > /dev/null 2>&1
rm -f /etc/systemd/system/telegraf.service.d/00-stats4lox.conf > /dev/null 2>&1
rm -f /etc/systemd/system/grafana-server.service.d/00-stats4lox.conf > /dev/null 2>&1
rm -f /etc/systemd/system/influxdb.service.d/00-stats4lox-influxdb.conf > /dev/null 2>&1
rm -f /etc/systemd/system/telegraf.service.d/00-stats4lox-telegraf.conf > /dev/null 2>&1
rm -f /etc/systemd/system/grafana-server.service.d/00-stats4lox-grafana.conf > /dev/null 2>&1
mkdir -p /etc/systemd/system/influxdb.service.d
mkdir -p /etc/systemd/system/telegraf.service.d
mkdir -p /etc/systemd/system/grafana-server.service.d
ln -s $PCONFIG/systemd/00-stats4lox-influxdb.conf /etc/systemd/system/influxdb.service.d/00-stats4lox-influxdb.conf
ln -s $PCONFIG/systemd/00-stats4lox-telegraf.conf /etc/systemd/system/telegraf.service.d/00-stats4lox-telegraf.conf
ln -s $PCONFIG/systemd/00-stats4lox-grafana.conf /etc/systemd/system/grafana-server.service.d/00-stats4lox-grafana.conf
systemctl daemon-reload
# Activate InfluxDB service and start
echo "<INFO> Starting InfluxDB..."
systemctl unmask influxdb.service
systemctl enable --now influxdb
systemctl daemon-reload
systemctl start influxdb
sleep 3
# Check status
systemctl status influxdb > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo "<FAIL> Seems that InfluxDB could not be started. Giving up."
exit 2
else
echo "<OK> InfluxDB service is running."
fi
# Check InfluxDB user. Create it if not exists
#RESP=`$PBIN/s4linflux -execute "SHOW USERS" | grep -e "^$INFLUXDBUSER\W*true$" | wc -l`
#RESP=`$INFLUXBIN -ssl -unsafeSsl -username $INFLUXDBUSER -password '$INFLUXDBPASS' -execute "SHOW USERS" | grep -e "^$INFLUXDBUSER\W*true$" | wc -l`
#echo "Response checking Influx user is: $RESP"
if [ $UPGRADE -eq "0" ]; then
echo "<INFO> Creating default InfluxDB user 'stats4lox' as admin user."
INFLUXDBUSER="stats4lox"
INFLUXDBPASS=`head /dev/urandom | tr -dc A-Za-z0-9 | head -c16`
# Debug
#echo "Influx User: $INFLUXDBUSER"
#echo "Influx Pass: $INFLUXDBPASS"
$INFLUXBIN -ssl -unsafeSsl -execute "CREATE USER $INFLUXDBUSER WITH PASSWORD '$INFLUXDBPASS' WITH ALL PRIVILEGES"
#echo "Coammand is: $INFLUXBIN -ssl -unsafeSsl -execute \"CREATE USER $INFLUXDBUSER WITH PASSWORD '$INFLUXDBPASS' WITH ALL PRIVILEGES\""
#echo "Response creating Influx user is: $?"
if [ $? -ne 0 ]; then
echo "<ERROR> Could not create default InfluxDB user. Giving up."
exit 2
else
echo "<OK> Default InfluxDB user '$INFLUXDBUSER' created successfully."
echo "<INFO> Saving credentials in cred.json."
jq ".influx.influxdbuser = \"$INFLUXDBUSER\"" $PCONFIG/cred.json > $PCONFIG/cred.json.new
mv $PCONFIG/cred.json.new $PCONFIG/cred.json
jq ".influx.influxdbpass = \"$INFLUXDBPASS\"" $PCONFIG/cred.json > $PCONFIG/cred.json.new
mv $PCONFIG/cred.json.new $PCONFIG/cred.json
chown loxberry:loxberry $PCONFIG/cred.json
chmod 640 $PCONFIG/cred.json
fi
else
echo "<OK> We are in Upgrade mode. I will use existing credentials."
fi
# Check for stats4lox database. Create it if not exists
#RESP=`$PBIN/s4linflux -execute "SHOW DATABASES" | grep -e "^stats4lox$" | wc -l`
#if [ $RESP -eq 0 ]; then
if [ $UPGRADE -eq "0" ]; then
echo "<INFO> Creating default InfluxDB database 'stats4lox'."
$PBIN/s4linflux -execute "CREATE DATABASE stats4lox"
if [ $? -gt 0 ]; then
echo "<ERROR> Could not create default InfluxDB database. Giving up."
exit 2
else
echo "<OK> InfluxDB database 'stats4lox' created successfully."
fi
else
echo "<OK> We are in Upgrade mode. I will use existing database stats4lox."
fi
# Activating own telegraf config which is delivered with the plugin
echo "<INFO> Activating my own Telegraf configuration."
if [ -d /etc/telegraf ] && [ ! -L /etc/telegraf ]; then
rm -rf /etc/telegraf.orig
mv /etc/telegraf /etc/telegraf.orig
fi
if [ ! -L /etc/default/telegraf ]; then
rm -f /etc/default/telegraf.orig
mv /etc/default/telegraf /etc/default/telegraf.orig
fi
rm -rf /etc/telegraf > /dev/null 2>&1
rm -f /etc/default/telegraf > /dev/null 2>&1
ln -s $PCONFIG/telegraf /etc/telegraf
ln -s $PCONFIG/telegraf/telegraf.env /etc/default/telegraf
# Correct permissions - influxdb must have write permissions to database folders
echo "<INFO> Set permissions for user telegraf for all config/data folders: $PDATA/telegraf $PCONFIG/telegraf"
chown -R telegraf:loxberry $PDATA/telegraf
chown -R telegraf:loxberry $PCONFIG/telegraf
# Debug:
echo "<INFO> Current file permisssions in $PDATA/telegraf"
ls -l $PDATA/telegraf
echo "<INFO> Current file permisssions in $PCONFIG/telegraf"
ls -l $PCONFIG/telegraf
# Saving InfluxDB credentials in Telegraf config and set restrictive permissions to that file
#
# REPLACE THIS WITH CONFIG-HANDLER LATER ON
#
echo "<INFO> Saving credentials in Telegraf configuration (telegraf.env) and restart Telegraf afterwards."
awk -v s="USER_INFLUXDB=\"$INFLUXDBUSER\"" '/^USER_INFLUXDB=/{$0=s;f=1} {a[++n]=$0} END{if(!f)a[++n]=s;for(i=1;i<=n;i++)print a[i]>ARGV[1]}' $PCONFIG/telegraf/telegraf.env
awk -v s="PASS_INFLUXDB=\"$INFLUXDBPASS\"" '/^PASS_INFLUXDB=/{$0=s;f=1} {a[++n]=$0} END{if(!f)a[++n]=s;for(i=1;i<=n;i++)print a[i]>ARGV[1]}' $PCONFIG/telegraf/telegraf.env
chown telegraf:loxberry $PCONFIG/telegraf/telegraf.env
chmod 660 $PCONFIG/telegraf/telegraf.env
# Use correct Webserver Port in Telegraf
#
# REPLACE THIS WITH CONFIG-HANDLER LATER ON
#
echo "<INFO> Activating LB Webserver Port in Telegraf configuration (telegraf.d/stats4lox_loxone.conf) and restart Telegraf afterwards."
LBWEBSERVERPORT=`perl -e 'use LoxBerry::System; print lbwebserverport();'`
sed -i "s/^ urls = .*$/ urls = [ \"http:\/\/localhost:$LBWEBSERVERPORT\/admin\/plugins\/$PDIR\/grabber\/grabber_loxone.cgi\" ]/g" $PCONFIG/telegraf/telegraf.d/stats4lox_loxone.conf
# Telegraf mit neuer Config starten
echo "<INFO> Starting Telegraf..."
systemctl unmask telegraf.service
systemctl enable --now telegraf
systemctl daemon-reload
systemctl start telegraf
sleep 3
# Check status
systemctl status telegraf > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo "<FAIL> Seems that Telegraf could not be started. Giving up."
exit 2
else
echo "<OK> Telegraf service is running."
fi
# Activate own config delivered with plugin
echo "<INFO> Activating my own Grafana configuration."
if [ -d /etc/grafana ] && [ ! -L /etc/grafana ]; then
rm -rf /etc/grafana.orig
mv /etc/grafana /etc/grafana.orig
fi
rm -rf /etc/grafana > /dev/null 2>&1
ln -s $PCONFIG/grafana /etc/grafana
# Give grafana user permissions to data/provisioning
$PBIN/provisioning/set_datasource_influx.pl
$PBIN/provisioning/set_dashboard_provider.pl
# Correct permissions - influxdb must have write permissions to database folders
echo "<INFO> Set permissions for user grafana for all config/data folders: $PDATA/grafana $PCONFIG/grafana"
chown -R grafana:loxberry $PDATA/grafana
chown -R grafana:loxberry $PCONFIG/grafana
# Debug:
echo "<INFO> Current file permisssions in $PDATA/grafana:"
ls -l $PDATA/grafana
echo "<INFO> Current file permisssions in $PCONFIG/grafana"
ls -l $PCONFIG/grafana
# Activate Grafana
echo "<INFO> Starting Grafana..."
systemctl enable --now grafana-server
systemctl daemon-reload
systemctl start grafana-server
sleep 3
# Start/Stop MQTT Live Service
echo "<INFO> Starting MQTTLive Service..."
su loxberry -c "$PBIN/mqtt/mqttlive.php >> $PLOG/mqttlive.log 2>&1 &"
# Adjust owner of config-handler
echo "<INFO> Chown config-handler to root..."
chown root:root $PBIN/config-handler.pl
# For debugging
if [ $UPGRADE -eq "1" ]; then
echo "<INFO> We are in Upgrade mode. Do some checks for debugging..."
echo "<INFO> Existing users (gives an error if we have wrong credentials):"
$PBIN/s4linflux -execute "SHOW USERS"
echo "<INFO> Existing databases (gives an error if we have wrong credentials):"
$PBIN/s4linflux -execute "SHOW DATABASES"
fi
exit 0