cmd/looper: added build & run utilities including .service file for robust execution

This commit is contained in:
Saxon Nelson-Milton 2020-12-17 21:09:52 +10:30
parent 528733c559
commit 1a4ba41322
3 changed files with 117 additions and 26 deletions

View File

@ -1,52 +1,84 @@
# Install rc.local file
# Install files and directories required by NetSender clients (such as gpio-netsender, rv, etc.)
# and create a dhcpcd.enter-hook for setting the MAC address.
# MA and DK can be optionally passed to Make, e.g, for a hard (first-time) installation:
# sudo MA=mac DK=dk install_hard
# NB: The default (soft) install does not override conf files.
USER := $(shell whoami)
PATH := /usr/local/go/bin:$(PATH)
ifeq ($(MA),)
MA := "00:E0:4C:00:00:01"
endif
ifeq ($(DK),)
DK := 0
endif
.SILENT:copy_files
.SILENT:make_dirs
.SILENT:soft_copy_files
.SILENT:hard_copy_files
.SILENT:build
.SILENT:set_mac
.SILENT:syncreboot
.SILENT:clean
install: as_root copy_files build
install: as_root make_dirs soft_copy_files
@echo "Install complete"
install_hard: as_root hard_copy_files build
@echo "Install complete"
install_hard: as_root make_dirs hard_copy_files set_mac syncreboot
@echo "Hard install complete"
as_root:
ifneq ($(USER),root)
$(error Must run as superuser!)
endif
copy_files:
if [ -f /etc/rc.local ] ; then \
echo "/etc/rc.local left unmodified" ; \
else \
cp rc.local /etc; \
make_dirs:
if [ ! -d /var/netsender ] ; then \
mkdir /var/netsender; \
chmod guo+rwx /var/netsender; \
fi
if [ ! -d /var/log/netsender ] ; then \
mkdir /var/log/netsender; \
chmod guo+rwx /var/log/netsender; \
fi
soft_copy_files:
if [ -f /etc/systemd/system/looper.service ] ; then \
echo "/etc/systemd/system/looper.service left unmodified" ; \
else \
cp looper.service /etc/systemd/system; \
fi
systemctl enable looper.service
chmod +x pi_run.sh
if [ -f /etc/netsender.conf ] ; then \
echo "/etc/netsender.conf left unmodified" ; \
else \
cp netsender.conf /etc; \
printf "ma $(MA)\ndk $(DK)\n" > /etc/netsender.conf; \
chown pi /etc/netsender.conf; \
fi
hard_copy_files:
if [ -f /etc/rc.local ] ; then \
echo "Backed up rc.local to /etc/rc.local.bak" ; \
cp /etc/rc.local /etc/rc.local.bak ; \
if [ -f /etc/systemd/system/looper.service ] ; then \
echo "/etc/systemd/system/looper.service overwritten" ; \
fi
cp -f rc.local /etc
cp -f looper.service /etc/systemd/system
systemctl enable looper.service
chmod +x pi_run.sh
if [ -f /etc/netsender.conf ] ; then \
echo "Backed up netsender.conf to /etc/netsender.conf.bak" ; \
echo "Backed up netsender.conf to /etc/netsender.conf.bak"; \
cp /etc/netsender.conf /etc/netsender.conf.bak ; \
fi
cp -f netsender.conf /etc
printf "ma $(MA)\ndk $(DK)\n" > /etc/netsender.conf
chown pi /etc/netsender.conf
build:
if grep -q 'Raspberry Pi 3' '/proc/device-tree/model'; then \
echo "Compiling for Raspberry pi 3";\
go build -tags pi3;\
else \
echo "Compiling for Raspberry pi 0";\
go build -tags pi0;\
fi
set_mac:
printf "ip link set eth0 address $(MA)\n" > /etc/dhcpcd.enter-hook
chmod guo+x /etc/dhcpcd.enter-hook
syncreboot:
cd ../../utils/cmd/syncreboot; make; make install
clean: as_root
rm -rf /var/netsender
rm -rf /var/log/netsender
rm -rf /etc/systemd/system/looper.service
rm -rf /etc/netsender.conf
@echo "Clean complete"

10
cmd/looper/looper.service Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=looper is a utility to robustly loop a sound file from a raspberry pi.
[Service]
Type=simple
ExecStart=/home/pi/go/src/bitbucket.org/ausocean/av/cmd/looper/pi_run.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target

49
cmd/looper/pi_run.sh Normal file
View File

@ -0,0 +1,49 @@
#!/bin/sh -e
# This script launches looper on a pi, intended to run at boot time.
LOOPER_PATH=/home/pi/go/src/bitbucket.org/ausocean/av/cmd/looper
echo Set kernel parameters:
# kernel settings to improve performance on Raspberry Pi
# tell Linux to fork optimistically
sudo sysctl -w vm.overcommit_memory=1
# minimize swapping, without disabling it completely
sudo sysctl -w vm.swappiness=1
# the following required directories _should_ already exist
if [ ! -d /var/log/netsender ]; then
sudo mkdir /var/log/netsender
chmod guo+rwx /var/log/netsender
fi
if [ ! -d /var/netsender ]; then
sudo mkdir /var/netsender
chmod guo+rwx /var/netsender
fi
# show IP addresses
echo Our IP addresses:
sudo ip addr show | grep inet
# Start gpio stuff.
sudo systemctl start pigpiod
# capture stdout and stderr to a secondary log file (just in case)
exec 2> /var/log/netsender/stream.log
exec 1>&2
# set env, working dir and run looper as pi user
HOME=/home/pi
GOPATH=$HOME/go
LOOPER_PATH=$GOPATH/src/bitbucket.org/ausocean/av/cmd/looper
PATH=$PATH:/usr/local/go/bin:$LOOPER_PATH
cd $LOOPER_PATH
sudo HOME=$HOME GOPATH=$GOPATH PATH=$PATH ./looper
if [ $? -eq 0 ]
then
echo "Successfully exited looper"
exit 0
else
echo "looper exited with code: $?" >&2
exit 1
fi