2022-04-05 04:46:48 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
|
|
|
|
SYSTEMD_UNIT_FILENAME="infection-monkey.service"
|
2022-04-05 04:46:48 +08:00
|
|
|
SYSTEMD_DIR="/lib/systemd/system"
|
|
|
|
MONKEY_BIN="/opt/infection-monkey/bin"
|
2022-04-11 02:57:13 +08:00
|
|
|
APPIMAGE_NAME="InfectionMonkey.AppImage"
|
2022-04-05 04:46:48 +08:00
|
|
|
|
|
|
|
echo_help() {
|
2022-04-10 21:58:50 +08:00
|
|
|
echo "Installs the Infection Monkey service to run on boot."
|
2022-04-05 04:46:48 +08:00
|
|
|
echo ""
|
2022-04-08 19:53:18 +08:00
|
|
|
echo "Usage:"
|
2022-04-10 21:58:50 +08:00
|
|
|
echo " install-infection-monkey-service.sh --user <USERNAME> --appimage <PATH>"
|
2022-04-08 19:53:18 +08:00
|
|
|
echo " install-infection-monkey-service.sh --uninstall"
|
|
|
|
echo " install-infection-monkey-service.sh -h|--help"
|
|
|
|
echo ""
|
|
|
|
echo "Options:"
|
|
|
|
echo " --user User to run the service as"
|
2022-04-10 21:58:50 +08:00
|
|
|
echo " --appimage Path to AppImage"
|
|
|
|
echo " --uninstall Uninstall the Infection Monkey service"
|
2022-04-05 04:46:48 +08:00
|
|
|
}
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
install_service() {
|
|
|
|
move_appimage "$2"
|
|
|
|
|
2022-04-05 04:46:48 +08:00
|
|
|
cat > "${SCRIPT_DIR}/${SYSTEMD_UNIT_FILENAME}" << EOF
|
|
|
|
[Unit]
|
2022-04-08 19:53:18 +08:00
|
|
|
Description=Infection Monkey Runner
|
2022-04-05 04:46:48 +08:00
|
|
|
After=network.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
User=$1
|
|
|
|
Type=simple
|
|
|
|
ExecStart="${MONKEY_BIN}/${APPIMAGE_NAME}"
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
EOF
|
|
|
|
|
2022-04-11 02:34:10 +08:00
|
|
|
umask 077
|
2022-04-05 04:46:48 +08:00
|
|
|
sudo mv "${SCRIPT_DIR}/${SYSTEMD_UNIT_FILENAME}" "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}"
|
|
|
|
sudo systemctl enable "${SYSTEMD_UNIT_FILENAME}" &>/dev/null
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
echo -e "The Infection Monkey service has been installed and will start on boot.\n\
|
|
|
|
Run 'systemctl start infection-monkey' to start the service now."
|
|
|
|
}
|
2022-04-05 04:46:48 +08:00
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
uninstall_service() {
|
2022-04-05 04:46:48 +08:00
|
|
|
if [ -f "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then
|
|
|
|
sudo rm -f "${MONKEY_BIN}/${APPIMAGE_NAME}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}" ] ; then
|
|
|
|
sudo systemctl stop "${SYSTEMD_UNIT_FILENAME}" 2>/dev/null
|
|
|
|
sudo systemctl disable "${SYSTEMD_UNIT_FILENAME}" &>/dev/null
|
|
|
|
sudo rm "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}"
|
|
|
|
sudo systemctl daemon-reload
|
|
|
|
fi
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
echo "The Infection Monkey service has been uninstalled"
|
|
|
|
}
|
|
|
|
|
|
|
|
move_appimage() {
|
2022-04-11 02:45:58 +08:00
|
|
|
sudo mkdir --mode=0755 -p "${MONKEY_BIN}"
|
2022-04-08 19:53:18 +08:00
|
|
|
|
|
|
|
if [ "$1" != "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then
|
2022-04-11 02:31:51 +08:00
|
|
|
umask 022
|
2022-04-08 19:53:18 +08:00
|
|
|
sudo cp "$appimage_path" "${MONKEY_BIN}/${APPIMAGE_NAME}"
|
2022-04-11 02:31:51 +08:00
|
|
|
sudo chmod 755 "${MONKEY_BIN}/${APPIMAGE_NAME}"
|
2022-04-08 19:53:18 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
user_exists() {
|
|
|
|
id -u "$1" &>/dev/null
|
|
|
|
}
|
|
|
|
|
2022-04-10 23:20:39 +08:00
|
|
|
assert_parameter_supplied() {
|
2022-04-08 19:53:18 +08:00
|
|
|
if [ -z "$2" ] ; then
|
2022-04-11 02:32:38 +08:00
|
|
|
echo "Error: missing required parameter '$1'"
|
2022-04-08 19:53:18 +08:00
|
|
|
echo_help
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-04-05 04:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
has_sudo() {
|
|
|
|
# 0 true, 1 false
|
|
|
|
sudo -nv > /dev/null 2>&1
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
exit_if_missing_argument() {
|
|
|
|
if [ -z "$2" ] || [ "${2:0:1}" == "-" ]; then
|
2022-04-11 02:32:38 +08:00
|
|
|
echo "Error: Argument for parameter '$1' is missing" >&2
|
2022-04-11 02:38:24 +08:00
|
|
|
echo_help
|
2022-04-05 04:46:48 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
do_uninstall=false
|
2022-04-05 04:46:48 +08:00
|
|
|
uname=""
|
|
|
|
appimage_path=""
|
|
|
|
|
|
|
|
while (( "$#" )); do
|
|
|
|
case "$1" in
|
|
|
|
--user)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
uname=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--appimage)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
appimage_path=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--uninstall)
|
2022-04-08 19:53:18 +08:00
|
|
|
do_uninstall=true
|
|
|
|
shift
|
2022-04-05 04:46:48 +08:00
|
|
|
;;
|
|
|
|
-h|--help)
|
|
|
|
echo_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Error: Unsupported parameter $1" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if ! has_sudo; then
|
|
|
|
echo "Error: You need root permissions for some of this script operations. \
|
|
|
|
Run \`sudo -v\`, enter your password, and then re-run this script."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
if $do_uninstall ; then
|
|
|
|
uninstall_service
|
|
|
|
exit 0
|
2022-04-05 04:46:48 +08:00
|
|
|
fi
|
|
|
|
|
2022-04-10 23:20:39 +08:00
|
|
|
assert_parameter_supplied "--user" "$uname"
|
|
|
|
assert_parameter_supplied "--appimage" "$appimage_path"
|
2022-04-08 19:53:18 +08:00
|
|
|
|
|
|
|
if ! user_exists "$uname" ; then
|
|
|
|
echo "Error: User '$uname' does not exist"
|
2022-04-05 04:46:48 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
if [ ! -f "$appimage_path" ] ; then
|
|
|
|
if [ ! -f "${SCRIPT_DIR}/$appimage_path" ] ; then
|
|
|
|
echo "Error: AppImage '$appimage_path' does not exist"
|
2022-04-05 04:46:48 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-04-08 19:53:18 +08:00
|
|
|
appimage_path="${SCRIPT_DIR}/$appimage_path"
|
2022-04-05 04:46:48 +08:00
|
|
|
fi
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
install_service "$uname" "$appimage_path"
|