forked from wmbusmeters/wmbusmeters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·374 lines (321 loc) · 10 KB
/
install.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
370
371
372
373
374
#!/bin/bash
if [ "$1" == "" ] || [ "$1" == "-h" ]
then
echo "Usage: install.sh [binary] [root] [OPTIONS]
Example: install.sh build/wmbusmeters /
Options:
--no-adduser Do not add wmbusmeters user
--no-udev-rules Do not add udev rules
"
exit 0
fi
if [ ! "$(basename "$1")" = "wmbusmeters" ]
then
echo Oups, please only try to install wmbusmeters using this script.
exit 1
fi
if [ ! -x "$1" ]
then
echo This is not an executable.
exit 1
fi
if [ ! -d "$2" ]
then
echo Oups, please supply a valid root directory.
exit 1
fi
SRC=$1
ROOT="${2%/}"
ADDUSER=true
ADDUDEVRULES=true
while [ $# -ne 0 ]
do
ARG="$1"
shift
case "$ARG" in
--no-adduser)
ADDUSER=false
;;
--no-udev-rules)
ADDUDEVRULES=false
shift
;;
esac
done
####################################################################
##
## Intall binaries
##
rm -f "$ROOT"/usr/bin/wmbusmeters "$ROOT"/usr/sbin/wmbusmetersd
mkdir -p "$ROOT"/usr/bin
mkdir -p "$ROOT"/usr/sbin
cp "$SRC" "$ROOT"/usr/bin/wmbusmeters
ln -s /usr/bin/wmbusmeters "$ROOT"/usr/sbin/wmbusmetersd
echo binaries: installed "$ROOT"/usr/bin/wmbusmeters and "$ROOT"/usr/sbin/wmbusmetersd
####################################################################
##
## Intall wmbusmeters manual page
##
rm -f "$ROOT"/usr/share/man/man1/wmbusmeters.1.gz
mkdir -p "$ROOT"/usr/share/man/man1
gzip -c wmbusmeters.1 > "$ROOT"/usr/share/man/man1/wmbusmeters.1.gz
echo man page: installed "$ROOT"/usr/share/man/man1/wmbusmeters.1.gz
####################################################################
##
## Create wmbusmeters user
##
ID=$(id -u wmbusmeters 2>/dev/null)
if [ -f "$ROOT"/usr/sbin/nologin ]
then
USERSHELL="$ROOT/usr/sbin/nologin"
elif [ -f "$ROOT"/sbin/nologin ]
then
USERSHELL="$ROOT/sbin/nologin"
else
USERSHELL="/bin/false"
fi
if [ "$ADDUSER" = "true" ]
then
if [ "$ID" = "" ]
then
# Create the wmbusmeters user
useradd --system --shell $USERSHELL --groups dialout wmbusmeters
echo user: added wmbusmeters
else
echo user: wmbusmeters unmodified
fi
if [ ! -z "$SUDO_USER" ]
then
if [ "$(groups $SUDO_USER | grep -o wmbusmeters)" = "" ]
then
# Add user to the wmbusmeters group.
usermod -a -G wmbusmeters $SUDO_USER
echo user: added $SUDO_USER to group wmbusmeters
else
echo user: user $SUDO_USER already added group wmbusmeters
fi
fi
fi
####################################################################
##
## Prepare for /run/wmbusmeters.pid
##
#if [ ! -d "$ROOT"/var/run ]
#then
# # Create /var/run
# mkdir -p "$ROOT"/var/run
# echo pid store: created /var/run
#fi
####################################################################
##
## Prepare for /var/log/wmbusmeters and /var/log/wmbusmeters/meter_readings
##
if [ ! -d "$ROOT"/var/log/wmbusmeters/meter_readings ]
then
# Create the log directories
mkdir -p "$ROOT"/var/log/wmbusmeters/meter_readings
chown -R wmbusmeters:wmbusmeters "$ROOT"/var/log/wmbusmeters
echo log: created "$ROOT"/var/log/wmbusmeters/meter_readings
fi
####################################################################
##
## Install /etc/logrotate.d/wmbusmeters
##
if [ ! -f "$ROOT"/etc/logrotate.d/wmbusmeters ]
then
mkdir -p "$ROOT"/etc/logrotate.d
# Create logrotate file
cat <<EOF > "$ROOT"/etc/logrotate.d/wmbusmeters
/var/log/wmbusmeters/*.log {
rotate 12
weekly
compress
missingok
postrotate
/bin/kill -HUP `cat /run/wmbusmeters/wmbusmeters.pid 2> /dev/null` 2> /dev/null || true
endscript
EOF
echo logrotate: created "$ROOT"/etc/logrotate.d/wmbusmeters
else
echo conf file: "$ROOT"/etc/logrotate.d/wmbusmeters unchanged
fi
####################################################################
##
## Install /etc/wmbusmeters.conf
##
if [ ! -f "$ROOT"/etc/wmbusmeters.conf ]
then
# Create default configuration
mkdir -p "$ROOT"/etc/
cat <<EOF > "$ROOT"/etc/wmbusmeters.conf
loglevel=normal
device=auto
logtelegrams=false
format=json
meterfiles=/var/log/wmbusmeters/meter_readings
meterfilesaction=overwrite
logfile=/var/log/wmbusmeters/wmbusmeters.log
EOF
chmod 644 "$ROOT"/etc/wmbusmeters.conf
echo conf file: created "$ROOT"/etc/wmbusmeters.conf
else
echo conf file: "$ROOT"/etc/wmbusmeters.conf unchanged
fi
####################################################################
##
## Create /etc/wmbusmeters.d
##
if [ ! -d "$ROOT"/etc/wmbusmeters.d ]
then
# Create the configuration directory
mkdir -p "$ROOT"/etc/wmbusmeters.d
chmod -R 755 "$ROOT"/etc/wmbusmeters.d
echo conf dir: created "$ROOT"/etc/wmbusmeters.d
else
echo conf dir: "$ROOT"/etc/wmbusmeters.d unchanged
fi
####################################################################
##
## Create /lib/systemd/system/wmbusmeters.service
##
SYSTEMD_NEEDS_RELOAD=false
mkdir -p "$ROOT"/lib/systemd/system/
OLD_WMBS=~/old.wmbusmeters.service.backup
CURR_WMBS="$ROOT"/lib/systemd/system/wmbusmeters.service
if [ -f $CURR_WMBS ]
then
echo systemd: backing up $CURR_WMBS to here: $OLD_WMBS
cp $CURR_WMBS $OLD_WMBS 2>/dev/null
fi
# Create service file for starting daemon without explicit device.
# This means that wmbusmeters will rely on the conf file device setting.
cat <<'EOF' > $CURR_WMBS
[Unit]
Description="wmbusmeters service (no udev trigger)"
Documentation=https://github.com/weetmuts/wmbusmeters
Documentation=man:wmbusmeters(1)
After=network.target
StopWhenUnneeded=false
StartLimitIntervalSec=10
StartLimitInterval=10
StartLimitBurst=3
[Service]
Type=forking
PrivateTmp=yes
User=wmbusmeters
Group=wmbusmeters
Restart=always
RestartSec=1
# Run ExecStartPre with root-permissions
PermissionsStartOnly=true
ExecStartPre=-/bin/mkdir -p /var/log/wmbusmeters/meter_readings
ExecStartPre=/bin/chown -R wmbusmeters:wmbusmeters /var/log/wmbusmeters
ExecStartPre=-/bin/mkdir -p /run/wmbusmeters
ExecStartPre=/bin/chown -R wmbusmeters:wmbusmeters /run/wmbusmeters
ExecStart=/usr/sbin/wmbusmetersd /run/wmbusmeters/wmbusmeters.pid
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/run/wmbusmeters/wmbusmeters.pid
[Install]
WantedBy=multi-user.target
EOF
if diff $OLD_WMBS $CURR_WMBS 1>/dev/null
then
echo systemd: no changes to $CURR_WMBS
else
echo systemd: updated $CURR_WMBS
SYSTEMD_NEEDS_RELOAD=true
fi
OLD_WMBAS=~/old.wmbusmeters@.service.backup
CURR_WMBAS="$ROOT"/lib/systemd/system/wmbusmeters@.service
if [ -f $CURR_WMBAS ]
then
echo systemd: backing up $CURR_WMBAS to here: $OLD_WMBAS
cp $CURR_WMBAS $OLD_WMBAS 2>/dev/null
fi
# Create service file that needs an argument eg.
# sudo systemctl start wmbusmeters@/dev/im871a_1.service
cat <<'EOF' > "$ROOT"/lib/systemd/system/wmbusmeters@.service
[Unit]
Description="wmbusmeters service (udev triggered by %I)"
Documentation=https://github.com/weetmuts/wmbusmeters
Documentation=man:wmbusmeters(1)
After=network.target
StopWhenUnneeded=true
StartLimitIntervalSec=10
StartLimitInterval=10
StartLimitBurst=3
[Service]
Type=forking
PrivateTmp=yes
User=wmbusmeters
Group=wmbusmeters
Restart=always
RestartSec=1
# Run ExecStartPre with root-permissions
PermissionsStartOnly=true
ExecStartPre=-/bin/mkdir -p /var/log/wmbusmeters/meter_readings
ExecStartPre=/bin/chown -R wmbusmeters:wmbusmeters /var/log/wmbusmeters
ExecStartPre=-/bin/mkdir -p /run/wmbusmeters
ExecStartPre=/bin/chown -R wmbusmeters:wmbusmeters /run/wmbusmeters
ExecStart=/usr/sbin/wmbusmetersd --device='%I' /run/wmbusmeters/wmbusmeters-%i.pid
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/run/wmbusmeters/wmbusmeters-%i.pid
[Install]
WantedBy=multi-user.target
EOF
if diff $OLD_WMBAS $CURR_WMBAS 1>/dev/null
then
echo systemd: no changes to $CURR_WMBAS
else
echo systemd: updated $CURR_WMBAS
SYSTEMD_NEEDS_RELOAD=true
fi
####################################################################
##
## Create /etc/udev/rules.d/99-wmbus-usb-serial.rules
##
UDEV_NEEDS_RELOAD=false
if [ "$ADDUDEVRULES" = "true" ]
then
if [ -f "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules ]
then
echo udev: removing "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules
echo udev: backup stored here: ~/old.wmbusmeters-wmbus-usb-serial.rules.backup
cp "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules ~/old.wmbusmeters-wmbus-usb-serial.rules.backup
rm "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules
UDEV_NEEDS_RELOAD=true
fi
if [ ! -f "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules ]
then
mkdir -p "$ROOT"/etc/udev/rules.d
# Create service file
cat <<EOF > "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules
SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60",SYMLINK+="im871a_%n",MODE="0660", GROUP="wmbusmeters",TAG+="systemd",ENV{SYSTEMD_WANTS}="wmbusmeters@/dev/im871a_%n.service"
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001",SYMLINK+="amb8465_%n",MODE="0660", GROUP="wmbusmeters",TAG+="systemd",ENV{SYSTEMD_WANTS}="wmbusmeters@/dev/amb8465_%n.service"
SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2838",SYMLINK+="rtlsdr_%n",MODE="0660", GROUP="wmbusmeters",TAG+="systemd",ENV{SYSTEMD_WANTS}="wmbusmeters@/dev/rtlsdr_%n.service"
SUBSYSTEM=="usb", ATTRS{idVendor}=="2047", ATTRS{idProduct}=="0863",SYMLINK+="rfmrx2_%n",MODE="0660", GROUP="wmbusmeters",TAG+="systemd",ENV{SYSTEMD_WANTS}="wmbusmeters@/dev/rfmrx2_%n.service"
EOF
echo udev: installed "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules
else
echo udev: "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules unchanged
fi
fi
if [ "$SYSTEMD_NEEDS_RELOAD" = "true" ]
then
echo
echo
echo You need to reload systemd configuration! Please do:
echo sudo systemctl daemon-reload
fi
if [ "$UDEV_NEEDS_RELOAD" = "true" ]
then
D=$(diff "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules ~/old.wmbusmeters-wmbus-usb-serial.rules.backup)
if [ "$D" != "" ]
then
echo
echo
echo You need to reload udev configuration! Please do:
echo "sudo udevadm control --reload-rules"
echo "sudo udevadm trigger"
fi
fi