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"
|
|
|
|
PKGLIST="${PKGLIST} libsodium-dev zlib1g-dev libjpeg-dev"
|
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
|
|
|
|
virtualenv -p /usr/bin/python3 ${PYTHONDIR}
|
|
|
|
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()
|
|
|
|
{
|
2020-12-06 14:25:50 +03:00
|
|
|
# Create systemd service file
|
|
|
|
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..."
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
start_software
|