Skip to content

Instantly share code, notes, and snippets.

@bodziek666
Last active November 19, 2021 18:52
Show Gist options
  • Save bodziek666/2ad4aa472219cf4823abe0f67b8b0a89 to your computer and use it in GitHub Desktop.
Save bodziek666/2ad4aa472219cf4823abe0f67b8b0a89 to your computer and use it in GitHub Desktop.
Manage Flexget daemon which is running inside a separated network namespace
#!/bin/bash
set -euo pipefail
NETNS="torrent"
FLEXGETDIR="/opt/flexget"
# sanity check
if [ $# -gt 1 ]; then
echo "Too many arguments"
exit 1
elif [ $# -lt 1 ]; then
echo "Too few arguments, see $0 help for more details"
exit 1
fi
# check if flexget is running
FLEXGETPID=$(pgrep -x flexget)
if [ -n "${FLEXGETPID}" ]; then
FLEXGETUSER=$(ps -o euser= $FLEXGETPID)
else
echo "FlexGet daemon is not running, see systemctl status flexget for more details"
exit 1
fi
# runtime
case "${1}" in
status)
ip netns exec "${NETNS}" sudo -u "${FLEXGETUSER}" "${FLEXGETDIR}/bin/flexget" "daemon" "status"
;;
reload-config)
ip netns exec "${NETNS}" sudo -u "${FLEXGETUSER}" "${FLEXGETDIR}/bin/flexget" "daemon" "reload-config"
;;
execute)
ip netns exec "${NETNS}" sudo -u "${FLEXGETUSER}" "${FLEXGETDIR}/bin/flexget" "execute"
;;
check)
ip netns exec "${NETNS}" sudo -u "${FLEXGETUSER}" "${FLEXGETDIR}/bin/flexget" "check"
;;
# help: https://flexget.com/Daemon
help)
echo "Control Flexget Daemon running inside a separated network namespace"
echo "Usage:"
echo -e "\t$0 status \t\tdisplay status of configured tasks"
echo -e "\t$0 reload-config \treload configuration"
echo -e "\t$0 execute \t\texecute all tasks immediatelly"
echo -e "\t$0 check \t\tcheck configuration"
exit 0
;;
# exit if invalid/unknown option is used
*)
echo "Invalid option, see $0 help for more details"
exit 1
;;
esac
# /etc/sudoers.d/torrent
Cmnd_Alias TR_START=/usr/bin/systemctl start transmission-daemon*
Cmnd_Alias TR_STATUS=/usr/bin/systemctl status transmission-daemon*
Cmnd_Alias TR_RESTART=/usr/bin/systemctl restart transmission-daemon*
Cmnd_Alias TR_STOP=/usr/bin/systemctl stop transmission-daemon*
Cmnd_Alias FLEXGETCTL=/usr/local/bin/flexgetctl
pawel ALL=(ALL) NOPASSWD: TR_START,TR_STATUS,TR_RESTART,TR_STOP,FLEXGETCTL
@bodziek666
Copy link
Author

bodziek666 commented Nov 16, 2021

Sudoers example:

pawel ALL=(ALL) NOPASSWD:/usr/local/bin/flexgetctl

and:

chown root:root /usr/local/bin/flexgetctl
chmod 755 /usr/local/bin/flexgetctl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment