Last active
March 16, 2019 01:46
-
-
Save jsatt/95f4471ad8d5066498c774949d48a55d to your computer and use it in GitHub Desktop.
Automate setup of Octoprint. Intended to be run on a fresh install of Raspbian.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
OCTO_USER=octoprint | |
BASE_DIR=/home/$OCTO_USER | |
if [ ! `id -u` == "0" ]; then | |
echo "You must run this script as root." | |
exit 1 | |
fi | |
add_octoprint_user(){ | |
adduser --disabled-password --gecos "Octoprint" $OCTO_USER | |
adduser $OCTO_USER tty | |
adduser $OCTO_USER dialout | |
adduser $OCTO_USER video | |
echo "$OCTO_USER ALL=NOPASSWD: /sbin/shutdown" > /etc/sudoers.d/octoprint-shutdown | |
echo "$OCTO_USER ALL=NOPASSWD: /usr/sbin/service" > /etc/sudoers.d/octoprint-service | |
} | |
install_octoprint(){ | |
apt-get update && apt-get install -y --no-install-recommends \ | |
avrdude \ | |
build-essential \ | |
cmake \ | |
git \ | |
haproxy \ | |
libyaml-dev \ | |
psmisc \ | |
python-dev \ | |
supervisor \ | |
unzip \ | |
wget | |
wget -O - https://bootstrap.pypa.io/get-pip.py | python | |
pip install --no-binary :all: https://get.octoprint.org/latest | |
write_octoprint_config | |
write_haproxy_config | |
wget https://github.com/foosel/OctoPrint/raw/master/scripts/octoprint.init && sudo mv octoprint.init /etc/init.d/octoprint | |
write_defaults_file | |
sudo chmod +x /etc/init.d/octoprint | |
sudo update-rc.d octoprint defaults | |
} | |
install_camera_support(){ | |
apt-get update && apt-get install -y --no-install-recommends \ | |
ffmpeg \ | |
imagemagick \ | |
libav-tools \ | |
v4l-utils \ | |
libjpeg-dev \ | |
libjpeg62-turbo \ | |
libprotobuf-dev \ | |
libv4l-dev \ | |
zlib1g-dev | |
if [ ! `which mjpg_streamer` ]; then | |
prev_dir=`pwd` | |
cd /tmp/ | |
git clone https://github.com/jacksonliam/mjpg-streamer.git | |
cd mjpg-streamer/mjpg-streamer-experimental | |
export LD_LIBRARY_PATH=. | |
make | |
make install | |
rm -r /tmp/mjpg-streamer | |
cd $prev_dir | |
fi | |
write_webcam_scripts | |
} | |
write_octoprint_config(){ | |
echo " | |
system: | |
commands: | |
systemShutdownCommand: sudo shutdown -h now | |
systemRestartCommand: sudo shutdown -r now | |
serverRestartCommand: sudo service octoprint restart | |
actions: | |
- action: streamon | |
command: $BASE_DIR/scripts/webcam start | |
confirm: false | |
name: Start video stream | |
- action: streamoff | |
command: $BASE_DIR/scripts/webcam stop | |
confirm: false | |
name: Stop video stream | |
webcam: | |
ffmpeg: /usr/bin/ffmpeg | |
snapshot: /webcam/?action=snapshot | |
stream: /webcam/?action=stream | |
" > $BASE_DIR/config.yaml | |
} | |
write_defaults_file(){ | |
echo "OCTOPRINT_USER=$OCTO_USER | |
BASEDIR=$BASE_DIR | |
CONFIGFILE=$BASE_DIR/config.yaml | |
PORT=5000 | |
DAEMON=/usr/local/bin/octoprint | |
DAEMON_ARGS="--port=\$PORT" | |
UMASK=022 | |
NICELEVEL=-2 | |
START=yes" > /etc/default/octoprint | |
} | |
write_haproxy_config(){ | |
echo " | |
global | |
maxconn 4096 | |
user haproxy | |
group haproxy | |
daemon | |
log 127.0.0.1 local0 debug | |
defaults | |
log global | |
mode http | |
option httplog | |
option dontlognull | |
retries 3 | |
option redispatch | |
option http-server-close | |
option forwardfor | |
maxconn 2000 | |
timeout connect 5s | |
timeout client 15min | |
timeout server 15min | |
frontend public | |
bind :::80 v4v6 | |
use_backend webcam if { path_beg /webcam/ } | |
default_backend octoprint | |
backend octoprint | |
reqrep ^([^\ :]*)\ /(.*) \1\ /\2 | |
option forwardfor | |
server octoprint1 127.0.0.1:5000 | |
backend webcam | |
reqrep ^([^\ :]*)\ /webcam/(.*) \1\ /\2 | |
server webcam1 127.0.0.1:8080 | |
" > /etc/haproxy/haproxy.cfg | |
} | |
write_webcam_scripts(){ | |
if [ ! -d "$BASE_DIR/scripts" ]; then | |
mkdir $BASE_DIR/scripts | |
chown $OCTO_USER.$OCTO_USER $BASE_DIR/scripts | |
fi | |
echo "#!/bin/bash | |
# Start / stop streamer daemon | |
case "\$1" in | |
start) | |
$BASE_DIR/scripts/webcamDaemon >/dev/null 2>&1 & | |
echo \"\$0: started\" | |
;; | |
stop) | |
pkill -x webcamDaemon | |
pkill -x mjpg_streamer | |
echo \"\$0: stopped\" | |
;; | |
*) | |
echo \"Usage: \$0 \{start\|stop\}\" >&2 | |
;; | |
esac | |
" > $BASE_DIR/scripts/webcam | |
echo "#!/bin/bash | |
MJPGSTREAMER_INPUT_USB=\"input_uvc.so\" | |
MJPGSTREAMER_INPUT_RASPICAM=\"input_raspicam.so\" | |
# init configuration | |
camera=\"auto\" | |
camera_usb_options=\"-r 640x480 -f 10\" | |
camera_raspi_options=\"-fps 10\" | |
if [ -e \"/boot/octopi.txt\" ]; then | |
source \"/boot/octopi.txt\" | |
fi | |
# runs MJPG Streamer, using the provided input plugin + configuration | |
function runMjpgStreamer { | |
input=\$1 | |
echo Running mjpg_streamer -o \"output_http.so -w ./www\" -i \"\$input\" | |
LD_LIBRARY_PATH=. mjpg_streamer -o \"output_http.so -w ./www\" -i \"\$input\" | |
} | |
# starts up the RasPiCam | |
function startRaspi { | |
logger \"Starting Raspberry Pi camera\" | |
runMjpgStreamer \"\$MJPGSTREAMER_INPUT_RASPICAM \$camera_raspi_options\" | |
} | |
# starts up the USB webcam | |
function startUsb { | |
logger \"Starting USB webcam\" | |
runMjpgStreamer \"\$MJPGSTREAMER_INPUT_USB \$camera_usb_options\" | |
} | |
# we need this to prevent the later calls to vcgencmd from blocking | |
# I have no idea why, but that's how it is... | |
vcgencmd version | |
# echo configuration | |
echo camera: \$camera | |
echo usb options: \$camera_usb_options | |
echo raspi options: \$camera_raspi_options | |
# keep mjpg streamer running if some camera is attached | |
while true; do | |
if [ -e \"/dev/video0\" ] && { [ \"\$camera\" = \"auto\" ] || [ \"\$camera\" = \"usb\" ] ; }; then | |
startUsb | |
elif [ \"\`vcgencmd get_camera\`\" = \"supported=1 detected=1\" ] && { [ \"\$camera\" = \"auto\" ] || [ \"\$camera\" = \"raspi\" ] ; }; then | |
startRaspi | |
fi | |
sleep 120 | |
done" > $BASE_DIR/scripts/webcamDaemon | |
chmod +x $BASE_DIR/scripts/webcam $BASE_DIR/scripts/webcamDaemon | |
} | |
user_missing=$(id -u $OCTO_USER > /dev/null 2>&1; echo $?) | |
if [ "$user_missing" == "1" ]; then | |
add_octoprint_user | |
fi | |
install_octoprint | |
read -n1 -r -p "Enabled camera support? (Press 'Y' or 'N') " camera | |
echo | |
if [[ "yY" == *"$camera"* ]]; then | |
install_camera_support | |
fi | |
echo | |
echo | |
echo "Octoprint setup is complete. The system needs to reboot." | |
read -n1 -r -p "Reboot now? (Press 'Y' or 'N') " reboot_now | |
echo | |
if [[ "yY" == *"$reboot_now"* ]]; then | |
reboot | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment