2019-02-23 17:30:16 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2019-04-25 20:29:15 +00:00
|
|
|
if [ "$1" == "" ] || [ "$1" == "-h" ]
|
2019-02-23 17:30:16 +00:00
|
|
|
then
|
2019-04-26 14:58:47 +00:00
|
|
|
echo "Usage: install.sh [binary] [root] [OPTIONS]
|
2019-04-25 20:29:15 +00:00
|
|
|
Example: install.sh build/wmbusmeters /
|
2019-05-21 12:19:54 +00:00
|
|
|
|
2020-10-26 11:28:23 +00:00
|
|
|
Options:
|
|
|
|
--no-adduser Do not add wmbusmeters user
|
|
|
|
"
|
2019-02-23 17:30:16 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
if [ ! "$(basename "$1")" = "wmbusmeters" ]
|
2019-02-23 17:30:16 +00:00
|
|
|
then
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "Oups, please only try to install wmbusmeters using this script."
|
2019-02-23 17:30:16 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -x "$1" ]
|
|
|
|
then
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "This is not an executable."
|
2019-02-23 17:30:16 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-02-23 18:49:30 +00:00
|
|
|
if [ ! -d "$2" ]
|
|
|
|
then
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "Oups, please supply a valid root directory."
|
2019-02-23 18:49:30 +00:00
|
|
|
exit 1
|
2019-02-23 17:30:16 +00:00
|
|
|
fi
|
|
|
|
|
2019-04-26 14:58:47 +00:00
|
|
|
SRC=$1
|
2019-02-23 18:49:30 +00:00
|
|
|
ROOT="${2%/}"
|
2019-04-25 20:29:15 +00:00
|
|
|
ADDUSER=true
|
|
|
|
|
|
|
|
while [ $# -ne 0 ]
|
|
|
|
do
|
|
|
|
ARG="$1"
|
2019-05-21 12:19:54 +00:00
|
|
|
shift
|
2019-04-25 20:29:15 +00:00
|
|
|
case "$ARG" in
|
|
|
|
--no-adduser)
|
|
|
|
ADDUSER=false
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-02-24 08:58:31 +00:00
|
|
|
####################################################################
|
|
|
|
##
|
|
|
|
## Intall binaries
|
|
|
|
##
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
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
|
2020-02-24 21:31:45 +00:00
|
|
|
ln -s /usr/bin/wmbusmeters "$ROOT"/usr/sbin/wmbusmetersd
|
2019-02-23 17:30:16 +00:00
|
|
|
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "binaries: installed $ROOT/usr/bin/wmbusmeters and $ROOT/usr/sbin/wmbusmetersd"
|
2019-02-23 17:30:16 +00:00
|
|
|
|
2019-02-26 21:19:16 +00:00
|
|
|
####################################################################
|
|
|
|
##
|
|
|
|
## Intall wmbusmeters manual page
|
|
|
|
##
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
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
|
2019-02-26 21:19:16 +00:00
|
|
|
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "man page: installed $ROOT/usr/share/man/man1/wmbusmeters.1.gz"
|
2019-02-26 21:19:16 +00:00
|
|
|
|
2019-02-24 08:58:31 +00:00
|
|
|
####################################################################
|
|
|
|
##
|
|
|
|
## Create wmbusmeters user
|
|
|
|
##
|
|
|
|
|
2020-10-25 15:00:06 +00:00
|
|
|
ID=$(id -u wmbusmeters 2>/dev/null)
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
if [ -f "$ROOT"/usr/sbin/nologin ]
|
2019-04-25 20:29:15 +00:00
|
|
|
then
|
2020-10-26 11:28:23 +00:00
|
|
|
USERSHELL="$ROOT/usr/sbin/nologin"
|
2019-04-26 16:32:36 +00:00
|
|
|
elif [ -f "$ROOT"/sbin/nologin ]
|
2019-04-25 20:29:15 +00:00
|
|
|
then
|
2020-10-26 11:28:23 +00:00
|
|
|
USERSHELL="$ROOT/sbin/nologin"
|
2019-04-25 20:29:15 +00:00
|
|
|
else
|
2020-10-26 11:28:23 +00:00
|
|
|
USERSHELL="/bin/false"
|
2019-04-25 20:29:15 +00:00
|
|
|
fi
|
|
|
|
|
2019-02-23 18:49:30 +00:00
|
|
|
if [ "$ADDUSER" = "true" ]
|
2019-02-23 17:30:16 +00:00
|
|
|
then
|
2020-10-25 15:00:06 +00:00
|
|
|
if [ $(getent group wmbusmeters) ]
|
|
|
|
then
|
2020-10-26 11:28:23 +00:00
|
|
|
echo "group: wmbusmeters unmodified"
|
2020-10-25 15:00:06 +00:00
|
|
|
else
|
2020-10-26 11:28:23 +00:00
|
|
|
groupadd -f wmbusmeters
|
|
|
|
echo "group: added wmbusmeters"
|
2020-10-25 15:00:06 +00:00
|
|
|
fi
|
2020-09-18 18:05:59 +00:00
|
|
|
|
2020-08-30 19:33:48 +00:00
|
|
|
if [ -z "$ID" ]
|
2019-02-23 18:49:30 +00:00
|
|
|
then
|
|
|
|
# Create the wmbusmeters user
|
2020-09-18 18:05:59 +00:00
|
|
|
useradd --system --shell $USERSHELL -g wmbusmeters wmbusmeters
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "user: added wmbusmeters"
|
2019-02-23 18:49:30 +00:00
|
|
|
else
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "user: wmbusmeters unmodified"
|
2019-02-23 18:49:30 +00:00
|
|
|
fi
|
2020-08-30 19:33:48 +00:00
|
|
|
|
2020-10-25 15:00:06 +00:00
|
|
|
if [ $(getent group dialout) ]
|
2020-08-30 19:33:48 +00:00
|
|
|
then
|
2020-10-26 11:28:23 +00:00
|
|
|
if [ "$(groups wmbusmeters | grep -o dialout)" = "" ]
|
|
|
|
then
|
|
|
|
# Add the wmbusmeters user to dialout
|
|
|
|
usermod -a -G dialout wmbusmeters
|
|
|
|
echo "user: added wmbusmeters to dialout group"
|
|
|
|
else
|
|
|
|
echo "user: wmbusmeters already added to dialout"
|
|
|
|
fi
|
2020-08-30 19:33:48 +00:00
|
|
|
else
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "dialout group does not exist"
|
2020-08-30 19:33:48 +00:00
|
|
|
fi
|
2020-09-18 18:05:59 +00:00
|
|
|
|
2020-10-25 15:00:06 +00:00
|
|
|
if [ $(getent group uucp) ]
|
2020-09-18 18:05:59 +00:00
|
|
|
then
|
2020-10-26 11:28:23 +00:00
|
|
|
if [ "$(groups wmbusmeters | grep -o uucp)" = "" ]
|
|
|
|
then
|
|
|
|
# Add the wmbusmeters user to uucp
|
|
|
|
usermod -a -G uucp wmbusmeters
|
|
|
|
echo "user: added wmbusmeters to uucp group"
|
|
|
|
else
|
|
|
|
echo "user: wmbusmeters already added to uucp"
|
|
|
|
fi
|
2020-09-18 18:05:59 +00:00
|
|
|
else
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "uucp group does not exist"
|
2020-02-22 20:16:24 +00:00
|
|
|
fi
|
2020-10-26 11:28:23 +00:00
|
|
|
|
|
|
|
if [ $(getent group plugdev) ]
|
|
|
|
then
|
|
|
|
if [ "$(groups wmbusmeters | grep -o plugdev)" = "" ]
|
|
|
|
then
|
|
|
|
# Add the wmbusmeters user to plugdev
|
|
|
|
usermod -a -G plugdev wmbusmeters
|
|
|
|
echo "user: added wmbusmeters to plugdev group"
|
|
|
|
else
|
|
|
|
echo user: wmbusmeters already added to plugdev
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "plugdev group does not exist"
|
|
|
|
fi
|
2019-02-23 17:30:16 +00:00
|
|
|
fi
|
|
|
|
|
2019-02-24 08:58:31 +00:00
|
|
|
####################################################################
|
|
|
|
##
|
2020-03-03 12:29:21 +00:00
|
|
|
## Prepare for /run/wmbusmeters.pid
|
2019-02-24 08:58:31 +00:00
|
|
|
##
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
#if [ ! -d "$ROOT"/var/run ]
|
2019-02-24 08:58:31 +00:00
|
|
|
#then
|
|
|
|
# # Create /var/run
|
2019-04-26 16:32:36 +00:00
|
|
|
# mkdir -p "$ROOT"/var/run
|
2019-02-24 08:58:31 +00:00
|
|
|
# echo pid store: created /var/run
|
|
|
|
#fi
|
|
|
|
|
|
|
|
####################################################################
|
|
|
|
##
|
|
|
|
## Prepare for /var/log/wmbusmeters and /var/log/wmbusmeters/meter_readings
|
|
|
|
##
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
if [ ! -d "$ROOT"/var/log/wmbusmeters/meter_readings ]
|
2019-02-24 08:58:31 +00:00
|
|
|
then
|
2019-05-21 12:19:54 +00:00
|
|
|
# Create the log directories
|
2019-04-26 16:32:36 +00:00
|
|
|
mkdir -p "$ROOT"/var/log/wmbusmeters/meter_readings
|
|
|
|
chown -R wmbusmeters:wmbusmeters "$ROOT"/var/log/wmbusmeters
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "log: created $ROOT/var/log/wmbusmeters/meter_readings"
|
2019-02-24 08:58:31 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
####################################################################
|
|
|
|
##
|
|
|
|
## Install /etc/logrotate.d/wmbusmeters
|
|
|
|
##
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
if [ ! -f "$ROOT"/etc/logrotate.d/wmbusmeters ]
|
2019-02-24 08:58:31 +00:00
|
|
|
then
|
2019-04-26 16:32:36 +00:00
|
|
|
mkdir -p "$ROOT"/etc/logrotate.d
|
2019-02-24 08:58:31 +00:00
|
|
|
# Create logrotate file
|
2019-04-26 16:32:36 +00:00
|
|
|
cat <<EOF > "$ROOT"/etc/logrotate.d/wmbusmeters
|
2019-02-24 08:58:31 +00:00
|
|
|
/var/log/wmbusmeters/*.log {
|
|
|
|
rotate 12
|
|
|
|
weekly
|
|
|
|
compress
|
|
|
|
missingok
|
|
|
|
postrotate
|
2020-03-03 12:29:21 +00:00
|
|
|
/bin/kill -HUP `cat /run/wmbusmeters/wmbusmeters.pid 2> /dev/null` 2> /dev/null || true
|
2019-02-24 08:58:31 +00:00
|
|
|
endscript
|
|
|
|
EOF
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "logrotate: created $ROOT/etc/logrotate.d/wmbusmeters"
|
2019-06-13 14:42:00 +00:00
|
|
|
else
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "conf file: $ROOT/etc/logrotate.d/wmbusmeters unchanged"
|
2019-02-24 08:58:31 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
####################################################################
|
|
|
|
##
|
|
|
|
## Install /etc/wmbusmeters.conf
|
|
|
|
##
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
if [ ! -f "$ROOT"/etc/wmbusmeters.conf ]
|
2019-02-23 17:30:16 +00:00
|
|
|
then
|
|
|
|
# Create default configuration
|
2019-04-26 16:32:36 +00:00
|
|
|
mkdir -p "$ROOT"/etc/
|
|
|
|
cat <<EOF > "$ROOT"/etc/wmbusmeters.conf
|
2019-02-24 16:31:32 +00:00
|
|
|
loglevel=normal
|
2019-02-23 17:30:16 +00:00
|
|
|
device=auto
|
|
|
|
logtelegrams=false
|
2019-02-26 21:19:16 +00:00
|
|
|
format=json
|
|
|
|
meterfiles=/var/log/wmbusmeters/meter_readings
|
2019-04-20 10:57:11 +00:00
|
|
|
meterfilesaction=overwrite
|
2019-02-24 14:20:55 +00:00
|
|
|
logfile=/var/log/wmbusmeters/wmbusmeters.log
|
2019-02-23 17:30:16 +00:00
|
|
|
EOF
|
2019-04-26 16:32:36 +00:00
|
|
|
chmod 644 "$ROOT"/etc/wmbusmeters.conf
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "conf file: created $ROOT/etc/wmbusmeters.conf"
|
2019-02-23 17:30:16 +00:00
|
|
|
else
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "conf file: $ROOT/etc/wmbusmeters.conf unchanged"
|
2019-02-23 17:30:16 +00:00
|
|
|
fi
|
|
|
|
|
2019-02-24 08:58:31 +00:00
|
|
|
####################################################################
|
|
|
|
##
|
|
|
|
## Create /etc/wmbusmeters.d
|
|
|
|
##
|
|
|
|
|
2019-04-26 16:32:36 +00:00
|
|
|
if [ ! -d "$ROOT"/etc/wmbusmeters.d ]
|
2019-02-23 17:30:16 +00:00
|
|
|
then
|
|
|
|
# Create the configuration directory
|
2019-04-26 16:32:36 +00:00
|
|
|
mkdir -p "$ROOT"/etc/wmbusmeters.d
|
2020-03-03 09:21:33 +00:00
|
|
|
chmod -R 755 "$ROOT"/etc/wmbusmeters.d
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "conf dir: created $ROOT/etc/wmbusmeters.d"
|
2019-02-23 17:30:16 +00:00
|
|
|
else
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "conf dir: $ROOT/etc/wmbusmeters.d unchanged"
|
2019-02-23 17:30:16 +00:00
|
|
|
fi
|
|
|
|
|
2019-02-24 08:58:31 +00:00
|
|
|
####################################################################
|
|
|
|
##
|
2020-03-01 19:41:19 +00:00
|
|
|
## Create /lib/systemd/system/wmbusmeters.service
|
2019-02-24 08:58:31 +00:00
|
|
|
##
|
|
|
|
|
2020-01-19 22:21:17 +00:00
|
|
|
SYSTEMD_NEEDS_RELOAD=false
|
2020-04-03 11:21:21 +00:00
|
|
|
mkdir -p "$ROOT"/lib/systemd/system/
|
2020-01-19 22:21:17 +00:00
|
|
|
|
2020-04-03 11:21:21 +00:00
|
|
|
OLD_WMBS=~/old.wmbusmeters.service.backup
|
|
|
|
CURR_WMBS="$ROOT"/lib/systemd/system/wmbusmeters.service
|
|
|
|
if [ -f $CURR_WMBS ]
|
2020-01-19 22:21:17 +00:00
|
|
|
then
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "systemd: backing up $CURR_WMBS to here: $OLD_WMBS"
|
2020-04-03 11:21:21 +00:00
|
|
|
cp $CURR_WMBS $OLD_WMBS 2>/dev/null
|
2020-01-19 22:21:17 +00:00
|
|
|
fi
|
|
|
|
|
2020-04-03 11:21:21 +00:00
|
|
|
# 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]
|
2020-09-18 18:05:59 +00:00
|
|
|
Description="wmbusmeters service"
|
2020-04-03 11:21:21 +00:00
|
|
|
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
|
2020-01-19 22:21:17 +00:00
|
|
|
then
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "systemd: no changes to $CURR_WMBS"
|
2020-04-03 11:21:21 +00:00
|
|
|
else
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "systemd: updated $CURR_WMBS"
|
2020-01-19 22:21:17 +00:00
|
|
|
SYSTEMD_NEEDS_RELOAD=true
|
|
|
|
fi
|
|
|
|
|
2020-04-03 11:21:21 +00:00
|
|
|
OLD_WMBAS=~/old.wmbusmeters@.service.backup
|
|
|
|
CURR_WMBAS="$ROOT"/lib/systemd/system/wmbusmeters@.service
|
|
|
|
if [ -f $CURR_WMBAS ]
|
2019-02-23 17:30:16 +00:00
|
|
|
then
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "systemd: removing $CURR_WMBAS"
|
|
|
|
echo "systemd: backing up $CURR_WMBAS to here: $OLD_WMBAS"
|
2020-04-03 11:21:21 +00:00
|
|
|
cp $CURR_WMBAS $OLD_WMBAS 2>/dev/null
|
2020-08-30 19:33:48 +00:00
|
|
|
rm $CURR_WMBAS
|
2020-04-03 11:21:21 +00:00
|
|
|
SYSTEMD_NEEDS_RELOAD=true
|
2019-02-23 17:30:16 +00:00
|
|
|
fi
|
2019-02-24 08:58:31 +00:00
|
|
|
|
|
|
|
####################################################################
|
|
|
|
##
|
2020-08-30 19:33:48 +00:00
|
|
|
## Remove any existing /etc/udev/rules.d/99-wmbus-usb-serial.rules
|
|
|
|
## The new wmbuseters daemon is not going to use these.
|
2019-02-24 08:58:31 +00:00
|
|
|
##
|
2020-01-19 22:21:17 +00:00
|
|
|
|
2020-08-30 19:33:48 +00:00
|
|
|
if [ -f "$ROOT"/etc/udev/rules.d/99-wmbus-usb-serial.rules ]
|
2019-02-24 08:58:31 +00:00
|
|
|
then
|
2020-10-25 15:00:06 +00:00
|
|
|
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"
|
2020-08-30 19:33:48 +00:00
|
|
|
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
|
2019-02-24 08:58:31 +00:00
|
|
|
fi
|
2020-01-19 22:21:17 +00:00
|
|
|
|
|
|
|
if [ "$SYSTEMD_NEEDS_RELOAD" = "true" ]
|
|
|
|
then
|
2020-04-03 11:21:21 +00:00
|
|
|
echo
|
|
|
|
echo
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "You need to reload systemd configuration! Please do:"
|
|
|
|
echo "sudo systemctl daemon-reload"
|
2020-01-19 22:21:17 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$UDEV_NEEDS_RELOAD" = "true" ]
|
|
|
|
then
|
2020-08-30 19:33:48 +00:00
|
|
|
echo
|
|
|
|
echo
|
2020-10-25 15:00:06 +00:00
|
|
|
echo "You need to reload udev configuration! Please do:"
|
2020-08-30 19:33:48 +00:00
|
|
|
echo "sudo udevadm control --reload-rules"
|
|
|
|
echo "sudo udevadm trigger"
|
2020-01-19 22:21:17 +00:00
|
|
|
fi
|