Merge branch 'cam_haproxy'

This commit is contained in:
paukstelis
2022-12-30 09:07:43 -05:00
2 changed files with 99 additions and 53 deletions

View File

@@ -28,6 +28,7 @@ These files provide a bash script for quickly deploying multiple octoprint insta
* General Linux (Ubuntu/Mint/RPiOS/Debian/Fedora/Arch/etc.)
* __You do not need to install OctoPrint using any Wiki instructions, snap, etc. The script will do it for you.__
* octoprint_deploy uses systemd services, so avoid distros that do not use systemd by default (MX Linux or chroot based systems like Chrome+crouton)
* SELinux (Fedora) casues issues, particularly with camera services. Use at your own risk (or desiable SELinux).
* Basic guide video here: https://youtu.be/1YINWQ5fNn0
* All commands assume you are operating out of your home directory.
* Install Ubuntu 20+, Mint 20.3+, Debian, DietPi, RPiOS, Armbian, Fedora35+, or ArchLinux on your system (make sure your user is admin for sudo).
@@ -35,7 +36,7 @@ These files provide a bash script for quickly deploying multiple octoprint insta
* run the command `git clone https://github.com/paukstelis/octoprint_deploy.git`.
* run the command `sudo octoprint_deploy/octoprint_deploy.sh`.
* Choose `Prepare System` from the menu. Select your distribution type. All deb-based system use the same selection. This will install necessary packages, install OctoPrint, and start a template instance.
* You will be asked if you want to use haproxy. This will make your instances available on port 80 (e.g. http://localhost/instancename/).
* You will be asked if you want to use haproxy. This will make your instances available on port 80 e.g. http://localhost/instancename/. PLEASE NOTE that the trailing slash is required!
* You will be asked which streamer you would like to install (mjpg-streamer or ustreamer).
* You will be prompted if you want to setup the admin user and do the first run wizard via the commandline. If you do this now you can start making new instances as soon as the system preparation is complete.
* You will be prompted if you want to install recommended plugins and cloud plugins. This can be useful if you want to configure plugins in your template instance, before adding new instances.
@@ -59,20 +60,13 @@ These files provide a bash script for quickly deploying multiple octoprint insta
* Change udev rules for an instance with `sudo octoprint_deploy/octoprint_deploy.sh replace`
* Always a good idea to update octoprint_deploy from time-to-time with `git -C octoprint_deploy pull`
# Recent Changes
* Multi-camera support (experimental). Clean-up of haproxy.cfg on instance removal still needs work.
* If haproxy is used, cameras stream can be placed behind it. PLEASE NOTE: if cameras are used with haproxy a relative stream path is used. This means that your stream will not show up in the Control tab unless you access with the haproxy path (http://host/instancename/) (remember, trailing slash is required!)
* Allow making backups of generic instance.
* Add 'noserial' command line option. Currently for cameras only, this will unset the serial number in cases where cameras are known to share a serial number.
* Add detection for ch34x driver. This is used by Weedo printers and must be compiled separately.
* Utility sub-menu for less used options.
* Share Uploads option which will set the same upload directory for all instances.
* Filter out `generic` instance from lists where it does not need to be.
* Added `Instance Status` option which will report the status of each instance (as seen been systemctl status).
* Added `Sync Users` option. It will copy users.yaml file from selected instances to all other instances (including template)
* Starting with 0.1.4, added `Update` in the menu which will update octoprint_deploy via git (then exit)
* Rename printers_udev.sh to udev_rules.sh. Allows writing udev rules for both printers and cameras without full deployment.
* 0.1.3, printer and camera detection now done with dmesg instead of journalctl. This allows faster timeouts when a device is detected by the USB port but it does not have a serial number.
* Fail if sudouser is root.
* Add MIT license
# TODO
* Multiple cameras for an instance (see multi-camera branch)

View File

@@ -269,24 +269,6 @@ new_instance () {
$DAEMONPATH --basedir $OCTOCONFIG/.$INSTANCE config set plugins.tracking.unique_id $(uuidgen)
$DAEMONPATH --basedir $OCTOCONFIG/.$INSTANCE config set serial.port /dev/octo_$INSTANCE
if [[ -n $CAM || -n $USBCAM ]]; then
write_camera
fi
#Reset udev
udevadm control --reload-rules
udevadm trigger
systemctl daemon-reload
sleep 1
#Start and enable system processes
systemctl start $INSTANCE.service
systemctl enable $INSTANCE.service
if [[ -n $CAM || -n $USBCAM ]]; then
systemctl start cam_$INSTANCE.service
systemctl enable cam_$INSTANCE.service
fi
if [ "$HAPROXY" == true ]; then
HAversion=$(haproxy -v | sed -n 's/^.*version \([0-9]\).*/\1/p')
#find frontend line, do insert
@@ -317,6 +299,25 @@ new_instance () {
sudo systemctl restart haproxy.service
fi
if [[ -n $CAM || -n $USBCAM ]]; then
write_camera
fi
#Reset udev
udevadm control --reload-rules
udevadm trigger
systemctl daemon-reload
sleep 1
#Start and enable system processes
systemctl start $INSTANCE.service
systemctl enable $INSTANCE.service
if [[ -n $CAM || -n $USBCAM ]]; then
systemctl start cam_$INSTANCE.service
systemctl enable cam_$INSTANCE.service
fi
fi
main_menu
@@ -333,45 +334,69 @@ write_camera() {
if [ "$STREAMER" == mjpg-streamer ]; then
cat $SCRIPTDIR/octocam_mjpg.service | \
sed -e "s/OCTOUSER/$OCTOUSER/" \
-e "s/OCTOCAM/cam_$INSTANCE/" \
-e "s/OCTOCAM/cam${INUM}_$INSTANCE/" \
-e "s/RESOLUTION/$RESOLUTION/" \
-e "s/FRAMERATE/$FRAMERATE/" \
-e "s/CAMPORT/$CAMPORT/" > $SCRIPTDIR/cam_$INSTANCE.service
-e "s/CAMPORT/$CAMPORT/" > $SCRIPTDIR/cam${INUM}_$INSTANCE.service
fi
#ustreamer
if [ "$STREAMER" == ustreamer ]; then
cat $SCRIPTDIR/octocam_ustream.service | \
sed -e "s/OCTOUSER/$OCTOUSER/" \
-e "s/OCTOCAM/cam_$INSTANCE/" \
-e "s/OCTOCAM/cam${INUM}_$INSTANCE/" \
-e "s/RESOLUTION/$RESOLUTION/" \
-e "s/FRAMERATE/$FRAMERATE/" \
-e "s/CAMPORT/$CAMPORT/" > $SCRIPTDIR/cam_$INSTANCE.service
-e "s/CAMPORT/$CAMPORT/" > $SCRIPTDIR/cam${INUM}_$INSTANCE.service
fi
mv $SCRIPTDIR/cam_$INSTANCE.service /etc/systemd/system/
mv $SCRIPTDIR/cam${INUM}_$INSTANCE.service /etc/systemd/system/
echo $CAMPORT >> /etc/camera_ports
#config.yaml modifications
echo "webcam:" >> $OCTOCONFIG/.$INSTANCE/config.yaml
echo " snapshot: http://$(hostname).local:$CAMPORT?action=snapshot" >> $OCTOCONFIG/.$INSTANCE/config.yaml
echo " stream: http://$(hostname).local:$CAMPORT?action=stream" >> $OCTOCONFIG/.$INSTANCE/config.yaml
$OCTOEXEC --basedir $OCTOCONFIG/.$INSTANCE config append_value --json system.actions "{\"action\": \"Reset video streamer\", \"command\": \"sudo systemctl restart cam_$INSTANCE\", \"name\": \"Restart webcam\"}"
#Either Serial number or USB port
#config.yaml modifications - only if INUM not set
if [ -z "$INUM" ]; then
echo "webcam:" >> $OCTOCONFIG/.$INSTANCE/config.yaml
echo " snapshot: http://$(hostname).local:$CAMPORT?action=snapshot" >> $OCTOCONFIG/.$INSTANCE/config.yaml
if [ -z "$CAMHAPROXY" ]; then
echo " stream: http://$(hostname).local:$CAMPORT?action=stream" >> $OCTOCONFIG/.$INSTANCE/config.yaml
else
echo " stream: /cam_$INSTANCE/?action=stream" >> $OCTOCONFIG/.$INSTANCE/config.yaml
fi
$OCTOEXEC --basedir $OCTOCONFIG/.$INSTANCE config append_value --json system.actions "{\"action\": \"Reset video streamer\", \"command\": \"sudo systemctl restart cam_$INSTANCE\", \"name\": \"Restart webcam\"}"
fi
#Either Serial number or USB port
#Serial Number
if [ -n "$CAM" ]; then
echo SUBSYSTEM==\"video4linux\", ATTRS{serial}==\"$CAM\", ATTR{index}==\"0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
echo SUBSYSTEM==\"video4linux\", ATTRS{serial}==\"$CAM\", ATTR{index}==\"0\", SYMLINK+=\"cam${INUM}_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi
#USB port camera
if [ -n "$USBCAM" ]; then
echo SUBSYSTEM==\"video4linux\",KERNELS==\"$USBCAM\", SUBSYSTEMS==\"usb\", ATTR{index}==\"0\", DRIVERS==\"uvcvideo\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
echo SUBSYSTEM==\"video4linux\",KERNELS==\"$USBCAM\", SUBSYSTEMS==\"usb\", ATTR{index}==\"0\", DRIVERS==\"uvcvideo\", SYMLINK+=\"cam${INUM}_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi
if [ -n $CAMHAPROXY ]; then
HAversion=$(haproxy -v | sed -n 's/^.*version \([0-9]\).*/\1/p')
#find frontend line, do insert
sed -i "/option forwardfor except 127.0.0.1/a\ use_backend cam${INUM}_$INSTANCE if { path_beg /cam${INUM}_$INSTANCE/ }" /etc/haproxy/haproxy.cfg
echo "#cam${INUM}_$INSTANCE start" >> /etc/haproxy/haproxy.cfg
echo "backend cam${INUM}_$INSTANCE" >> /etc/haproxy/haproxy.cfg
if [ $HAversion -gt 1 ]; then
echo " http-request replace-path /cam${INUM}_$INSTANCE/(.*) /\1" >> /etc/haproxy/haproxy.cfg
echo " server webcam1 127.0.0.1:$CAMPORT" >> /etc/haproxy/haproxy.cfg
else
echo " reqrep ^([^\ :]*)\ /cam${INUM}_$INSTANCE/(.*) \1\ /\2" >> /etc/haproxy/haproxy.cfg
echo " server webcam1 127.0.0.1:$CAMPORT" >> /etc/haproxy/haproxy.cfg
fi
echo "#cam${INUM}_$INSTANCE stop" >> /etc/haproxy/haproxy.cfg
systemctl restart haproxy
fi
}
add_camera() {
PI=$1
INUM=''
get_settings
if [ $SUDO_USER ]; then user=$SUDO_USER; fi
echo 'Adding camera' | log
if [ -z "$INSTANCE" ]; then
@@ -379,7 +404,6 @@ add_camera() {
readarray -t options < <(cat /etc/octoprint_instances | sed -n -e 's/^instance:\([[:graph:]]*\) .*/\1/p')
options+=("Quit")
unset 'options[0]'
#Not yet check to see if instance already has a camera
select camopt in "${options[@]}"
do
if [ "$camopt" == Quit ]; then
@@ -389,9 +413,30 @@ add_camera() {
INSTANCE=$camopt
OCTOCONFIG="/home/$user/"
OCTOUSER=$user
if grep -q "cam_$INSTANCE" /etc/udev/rules.d/99-octoprint.rules; then
echo "It appears this instance already has at least one camera."
if prompt_confirm "Do you want to add an additional camera for this instance?"; then
echo "Enter a number for this camera."
echo "Ex. entering 2 will setup a service called cam2_$INSTANCE"
echo
read INUM
if [ -z "$INUM" ]; then
echo "No value given, setting as 2"
INUM='2'
fi
else
main_menu
fi
fi
break
done
fi
#for now just set a flag that we are going to write cameras behind haproxy
if [ "$HAPROXY" == true ]; then
if prompt_confirm "Add cameras to haproxy?"; then
CAMHAPROXY=1
fi
fi
if [ -z "$PI" ]; then
detect_camera
@@ -408,11 +453,11 @@ add_camera() {
if [ -z "$CAM" ]; then
echo "Camera Serial Number not detected" | log
echo -e "Camera will be setup with physical USB address of \033[0;34m $TEMPUSBCAM.\033[0m" | log
echo -e "Camera will be setup with physical USB address of \033[0;32m $TEMPUSBCAM.\033[0m" | log
echo "The camera will have to stay plugged into this location." | log
USBCAM=$TEMPUSBCAM
else
echo -e "Camera detected with serial number: \033[0;34m $CAM \033[0m" | log
echo -e "Camera detected with serial number: \033[0;32m $CAM \033[0m" | log
check_sn "$CAM"
fi
@@ -438,7 +483,7 @@ add_camera() {
CAMPORT=$((CAMPORT+1))
echo Selected port is: $CAMPORT | log
fi
echo "Settings can be modified after initial setup in /etc/systemd/system/cam_$INSTANCE.service"
echo "Settings can be modified after initial setup in /etc/systemd/system/cam${INUM}_$INSTANCE.service"
echo
while true; do
echo "Camera Resolution [default: 640x480]:"
@@ -466,10 +511,10 @@ add_camera() {
write_camera
#Pi Cam setup, replace cam_INSTANCE with /dev/video0
if [ -n "$PI" ]; then
echo SUBSYSTEM==\"video4linux\", ATTRS{name}==\"camera0\", SYMLINK+=\"cam_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
echo SUBSYSTEM==\"video4linux\", ATTRS{name}==\"camera0\", SYMLINK+=\"cam${INUM}_$INSTANCE\" >> /etc/udev/rules.d/99-octoprint.rules
fi
systemctl start cam_$INSTANCE.service
systemctl enable cam_$INSTANCE.service
systemctl start cam${INUM}_$INSTANCE.service
systemctl enable cam${INUM}_$INSTANCE.service
systemctl daemon-reload
udevadm control --reload-rules
udevadm trigger
@@ -542,8 +587,8 @@ remove_instance() {
if [ -f /etc/systemd/system/cam_$opt.service ]; then
systemctl stop cam_$opt.service
systemctl disable cam_$opt.service
rm /etc/systemd/system/cam_$opt.service
sed -i "/cam_$opt/d" /etc/udev/rules.d/99-octoprint.rules
rm /etc/systemd/system/cam*_$opt.service
sed -i "/cam.*_$opt/d" /etc/udev/rules.d/99-octoprint.rules
fi
#remove udev entry
sed -i "/$opt/d" /etc/udev/rules.d/99-octoprint.rules
@@ -555,6 +600,8 @@ remove_instance() {
if [ -f /etc/haproxy/haproxy.cfg ]; then
sed -i "/use_backend $opt/d" /etc/haproxy/haproxy.cfg
sed -i "/#$opt start/,/#$opt stop/d" /etc/haproxy/haproxy.cfg
sed -i "/use_backend cam_$opt/d" /etc/haproxy/haproxy.cfg
sed -i "/#cam_$opt start/,/#cam_$opt stop/d" /etc/haproxy/haproxy.cfg
systemctl restart haproxy.service
fi
fi
@@ -1000,6 +1047,8 @@ remove_everything() {
if [ -f /etc/haproxy/haproxy.cfg ]; then
sed -i "/use_backend $instance/d" /etc/haproxy/haproxy.cfg
sed -i "/#$instance start/,/#$instance stop/d" /etc/haproxy/haproxy.cfg
sed -i "/use_backend cam_$instance/d" /etc/haproxy/haproxy.cfg
sed -i "/#cam_$instance start/,/#cam_$instance stop/d" /etc/haproxy/haproxy.cfg
fi
done
echo "Removing system stuff"
@@ -1014,6 +1063,8 @@ remove_everything() {
echo "Removing template"
rm -rf /home/$user/.octoprint
rm -rf /home/$user/OctoPrint
rm -rf /home/$user/ustreamer
rm -rf /home/$user/mjpg-streamer
systemctl restart haproxy.service
systemctl daemon-reload
@@ -1231,7 +1282,7 @@ restore_menu() {
fi
echo "Selected $opt to restore" | log
tar -xvf $opt
tar --same-owner -xvfp $opt
main_menu
done
}
@@ -1309,7 +1360,7 @@ instance_status() {
}
main_menu() {
VERSION=0.1.9
VERSION=0.2.0
#reset
UDEV=''
TEMPUSB=''
@@ -1317,6 +1368,7 @@ main_menu() {
TEMPUSBCAM=''
INSTANCE=''
INSTALL=''
CAMHAPROXY=''
echo
echo
echo "*************************"