2020-07-02 04:21:35 +03:00
|
|
|
#!/bin/bash
|
|
|
|
# This script installs Moonraker on a Raspberry Pi machine running
|
|
|
|
# Raspbian/Raspberry Pi OS based distributions.
|
|
|
|
|
|
|
|
PYTHONDIR="${HOME}/moonraker-env"
|
2020-12-06 14:25:50 +03:00
|
|
|
SYSTEMDDIR="/etc/systemd/system"
|
2020-11-02 15:35:07 +03:00
|
|
|
REBUILD_ENV="n"
|
|
|
|
FORCE_DEFAULTS="n"
|
|
|
|
CONFIG_PATH="${HOME}/moonraker.conf"
|
2021-05-29 22:55:19 +03:00
|
|
|
LOG_PATH="/tmp/moonraker.log"
|
2020-07-02 04:21:35 +03:00
|
|
|
|
2020-12-06 14:25:50 +03:00
|
|
|
# Step 2: Clean up legacy installation
|
|
|
|
cleanup_legacy() {
|
|
|
|
if [ -f "/etc/init.d/moonraker" ]; then
|
|
|
|
# Stop Moonraker Service
|
|
|
|
echo "#### Cleanup legacy install script"
|
|
|
|
sudo systemctl stop moonraker
|
|
|
|
sudo update-rc.d -f moonraker remove
|
|
|
|
sudo rm -f /etc/init.d/moonraker
|
|
|
|
sudo rm -f /etc/default/moonraker
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Step 3: Install packages
|
2020-07-02 04:21:35 +03:00
|
|
|
install_packages()
|
|
|
|
{
|
2021-09-30 00:22:26 +03:00
|
|
|
PKGLIST="python3-virtualenv python3-dev libopenjp2-7 python3-libgpiod"
|
2021-10-31 13:03:56 +03:00
|
|
|
PKGLIST="${PKGLIST} curl libcurl4-openssl-dev libssl-dev liblmdb-dev"
|
2022-01-23 16:08:12 +03:00
|
|
|
PKGLIST="${PKGLIST} libsodium-dev zlib1g-dev libjpeg-dev packagekit"
|
2020-07-02 04:21:35 +03:00
|
|
|
|
|
|
|
# Update system package info
|
|
|
|
report_status "Running apt-get update..."
|
2021-08-24 13:41:45 +03:00
|
|
|
sudo apt-get update --allow-releaseinfo-change
|
2020-07-02 04:21:35 +03:00
|
|
|
|
|
|
|
# Install desired packages
|
|
|
|
report_status "Installing packages..."
|
|
|
|
sudo apt-get install --yes ${PKGLIST}
|
|
|
|
}
|
|
|
|
|
2020-12-06 14:25:50 +03:00
|
|
|
# Step 4: Create python virtual environment
|
2020-07-02 04:21:35 +03:00
|
|
|
create_virtualenv()
|
|
|
|
{
|
2020-11-02 15:35:07 +03:00
|
|
|
report_status "Installing python virtual environment..."
|
2020-07-02 04:21:35 +03:00
|
|
|
|
2020-11-02 15:35:07 +03:00
|
|
|
# If venv exists and user prompts a rebuild, then do so
|
|
|
|
if [ -d ${PYTHONDIR} ] && [ $REBUILD_ENV = "y" ]; then
|
|
|
|
report_status "Removing old virtualenv"
|
|
|
|
rm -rf ${PYTHONDIR}
|
|
|
|
fi
|
|
|
|
|
2020-12-22 23:41:42 +03:00
|
|
|
if [ ! -d ${PYTHONDIR} ]; then
|
2022-01-30 22:08:13 +03:00
|
|
|
GET_PIP="${HOME}/get-pip.py"
|
|
|
|
virtualenv --no-pip -p /usr/bin/python3 ${PYTHONDIR}
|
|
|
|
curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o ${GET_PIP}
|
|
|
|
${PYTHONDIR}/bin/python ${GET_PIP}
|
|
|
|
rm ${GET_PIP}
|
2020-12-22 23:41:42 +03:00
|
|
|
fi
|
2020-07-02 04:21:35 +03:00
|
|
|
|
|
|
|
# Install/update dependencies
|
|
|
|
${PYTHONDIR}/bin/pip install -r ${SRCDIR}/scripts/moonraker-requirements.txt
|
|
|
|
}
|
|
|
|
|
2020-12-06 14:25:50 +03:00
|
|
|
# Step 5: Install startup script
|
2020-07-02 04:21:35 +03:00
|
|
|
install_script()
|
|
|
|
{
|
2022-01-26 15:55:24 +03:00
|
|
|
# Create systemd service file
|
2020-12-06 14:25:50 +03:00
|
|
|
SERVICE_FILE="${SYSTEMDDIR}/moonraker.service"
|
|
|
|
[ -f $SERVICE_FILE ] && [ $FORCE_DEFAULTS = "n" ] && return
|
2020-07-02 04:21:35 +03:00
|
|
|
report_status "Installing system start script..."
|
2022-01-26 15:55:24 +03:00
|
|
|
sudo groupadd -f moonraker-admin
|
2020-12-06 14:25:50 +03:00
|
|
|
sudo /bin/sh -c "cat > ${SERVICE_FILE}" << EOF
|
|
|
|
#Systemd service file for moonraker
|
|
|
|
[Unit]
|
2021-05-30 00:54:08 +03:00
|
|
|
Description=API Server for Klipper
|
2021-10-08 15:07:03 +03:00
|
|
|
Requires=network-online.target
|
|
|
|
After=network-online.target
|
2020-12-06 14:25:50 +03:00
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
Type=simple
|
|
|
|
User=$USER
|
2022-01-26 15:55:24 +03:00
|
|
|
SupplementaryGroups=moonraker-admin
|
2020-12-06 14:25:50 +03:00
|
|
|
RemainAfterExit=yes
|
2021-05-30 00:54:08 +03:00
|
|
|
WorkingDirectory=${SRCDIR}
|
|
|
|
ExecStart=${LAUNCH_CMD} -c ${CONFIG_PATH} -l ${LOG_PATH}
|
2020-12-06 14:25:50 +03:00
|
|
|
Restart=always
|
|
|
|
RestartSec=10
|
2020-07-02 04:21:35 +03:00
|
|
|
EOF
|
2020-12-06 14:25:50 +03:00
|
|
|
# Use systemctl to enable the klipper systemd service script
|
|
|
|
sudo systemctl enable moonraker.service
|
2021-05-30 00:54:08 +03:00
|
|
|
sudo systemctl daemon-reload
|
2020-07-02 04:21:35 +03:00
|
|
|
}
|
|
|
|
|
2022-01-26 15:55:24 +03:00
|
|
|
check_polkit_rules()
|
|
|
|
{
|
|
|
|
if [ ! -x "$(command -v pkaction)" ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
POLKIT_VERSION="$( pkaction --version | grep -Po "(\d?\.\d+)" )"
|
|
|
|
if [ "$POLKIT_VERSION" = "0.105" ]; then
|
|
|
|
POLKIT_LEGACY_FILE="/etc/polkit-1/localauthority/50-local.d/10-moonraker.pkla"
|
|
|
|
# legacy policykit rules don't give users other than root read access
|
|
|
|
if sudo [ ! -f $POLKIT_LEGACY_FILE ]; then
|
|
|
|
echo -e "\n*** No PolicyKit Rules detected, run 'set-policykit-rules.sh'"
|
|
|
|
echo "*** if you wish to grant Moonraker authorization to manage"
|
|
|
|
echo "*** system services, reboot/shutdown the system, and update"
|
|
|
|
echo "*** packages."
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
POLKIT_FILE="/etc/polkit-1/rules.d/moonraker.rules"
|
|
|
|
POLKIT_USR_FILE="/usr/share/polkit-1/rules.d/moonraker.rules"
|
|
|
|
if [ ! -f $POLKIT_FILE ] && [ ! -f $POLKIT_USR_FILE ]; then
|
|
|
|
echo -e "\n*** No PolicyKit Rules detected, run 'set-policykit-rules.sh'"
|
|
|
|
echo "*** if you wish to grant Moonraker authorization to manage"
|
|
|
|
echo "*** system services, reboot/shutdown the system, and update"
|
|
|
|
echo "*** packages."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
2020-12-06 14:25:50 +03:00
|
|
|
|
2020-10-26 21:22:20 +03:00
|
|
|
# Step 6: Start server
|
2020-07-02 04:21:35 +03:00
|
|
|
start_software()
|
|
|
|
{
|
|
|
|
report_status "Launching Moonraker API Server..."
|
2020-12-06 14:25:50 +03:00
|
|
|
sudo systemctl restart moonraker
|
2020-07-02 04:21:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Helper functions
|
|
|
|
report_status()
|
|
|
|
{
|
|
|
|
echo -e "\n\n###### $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
verify_ready()
|
|
|
|
{
|
|
|
|
if [ "$EUID" -eq 0 ]; then
|
|
|
|
echo "This script must not run as root"
|
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Force script to exit if an error occurs
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Find SRCDIR from the pathname of this script
|
|
|
|
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )"
|
2021-05-30 00:54:08 +03:00
|
|
|
LAUNCH_CMD="${PYTHONDIR}/bin/python ${SRCDIR}/moonraker/moonraker.py"
|
2020-07-02 04:21:35 +03:00
|
|
|
|
2020-11-02 15:35:07 +03:00
|
|
|
# Parse command line arguments
|
2021-05-29 22:55:19 +03:00
|
|
|
while getopts "rfc:l:" arg; do
|
2020-11-02 15:35:07 +03:00
|
|
|
case $arg in
|
|
|
|
r) REBUILD_ENV="y";;
|
|
|
|
f) FORCE_DEFAULTS="y";;
|
|
|
|
c) CONFIG_PATH=$OPTARG;;
|
2021-05-29 22:55:19 +03:00
|
|
|
l) LOG_PATH=$OPTARG;;
|
2020-11-02 15:35:07 +03:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-07-02 04:21:35 +03:00
|
|
|
# Run installation steps defined above
|
|
|
|
verify_ready
|
2020-12-06 15:28:11 +03:00
|
|
|
cleanup_legacy
|
2020-07-02 04:21:35 +03:00
|
|
|
install_packages
|
|
|
|
create_virtualenv
|
|
|
|
install_script
|
2022-01-26 15:55:24 +03:00
|
|
|
check_polkit_rules
|
2020-07-02 04:21:35 +03:00
|
|
|
start_software
|