Add setup-cron

Add a setup script that adds a crontab entry that
runs 'portzap pull' everyday at 12AM localtime
This commit is contained in:
0x1eef 2024-06-30 04:23:57 -03:00
parent d3c3e3e2f0
commit 69a66825ab
5 changed files with 106 additions and 12 deletions

View file

@ -5,11 +5,12 @@ LIBEXECDIR = $(PREFIX)/libexec/portzap
SHAREDIR = $(PREFIX)/share/portzap SHAREDIR = $(PREFIX)/share/portzap
install: install:
install -d $(BINDIR) $(LIBEXECDIR) $(LIBEXECDIR)/commands $(LIBEXECDIR)/utils $(SHAREDIR) $(MANDIR) install -d $(BINDIR) $(LIBEXECDIR) $(LIBEXECDIR)/commands $(LIBEXECDIR)/utils $(LIBEXECDIR)/setup $(SHAREDIR) $(MANDIR)
install -m 0755 bin/portzap $(BINDIR) install -m 0755 bin/portzap $(BINDIR)
install -m 0755 bin/setup-portzap $(BINDIR) install -m 0755 bin/setup-portzap $(BINDIR)
install -m 0755 libexec/portzap/commands/* $(LIBEXECDIR)/commands install -m 0755 libexec/portzap/commands/* $(LIBEXECDIR)/commands
install -m 0755 libexec/portzap/utils/* $(LIBEXECDIR)/utils install -m 0755 libexec/portzap/utils/* $(LIBEXECDIR)/utils
install -m 0755 libexec/portzap/setup/* $(LIBEXECDIR)/setup
install -m 0644 share/portzap/* $(SHAREDIR) install -m 0644 share/portzap/* $(SHAREDIR)
install -m 0644 man/man8/portzap.8 $(MANDIR) install -m 0644 man/man8/portzap.8 $(MANDIR)

View file

@ -4,9 +4,8 @@ set -e
## ##
# variables # variables
localbase=${LOCALBASE:-$(realpath "$(dirname "$0")"/..)} localbase=${LOCALBASE:-$(realpath "$(dirname "$0")"/..)}
sharedir="${localbase}"/share/portzap
libexec="${localbase}"/libexec/portzap libexec="${localbase}"/libexec/portzap
conf=$(cat "${localbase}"/share/portzap/doas.conf)
doas="${localbase}"/etc/doas.conf
## ##
# functions # functions
@ -33,15 +32,10 @@ else
-m \ -m \
-s /sbin/nologin -s /sbin/nologin
chmod u=rwX,g=rX,o= /home/_portzap/ chmod u=rwX,g=rX,o= /home/_portzap/
printok "create _portzap user" printok "_portzap user created"
fi fi
if grep -F "^${conf}$" "${doas}" > /dev/null 2>&1; then "${libexec}"/setup/setup-doas
printok "${doas} is up to date" "${libexec}"/setup/setup-cron
else printf "\nAdd user(s) to the _portzap group:\n"
echo "$conf" >> "$doas"
printok "update ${doas} (note: review the update)"
fi
printf "Add user(s) to the _portzap group:\n"
printf "root# pw groupmod -n _portzap -m user1,user2\n" printf "root# pw groupmod -n _portzap -m user1,user2\n"

View file

@ -0,0 +1,63 @@
#!/bin/sh
set -e
##
# variables
user="_portzap"
localbase="$(realpath $(dirname $0)/../../..)"
libexec="${localbase}/libexec/portzap"
sharedir="${localbase}/share/portzap"
##
# functions
printok() {
"${libexec}"/utils/printok "$1"
}
printerr() {
"${libexec}"/utils/printerr "$1"
}
verify_crontab()
{
allowfile="/var/cron/allow"
if [ -e "${allowfile}" ]; then
if ! grep "${user}" "${allowfile}" > /dev/null 2>&1; then
printerr "in order to use the portzap crontab, add ${user} to ${allowfile}"
exit 1
fi
printok "${user} exists in ${allowfile}"
fi
}
install_crontab()
{
src="${sharedir}/crontab"
dest="/var/cron/tabs/${user}"
if [ -e "${dest}" ]; then
yes | crontab -u "${user}" -r
printok "crontab removed (${dest})"
fi
crontab -u "${user}" "${src}"
chmod u=rw,g=,o= "${dest}"
printok "crontab installed (${dest})"
}
##
# main
printf "Do you want to run 'portzap pull' daily via cron(8) ? (yes|no) "
while read -r r; do
case "${r}" in
y|Y|yes|YES)
verify_crontab
install_crontab
break
;;
n|N|no|NO)
break
;;
*)
printf "Please answer yes or no: "
;;
esac
done

View file

@ -0,0 +1,35 @@
#!/bin/sh
set -e
##
# variables
user="_portzap"
localbase="$(realpath $(dirname $0)/../../..)"
libexec="${localbase}/libexec/portzap"
sharedir="${localbase}/share/portzap"
##
# functions
printok() {
"${libexec}"/utils/printok "$1"
}
printerr() {
"${libexec}"/utils/printerr "$1"
}
install_doasconf()
{
src="${sharedir}/doas.conf"
dest="${localbase}/etc/doas.conf"
if grep -Fq "$(cat "${src}")" "${dest}"; then
printok "doas.conf looks up to date"
else
cat "${src}" >> "${dest}"
printok "${dest} has been updated"
fi
}
##
# main
install_doasconf

1
share/portzap/crontab Normal file
View file

@ -0,0 +1 @@
0 0 * * * /usr/local/bin/portzap pull